qstr.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "py/mpconfig.h"
  29. #include "py/misc.h"
  30. // See qstrdefs.h for a list of qstr's that are available as constants.
  31. // Reference them as MP_QSTR_xxxx.
  32. //
  33. // Note: it would be possible to define MP_QSTR_xxx as qstr_from_str("xxx")
  34. // for qstrs that are referenced this way, but you don't want to have them in ROM.
  35. // first entry in enum will be MP_QSTRnull=0, which indicates invalid/no qstr
  36. enum {
  37. #ifndef NO_QSTR
  38. #define QDEF0(id, hash, len, str) id,
  39. #define QDEF1(id, hash, len, str)
  40. #include "genhdr/qstrdefs.generated.h"
  41. #undef QDEF0
  42. #undef QDEF1
  43. #endif
  44. MP_QSTRnumber_of_static,
  45. MP_QSTRstart_of_main = MP_QSTRnumber_of_static - 1, // unused but shifts the enum counter back one
  46. #ifndef NO_QSTR
  47. #define QDEF0(id, hash, len, str)
  48. #define QDEF1(id, hash, len, str) id,
  49. #include "genhdr/qstrdefs.generated.h"
  50. #undef QDEF0
  51. #undef QDEF1
  52. #endif
  53. MP_QSTRnumber_of, // no underscore so it can't clash with any of the above
  54. };
  55. typedef size_t qstr;
  56. typedef uint16_t qstr_short_t;
  57. #if MICROPY_QSTR_BYTES_IN_HASH == 1
  58. typedef uint8_t qstr_hash_t;
  59. #elif MICROPY_QSTR_BYTES_IN_HASH == 2
  60. typedef uint16_t qstr_hash_t;
  61. #else
  62. #error unimplemented qstr hash decoding
  63. #endif
  64. #if MICROPY_QSTR_BYTES_IN_LEN == 1
  65. typedef uint8_t qstr_len_t;
  66. #elif MICROPY_QSTR_BYTES_IN_LEN == 2
  67. typedef uint16_t qstr_len_t;
  68. #else
  69. #error unimplemented qstr length decoding
  70. #endif
  71. typedef struct _qstr_pool_t {
  72. const struct _qstr_pool_t *prev;
  73. size_t total_prev_len : (8 * sizeof(size_t) - 1);
  74. size_t is_sorted : 1;
  75. size_t alloc;
  76. size_t len;
  77. qstr_hash_t *hashes;
  78. qstr_len_t *lengths;
  79. const char *qstrs[];
  80. } qstr_pool_t;
  81. #define QSTR_TOTAL() (MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len)
  82. void qstr_init(void);
  83. size_t qstr_compute_hash(const byte *data, size_t len);
  84. qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTRnull if not found
  85. qstr qstr_from_str(const char *str);
  86. qstr qstr_from_strn(const char *str, size_t len);
  87. mp_uint_t qstr_hash(qstr q);
  88. const char *qstr_str(qstr q);
  89. size_t qstr_len(qstr q);
  90. const byte *qstr_data(qstr q, size_t *len);
  91. void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes);
  92. void qstr_dump_data(void);
  93. #if MICROPY_ROM_TEXT_COMPRESSION
  94. void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src);
  95. #define MP_IS_COMPRESSED_ROM_STRING(s) (*(byte *)(s) == 0xff)
  96. #endif
  97. #endif // MICROPY_INCLUDED_PY_QSTR_H