5 #include <libutl/FlagsMI.h> 43 String(
const char* s,
bool owner =
true,
bool duplicate =
true,
bool caseSensitive =
true);
68 virtual void steal(
Object& rhs);
70 virtual size_t hash(
size_t size)
const;
74 virtual size_t innerAllocatedSize()
const;
93 if (_size == 0) _size = length() + 1;
105 return !getFlag(flg_caseInsensitive);
112 setFlag(flg_caseInsensitive, !caseSensitive);
119 return getFlag(flg_marked);
126 setFlag(flg_marked, marked);
133 int compareSubstring(
size_t begin,
const String& rhs,
size_t n);
139 return compareSubstring(begin,
String(rhs,
false), len);
143 int comparePrefix(
const String& rhs)
const;
149 return comparePrefix(
String(rhs,
false));
153 int compareSuffix(
const String& rhs)
const;
159 return compareSuffix(
String(rhs,
false));
164 strcmp(
const char* lhs,
const char* rhs)
const 166 if (isCaseSensitive())
168 int d = (int)*lhs - (
int)*rhs;
169 if (d != 0)
return d;
170 return ::strcmp(lhs + 1, rhs + 1);
174 int d = tolower(*lhs) - tolower(*rhs);
175 if (d != 0)
return d;
176 return ::strcasecmp(lhs + 1, rhs + 1);
181 inline std::function<int(const char*, const char*)>
184 return isCaseSensitive() ? ::strcmp : ::strcasecmp;
189 strncmp(
const char* lhs,
const char* rhs,
size_t n)
const 191 return isCaseSensitive() ? ::strncmp(lhs, rhs, n) : ::strncasecmp(lhs, rhs, n);
195 inline std::function<int(const char*, const char*, size_t)>
198 return isCaseSensitive() ? ::strncmp : ::strncasecmp;
208 return backslashEscaped(
String(specials,
false));
215 String backslashUnescaped()
const;
221 return (length() == 0);
230 size_t find(
const String& str,
size_t begin = 0)
const;
239 find(
const char* str,
size_t begin = 0)
const 241 return find(
String(str,
false), begin);
250 size_t find(
char c,
size_t begin = 0)
const;
257 size_t findBM(
const String& str,
size_t begin = 0)
const;
265 findBM(
const char* str,
size_t begin = 0)
const 267 return findBM(
String(str,
false), begin);
281 size_t len = length();
282 if (len == 0)
return '\0';
297 if (_length ==
size_t_max) _length = strlen(_s);
298 ASSERTD(_length == strlen(_s));
316 return subString(0, n);
341 size_t len = length();
342 if (n >= len)
return self;
343 return subString(len - n, n);
355 String nextToken(
size_t& idx,
char delim =
' ',
bool processQuotes =
false)
const;
396 if (_size == 0) _assertOwner();
408 if (_size < size) _assertOwner(size, increment);
422 if (_size < size) _assertOwner(size, increment);
432 return append(s._s, s.
length());
440 return append(s, strlen(s));
448 String& append(
const char* s,
size_t len);
463 void remove(
size_t begin,
size_t len =
size_t_max);
479 return replace(
String(lhs,
false), rhs);
486 return replace(lhs,
String(rhs,
false));
496 String& replace(
size_t begin,
size_t len,
const String& str);
508 void set(
const char* s,
bool owner =
true,
bool duplicate =
true,
size_t length =
size_t_max);
532 String& padBegin(
size_t len,
char c =
' ');
540 String& padEnd(
size_t len,
char c =
' ');
560 static String repeat(
char c,
size_t num);
563 static String spaces(
size_t num);
571 String res(this->length() + 2);
581 return self +
String(str,
false);
602 return append(
String(rhs,
false));
641 inline operator char*()
647 inline operator const char*()
653 inline operator const char*()
const 659 inline operator void*()
665 inline operator const void*()
671 inline operator const void*()
const 676 inline bool operator<(
const Object& rhs)
const 680 inline bool operator<=(
const Object& rhs)
const 684 inline bool operator>(
const Object& rhs)
const 688 inline bool operator>=(
const Object& rhs)
const 692 inline bool operator==(
const Object& rhs)
const 696 inline bool operator!=(
const Object& rhs)
const 701 inline bool operator<(
const String& rhs)
const 705 inline bool operator<=(
const String& rhs)
const 709 inline bool operator>(
const String& rhs)
const 713 inline bool operator>=(
const String& rhs)
const 717 inline bool operator==(
const String& rhs)
const 721 inline bool operator!=(
const String& rhs)
const 728 setBounds(
size_t& begin,
size_t& end,
size_t& len,
size_t myLen)
730 if (begin > myLen) begin = myLen;
731 if (len > myLen) len = myLen;
733 if (end > myLen) end = myLen;
740 mutable size_t _length;
751 init(
bool caseSensitive =
true)
756 if (!caseSensitive) setCaseSensitive(
false);
764 bool lengthOK = ((_length ==
size_t_max) || (_length == strlen(_s)));
770 if (isOwner())
delete[] _s;
774 void _assertOwner(
size_t size,
size_t increment);
777 static char nullChar;
794 inline bool operator==(
const utl::String& lhs_,
const char* rhs)
797 const char* lhs = lhs_.
get();
798 return (lhs_.
strcmp(lhs, rhs) == 0);
803 inline bool operator!=(
const utl::String& lhs_,
const char* rhs)
806 const char* lhs = lhs_.
get();
807 return (lhs_.
strcmp(lhs, rhs) != 0);
812 inline bool operator==(
const char* lhs,
const utl::String& rhs_)
815 const char* rhs = rhs_.
get();
816 return (rhs_.
strcmp(lhs, rhs) == 0);
821 inline bool operator!=(
const char* lhs,
const utl::String& rhs_)
824 const char* rhs = rhs_.
get();
825 return (rhs_.
strcmp(lhs, rhs) != 0);
830 inline bool operator==(
const utl::String& lhs_,
const char rhs)
833 const char* lhs = lhs_.
get();
835 return (tolower(*lhs) == tolower(rhs)) && (lhs[1] ==
'\0');
840 inline bool operator!=(
const utl::String& lhs_,
const char rhs)
843 const char* lhs = lhs_.
get();
845 return (tolower(*lhs) != tolower(rhs)) || (lhs[1] !=
'\0');
850 inline bool operator==(
char lhs,
const utl::String& rhs_)
853 const char* rhs = rhs_.
get();
855 return (tolower(lhs) == tolower(*rhs)) && (rhs[1] ==
'\0');
860 inline bool operator!=(
char lhs,
const utl::String& rhs_)
863 const char* rhs = rhs_.
get();
865 return (tolower(lhs) != tolower(*rhs)) || (rhs[1] !=
'\0');
String & operator+=(const char *rhs)
Append the given string to self.
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
int strncmp(const char *lhs, const char *rhs, size_t n) const
Compare (up to) the first n bytes of two strings (case sensitive iff isCaseSensitive()).
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
size_t find(const char *str, size_t begin=0) const
Find the first instance of the given string in self.
String operator+(char c) const
Get a copy of self with the given character appended.
String backslashEscaped(const char *specials) const
Backslash-escape a string.
String & operator=(char c)
Make self equal to the given character.
void deInit()
De-initialize UTL++.
void setMarked(bool marked=true)
Set the marked flag.
String & append(const char *s)
Append the given string.
char firstChar() const
Get the first character in the string (nul if empty).
String & replace(const String &lhs, const char *rhs)
Replace all instances of lhs with rhs.
std::function< int(const char *, const char *)> strcmp() const
Get the string comparison function (strcmp or strcasecmp).
int compareSubstring(size_t begin, const char *rhs, size_t len)
Compare the given string against a substring of self.
String suffix(size_t n) const
Get the last n characters of the string.
int comparePrefix(const char *rhs) const
Compare the given string against the beginning of self.
default representation (via getSerializeMode())
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
String operator+(const String &rhs) const
Get a copy of self with the given string appended.
String(char c)
Constructor.
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
bool isOwner() const
Get the ownership flag.
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
const size_t size_t_max
Maximum size_t value.
#define ASSERT(x)
Declares a defect if x is false (or 0).
bool isCaseSensitive() const
Get the case-sensitive flag.
char & operator[](size_t i)
Array access operator.
const String emptyString
An empty string.
String & clear()
Reset to empty string.
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
String & operator+=(const String &rhs)
Append the given string to self.
String operator+(const char *str) const
Get a copy of self with the given string appended.
const char & operator[](size_t i) const
Array access operator.
size_t length() const
Get the length of the string.
bool isMarked() const
Get the marked flag.
String & replace(const char *lhs, const char *rhs)
Replace all instances of lhs with rhs.
size_t findBM(const char *str, size_t begin=0) const
Use Boyer-Moore algorithm to find the first instance of the given string.
int strcmp(const char *lhs, const char *rhs) const
Compare two strings (case sensitive comparison iff isCaseSensitive()).
unsigned int uint_t
Unsigned integer.
String & operator+=(char c)
Append the given character to self.
String & append(char c)
Append the given character.
String & replace(const char *lhs, const String &rhs)
Replace all instances of lhs with rhs.
void assertOwner(size_t size, size_t increment=8)
Make sure self has its own copy of the string.
bool empty() const
Is the string empty?
void setCaseSensitive(bool caseSensitive)
Set the case-sensitive flag.
char lastChar() const
Get the last character in the string (nul if empty).
size_t size() const
Get the size of the allocated character array.
std::function< int(const char *, const char *, size_t)> strncmp() const
Get the length-limited string comparison function (strncmp or strncasecmp).
String prefix(size_t n) const
Get the first n characters of the string.
String & operator=(const char *str)
Make self equal to a copy of the given character array.
Root of UTL++ class hierarchy.
int compare(bool lhs, bool rhs)
Compare two boolean values.
String & append(const String &s)
Append the given string.
int compareSuffix(const char *rhs) const
Compare the given string against the end of self.
void lengthInvalidate() const
Invalidate the cached length of the string.
void init()
Initialize UTL++.
void setOwner(bool owner)
Set the ownership flag (without doing anything else).
const char * get() const
Get const char*.
#define ASSERTD
Do an assertion in DEBUG mode only.
String & assertOwner()
Make sure self has its own copy of the string.
void reserve(size_t size, size_t increment=8)
Grow to the specified size.