String.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /******************************************************************************
  2. Use 'Str' to handle text string management.
  3. Sample Usage:
  4. Str s="abc"; // set 's' string to "abc" text
  5. s+="def"; // append 's' string by "def" text (now 's' has "abcdef" text)
  6. s=S+"text "+5; // set 's' string from text appended by number (now 's' has "text 5" text)
  7. Use 'StrLibrary' to store multiple strings in a file using least possible amount of bytes.
  8. 'StrLibrary' works by creating first a database of common strings which may occur frequently when storing them in files.
  9. Later when strings are written to files, they are first checked if they're present in the database,
  10. if they are, then only index is stored (saving index requires less space than saving full string, which saves space).
  11. If the string is not present in the database then it is stored not as index but as a string.
  12. For example if string library was created with "abcdef" "zzzz" strings in the database:
  13. storing "abcdef" string in the file will require storing only index
  14. storing "qwerty" string in the file will require storing full string.
  15. Please remember that this implies the requirement that when reading previously written strings,
  16. you must use a 'StrLibrary' which was initialized with the exact same common strings array database
  17. as the 'StrLibrary' which was used for writing the strings.
  18. /******************************************************************************/
  19. // STRING
  20. /******************************************************************************/
  21. struct Str // Text String (16-bit per character)
  22. {
  23. // get
  24. operator CChar*()C {return _d.data();} // cast to CChar*
  25. CChar* operator()( )C {return _d.data();} // get text data
  26. Char operator[](Int i)C; // get i-th character, returns '\0' if 'i' is out of range
  27. Bool is ()C {return _length>0 ;} // if contains any data
  28. Int length ()C {return _length ;} // get current length
  29. Char first ()C {return _length ? _d[ 0] : '\0';} // get first character present in the string, '\0' if empty
  30. Char last ()C {return _length ? _d[_length-1] : '\0';} // get last character present in the string, '\0' if empty
  31. UInt memUsage()C {return _d.memUsage() ;} // get memory usage
  32. // operations
  33. Str& del ( ); // clear stored data and free helper memory
  34. Str& clear ( ); // clear stored data
  35. Str& insert (Int i, Char c ); // insert 'c' at 'i' string position
  36. Str& insert (Int i, C Str &text ); // insert 'text' at 'i' string position
  37. Str& remove (Int i, Int num=1); // remove 'num' characters starting from 'i-th'
  38. Str& removeLast( Int num=1); // remove last 'num' characters in the string
  39. Str& trim (Int pos, Int length); // trim string by removing start and end, to keep only the part starting at 'pos' of 'length' length
  40. Str& clip (Int length ); // clip current length to 'length'
  41. Str& reserve (Int length ); // allocate enough space for the string to handle 'length' characters, if the parameter is smaller than current string length then no operation is performed
  42. Str& reverse ( ); // reverse the order of characters
  43. Str& replace (Char src, Char dest); // replace all 'src' characters to 'dest'
  44. Str& setChar (Int i , Char c ); // replace i-th character with 'c' (if 'i' is at the end of the string, then 'c' will be appended, if 'i' is after the end, then nothing will happen)
  45. Str& tailSlash (Bool on ); // exclude or include slash after string (if including and string is empty then slash will not be added, if excluding and string consists only of slash then it will not be removed)
  46. Str& removeOuterWhiteChars(); // remove white characters at the start and end of the string
  47. Str& space(Int num=1); // add a space if string isn't empty and does not end with a new line or space, 'num'=number of spaces to insert
  48. Str& line (Int num=1); // add a line if string isn't empty and does not end with a new line , 'num'=number of lines to insert
  49. #if EE_PRIVATE
  50. #if WINDOWS
  51. ASSERT(SIZE(wchar_t)==SIZE(Char));
  52. operator C wchar_t*()C {return (wchar_t*)T();} // cast to C wchar_t*
  53. #elif APPLE
  54. Str(NSString *s); Str& operator=(NSString *s); Str& operator+=(NSString *s); Str operator+(NSString *s)C;
  55. #endif
  56. void alwaysAppend(Char c);
  57. explicit Str(C Str8 &s, Int additional_length);
  58. explicit Str(C Str &s, Int additional_length);
  59. #endif
  60. Str( );
  61. Str( Str &&s); Str& operator=( Str &&s);
  62. Str( Char c); Str& operator=( Char c); Str& operator+=( Char c); Str operator+( Char c)C;
  63. Str( Char8 c); Str& operator=( Char8 c); Str& operator+=( Char8 c); Str operator+( Char8 c)C;
  64. Str( CChar *t); Str& operator=( CChar *t); Str& operator+=( CChar *t); Str operator+( CChar *t)C;
  65. Str( CChar8 *t); Str& operator=( CChar8 *t); Str& operator+=( CChar8 *t); Str operator+( CChar8 *t)C;
  66. Str(C wchar_t*t); Str& operator=(C wchar_t*t); Str& operator+=(C wchar_t*t); Str operator+(C wchar_t*t)C;
  67. Str(C Str &s); Str& operator=(C Str &s); Str& operator+=(C Str &s); Str operator+(C Str &s)C;
  68. Str(C Str8 &s); Str& operator=(C Str8 &s); Str& operator+=(C Str8 &s); Str operator+(C Str8 &s)C;
  69. Str( Bool b); Str& operator=( Bool b); Str& operator+=( Bool b); Str operator+( Bool b)C;
  70. Str( SByte i); Str& operator=( SByte i); Str& operator+=( SByte i); Str operator+( SByte i)C;
  71. Str( Int i); Str& operator=( Int i); Str& operator+=( Int i); Str operator+( Int i)C;
  72. Str( Long i); Str& operator=( Long i); Str& operator+=( Long i); Str operator+( Long i)C;
  73. Str( Byte u); Str& operator=( Byte u); Str& operator+=( Byte u); Str operator+( Byte u)C;
  74. Str( UInt u); Str& operator=( UInt u); Str& operator+=( UInt u); Str operator+( UInt u)C;
  75. Str( ULong u); Str& operator=( ULong u); Str& operator+=( ULong u); Str operator+( ULong u)C;
  76. Str( Flt f); Str& operator=( Flt f); Str& operator+=( Flt f); Str operator+( Flt f)C;
  77. Str( Dbl d); Str& operator=( Dbl d); Str& operator+=( Dbl d); Str operator+( Dbl d)C;
  78. Str( CPtr p); Str& operator=( CPtr p); Str& operator+=( CPtr p); Str operator+( CPtr p)C;
  79. Str(C Vec2 &v); Str& operator=(C Vec2 &v); Str& operator+=(C Vec2 &v); Str operator+(C Vec2 &v)C;
  80. Str(C VecD2 &v); Str& operator=(C VecD2 &v); Str& operator+=(C VecD2 &v); Str operator+(C VecD2 &v)C;
  81. Str(C VecI2 &v); Str& operator=(C VecI2 &v); Str& operator+=(C VecI2 &v); Str operator+(C VecI2 &v)C;
  82. Str(C VecB2 &v); Str& operator=(C VecB2 &v); Str& operator+=(C VecB2 &v); Str operator+(C VecB2 &v)C;
  83. Str(C VecSB2 &v); Str& operator=(C VecSB2 &v); Str& operator+=(C VecSB2 &v); Str operator+(C VecSB2 &v)C;
  84. Str(C VecUS2 &v); Str& operator=(C VecUS2 &v); Str& operator+=(C VecUS2 &v); Str operator+(C VecUS2 &v)C;
  85. Str(C Vec &v); Str& operator=(C Vec &v); Str& operator+=(C Vec &v); Str operator+(C Vec &v)C;
  86. Str(C VecD &v); Str& operator=(C VecD &v); Str& operator+=(C VecD &v); Str operator+(C VecD &v)C;
  87. Str(C VecI &v); Str& operator=(C VecI &v); Str& operator+=(C VecI &v); Str operator+(C VecI &v)C;
  88. Str(C VecB &v); Str& operator=(C VecB &v); Str& operator+=(C VecB &v); Str operator+(C VecB &v)C;
  89. Str(C VecSB &v); Str& operator=(C VecSB &v); Str& operator+=(C VecSB &v); Str operator+(C VecSB &v)C;
  90. Str(C VecUS &v); Str& operator=(C VecUS &v); Str& operator+=(C VecUS &v); Str operator+(C VecUS &v)C;
  91. Str(C Vec4 &v); Str& operator=(C Vec4 &v); Str& operator+=(C Vec4 &v); Str operator+(C Vec4 &v)C;
  92. Str(C VecD4 &v); Str& operator=(C VecD4 &v); Str& operator+=(C VecD4 &v); Str operator+(C VecD4 &v)C;
  93. Str(C VecI4 &v); Str& operator=(C VecI4 &v); Str& operator+=(C VecI4 &v); Str operator+(C VecI4 &v)C;
  94. Str(C VecB4 &v); Str& operator=(C VecB4 &v); Str& operator+=(C VecB4 &v); Str operator+(C VecB4 &v)C;
  95. Str(C VecSB4 &v); Str& operator=(C VecSB4 &v); Str& operator+=(C VecSB4 &v); Str operator+(C VecSB4 &v)C;
  96. Str(C BStr &s); Str& operator=(C BStr &s); Str& operator+=(C BStr &s); Str operator+(C BStr &s)C;
  97. T1(TYPE) Str(TYPE i, ENABLE_IF_ENUM(TYPE, Ptr ) dummy=null) : Str(Int(i)) {}
  98. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str&) operator =(TYPE i) {T =Int(i); return T;}
  99. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str&) operator+=(TYPE i) {T+=Int(i); return T;}
  100. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str ) operator+ (TYPE i)C {return T+Int(i);}
  101. // io
  102. Bool save(File &f)C; // save string using f.putStr(T), false on fail
  103. Bool load(File &f) ; // load string using f.getStr(T), false on fail
  104. #if !EE_PRIVATE
  105. private:
  106. #endif
  107. Mems<Char> _d;
  108. Int _length;
  109. friend struct _List;
  110. };extern
  111. const Str S; // Constant Empty String
  112. /******************************************************************************/
  113. // STRING LIBRARY
  114. /******************************************************************************/
  115. struct StrLibrary // String Library, efficient solution for storing multiple strings in a file using least possible amount of bytes
  116. {
  117. // manage
  118. void del ( ); // delete manually
  119. void create(C MemPtr<Str> &strings, Bool case_sensitive, Bool paths); // create library database from 'strings' array of strings, 'paths'=if treat strings as paths (they can have '/' replaced with '\')
  120. // get / set
  121. Int elms( )C {return _elms;} // get number of strings stored in this library
  122. Str elm (Int i)C; // get i-th string stored in this library, null on fail
  123. void putStr(File &f, C Str &str)C; // put 'str' string into 'f' file
  124. void getStr(File &f, Str &str)C; Str getStr(File &f)C {Str s; getStr(f, s); return s;} // get 'str' string from 'f' file
  125. // io
  126. Bool save(File &f)C; // save, false on fail
  127. Bool load(File &f) ; // load, false on fail
  128. ~StrLibrary() {del();}
  129. StrLibrary();
  130. explicit StrLibrary(C MemPtr<Str> &strings, Bool case_sensitive, Bool paths);
  131. private:
  132. Bool _case_sensitive, _paths;
  133. Int _elms, _size, *_index;
  134. Byte *_data;
  135. NO_COPY_CONSTRUCTOR(StrLibrary);
  136. };
  137. /******************************************************************************/
  138. // MAIN
  139. /******************************************************************************/
  140. inline Int Elms(C Str &str) {return str.length();}
  141. /******************************************************************************/
  142. // OPERATORS
  143. /******************************************************************************/
  144. inline Str&& operator+(Str &&a, CChar *b) {return RValue(a+=b);}
  145. inline Str&& operator+(Str &&a, CChar8 *b) {return RValue(a+=b);}
  146. inline Str&& operator+(Str &&a, C wchar_t*b) {return RValue(a+=b);}
  147. inline Str&& operator+(Str &&a, C Str &b) {return RValue(a+=b);}
  148. inline Str&& operator+(Str &&a, C Str8 &b) {return RValue(a+=b);}
  149. inline Str&& operator+(Str &&a, Char b) {return RValue(a+=b);}
  150. inline Str&& operator+(Str &&a, Char8 b) {return RValue(a+=b);}
  151. inline Str&& operator+(Str &&a, Bool b) {return RValue(a+=b);}
  152. inline Str&& operator+(Str &&a, SByte b) {return RValue(a+=b);}
  153. inline Str&& operator+(Str &&a, Int b) {return RValue(a+=b);}
  154. inline Str&& operator+(Str &&a, Long b) {return RValue(a+=b);}
  155. inline Str&& operator+(Str &&a, Byte b) {return RValue(a+=b);}
  156. inline Str&& operator+(Str &&a, UInt b) {return RValue(a+=b);}
  157. inline Str&& operator+(Str &&a, ULong b) {return RValue(a+=b);}
  158. inline Str&& operator+(Str &&a, Flt b) {return RValue(a+=b);}
  159. inline Str&& operator+(Str &&a, Dbl b) {return RValue(a+=b);}
  160. inline Str&& operator+(Str &&a, CPtr b) {return RValue(a+=b);}
  161. inline Str&& operator+(Str &&a, C Vec2 &b) {return RValue(a+=b);}
  162. inline Str&& operator+(Str &&a, C VecD2 &b) {return RValue(a+=b);}
  163. inline Str&& operator+(Str &&a, C VecI2 &b) {return RValue(a+=b);}
  164. inline Str&& operator+(Str &&a, C VecB2 &b) {return RValue(a+=b);}
  165. inline Str&& operator+(Str &&a, C VecSB2 &b) {return RValue(a+=b);}
  166. inline Str&& operator+(Str &&a, C VecUS2 &b) {return RValue(a+=b);}
  167. inline Str&& operator+(Str &&a, C Vec &b) {return RValue(a+=b);}
  168. inline Str&& operator+(Str &&a, C VecD &b) {return RValue(a+=b);}
  169. inline Str&& operator+(Str &&a, C VecI &b) {return RValue(a+=b);}
  170. inline Str&& operator+(Str &&a, C VecB &b) {return RValue(a+=b);}
  171. inline Str&& operator+(Str &&a, C VecSB &b) {return RValue(a+=b);}
  172. inline Str&& operator+(Str &&a, C VecUS &b) {return RValue(a+=b);}
  173. inline Str&& operator+(Str &&a, C Vec4 &b) {return RValue(a+=b);}
  174. inline Str&& operator+(Str &&a, C VecD4 &b) {return RValue(a+=b);}
  175. inline Str&& operator+(Str &&a, C VecI4 &b) {return RValue(a+=b);}
  176. inline Str&& operator+(Str &&a, C VecB4 &b) {return RValue(a+=b);}
  177. inline Str&& operator+(Str &&a, C VecSB4 &b) {return RValue(a+=b);}
  178. inline Str&& operator+(Str &&a, C BStr &b) {return RValue(a+=b);}
  179. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str&&) operator+(Str &&a, TYPE b) {return RValue(a+=Int(b));}
  180. /******************************************************************************/