str.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #pragma once
  2. #include "common.h"
  3. #include "memory.h"
  4. #include "vector.h"
  5. namespace pkpy {
  6. inline int utf8len(unsigned char c, bool suppress=false){
  7. if((c & 0b10000000) == 0) return 1;
  8. if((c & 0b11100000) == 0b11000000) return 2;
  9. if((c & 0b11110000) == 0b11100000) return 3;
  10. if((c & 0b11111000) == 0b11110000) return 4;
  11. if((c & 0b11111100) == 0b11111000) return 5;
  12. if((c & 0b11111110) == 0b11111100) return 6;
  13. if(!suppress) throw std::runtime_error("invalid utf8 char: " + std::to_string(c));
  14. return 0;
  15. }
  16. struct Str{
  17. int size;
  18. bool is_ascii;
  19. char* data;
  20. char _inlined[16];
  21. const char* _cached_c_str = nullptr;
  22. bool is_inlined() const { return data == _inlined; }
  23. Str(): size(0), is_ascii(true), data(_inlined) {}
  24. void _alloc(){
  25. if(size <= 16){
  26. this->data = _inlined;
  27. }else{
  28. this->data = (char*)pool64.alloc(size);
  29. }
  30. }
  31. Str(int size, bool is_ascii): size(size), is_ascii(is_ascii) {
  32. _alloc();
  33. }
  34. #define STR_INIT() \
  35. _alloc(); \
  36. for(int i=0; i<size; i++){ \
  37. data[i] = s[i]; \
  38. if(!isascii(s[i])) is_ascii = false; \
  39. }
  40. Str(const std::string& s): size(s.size()), is_ascii(true) {
  41. STR_INIT()
  42. }
  43. Str(std::string_view s): size(s.size()), is_ascii(true) {
  44. STR_INIT()
  45. }
  46. Str(const char* s): size(strlen(s)), is_ascii(true) {
  47. STR_INIT()
  48. }
  49. Str(const char* s, int len): size(len), is_ascii(true) {
  50. STR_INIT()
  51. }
  52. #undef STR_INIT
  53. Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
  54. _alloc();
  55. memcpy(data, other.data, size);
  56. }
  57. Str(Str&& other): size(other.size), is_ascii(other.is_ascii) {
  58. if(other.is_inlined()){
  59. data = _inlined;
  60. for(int i=0; i<size; i++) _inlined[i] = other._inlined[i];
  61. }else{
  62. data = other.data;
  63. other.data = other._inlined;
  64. other.size = 0;
  65. }
  66. }
  67. const char* begin() const { return data; }
  68. const char* end() const { return data + size; }
  69. char operator[](int idx) const { return data[idx]; }
  70. int length() const { return size; }
  71. bool empty() const { return size == 0; }
  72. size_t hash() const{ return std::hash<std::string_view>()(sv()); }
  73. Str& operator=(const Str& other){
  74. if(!is_inlined()) pool64.dealloc(data);
  75. size = other.size;
  76. is_ascii = other.is_ascii;
  77. _alloc();
  78. memcpy(data, other.data, size);
  79. return *this;
  80. }
  81. ~Str(){
  82. if(!is_inlined()) pool64.dealloc(data);
  83. if(_cached_c_str != nullptr) free((void*)_cached_c_str);
  84. }
  85. Str operator+(const Str& other) const {
  86. Str ret(size + other.size, is_ascii && other.is_ascii);
  87. memcpy(ret.data, data, size);
  88. memcpy(ret.data + size, other.data, other.size);
  89. return ret;
  90. }
  91. Str operator+(const char* p) const {
  92. Str other(p);
  93. return *this + other;
  94. }
  95. friend Str operator+(const char* p, const Str& str){
  96. Str other(p);
  97. return other + str;
  98. }
  99. friend std::ostream& operator<<(std::ostream& os, const Str& str){
  100. os.write(str.data, str.size);
  101. return os;
  102. }
  103. bool operator==(const Str& other) const {
  104. if(size != other.size) return false;
  105. return memcmp(data, other.data, size) == 0;
  106. }
  107. bool operator!=(const Str& other) const {
  108. if(size != other.size) return true;
  109. return memcmp(data, other.data, size) != 0;
  110. }
  111. bool operator==(const std::string_view other) const {
  112. if(size != (int)other.size()) return false;
  113. return memcmp(data, other.data(), size) == 0;
  114. }
  115. bool operator!=(const std::string_view other) const {
  116. if(size != (int)other.size()) return true;
  117. return memcmp(data, other.data(), size) != 0;
  118. }
  119. bool operator==(const char* p) const {
  120. return *this == std::string_view(p);
  121. }
  122. bool operator!=(const char* p) const {
  123. return *this != std::string_view(p);
  124. }
  125. bool operator<(const Str& other) const {
  126. int ret = strncmp(data, other.data, std::min(size, other.size));
  127. if(ret != 0) return ret < 0;
  128. return size < other.size;
  129. }
  130. bool operator<(const std::string_view other) const {
  131. int ret = strncmp(data, other.data(), std::min(size, (int)other.size()));
  132. if(ret != 0) return ret < 0;
  133. return size < (int)other.size();
  134. }
  135. friend bool operator<(const std::string_view other, const Str& str){
  136. return str > other;
  137. }
  138. bool operator>(const Str& other) const {
  139. int ret = strncmp(data, other.data, std::min(size, other.size));
  140. if(ret != 0) return ret > 0;
  141. return size > other.size;
  142. }
  143. bool operator<=(const Str& other) const {
  144. int ret = strncmp(data, other.data, std::min(size, other.size));
  145. if(ret != 0) return ret < 0;
  146. return size <= other.size;
  147. }
  148. bool operator>=(const Str& other) const {
  149. int ret = strncmp(data, other.data, std::min(size, other.size));
  150. if(ret != 0) return ret > 0;
  151. return size >= other.size;
  152. }
  153. Str substr(int start, int len) const {
  154. Str ret(len, is_ascii);
  155. memcpy(ret.data, data + start, len);
  156. return ret;
  157. }
  158. Str substr(int start) const {
  159. return substr(start, size - start);
  160. }
  161. char* c_str_dup() const {
  162. char* p = (char*)malloc(size + 1);
  163. memcpy(p, data, size);
  164. p[size] = 0;
  165. return p;
  166. }
  167. const char* c_str(){
  168. if(_cached_c_str == nullptr){
  169. _cached_c_str = c_str_dup();
  170. }
  171. return _cached_c_str;
  172. }
  173. std::string_view sv() const {
  174. return std::string_view(data, size);
  175. }
  176. std::string str() const {
  177. return std::string(data, size);
  178. }
  179. Str lstrip() const {
  180. std::string copy(data, size);
  181. copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
  182. // std::isspace(c) does not working on windows (Debug)
  183. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  184. }));
  185. return Str(copy);
  186. }
  187. Str strip() const {
  188. std::string copy(data, size);
  189. copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
  190. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  191. }));
  192. copy.erase(std::find_if(copy.rbegin(), copy.rend(), [](char c) {
  193. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  194. }).base(), copy.end());
  195. return Str(copy);
  196. }
  197. Str lower() const{
  198. std::string copy(data, size);
  199. std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::tolower(c); });
  200. return Str(copy);
  201. }
  202. Str upper() const{
  203. std::string copy(data, size);
  204. std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::toupper(c); });
  205. return Str(copy);
  206. }
  207. Str escape(bool single_quote=true) const {
  208. std::stringstream ss;
  209. ss << (single_quote ? '\'' : '"');
  210. for (int i=0; i<length(); i++) {
  211. char c = this->operator[](i);
  212. switch (c) {
  213. case '"':
  214. if(!single_quote) ss << '\\';
  215. ss << '"';
  216. break;
  217. case '\'':
  218. if(single_quote) ss << '\\';
  219. ss << '\'';
  220. break;
  221. case '\\': ss << '\\' << '\\'; break;
  222. case '\n': ss << "\\n"; break;
  223. case '\r': ss << "\\r"; break;
  224. case '\t': ss << "\\t"; break;
  225. default:
  226. if ('\x00' <= c && c <= '\x1f') {
  227. ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)c;
  228. } else {
  229. ss << c;
  230. }
  231. }
  232. }
  233. ss << (single_quote ? '\'' : '"');
  234. return ss.str();
  235. }
  236. int index(const Str& sub, int start=0) const {
  237. auto p = std::search(data + start, data + size, sub.data, sub.data + sub.size);
  238. if(p == data + size) return -1;
  239. return p - data;
  240. }
  241. Str replace(const Str& old, const Str& new_, int count=-1) const {
  242. std::stringstream ss;
  243. int start = 0;
  244. while(true){
  245. int i = index(old, start);
  246. if(i == -1) break;
  247. ss << substr(start, i - start);
  248. ss << new_;
  249. start = i + old.size;
  250. if(count != -1 && --count == 0) break;
  251. }
  252. ss << substr(start, size - start);
  253. return ss.str();
  254. }
  255. /*************unicode*************/
  256. int _unicode_index_to_byte(int i) const{
  257. if(is_ascii) return i;
  258. int j = 0;
  259. while(i > 0){
  260. j += utf8len(data[j]);
  261. i--;
  262. }
  263. return j;
  264. }
  265. int _byte_index_to_unicode(int n) const{
  266. if(is_ascii) return n;
  267. int cnt = 0;
  268. for(int i=0; i<n; i++){
  269. if((data[i] & 0xC0) != 0x80) cnt++;
  270. }
  271. return cnt;
  272. }
  273. Str u8_getitem(int i) const{
  274. i = _unicode_index_to_byte(i);
  275. return substr(i, utf8len(data[i]));
  276. }
  277. Str u8_slice(int start, int stop, int step) const{
  278. std::stringstream ss;
  279. if(is_ascii){
  280. for(int i=start; step>0?i<stop:i>stop; i+=step) ss << data[i];
  281. }else{
  282. for(int i=start; step>0?i<stop:i>stop; i+=step) ss << u8_getitem(i);
  283. }
  284. return ss.str();
  285. }
  286. int u8_length() const {
  287. return _byte_index_to_unicode(size);
  288. }
  289. };
  290. template<typename... Args>
  291. inline std::string fmt(Args&&... args) {
  292. std::stringstream ss;
  293. (ss << ... << args);
  294. return ss.str();
  295. }
  296. const uint32_t kLoRangeA[] = {170,186,443,448,660,1488,1519,1568,1601,1646,1649,1749,1774,1786,1791,1808,1810,1869,1969,1994,2048,2112,2144,2208,2230,2308,2365,2384,2392,2418,2437,2447,2451,2474,2482,2486,2493,2510,2524,2527,2544,2556,2565,2575,2579,2602,2610,2613,2616,2649,2654,2674,2693,2703,2707,2730,2738,2741,2749,2768,2784,2809,2821,2831,2835,2858,2866,2869,2877,2908,2911,2929,2947,2949,2958,2962,2969,2972,2974,2979,2984,2990,3024,3077,3086,3090,3114,3133,3160,3168,3200,3205,3214,3218,3242,3253,3261,3294,3296,3313,3333,3342,3346,3389,3406,3412,3423,3450,3461,3482,3507,3517,3520,3585,3634,3648,3713,3716,3718,3724,3749,3751,3762,3773,3776,3804,3840,3904,3913,3976,4096,4159,4176,4186,4193,4197,4206,4213,4238,4352,4682,4688,4696,4698,4704,4746,4752,4786,4792,4800,4802,4808,4824,4882,4888,4992,5121,5743,5761,5792,5873,5888,5902,5920,5952,5984,5998,6016,6108,6176,6212,6272,6279,6314,6320,6400,6480,6512,6528,6576,6656,6688,6917,6981,7043,7086,7098,7168,7245,7258,7401,7406,7413,7418,8501,11568,11648,11680,11688,11696,11704,11712,11720,11728,11736,12294,12348,12353,12447,12449,12543,12549,12593,12704,12784,13312,19968,40960,40982,42192,42240,42512,42538,42606,42656,42895,42999,43003,43011,43015,43020,43072,43138,43250,43259,43261,43274,43312,43360,43396,43488,43495,43514,43520,43584,43588,43616,43633,43642,43646,43697,43701,43705,43712,43714,43739,43744,43762,43777,43785,43793,43808,43816,43968,44032,55216,55243,63744,64112,64285,64287,64298,64312,64318,64320,64323,64326,64467,64848,64914,65008,65136,65142,65382,65393,65440,65474,65482,65490,65498,65536,65549,65576,65596,65599,65616,65664,66176,66208,66304,66349,66370,66384,66432,66464,66504,66640,66816,66864,67072,67392,67424,67584,67592,67594,67639,67644,67647,67680,67712,67808,67828,67840,67872,67968,68030,68096,68112,68117,68121,68192,68224,68288,68297,68352,68416,68448,68480,68608,68864,69376,69415,69424,69600,69635,69763,69840,69891,69956,69968,70006,70019,70081,70106,70108,70144,70163,70272,70280,70282,70287,70303,70320,70405,70415,70419,70442,70450,70453,70461,70480,70493,70656,70727,70751,70784,70852,70855,71040,71128,71168,71236,71296,71352,71424,71680,71935,72096,72106,72161,72163,72192,72203,72250,72272,72284,72349,72384,72704,72714,72768,72818,72960,72968,72971,73030,73056,73063,73066,73112,73440,73728,74880,77824,82944,92160,92736,92880,92928,93027,93053,93952,94032,94208,100352,110592,110928,110948,110960,113664,113776,113792,113808,123136,123214,123584,124928,126464,126469,126497,126500,126503,126505,126516,126521,126523,126530,126535,126537,126539,126541,126545,126548,126551,126553,126555,126557,126559,126561,126564,126567,126572,126580,126585,126590,126592,126603,126625,126629,126635,131072,173824,177984,178208,183984,194560};
  297. const uint32_t kLoRangeB[] = {170,186,443,451,660,1514,1522,1599,1610,1647,1747,1749,1775,1788,1791,1808,1839,1957,1969,2026,2069,2136,2154,2228,2237,2361,2365,2384,2401,2432,2444,2448,2472,2480,2482,2489,2493,2510,2525,2529,2545,2556,2570,2576,2600,2608,2611,2614,2617,2652,2654,2676,2701,2705,2728,2736,2739,2745,2749,2768,2785,2809,2828,2832,2856,2864,2867,2873,2877,2909,2913,2929,2947,2954,2960,2965,2970,2972,2975,2980,2986,3001,3024,3084,3088,3112,3129,3133,3162,3169,3200,3212,3216,3240,3251,3257,3261,3294,3297,3314,3340,3344,3386,3389,3406,3414,3425,3455,3478,3505,3515,3517,3526,3632,3635,3653,3714,3716,3722,3747,3749,3760,3763,3773,3780,3807,3840,3911,3948,3980,4138,4159,4181,4189,4193,4198,4208,4225,4238,4680,4685,4694,4696,4701,4744,4749,4784,4789,4798,4800,4805,4822,4880,4885,4954,5007,5740,5759,5786,5866,5880,5900,5905,5937,5969,5996,6000,6067,6108,6210,6264,6276,6312,6314,6389,6430,6509,6516,6571,6601,6678,6740,6963,6987,7072,7087,7141,7203,7247,7287,7404,7411,7414,7418,8504,11623,11670,11686,11694,11702,11710,11718,11726,11734,11742,12294,12348,12438,12447,12538,12543,12591,12686,12730,12799,19893,40943,40980,42124,42231,42507,42527,42539,42606,42725,42895,42999,43009,43013,43018,43042,43123,43187,43255,43259,43262,43301,43334,43388,43442,43492,43503,43518,43560,43586,43595,43631,43638,43642,43695,43697,43702,43709,43712,43714,43740,43754,43762,43782,43790,43798,43814,43822,44002,55203,55238,55291,64109,64217,64285,64296,64310,64316,64318,64321,64324,64433,64829,64911,64967,65019,65140,65276,65391,65437,65470,65479,65487,65495,65500,65547,65574,65594,65597,65613,65629,65786,66204,66256,66335,66368,66377,66421,66461,66499,66511,66717,66855,66915,67382,67413,67431,67589,67592,67637,67640,67644,67669,67702,67742,67826,67829,67861,67897,68023,68031,68096,68115,68119,68149,68220,68252,68295,68324,68405,68437,68466,68497,68680,68899,69404,69415,69445,69622,69687,69807,69864,69926,69956,70002,70006,70066,70084,70106,70108,70161,70187,70278,70280,70285,70301,70312,70366,70412,70416,70440,70448,70451,70457,70461,70480,70497,70708,70730,70751,70831,70853,70855,71086,71131,71215,71236,71338,71352,71450,71723,71935,72103,72144,72161,72163,72192,72242,72250,72272,72329,72349,72440,72712,72750,72768,72847,72966,72969,73008,73030,73061,73064,73097,73112,73458,74649,75075,78894,83526,92728,92766,92909,92975,93047,93071,94026,94032,100343,101106,110878,110930,110951,111355,113770,113788,113800,113817,123180,123214,123627,125124,126467,126495,126498,126500,126503,126514,126519,126521,126523,126530,126535,126537,126539,126543,126546,126548,126551,126553,126555,126557,126559,126562,126564,126570,126578,126583,126588,126590,126601,126619,126627,126633,126651,173782,177972,178205,183969,191456,195101};
  298. inline bool is_unicode_Lo_char(uint32_t c) {
  299. auto index = std::lower_bound(kLoRangeA, kLoRangeA + 476, c) - kLoRangeA;
  300. if(c == kLoRangeA[index]) return true;
  301. index -= 1;
  302. if(index < 0) return false;
  303. return c >= kLoRangeA[index] && c <= kLoRangeB[index];
  304. }
  305. struct StrName {
  306. uint16_t index;
  307. StrName(): index(0) {}
  308. explicit StrName(uint16_t index): index(index) {}
  309. StrName(const char* s): index(get(s).index) {}
  310. StrName(const Str& s){
  311. index = get(s.sv()).index;
  312. }
  313. std::string_view sv() const { return _r_interned[index-1].sv(); }
  314. bool empty() const { return index == 0; }
  315. friend std::ostream& operator<<(std::ostream& os, const StrName& sn){
  316. return os << sn.sv();
  317. }
  318. static bool is_valid(int index) {
  319. // check _r_interned[index-1] is valid
  320. return index > 0 && index <= _r_interned.size();
  321. }
  322. Str escape() const {
  323. return _r_interned[index-1].escape();
  324. }
  325. bool operator==(const StrName& other) const noexcept {
  326. return this->index == other.index;
  327. }
  328. bool operator!=(const StrName& other) const noexcept {
  329. return this->index != other.index;
  330. }
  331. bool operator<(const StrName& other) const noexcept {
  332. return this->index < other.index;
  333. }
  334. bool operator>(const StrName& other) const noexcept {
  335. return this->index > other.index;
  336. }
  337. inline static std::map<Str, uint16_t, std::less<>> _interned;
  338. inline static std::vector<Str> _r_interned;
  339. static StrName get(std::string_view s){
  340. auto it = _interned.find(s);
  341. if(it != _interned.end()) return StrName(it->second);
  342. uint16_t index = (uint16_t)(_r_interned.size() + 1);
  343. _interned[s] = index;
  344. _r_interned.push_back(s);
  345. return StrName(index);
  346. }
  347. };
  348. struct FastStrStream{
  349. pod_vector<const Str*> parts;
  350. FastStrStream& operator<<(const Str& s){
  351. parts.push_back(&s);
  352. return *this;
  353. }
  354. bool empty() const { return parts.empty(); }
  355. Str str() const{
  356. int len = 0;
  357. bool is_ascii = true;
  358. for(auto& s: parts){
  359. len += s->length();
  360. is_ascii &= s->is_ascii;
  361. }
  362. Str result(len, is_ascii);
  363. char* p = result.data;
  364. for(auto& s: parts){
  365. memcpy(p, s->data, s->length());
  366. p += s->length();
  367. }
  368. return result;
  369. }
  370. };
  371. struct CString{
  372. const char* ptr;
  373. CString(const char* ptr): ptr(ptr) {}
  374. operator const char*() const { return ptr; }
  375. };
  376. // unary operators
  377. const StrName __repr__ = StrName::get("__repr__");
  378. const StrName __str__ = StrName::get("__str__");
  379. const StrName __hash__ = StrName::get("__hash__"); // unused
  380. const StrName __len__ = StrName::get("__len__");
  381. const StrName __iter__ = StrName::get("__iter__");
  382. const StrName __next__ = StrName::get("__next__"); // unused
  383. const StrName __json__ = StrName::get("__json__");
  384. const StrName __neg__ = StrName::get("__neg__"); // unused
  385. const StrName __bool__ = StrName::get("__bool__"); // unused
  386. // logical operators
  387. const StrName __eq__ = StrName::get("__eq__");
  388. const StrName __lt__ = StrName::get("__lt__");
  389. const StrName __le__ = StrName::get("__le__");
  390. const StrName __gt__ = StrName::get("__gt__");
  391. const StrName __ge__ = StrName::get("__ge__");
  392. const StrName __contains__ = StrName::get("__contains__");
  393. // binary operators
  394. const StrName __add__ = StrName::get("__add__");
  395. const StrName __radd__ = StrName::get("__radd__");
  396. const StrName __sub__ = StrName::get("__sub__");
  397. const StrName __rsub__ = StrName::get("__rsub__");
  398. const StrName __mul__ = StrName::get("__mul__");
  399. const StrName __rmul__ = StrName::get("__rmul__");
  400. const StrName __truediv__ = StrName::get("__truediv__");
  401. const StrName __floordiv__ = StrName::get("__floordiv__");
  402. const StrName __mod__ = StrName::get("__mod__");
  403. const StrName __pow__ = StrName::get("__pow__");
  404. const StrName __matmul__ = StrName::get("__matmul__");
  405. const StrName __lshift__ = StrName::get("__lshift__");
  406. const StrName __rshift__ = StrName::get("__rshift__");
  407. const StrName __and__ = StrName::get("__and__");
  408. const StrName __or__ = StrName::get("__or__");
  409. const StrName __xor__ = StrName::get("__xor__");
  410. // indexer
  411. const StrName __getitem__ = StrName::get("__getitem__");
  412. const StrName __setitem__ = StrName::get("__setitem__");
  413. const StrName __delitem__ = StrName::get("__delitem__");
  414. #define DEF_SNAME(name) const static StrName name(#name)
  415. } // namespace pkpy