String8.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /******************************************************************************
  2. Use 'Str8' to handle 8-bit per character text string management.
  3. /******************************************************************************/
  4. struct Str8 // Text String (8-bit per character)
  5. {
  6. // get
  7. operator CChar8*()C {return _d.data();} // cast to CChar8*
  8. CChar8* operator()( )C {return _d.data();} // get text data
  9. Char8 operator[](Int i)C; // get i-th character, returns '\0' if 'i' is out of range
  10. Bool is ()C {return _length>0 ;} // if contains any data
  11. Int length ()C {return _length ;} // get current length
  12. Char8 first ()C {return _length ? _d[ 0] : '\0';} // get first character present in the string, '\0' if empty
  13. Char8 last ()C {return _length ? _d[_length-1] : '\0';} // get last character present in the string, '\0' if empty
  14. UInt memUsage()C {return _d.memUsage() ;} // get memory usage
  15. // operations
  16. Str8& del ( ); // clear stored data and free helper memory
  17. Str8& clear ( ); // clear stored data
  18. Str8& insert (Int i, Char8 c ); // insert 'c' at 'i' string position
  19. Str8& insert (Int i, C Str8 &text ); // insert 'text' at 'i' string position
  20. Str8& remove (Int i, Int num=1 ); // remove 'num' characters starting from 'i-th'
  21. Str8& removeLast( Int num=1 ); // remove last 'num' characters in the string
  22. Str8& trim (Int pos, Int length); // trim string by removing start and end, to keep only the part starting at 'pos' of 'length' length
  23. Str8& clip (Int length ); // clip current length to 'length'
  24. Str8& 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
  25. Str8& reverse ( ); // reverse the order of characters
  26. Str8& replace (Char8 src, Char8 dest); // replace all 'src' characters to 'dest'
  27. Str8& setChar (Int i , Char8 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)
  28. Str8& 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)
  29. Str8& removeOuterWhiteChars(); // remove white characters at the start and end of the string
  30. Str8& 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
  31. Str8& 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
  32. #if EE_PRIVATE
  33. void alwaysAppend(Char8 c);
  34. explicit Str8(C Str8 &s, Int additional_length);
  35. #endif
  36. Str8( );
  37. Str8( Str8 &&s); Str8& operator=( Str8 &&s);
  38. Str8( Char c); Str8& operator=( Char c); Str8& operator+=( Char c); Str operator+( Char c)C;
  39. Str8( Char8 c); Str8& operator=( Char8 c); Str8& operator+=( Char8 c); Str8 operator+( Char8 c)C;
  40. Str8( CChar *t); Str8& operator=( CChar *t); Str8& operator+=( CChar *t); Str operator+( CChar *t)C;
  41. Str8( CChar8 *t); Str8& operator=( CChar8 *t); Str8& operator+=( CChar8 *t); Str8 operator+( CChar8 *t)C;
  42. Str8(C wchar_t*t); Str8& operator=(C wchar_t*t); Str8& operator+=(C wchar_t*t); Str operator+(C wchar_t*t)C;
  43. Str8(C Str &s); Str8& operator=(C Str &s); Str8& operator+=(C Str &s); Str operator+(C Str &s)C;
  44. Str8(C Str8 &s); Str8& operator=(C Str8 &s); Str8& operator+=(C Str8 &s); Str8 operator+(C Str8 &s)C;
  45. Str8( Bool b); Str8& operator=( Bool b); Str8& operator+=( Bool b); Str8 operator+( Bool b)C;
  46. Str8( SByte i); Str8& operator=( SByte i); Str8& operator+=( SByte i); Str8 operator+( SByte i)C;
  47. Str8( Int i); Str8& operator=( Int i); Str8& operator+=( Int i); Str8 operator+( Int i)C;
  48. Str8( Long i); Str8& operator=( Long i); Str8& operator+=( Long i); Str8 operator+( Long i)C;
  49. Str8( Byte u); Str8& operator=( Byte u); Str8& operator+=( Byte u); Str8 operator+( Byte u)C;
  50. Str8( UInt u); Str8& operator=( UInt u); Str8& operator+=( UInt u); Str8 operator+( UInt u)C;
  51. Str8( ULong u); Str8& operator=( ULong u); Str8& operator+=( ULong u); Str8 operator+( ULong u)C;
  52. Str8( Flt f); Str8& operator=( Flt f); Str8& operator+=( Flt f); Str8 operator+( Flt f)C;
  53. Str8( Dbl d); Str8& operator=( Dbl d); Str8& operator+=( Dbl d); Str8 operator+( Dbl d)C;
  54. Str8( CPtr p); Str8& operator=( CPtr p); Str8& operator+=( CPtr p); Str8 operator+( CPtr p)C;
  55. Str8(C Vec2 &v); Str8& operator=(C Vec2 &v); Str8& operator+=(C Vec2 &v); Str8 operator+(C Vec2 &v)C;
  56. Str8(C VecD2 &v); Str8& operator=(C VecD2 &v); Str8& operator+=(C VecD2 &v); Str8 operator+(C VecD2 &v)C;
  57. Str8(C VecI2 &v); Str8& operator=(C VecI2 &v); Str8& operator+=(C VecI2 &v); Str8 operator+(C VecI2 &v)C;
  58. Str8(C VecB2 &v); Str8& operator=(C VecB2 &v); Str8& operator+=(C VecB2 &v); Str8 operator+(C VecB2 &v)C;
  59. Str8(C VecSB2 &v); Str8& operator=(C VecSB2 &v); Str8& operator+=(C VecSB2 &v); Str8 operator+(C VecSB2 &v)C;
  60. Str8(C VecUS2 &v); Str8& operator=(C VecUS2 &v); Str8& operator+=(C VecUS2 &v); Str8 operator+(C VecUS2 &v)C;
  61. Str8(C Vec &v); Str8& operator=(C Vec &v); Str8& operator+=(C Vec &v); Str8 operator+(C Vec &v)C;
  62. Str8(C VecD &v); Str8& operator=(C VecD &v); Str8& operator+=(C VecD &v); Str8 operator+(C VecD &v)C;
  63. Str8(C VecI &v); Str8& operator=(C VecI &v); Str8& operator+=(C VecI &v); Str8 operator+(C VecI &v)C;
  64. Str8(C VecB &v); Str8& operator=(C VecB &v); Str8& operator+=(C VecB &v); Str8 operator+(C VecB &v)C;
  65. Str8(C VecSB &v); Str8& operator=(C VecSB &v); Str8& operator+=(C VecSB &v); Str8 operator+(C VecSB &v)C;
  66. Str8(C VecUS &v); Str8& operator=(C VecUS &v); Str8& operator+=(C VecUS &v); Str8 operator+(C VecUS &v)C;
  67. Str8(C Vec4 &v); Str8& operator=(C Vec4 &v); Str8& operator+=(C Vec4 &v); Str8 operator+(C Vec4 &v)C;
  68. Str8(C VecD4 &v); Str8& operator=(C VecD4 &v); Str8& operator+=(C VecD4 &v); Str8 operator+(C VecD4 &v)C;
  69. Str8(C VecI4 &v); Str8& operator=(C VecI4 &v); Str8& operator+=(C VecI4 &v); Str8 operator+(C VecI4 &v)C;
  70. Str8(C VecB4 &v); Str8& operator=(C VecB4 &v); Str8& operator+=(C VecB4 &v); Str8 operator+(C VecB4 &v)C;
  71. Str8(C VecSB4 &v); Str8& operator=(C VecSB4 &v); Str8& operator+=(C VecSB4 &v); Str8 operator+(C VecSB4 &v)C;
  72. Str8(C BStr &s); Str8& operator=(C BStr &s); Str8& operator+=(C BStr &s); Str operator+(C BStr &s)C;
  73. T1(TYPE) Str8(TYPE i, ENABLE_IF_ENUM(TYPE, Ptr ) dummy=null) : Str8(Int(i)) {}
  74. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str8&) operator =(TYPE i) {T =Int(i); return T;}
  75. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str8&) operator+=(TYPE i) {T+=Int(i); return T;}
  76. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str8 ) operator+ (TYPE i)C {return T+Int(i);}
  77. // io
  78. Bool save(File &f)C; // save string using f.putStr(T), false on fail
  79. Bool load(File &f) ; // load string using f.getStr(T), false on fail
  80. #if !EE_PRIVATE
  81. private:
  82. #endif
  83. Mems<Char8> _d;
  84. Int _length;
  85. friend struct _List;
  86. };extern
  87. const Str8 S8; // Constant Empty String
  88. /******************************************************************************/
  89. inline Int Elms(C Str8 &str) {return str.length();}
  90. /******************************************************************************/
  91. // OPERATORS
  92. /******************************************************************************/
  93. inline Str8&& operator+(Str8 &&a, CChar *b) {return RValue(a+=b);}
  94. inline Str8&& operator+(Str8 &&a, CChar8 *b) {return RValue(a+=b);}
  95. inline Str8&& operator+(Str8 &&a, C wchar_t*b) {return RValue(a+=b);}
  96. inline Str8&& operator+(Str8 &&a, C Str &b) {return RValue(a+=b);}
  97. inline Str8&& operator+(Str8 &&a, C Str8 &b) {return RValue(a+=b);}
  98. inline Str8&& operator+(Str8 &&a, Char b) {return RValue(a+=b);}
  99. inline Str8&& operator+(Str8 &&a, Char8 b) {return RValue(a+=b);}
  100. inline Str8&& operator+(Str8 &&a, Bool b) {return RValue(a+=b);}
  101. inline Str8&& operator+(Str8 &&a, SByte b) {return RValue(a+=b);}
  102. inline Str8&& operator+(Str8 &&a, Int b) {return RValue(a+=b);}
  103. inline Str8&& operator+(Str8 &&a, Long b) {return RValue(a+=b);}
  104. inline Str8&& operator+(Str8 &&a, Byte b) {return RValue(a+=b);}
  105. inline Str8&& operator+(Str8 &&a, UInt b) {return RValue(a+=b);}
  106. inline Str8&& operator+(Str8 &&a, ULong b) {return RValue(a+=b);}
  107. inline Str8&& operator+(Str8 &&a, Flt b) {return RValue(a+=b);}
  108. inline Str8&& operator+(Str8 &&a, Dbl b) {return RValue(a+=b);}
  109. inline Str8&& operator+(Str8 &&a, CPtr b) {return RValue(a+=b);}
  110. inline Str8&& operator+(Str8 &&a, C Vec2 &b) {return RValue(a+=b);}
  111. inline Str8&& operator+(Str8 &&a, C VecD2 &b) {return RValue(a+=b);}
  112. inline Str8&& operator+(Str8 &&a, C VecI2 &b) {return RValue(a+=b);}
  113. inline Str8&& operator+(Str8 &&a, C VecB2 &b) {return RValue(a+=b);}
  114. inline Str8&& operator+(Str8 &&a, C VecSB2 &b) {return RValue(a+=b);}
  115. inline Str8&& operator+(Str8 &&a, C VecUS2 &b) {return RValue(a+=b);}
  116. inline Str8&& operator+(Str8 &&a, C Vec &b) {return RValue(a+=b);}
  117. inline Str8&& operator+(Str8 &&a, C VecD &b) {return RValue(a+=b);}
  118. inline Str8&& operator+(Str8 &&a, C VecI &b) {return RValue(a+=b);}
  119. inline Str8&& operator+(Str8 &&a, C VecB &b) {return RValue(a+=b);}
  120. inline Str8&& operator+(Str8 &&a, C VecSB &b) {return RValue(a+=b);}
  121. inline Str8&& operator+(Str8 &&a, C VecUS &b) {return RValue(a+=b);}
  122. inline Str8&& operator+(Str8 &&a, C Vec4 &b) {return RValue(a+=b);}
  123. inline Str8&& operator+(Str8 &&a, C VecD4 &b) {return RValue(a+=b);}
  124. inline Str8&& operator+(Str8 &&a, C VecI4 &b) {return RValue(a+=b);}
  125. inline Str8&& operator+(Str8 &&a, C VecB4 &b) {return RValue(a+=b);}
  126. inline Str8&& operator+(Str8 &&a, C VecSB4 &b) {return RValue(a+=b);}
  127. inline Str8&& operator+(Str8 &&a, C BStr &b) {return RValue(a+=b);}
  128. T1(TYPE) ENABLE_IF_ENUM(TYPE, Str8&&) operator+(Str8 &&a, TYPE b) {return RValue(a+=Int(b));}
  129. /******************************************************************************/