blob: 04ef4c3bf3b839a13f6902afa2b13169938c377c [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 George9ddbe292014-12-29 01:02:19 +000026#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 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//
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 George6942f802015-01-11 17:52:45 +000038// first entry in enum will be MP_QSTR_NULL=0, which indicates invalid/no qstr
Damien George55baff42014-01-21 21:40:13 +000039enum {
Damien George6942f802015-01-11 17:52:45 +000040#define QDEF(id, str) id,
Damien Georged553be52014-04-17 18:03:27 +010041#include "genhdr/qstrdefs.generated.h"
Damien George6942f802015-01-11 17:52:45 +000042#undef QDEF
Damien George55baff42014-01-21 21:40:13 +000043 MP_QSTR_number_of,
Damien George2813cb62014-04-12 17:53:05 +010044};
Damien George55baff42014-01-21 21:40:13 +000045
Damien George40f3c022014-07-03 13:25:24 +010046typedef mp_uint_t qstr;
Damien George55baff42014-01-21 21:40:13 +000047
Damien Georgeb4b10fd2015-01-01 23:30:53 +000048typedef struct _qstr_pool_t {
49 struct _qstr_pool_t *prev;
50 mp_uint_t total_prev_len;
51 mp_uint_t alloc;
52 mp_uint_t len;
53 const byte *qstrs[];
54} qstr_pool_t;
55
Damien George55baff42014-01-21 21:40:13 +000056#define QSTR_FROM_STR_STATIC(s) (qstr_from_strn((s), strlen(s)))
57
58void qstr_init(void);
59
Damien George39dc1452014-10-03 19:52:22 +010060mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len);
61qstr qstr_find_strn(const char *str, mp_uint_t str_len); // returns MP_QSTR_NULL if not found
Damien George5fa93b62014-01-22 14:35:10 +000062
Damien George55baff42014-01-21 21:40:13 +000063qstr qstr_from_str(const char *str);
Damien George39dc1452014-10-03 19:52:22 +010064qstr qstr_from_strn(const char *str, mp_uint_t len);
Damien George55baff42014-01-21 21:40:13 +000065
Damien George4dea9222015-04-09 15:29:54 +000066byte *qstr_build_start(mp_uint_t len, byte **q_ptr);
Damien George55baff42014-01-21 21:40:13 +000067qstr qstr_build_end(byte *q_ptr);
68
Damien George40f3c022014-07-03 13:25:24 +010069mp_uint_t qstr_hash(qstr q);
Damien George2801e6f2015-04-04 15:53:11 +010070const char *qstr_str(qstr q);
Damien George39dc1452014-10-03 19:52:22 +010071mp_uint_t qstr_len(qstr q);
Damien George2801e6f2015-04-04 15:53:11 +010072const byte *qstr_data(qstr q, mp_uint_t *len);
Damien George4d5b28c2014-01-29 18:56:46 +000073
Damien George39dc1452014-10-03 19:52:22 +010074void qstr_pool_info(mp_uint_t *n_pool, mp_uint_t *n_qstr, mp_uint_t *n_str_data_bytes, mp_uint_t *n_total_bytes);
Damien Georgeea0461d2015-02-10 11:02:28 +000075void qstr_dump_data(void);
Damien George9ddbe292014-12-29 01:02:19 +000076
77#endif // __MICROPY_INCLUDED_PY_QSTR_H__