1 /*
2  * This file is part of the MicroPython 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  */
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"
31 
32 // See qstrdefs.h for a list of qstr's that are available as constants.
33 // Reference them as MP_QSTR_xxxx.
34 //
35 // Note: it would be possible to define MP_QSTR_xxx as qstr_from_str("xxx")
36 // for qstrs that are referenced this way, but you don't want to have them in ROM.
37 
38 // first entry in enum will be MP_QSTRnull=0, which indicates invalid/no qstr
39 enum {
40     #ifndef NO_QSTR
41 #define QDEF(id, str) id,
42     #include "genhdr/qstrdefs.generated.h"
43 #undef QDEF
44     #endif
45     MP_QSTRnumber_of, // no underscore so it can't clash with any of the above
46 };
47 
48 typedef size_t qstr;
49 
50 typedef struct _qstr_pool_t {
51     struct _qstr_pool_t *prev;
52     size_t total_prev_len;
53     size_t alloc;
54     size_t len;
55     const byte *qstrs[];
56 } qstr_pool_t;
57 
58 #define QSTR_TOTAL() (MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len)
59 
60 void qstr_init(void);
61 
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_QSTRnull if not found
64 
65 qstr qstr_from_str(const char *str);
66 qstr qstr_from_strn(const char *str, size_t len);
67 
68 mp_uint_t qstr_hash(qstr q);
69 const char *qstr_str(qstr q);
70 size_t qstr_len(qstr q);
71 const byte *qstr_data(qstr q, size_t *len);
72 
73 void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes);
74 void qstr_dump_data(void);
75 
76 #if MICROPY_ROM_TEXT_COMPRESSION
77 void mp_decompress_rom_string(byte *dst, mp_rom_error_text_t src);
78 #define MP_IS_COMPRESSED_ROM_STRING(s) (*(byte *)(s) == 0xff)
79 #endif
80 
81 #endif // MICROPY_INCLUDED_PY_QSTR_H
82