str.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include "common.h"
  3. #include "memory.h"
  4. #include "vector.h"
  5. namespace pkpy {
  6. int utf8len(unsigned char c, bool suppress=false);
  7. struct Str{
  8. int size;
  9. bool is_ascii;
  10. char* data;
  11. char _inlined[16];
  12. mutable const char* _cached_c_str = nullptr;
  13. bool is_inlined() const { return data == _inlined; }
  14. Str(): size(0), is_ascii(true), data(_inlined) {}
  15. Str(int size, bool is_ascii);
  16. Str(const std::string& s);
  17. Str(std::string_view s);
  18. Str(std::nullptr_t) { FATAL_ERROR(); }
  19. Str(const char* s);
  20. Str(const char* s, int len);
  21. Str(const Str& other);
  22. Str(Str&& other);
  23. void _alloc();
  24. const char* begin() const { return data; }
  25. const char* end() const { return data + size; }
  26. char operator[](int idx) const { return data[idx]; }
  27. int length() const { return size; }
  28. bool empty() const { return size == 0; }
  29. size_t hash() const{ return std::hash<std::string_view>()(sv()); }
  30. Str& operator=(const Str& other);
  31. Str operator+(const Str& other) const;
  32. Str operator+(const char* p) const;
  33. bool operator==(const Str& other) const;
  34. bool operator!=(const Str& other) const;
  35. bool operator==(const std::string_view other) const;
  36. bool operator!=(const std::string_view other) const;
  37. bool operator==(const char* p) const;
  38. bool operator!=(const char* p) const;
  39. bool operator<(const Str& other) const;
  40. bool operator>(const Str& other) const;
  41. bool operator<=(const Str& other) const;
  42. bool operator>=(const Str& other) const;
  43. bool operator<(const std::string_view other) const;
  44. ~Str();
  45. friend Str operator+(const char* p, const Str& str);
  46. friend std::ostream& operator<<(std::ostream& os, const Str& str);
  47. friend bool operator<(const std::string_view other, const Str& str);
  48. Str substr(int start, int len) const;
  49. Str substr(int start) const;
  50. char* c_str_dup() const;
  51. const char* c_str() const;
  52. std::string_view sv() const;
  53. std::string str() const;
  54. Str lstrip() const;
  55. Str strip() const;
  56. Str lower() const;
  57. Str upper() const;
  58. Str escape(bool single_quote=true) const;
  59. int index(const Str& sub, int start=0) const;
  60. Str replace(char old, char new_) const;
  61. Str replace(const Str& old, const Str& new_, int count=-1) const;
  62. std::vector<std::string_view> split(const Str& sep) const;
  63. int count(const Str& sub) const;
  64. /*************unicode*************/
  65. int _unicode_index_to_byte(int i) const;
  66. int _byte_index_to_unicode(int n) const;
  67. Str u8_getitem(int i) const;
  68. Str u8_slice(int start, int stop, int step) const;
  69. int u8_length() const;
  70. };
  71. template<typename... Args>
  72. std::string fmt(Args&&... args) {
  73. std::stringstream ss;
  74. (ss << ... << args);
  75. return ss.str();
  76. }
  77. struct StrName {
  78. uint16_t index;
  79. StrName();
  80. explicit StrName(uint16_t index);
  81. StrName(const char* s);
  82. StrName(const Str& s);
  83. std::string_view sv() const;
  84. bool empty() const { return index == 0; }
  85. friend std::ostream& operator<<(std::ostream& os, const StrName& sn);
  86. Str escape() const;
  87. bool operator==(const StrName& other) const noexcept {
  88. return this->index == other.index;
  89. }
  90. bool operator!=(const StrName& other) const noexcept {
  91. return this->index != other.index;
  92. }
  93. bool operator<(const StrName& other) const noexcept {
  94. return this->index < other.index;
  95. }
  96. bool operator>(const StrName& other) const noexcept {
  97. return this->index > other.index;
  98. }
  99. static bool is_valid(int index);
  100. static StrName get(std::string_view s);
  101. static std::map<std::string, uint16_t, std::less<>>& _interned();
  102. static std::map<uint16_t, std::string>& _r_interned();
  103. static uint32_t _pesudo_random_index;
  104. };
  105. struct FastStrStream{
  106. pod_vector<const Str*> parts;
  107. FastStrStream& operator<<(const Str& s);
  108. bool empty() const { return parts.empty(); }
  109. Str str() const;
  110. };
  111. struct CString{
  112. const char* ptr;
  113. CString(const char* ptr): ptr(ptr) {}
  114. operator const char*() const { return ptr; }
  115. };
  116. // unary operators
  117. const StrName __repr__ = StrName::get("__repr__");
  118. const StrName __str__ = StrName::get("__str__");
  119. const StrName __hash__ = StrName::get("__hash__"); // unused
  120. const StrName __len__ = StrName::get("__len__");
  121. const StrName __iter__ = StrName::get("__iter__");
  122. const StrName __next__ = StrName::get("__next__"); // unused
  123. const StrName __json__ = StrName::get("__json__");
  124. const StrName __neg__ = StrName::get("__neg__"); // unused
  125. const StrName __bool__ = StrName::get("__bool__"); // unused
  126. // logical operators
  127. const StrName __eq__ = StrName::get("__eq__");
  128. const StrName __lt__ = StrName::get("__lt__");
  129. const StrName __le__ = StrName::get("__le__");
  130. const StrName __gt__ = StrName::get("__gt__");
  131. const StrName __ge__ = StrName::get("__ge__");
  132. const StrName __contains__ = StrName::get("__contains__");
  133. // binary operators
  134. const StrName __add__ = StrName::get("__add__");
  135. const StrName __radd__ = StrName::get("__radd__");
  136. const StrName __sub__ = StrName::get("__sub__");
  137. const StrName __rsub__ = StrName::get("__rsub__");
  138. const StrName __mul__ = StrName::get("__mul__");
  139. const StrName __rmul__ = StrName::get("__rmul__");
  140. const StrName __truediv__ = StrName::get("__truediv__");
  141. const StrName __floordiv__ = StrName::get("__floordiv__");
  142. const StrName __mod__ = StrName::get("__mod__");
  143. const StrName __pow__ = StrName::get("__pow__");
  144. const StrName __matmul__ = StrName::get("__matmul__");
  145. const StrName __lshift__ = StrName::get("__lshift__");
  146. const StrName __rshift__ = StrName::get("__rshift__");
  147. const StrName __and__ = StrName::get("__and__");
  148. const StrName __or__ = StrName::get("__or__");
  149. const StrName __xor__ = StrName::get("__xor__");
  150. const StrName __invert__ = StrName::get("__invert__");
  151. // indexer
  152. const StrName __getitem__ = StrName::get("__getitem__");
  153. const StrName __setitem__ = StrName::get("__setitem__");
  154. const StrName __delitem__ = StrName::get("__delitem__");
  155. // specials
  156. const StrName __new__ = StrName::get("__new__");
  157. const StrName __init__ = StrName::get("__init__");
  158. const StrName __call__ = StrName::get("__call__");
  159. const StrName __divmod__ = StrName::get("__divmod__");
  160. const StrName __enter__ = StrName::get("__enter__");
  161. const StrName __exit__ = StrName::get("__exit__");
  162. const StrName __name__ = StrName::get("__name__");
  163. const StrName __all__ = StrName::get("__all__");
  164. const StrName __package__ = StrName::get("__package__");
  165. const StrName __path__ = StrName::get("__path__");
  166. const StrName __class__ = StrName::get("__class__");
  167. const StrName pk_id_add = StrName::get("add");
  168. const StrName pk_id_set = StrName::get("set");
  169. const StrName pk_id_eval = StrName::get("eval");
  170. #define DEF_SNAME(name) const static StrName name(#name)
  171. } // namespace pkpy