]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/include/SString.h
   1 // SciTE - Scintilla based Text Editor 
   3  ** A simple string class. 
   5 // Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org> 
   6 // The License.txt file describes the conditions under which this software may be distributed. 
  12 // These functions are implemented because each platform calls them something different. 
  13 int CompareCaseInsensitive(const char *a
, const char *b
); 
  14 int CompareNCaseInsensitive(const char *a
, const char *b
, size_t len
); 
  15 bool EqualCaseInsensitive(const char *a
, const char *b
); 
  21 // Define another string class. 
  22 // While it would be 'better' to use std::string, that doubles the executable size. 
  23 // An SString may contain embedded nul characters. 
  26  * Base class from which the two other classes (SBuffer & SString) 
  31         /** Type of string lengths (sizes) and positions (indexes). */ 
  32         typedef size_t lenpos_t
; 
  33         /** Out of bounds value indicating that the string argument should be measured. */ 
  34         enum { measure_length
=0xffffffffU
}; 
  37         char *s
;                                ///< The C string 
  38         lenpos_t sSize
;                 ///< The size of the buffer, less 1: ie. the maximum size of the string 
  40         SContainer() : s(0), sSize(0) {} 
  42                 delete []s
;     // Suppose it was allocated using StringAllocate 
  46         /** Size of buffer. */ 
  47         lenpos_t 
