Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 26 | #ifndef __MICROPY_INCLUDED_PY_QSTR_H__ |
| 27 | #define __MICROPY_INCLUDED_PY_QSTR_H__ |
| 28 | |
| 29 | #include "py/mpconfig.h" |
| 30 | #include "py/misc.h" |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 31 | |
Chris Angelico | 29bf739 | 2014-06-04 03:15:46 +1000 | [diff] [blame] | 32 | // See qstrdefs.h for a list of qstr's that are available as constants. |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 33 | // Reference them as MP_QSTR_xxxx. |
| 34 | // |
| 35 | // Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx") |
| 36 | // for qstrs that are referenced this way, but you don't want to have them in ROM. |
| 37 | |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 38 | // first entry in enum will be MP_QSTR_NULL=0, which indicates invalid/no qstr |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 39 | enum { |
Paul Sokolovsky | f469c76 | 2016-06-16 01:40:31 +0300 | [diff] [blame] | 40 | #ifndef NO_QSTR |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 41 | #define QDEF(id, str) id, |
Damien George | d553be5 | 2014-04-17 18:03:27 +0100 | [diff] [blame] | 42 | #include "genhdr/qstrdefs.generated.h" |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 43 | #undef QDEF |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 44 | #endif |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 45 | MP_QSTRnumber_of, // no underscore so it can't clash with any of the above |
Damien George | 2813cb6 | 2014-04-12 17:53:05 +0100 | [diff] [blame] | 46 | }; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 47 | |
Damien George | 6e2fb56 | 2015-12-17 12:45:22 +0000 | [diff] [blame] | 48 | typedef size_t qstr; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 49 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 50 | typedef struct _qstr_pool_t { |
| 51 | struct _qstr_pool_t *prev; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 52 | size_t total_prev_len; |
| 53 | size_t alloc; |
| 54 | size_t len; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 55 | const byte *qstrs[]; |
| 56 | } qstr_pool_t; |
| 57 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 58 | #define QSTR_FROM_STR_STATIC(s) (qstr_from_strn((s), strlen(s))) |
| 59 | |
| 60 | void qstr_init(void); |
| 61 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 62 | mp_uint_t qstr_compute_hash(const byte *data, size_t len); |
| 63 | qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTR_NULL if not found |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 64 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 65 | qstr qstr_from_str(const char *str); |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 66 | qstr qstr_from_strn(const char *str, size_t len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 67 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 68 | byte *qstr_build_start(size_t len, byte **q_ptr); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 69 | qstr qstr_build_end(byte *q_ptr); |
| 70 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 71 | mp_uint_t qstr_hash(qstr q); |
Damien George | 2801e6f | 2015-04-04 15:53:11 +0100 | [diff] [blame] | 72 | const char *qstr_str(qstr q); |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 73 | size_t qstr_len(qstr q); |
| 74 | const byte *qstr_data(qstr q, size_t *len); |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 75 | |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 76 | void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 77 | void qstr_dump_data(void); |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 78 | |
| 79 | #endif // __MICROPY_INCLUDED_PY_QSTR_H__ |