str.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(const char* s);
  19. Str(const char* s, int len);
  20. Str(const Str& other);
  21. Str(Str&& other);
  22. void _alloc();
  23. const char* begin() const { return data; }
  24. const char* end() const { return data + size; }
  25. char operator[](int idx) const { return data[idx]; }
  26. int length() const { return size; }
  27. bool empty() const { return size == 0; }
  28. size_t hash() const{ return std::hash<std::string_view>()(sv()); }
  29. Str& operator=(const Str& other);
  30. Str operator+(const Str& other) const;
  31. Str operator+(const char* p) const;
  32. bool operator==(const Str& other) const;
  33. bool operator!=(const Str& other) const;
  34. bool operator==(const std::string_view other) const;
  35. bool operator!=(const std::string_view other) const;
  36. bool operator==(const char* p) const;
  37. bool operator!=(const char* p) const;
  38. bool operator<(const Str& other) 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 std::string_view other) const;
  43. ~Str();
  44. friend Str operator+(const char* p, const Str& str);
  45. friend std::ostream& operator<<(std::ostream& os, const Str& str);
  46. friend bool operator<(const std::string_view other, const Str& str);
  47. Str substr(int start, int len) const;
  48. Str substr(int start) const;
  49. char* c_str_dup() const;
  50. const char* c_str() const;
  51. std::string_view sv() const;
  52. std::string str() const;
  53. Str lstrip() const;
  54. Str strip() const;
  55. Str lower() const;
  56. Str upper() const;
  57. Str escape(bool single_quote=true) const;
  58. int index(const Str& sub, int start=0) const;
  59. Str replace(const Str& old, const Str& new_, int count=-1) const;
  60. /*************unicode*************/
  61. int _unicode_index_to_byte(int i) const;
  62. int _byte_index_to_unicode(int n) const;
  63. Str u8_getitem(int i) const;
  64. Str u8_slice(int start, int stop, int step) const;
  65. int u8_length() const;
  66. };
  67. template<typename... Args>
  68. std::string fmt(Args&&... args) {
  69. std::stringstream ss;
  70. (ss << ... << args);
  71. return ss.str();
  72. }
  73. struct StrName {
  74. uint16_t index;
  75. StrName();
  76. explicit StrName(uint16_t index);
  77. StrName(const char* s);
  78. StrName(const Str& s);
  79. std::string_view sv() const;
  80. bool empty() const { return index == 0; }
  81. friend std::ostream& operator<<(std::ostream& os, const StrName& sn);
  82. Str escape() const;
  83. bool operator==(const StrName& other) const noexcept {
  84. return this->index == other.index;
  85. }
  86. bool operator!=(const StrName& other) const noexcept {
  87. return this->index != other.index;
  88. }
  89. bool operator<(const StrName& other) const noexcept {
  90. return this->index < other.index;
  91. }
  92. bool operator>(const StrName& other) const noexcept {
  93. return this->index > other.index;
  94. }
  95. static bool is_valid(int index);
  96. static StrName get(std::string_view s);
  97. static std::map<std::string, uint16_t, std::less<>>& _interned();
  98. static std::vector<std::string>& _r_interned();
  99. };
  100. struct FastStrStream{
  101. pod_vector<const Str*> parts;
  102. FastStrStream& operator<<(const Str& s);
  103. bool empty() const { return parts.empty(); }
  104. Str str() const;
  105. };
  106. struct CString{
  107. const char* ptr;
  108. CString(const char* ptr): ptr(ptr) {}
  109. operator const char*() const { return ptr; }
  110. };
  111. // unary operators
  112. const StrName __repr__ = StrName::get("__repr__");
  113. const StrName __str__ = StrName::get("__str__");
  114. const StrName __hash__ = StrName::get("__hash__"); // unused
  115. const StrName __len__ = StrName::get("__len__");
  116. const StrName __iter__ = StrName::get("__iter__");
  117. const StrName __next__ = StrName::get("__next__"); // unused
  118. const StrName __json__ = StrName::get("__json__");
  119. const StrName __neg__ = StrName::get("__neg__"); // unused
  120. const StrName __bool__ = StrName::get("__bool__"); // unused
  121. // logical operators
  122. const StrName __eq__ = StrName::get("__eq__");
  123. const StrName __lt__ = StrName::get("__lt__");
  124. const StrName __le__ = StrName::get("__le__");
  125. const StrName __gt__ = StrName::get("__gt__");
  126. const StrName __ge__ = StrName::get("__ge__");
  127. const StrName __contains__ = StrName::get("__contains__");
  128. // binary operators
  129. const StrName __add__ = StrName::get("__add__");
  130. const StrName __radd__ = StrName::get("__radd__");
  131. const StrName __sub__ = StrName::get("__sub__");
  132. const StrName __rsub__ = StrName::get("__rsub__");
  133. const StrName __mul__ = StrName::get("__mul__");
  134. const StrName __rmul__ = StrName::get("__rmul__");
  135. const StrName __truediv__ = StrName::get("__truediv__");
  136. const StrName __floordiv__ = StrName::get("__floordiv__");
  137. const StrName __mod__ = StrName::get("__mod__");
  138. const StrName __pow__ = StrName::get("__pow__");
  139. const StrName __matmul__ = StrName::get("__matmul__");
  140. const StrName __lshift__ = StrName::get("__lshift__");
  141. const StrName __rshift__ = StrName::get("__rshift__");
  142. const StrName __and__ = StrName::get("__and__");
  143. const StrName __or__ = StrName::get("__or__");
  144. const StrName __xor__ = StrName::get("__xor__");
  145. const StrName __invert__ = StrName::get("__invert__");
  146. // indexer
  147. const StrName __getitem__ = StrName::get("__getitem__");
  148. const StrName __setitem__ = StrName::get("__setitem__");
  149. const StrName __delitem__ = StrName::get("__delitem__");
  150. #define DEF_SNAME(name) const static StrName name(#name)
  151. } // namespace pkpy