str.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "pocketpy/str.h"
  2. namespace pkpy {
  3. int utf8len(unsigned char c, bool suppress){
  4. if((c & 0b10000000) == 0) return 1;
  5. if((c & 0b11100000) == 0b11000000) return 2;
  6. if((c & 0b11110000) == 0b11100000) return 3;
  7. if((c & 0b11111000) == 0b11110000) return 4;
  8. if((c & 0b11111100) == 0b11111000) return 5;
  9. if((c & 0b11111110) == 0b11111100) return 6;
  10. if(!suppress) throw std::runtime_error("invalid utf8 char: " + std::to_string(c));
  11. return 0;
  12. }
  13. Str::Str(int size, bool is_ascii): size(size), is_ascii(is_ascii) {
  14. _alloc();
  15. }
  16. #define STR_INIT() \
  17. _alloc(); \
  18. for(int i=0; i<size; i++){ \
  19. data[i] = s[i]; \
  20. if(!isascii(s[i])) is_ascii = false; \
  21. }
  22. Str::Str(const std::string& s): size(s.size()), is_ascii(true) {
  23. STR_INIT()
  24. }
  25. Str::Str(std::string_view s): size(s.size()), is_ascii(true) {
  26. STR_INIT()
  27. }
  28. Str::Str(const char* s): size(strlen(s)), is_ascii(true) {
  29. STR_INIT()
  30. }
  31. Str::Str(const char* s, int len): size(len), is_ascii(true) {
  32. STR_INIT()
  33. }
  34. #undef STR_INIT
  35. Str::Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
  36. _alloc();
  37. memcpy(data, other.data, size);
  38. }
  39. Str::Str(Str&& other): size(other.size), is_ascii(other.is_ascii) {
  40. if(other.is_inlined()){
  41. data = _inlined;
  42. for(int i=0; i<size; i++) _inlined[i] = other._inlined[i];
  43. }else{
  44. data = other.data;
  45. other.data = other._inlined;
  46. other.size = 0;
  47. }
  48. }
  49. Str operator+(const char* p, const Str& str){
  50. Str other(p);
  51. return other + str;
  52. }
  53. std::ostream& operator<<(std::ostream& os, const Str& str){
  54. return os << str.sv();
  55. }
  56. bool operator<(const std::string_view other, const Str& str){
  57. return other < str.sv();
  58. }
  59. void Str::_alloc(){
  60. if(size <= 16){
  61. this->data = _inlined;
  62. }else{
  63. this->data = (char*)pool64_alloc(size);
  64. }
  65. }
  66. Str& Str::operator=(const Str& other){
  67. if(!is_inlined()) pool64_dealloc(data);
  68. size = other.size;
  69. is_ascii = other.is_ascii;
  70. _cached_c_str = nullptr;
  71. _alloc();
  72. memcpy(data, other.data, size);
  73. return *this;
  74. }
  75. Str Str::operator+(const Str& other) const {
  76. Str ret(size + other.size, is_ascii && other.is_ascii);
  77. memcpy(ret.data, data, size);
  78. memcpy(ret.data + size, other.data, other.size);
  79. return ret;
  80. }
  81. Str Str::operator+(const char* p) const {
  82. Str other(p);
  83. return *this + other;
  84. }
  85. bool Str::operator==(const Str& other) const {
  86. if(size != other.size) return false;
  87. return memcmp(data, other.data, size) == 0;
  88. }
  89. bool Str::operator!=(const Str& other) const {
  90. if(size != other.size) return true;
  91. return memcmp(data, other.data, size) != 0;
  92. }
  93. bool Str::operator==(const std::string_view other) const {
  94. if(size != (int)other.size()) return false;
  95. return memcmp(data, other.data(), size) == 0;
  96. }
  97. bool Str::operator!=(const std::string_view other) const {
  98. if(size != (int)other.size()) return true;
  99. return memcmp(data, other.data(), size) != 0;
  100. }
  101. bool Str::operator==(const char* p) const {
  102. return *this == std::string_view(p);
  103. }
  104. bool Str::operator!=(const char* p) const {
  105. return *this != std::string_view(p);
  106. }
  107. bool Str::operator<(const Str& other) const {
  108. return this->sv() < other.sv();
  109. }
  110. bool Str::operator<(const std::string_view other) const {
  111. return this->sv() < other;
  112. }
  113. bool Str::operator>(const Str& other) const {
  114. return this->sv() > other.sv();
  115. }
  116. bool Str::operator<=(const Str& other) const {
  117. return this->sv() <= other.sv();
  118. }
  119. bool Str::operator>=(const Str& other) const {
  120. return this->sv() >= other.sv();
  121. }
  122. Str::~Str(){
  123. if(!is_inlined()) pool64_dealloc(data);
  124. if(_cached_c_str != nullptr) free((void*)_cached_c_str);
  125. }
  126. Str Str::substr(int start, int len) const {
  127. Str ret(len, is_ascii);
  128. memcpy(ret.data, data + start, len);
  129. return ret;
  130. }
  131. Str Str::substr(int start) const {
  132. return substr(start, size - start);
  133. }
  134. char* Str::c_str_dup() const {
  135. char* p = (char*)malloc(size + 1);
  136. memcpy(p, data, size);
  137. p[size] = 0;
  138. return p;
  139. }
  140. const char* Str::c_str() const{
  141. if(_cached_c_str == nullptr){
  142. _cached_c_str = c_str_dup();
  143. }
  144. return _cached_c_str;
  145. }
  146. std::string_view Str::sv() const {
  147. return std::string_view(data, size);
  148. }
  149. std::string Str::str() const {
  150. return std::string(data, size);
  151. }
  152. Str Str::lstrip() const {
  153. std::string copy(data, size);
  154. copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
  155. // std::isspace(c) does not working on windows (Debug)
  156. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  157. }));
  158. return Str(copy);
  159. }
  160. Str Str::strip() const {
  161. std::string copy(data, size);
  162. copy.erase(copy.begin(), std::find_if(copy.begin(), copy.end(), [](char c) {
  163. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  164. }));
  165. copy.erase(std::find_if(copy.rbegin(), copy.rend(), [](char c) {
  166. return c != ' ' && c != '\t' && c != '\r' && c != '\n';
  167. }).base(), copy.end());
  168. return Str(copy);
  169. }
  170. Str Str::lower() const{
  171. std::string copy(data, size);
  172. std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::tolower(c); });
  173. return Str(copy);
  174. }
  175. Str Str::upper() const{
  176. std::string copy(data, size);
  177. std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::toupper(c); });
  178. return Str(copy);
  179. }
  180. Str Str::escape(bool single_quote) const {
  181. std::stringstream ss;
  182. ss << (single_quote ? '\'' : '"');
  183. for (int i=0; i<length(); i++) {
  184. char c = this->operator[](i);
  185. switch (c) {
  186. case '"':
  187. if(!single_quote) ss << '\\';
  188. ss << '"';
  189. break;
  190. case '\'':
  191. if(single_quote) ss << '\\';
  192. ss << '\'';
  193. break;
  194. case '\\': ss << '\\' << '\\'; break;
  195. case '\n': ss << "\\n"; break;
  196. case '\r': ss << "\\r"; break;
  197. case '\t': ss << "\\t"; break;
  198. default:
  199. if ('\x00' <= c && c <= '\x1f') {
  200. ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)c;
  201. } else {
  202. ss << c;
  203. }
  204. }
  205. }
  206. ss << (single_quote ? '\'' : '"');
  207. return ss.str();
  208. }
  209. int Str::index(const Str& sub, int start) const {
  210. auto p = std::search(data + start, data + size, sub.data, sub.data + sub.size);
  211. if(p == data + size) return -1;
  212. return p - data;
  213. }
  214. Str Str::replace(const Str& old, const Str& new_, int count) const {
  215. std::stringstream ss;
  216. int start = 0;
  217. while(true){
  218. int i = index(old, start);
  219. if(i == -1) break;
  220. ss << substr(start, i - start);
  221. ss << new_;
  222. start = i + old.size;
  223. if(count != -1 && --count == 0) break;
  224. }
  225. ss << substr(start, size - start);
  226. return ss.str();
  227. }
  228. int Str::_unicode_index_to_byte(int i) const{
  229. if(is_ascii) return i;
  230. int j = 0;
  231. while(i > 0){
  232. j += utf8len(data[j]);
  233. i--;
  234. }
  235. return j;
  236. }
  237. int Str::_byte_index_to_unicode(int n) const{
  238. if(is_ascii) return n;
  239. int cnt = 0;
  240. for(int i=0; i<n; i++){
  241. if((data[i] & 0xC0) != 0x80) cnt++;
  242. }
  243. return cnt;
  244. }
  245. Str Str::u8_getitem(int i) const{
  246. i = _unicode_index_to_byte(i);
  247. return substr(i, utf8len(data[i]));
  248. }
  249. Str Str::u8_slice(int start, int stop, int step) const{
  250. std::stringstream ss;
  251. if(is_ascii){
  252. for(int i=start; step>0?i<stop:i>stop; i+=step) ss << data[i];
  253. }else{
  254. for(int i=start; step>0?i<stop:i>stop; i+=step) ss << u8_getitem(i);
  255. }
  256. return ss.str();
  257. }
  258. int Str::u8_length() const {
  259. return _byte_index_to_unicode(size);
  260. }
  261. std::vector<std::string_view> Str::split(Str sep) const{
  262. std::vector<std::string_view> result;
  263. int start = 0;
  264. while(true){
  265. int i = index(sep, start);
  266. if(i == -1) break;
  267. result.push_back(sv().substr(start, i - start));
  268. start = i + sep.size;
  269. }
  270. result.push_back(sv().substr(start, size - start));
  271. return result;
  272. }
  273. std::ostream& operator<<(std::ostream& os, const StrName& sn){
  274. return os << sn.sv();
  275. }
  276. std::map<std::string, uint16_t, std::less<>>& StrName::_interned(){
  277. static std::map<std::string, uint16_t, std::less<>> interned;
  278. return interned;
  279. }
  280. std::vector<std::string>& StrName::_r_interned(){
  281. static std::vector<std::string> r_interned;
  282. return r_interned;
  283. }
  284. StrName StrName::get(std::string_view s){
  285. auto it = _interned().find(s);
  286. if(it != _interned().end()) return StrName(it->second);
  287. uint16_t index = (uint16_t)(_r_interned().size() + 1);
  288. std::string str(s);
  289. _interned()[str] = index;
  290. _r_interned().push_back(str);
  291. return StrName(index);
  292. }
  293. Str StrName::escape() const {
  294. return Str(sv()).escape();
  295. }
  296. bool StrName::is_valid(int index) {
  297. // check _r_interned()[index-1] is valid
  298. return index > 0 && index <= _r_interned().size();
  299. }
  300. StrName::StrName(): index(0) {}
  301. StrName::StrName(uint16_t index): index(index) {}
  302. StrName::StrName(const char* s): index(get(s).index) {}
  303. StrName::StrName(const Str& s){
  304. index = get(s.sv()).index;
  305. }
  306. std::string_view StrName::sv() const {
  307. const std::string& str = _r_interned()[index-1];
  308. return std::string_view(str);
  309. }
  310. FastStrStream& FastStrStream::operator<<(const Str& s){
  311. parts.push_back(&s);
  312. return *this;
  313. }
  314. Str FastStrStream::str() const{
  315. int len = 0;
  316. bool is_ascii = true;
  317. for(auto& s: parts){
  318. len += s->length();
  319. is_ascii &= s->is_ascii;
  320. }
  321. Str result(len, is_ascii);
  322. char* p = result.data;
  323. for(auto& s: parts){
  324. memcpy(p, s->data, s->length());
  325. p += s->length();
  326. }
  327. return result;
  328. }
  329. } // namespace pkpy