blob: 58ce285fd2c8a539117eeca26852b82faa496813 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_QSTR_H
27#define MICROPY_INCLUDED_PY_QSTR_H
Damien George9ddbe292014-12-29 01:02:19 +000028
29#include "py/mpconfig.h"
30#include "py/misc.h"
Damien George04b91472014-05-03 23:27:38 +010031
Chris Angelico29bf7392014-06-04 03:15:46 +100032// See qstrdefs.h for a list of qstr's that are available as constants.
Damien George55baff42014-01-21 21:40:13 +000033// Reference them as MP_QSTR_xxxx.
34//
stijnb9a35be2021-01-16 09:18:31 +010035// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str("xxx")
Damien George55baff42014-01-21 21:40:13 +000036// for qstrs that are referenced this way, but you don't want to have them in ROM.
37
Josh Lloyd7d58a192019-09-25 17:53:30 +120038// first entry in enum will be MP_QSTRnull=0, which indicates invalid/no qstr
Damien George55baff42014-01-21 21:40:13 +000039enum {
Damien George69661f32020-02-27 15:36:53 +110040 #ifndef NO_QSTR
Jim Mussared64c79a52023-02-15 16:09:04 +110041#define QDEF0(id, hash, len, str) id,
42#define QDEF1(id, hash, len, str)
Damien George69661f32020-02-27 15:36:53 +110043 #include "genhdr/qstrdefs.generated.h"
Jim Mussared64c79a52023-02-15 16:09:04 +110044#undef QDEF0
45#undef QDEF1
46 #endif
47 MP_QSTRnumber_of_static,
48 MP_QSTRstart_of_main = MP_QSTRnumber_of_static - 1, // unused but shifts the enum counter back one
49
50 #ifndef NO_QSTR
51#define QDEF0(id, hash, len, str)
52#define QDEF1(id, hash, len, str) id,
53 #include "genhdr/qstrdefs.generated.h"
54#undef QDEF0
55#undef QDEF1
Damien George69661f32020-02-27 15:36:53 +110056 #endif
Damien George0a2e9652016-01-31 22:24:16 +000057 MP_QSTRnumber_of, // no underscore so it can't clash with any of the above
Damien George2813cb62014-04-12 17:53:05 +010058};
Damien George55baff42014-01-21 21:40:13 +000059
Damien George6e2fb562015-12-17 12:45:22 +000060typedef size_t qstr;
Damien Georgef2040bf2021-10-22 22:22:47 +110061typedef uint16_t qstr_short_t;
Damien George55baff42014-01-21 21:40:13 +000062
Jim Mussared7ea50392023-02-03 16:42:03 +110063#if MICROPY_QSTR_BYTES_IN_HASH == 0
64// No qstr_hash_t type needed.
65#elif MICROPY_QSTR_BYTES_IN_HASH == 1
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040066typedef uint8_t qstr_hash_t;
67#elif MICROPY_QSTR_BYTES_IN_HASH == 2
68typedef uint16_t qstr_hash_t;
69#else
70#error unimplemented qstr hash decoding
71#endif
72
73#if MICROPY_QSTR_BYTES_IN_LEN == 1
74typedef uint8_t qstr_len_t;
75#elif MICROPY_QSTR_BYTES_IN_LEN == 2
76typedef uint16_t qstr_len_t;
77#else
78#error unimplemented qstr length decoding
79#endif
80
Damien Georgeb4b10fd2015-01-01 23:30:53 +000081typedef struct _qstr_pool_t {
Artyom Skrobovf46a7142021-05-04 03:35:45 -040082 const struct _qstr_pool_t *prev;
Jim Mussared64c79a52023-02-15 16:09:04 +110083 size_t total_prev_len : (8 * sizeof(size_t) - 1);
84 size_t is_sorted : 1;
Damien George25784852015-12-17 12:41:40 +000085 size_t alloc;
86 size_t len;
Jim Mussared7ea50392023-02-03 16:42:03 +110087 #if MICROPY_QSTR_BYTES_IN_HASH
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040088 qstr_hash_t *hashes;
Jim Mussared7ea50392023-02-03 16:42:03 +110089 #endif
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040090 qstr_len_t *lengths;
91 const char *qstrs[];
Damien Georgeb4b10fd2015-01-01 23:30:53 +000092} qstr_pool_t;
93
Damien Georgea8775aa2018-02-15 18:12:01 +110094#define QSTR_TOTAL() (MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len)
Damien George55baff42014-01-21 21:40:13 +000095
96void qstr_init(void);
97
Damien George82b35002022-08-11 16:34:02 +100098size_t qstr_compute_hash(const byte *data, size_t len);
Jim Mussared7ea50392023-02-03 16:42:03 +110099
Josh Lloyd7d58a192019-09-25 17:53:30 +1200100qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTRnull if not found
Damien George5fa93b62014-01-22 14:35:10 +0000101
Damien George55baff42014-01-21 21:40:13 +0000102qstr qstr_from_str(const char *str);
Damien Georgec3f64d92015-11-27 12:23:18 +0000103qstr qstr_from_strn(const char *str, size_t len);
Damien George55baff42014-01-21 21:40:13 +0000104
Damien George40f3c022014-07-03 13:25:24 +0100105mp_uint_t qstr_hash(qstr q);
Damien George2801e6f2015-04-04 15:53:11 +0100106const char *qstr_str(qstr q);
Damien Georgec3f64d92015-11-27 12:23:18 +0000107size_t qstr_len(qstr q);
108const byte *qstr_data(qstr q, size_t *len);
Damien George4d5b28c2014-01-29 18:56:46 +0000109
Damien George25784852015-12-17 12:41:40 +0000110void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes);
Damien Georgeea0461d2015-02-10 11:02:28 +0000111void qstr_dump_data(void);
Damien George9ddbe292014-12-29 01:02:19 +0000112
Jim Mussared154b4eb2019-09-26 22:19:29 +1000113#if MICROPY_ROM_TEXT_COMPRESSION
stijne82aa2a2022-07-14 15:24:27 +0200114void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src);
Jim Mussared154b4eb2019-09-26 22:19:29 +1000115#define MP_IS_COMPRESSED_ROM_STRING(s) (*(byte *)(s) == 0xff)
116#endif
117
Alexander Steffen299bc622017-06-29 23:14:58 +0200118#endif // MICROPY_INCLUDED_PY_QSTR_H