size() const { 
  56          * Allocate uninitialized memory big enough to fit a string of the given length. 
  57          * @return the pointer to the new string 
  59         static char *StringAllocate(lenpos_t len
); 
  61          * Duplicate a buffer/C string. 
  62          * Allocate memory of the given size, or big enough to fit the string if length isn't given; 
  63          * then copy the given string in the allocated memory. 
  64          * @return the pointer to the new string 
  66         static char *StringAllocate( 
  67                 const char *s
,                  ///< The string to duplicate 
  68                 lenpos_t len
=measure_length
);   ///< The length of memory to allocate. Optional. 
  73  * @brief A string buffer class. 
  75  * Main use is to ask an API the length of a string it can provide, 
  76  * then to allocate a buffer of the given size, and to provide this buffer 
  77  * to the API to put the string. 
  78  * This class is intended to be shortlived, to be transformed as SString 
  79  * as soon as it holds the string, so it has little members. 
  80  * Note: we assume the buffer is filled by the API. If the length can be shorter, 
  81  * we should set sLen to strlen(sb.ptr()) in related SString constructor and assignment. 
  83 class SBuffer 
: protected SContainer 
{ 
  85         SBuffer(lenpos_t len
) { 
  86                 s 
= StringAllocate(len
); 
  96         // Here only to be on the safe size, user should avoid returning SBuffer values. 
  97         SBuffer(const SBuffer 
&source
) : SContainer() { 
  98                 s 
= StringAllocate(source
.s
, source
.sSize
); 
  99                 sSize 
= (s
) ? source
.sSize 
: 0; 
 101         /// Default assignment operator 
 102         // Same here, shouldn't be used 
 103         SBuffer 
&operator=(const SBuffer 
&source
) { 
 104                 if (this != &source
) { 
 106                         s 
= StringAllocate(source
.s
, source
.sSize
); 
 107                         sSize 
= (s
) ? source
.sSize 
: 0; 
 112         /** Provide direct read/write access to buffer. */ 
 116         /** Ownership of the buffer have been taken, so release it. */ 
 121         /** Size of buffer. */ 
 122         lenpos_t 
size() const { 
 123                 return SContainer::size(); 
 129  * @brief A simple string class. 
 131  * Hold the length of the string for quick operations, 
 132  * can have a buffer bigger than the string to avoid too many memory allocations and copies. 
 133  * May have embedded zeroes as a result of @a substitute, but relies too heavily on C string 
 134  * functions to allow reliable manipulations of these strings, other than simple appends, etc. 
 136 class SString 
: protected SContainer 
{ 
 137         lenpos_t sLen
;                  ///< The size of the string in s 
 138         lenpos_t sizeGrowth
;    ///< Minimum growth size when appending strings 
 139         enum { sizeGrowthDefault 
= 64 }; 
 141         bool grow(lenpos_t lenNew
); 
 142         SString 
&assign(const char *sOther
, lenpos_t sSize_
=measure_length
); 
 145         SString() : sLen(0), sizeGrowth(sizeGrowthDefault
) {} 
 146         SString(const SString 
&source
) : SContainer(), sizeGrowth(sizeGrowthDefault
) { 
 147                 s 
= StringAllocate(source
.s
, source
.sLen
); 
 148                 sSize 
= sLen 
= (s
) ? source
.sLen 
: 0; 
 150         SString(const char *s_
) : sizeGrowth(sizeGrowthDefault
) { 
 151                 s 
= StringAllocate(s_
); 
 152                 sSize 
= sLen 
= (s
) ? strlen(s
) : 0; 
 154         SString(SBuffer 
&buf
) : sizeGrowth(sizeGrowthDefault
) { 
 156                 sSize 
= sLen 
= buf
.size(); 
 157                 // Consumes the given buffer! 
 160         SString(const char *s_
, lenpos_t first
, lenpos_t last
) : sizeGrowth(sizeGrowthDefault
) { 
 161                 // note: expects the "last" argument to point one beyond the range end (a la STL iterators) 
 162                 s 
= StringAllocate(s_ 
+ first
, last 
- first
); 
 163                 sSize 
= sLen 
= (s
) ? last 
- first 
: 0; 
 166         SString(double d
, int precision
); 
 176         /** Size of buffer. */ 
 177         lenpos_t 
size() const { 
 178                 return SContainer::size(); 
 180         /** Size of string in buffer. */ 
 181         lenpos_t 
length() const { 
 184         /** Read access to a character of the string. */ 
 185         char operator[](lenpos_t i
) const { 
 186                 return (s 
&& i 
< sSize
) ? s
[i
] : '\0'; 
 188         SString 
&operator=(const char *source
) { 
 189                 return assign(source
); 
 191         SString 
&operator=(const SString 
&source
) { 
 192                 if (this != &source
) { 
 193                         assign(source
.s
, source
.sLen
); 
 197         bool operator==(const SString 
&sOther
) const; 
 198         bool operator!=(const SString 
&sOther
) const { 
 199                 return !operator==(sOther
); 
 201         bool operator==(const char *sOther
) const; 
 202         bool operator!=(const char *sOther
) const { 
 203                 return !operator==(sOther
); 
 205         bool contains(char ch
) const { 
 206                 return (s 
&& *s
) ? strchr(s
, ch
) != 0 : false; 
 208         void setsizegrowth(lenpos_t sizeGrowth_
) { 
 209                 sizeGrowth 
= sizeGrowth_
; 
 211         const char *c_str() const { 
 214         /** Give ownership of buffer to caller which must use delete[] to free buffer. */ 
 222         SString 
substr(lenpos_t subPos
, lenpos_t subLen
=measure_length
) const; 
 223         SString 
&lowercase(lenpos_t subPos 
= 0, lenpos_t subLen
=measure_length
); 
 224         SString 
&uppercase(lenpos_t subPos 
= 0, lenpos_t subLen
=measure_length
); 
 225         SString 
&append(const char *sOther
, lenpos_t sLenOther
=measure_length
, char sep 
= '\0'); 
 226         SString 
&operator+=(const char *sOther
) { 
 227                 return append(sOther
, static_cast<lenpos_t
>(measure_length
)); 
 229         SString 
&operator+=(const SString 
&sOther
) { 
 230                 return append(sOther
.s
, sOther
.sLen
); 
 232         SString 
&operator+=(char ch
) { 
 233                 return append(&ch
, 1); 
 235         SString 
&appendwithseparator(const char *sOther
, char sep
) { 
 236                 return append(sOther
, strlen(sOther
), sep
); 
 238         SString 
&insert(lenpos_t pos
, const char *sOther
, lenpos_t sLenOther
=measure_length
); 
 241          * Remove @a len characters from the @a pos position, included. 
 242          * Characters at pos + len and beyond replace characters at pos. 
 243          * If @a len is 0, or greater than the length of the string 
 244          * starting at @a pos, the string is just truncated at @a pos. 
 246         void remove(lenpos_t pos
, lenpos_t len
); 
 248         SString 
&change(lenpos_t pos
, char ch
) { 
 249                 if (pos 
< sLen
) {                                       // character changed must be in string bounds 
 254         /** Read an integral numeric value from the string. */ 
 256                 return s 
? atoi(s
) : 0; 
 258         bool startswith(const char *prefix
); 
 259         bool endswith(const char *suffix
); 
 260         int search(const char *sFind
, lenpos_t start
=0) const; 
 261         bool contains(const char *sFind
) const { 
 262                 return search(sFind
) >= 0; 
 264         int substitute(char chFind
, char chReplace
); 
 265         int substitute(const char *sFind
, const char *sReplace
); 
 266         int remove(const char *sFind
) { 
 267                 return substitute(sFind
, ""); 
 273  * Duplicate a C string. 
 274  * Allocate memory of the given size, or big enough to fit the string if length isn't given; 
 275  * then copy the given string in the allocated memory. 
 276  * @return the pointer to the new string 
 278 inline char *StringDup( 
 279         const char *s
,                  ///< The string to duplicate 
 280         SContainer::lenpos_t len
=SContainer::measure_length
)    ///< The length of memory to allocate. Optional. 
 282         return SContainer::StringAllocate(s
, len
);