String Borrowed.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /******************************************************************************/
  2. struct BStr // string which uses text memory allocated externally, and limiting it to custom 'length', 'BStr' has case-sensitive compare operators
  3. {
  4. Bool is( )C {return _length>0;} // if string is valid (not empty)
  5. Int length( )C {return _length ;} // get string length
  6. Bool borrowed( )C {return !_custom ;} // if string is borrowed (uses external text memory), warning: this requires that the external memory is accessible during the lifetime of the string object
  7. Bool custom( )C {return _custom ;} // if string is custom (uses manually allocated text memory)
  8. CChar* operator()( )C {return _d ;} // warning: for borrowed strings returned pointer may not be null-terminated
  9. Char operator[](Int i)C {return InRange(i, T) ? _d[ i] : '\0';} // get i-th character
  10. Char last( )C {return _length ? _d[_length-1] : '\0';} // get last character
  11. Str asStr()C {return T;} // return as Str
  12. BStr& set (C BStr &s ); // set as exact copy of 's' string
  13. BStr& setCustom (C Str &s ); // manually allocate new memory for text, and set it from 's' string
  14. BStr& setCustom (CChar *t, Int length=-1); // manually allocate new memory for text, and set it from 't' text of 'length' (-1=auto-detect the actual 't' text length)
  15. BStr& setBorrowed(CChar *t, Int length=-1); // do not allocate new memory for text, instead use directly 't' text memory of custom 'length' (-1=auto-detect the actual 't' text length), warning: if 't' text memory becomes changed after calling this method, then this string will also be changed
  16. BStr& clear ( ); // clear string to null so it's empty
  17. BStr& extend(Int l=1); // extend string length by 'l' to use more characters from the borrowed text (for example if the string was set as borrowed from text "abcd" with length=2 then it's equal to "ab", after extending it with "l=1" it's now equal to "abc") warning: please take caution not to extend the string longer than the original text
  18. Bool operator==(CChar c)C; // if the string is equal to 'c' character (case-sensitive)
  19. Bool operator==(CChar8 c)C; // if the string is equal to 'c' character (case-sensitive)
  20. Bool operator==(CChar *t)C; // if the string is equal to 't' text (case-sensitive)
  21. Bool operator==(CChar8 *t)C; // if the string is equal to 't' text (case-sensitive)
  22. Bool operator==(C Str &s)C; // if the string is equal to 's' string (case-sensitive)
  23. Bool operator==(C Str8 &s)C; // if the string is equal to 's' string (case-sensitive)
  24. Bool operator==(C BStr &s)C; // if the string is equal to 's' string (case-sensitive)
  25. Bool operator!=(CChar c)C {return !(T==c);} // if the string is not equal to 'c' character (case-sensitive)
  26. Bool operator!=(CChar8 c)C {return !(T==c);} // if the string is not equal to 'c' character (case-sensitive)
  27. Bool operator!=(CChar *t)C {return !(T==t);} // if the string is not equal to 't' text (case-sensitive)
  28. Bool operator!=(CChar8 *t)C {return !(T==t);} // if the string is not equal to 't' text (case-sensitive)
  29. Bool operator!=(C Str &s)C {return !(T==s);} // if the string is not equal to 's' string (case-sensitive)
  30. Bool operator!=(C Str8 &s)C {return !(T==s);} // if the string is not equal to 's' string (case-sensitive)
  31. Bool operator!=(C BStr &s)C {return !(T==s);} // if the string is not equal to 's' string (case-sensitive)
  32. BStr& del() {return clear();}
  33. ~BStr() {del();}
  34. BStr() {_custom=false; _d=null; _length=0;}
  35. BStr(C BStr &src); // set as exact copy of 'src' string
  36. BStr& operator=(C BStr &src); // set as exact copy of 'src' string
  37. private:
  38. Bool _custom;
  39. CChar *_d;
  40. Int _length;
  41. };
  42. inline Int Elms(C BStr &s) {return s.length();}
  43. /******************************************************************************/