]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/include/SString.h
1 // SciTE - Scintilla based Text Editor
3 ** A simple string class.
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
11 // These functions are implemented because each platform calls them something different
12 int CompareCaseInsensitive(const char *a
, const char *b
);
13 int CompareNCaseInsensitive(const char *a
, const char *b
, int len
);
14 bool EqualCaseInsensitive(const char *a
, const char *b
);
16 // Define another string class.
17 // While it would be 'better' to use std::string, that doubles the executable size.
18 // An SString may contain embedded nul characters.
21 * Duplicate a C string.
22 * Allocate memory of the given size, or big enough to fit the string if length isn't given;
23 * then copy the given string in the allocated memory.
24 * @return the pointer to the new string
26 inline char *StringDup(
27 const char *s
, ///< The string to duplicate
28 int len
=-1) ///< The length of memory to allocate. Optional.
34 char *sNew
= new char[len
+ 1];
36 strncpy(sNew
, s
, len
);
43 * @brief A simple string class.
44 * Hold the length of the string for quick operations,
45 * can have a buffer bigger than the string to avoid too many memory allocations and copies.
46 * May have embedded zeroes as a result of @a substitute, but rely too heavily on C string
47 * functions to allow reliable manipulations of these strings.
50 char *s
; ///< The C string
51 int sSize
; ///< The size of the buffer, less 1: ie. the maximum size of the string
52 int sLen
; ///< The size of the string in s
53 int sizeGrowth
; ///< Minimum growth size when appending strings
54 enum { sizeGrowthDefault
= 64 };
57 typedef int size_type
;
59 SString() : s(0), sSize(0), sLen(0), sizeGrowth(sizeGrowthDefault
) {
61 SString(const SString
&source
) : sizeGrowth(sizeGrowthDefault
) {
62 s
= StringDup(source
.s
);
63 sSize
= sLen
= (s
) ? strlen(s
) : 0;
65 SString(const char *s_
) : sizeGrowth(sizeGrowthDefault
) {
67 sSize
= sLen
= (s
) ? strlen(s
) : 0;
69 SString(const char *s_
, int first
, int last
) : sizeGrowth(sizeGrowthDefault
) {
70 s
= StringDup(s_
+ first
, last
- first
);
71 sSize
= sLen
= (s
) ? strlen(s
) : 0;
73 SString(int i
) : sizeGrowth(sizeGrowthDefault
) {
75 sprintf(number
, "%0d", i
);
76 s
= StringDup(number
);
77 sSize
= sLen
= (s
) ? strlen(s
) : 0;
91 /** Size of buffer. */
92 size_type
size(void) const { ///<
98 /** Size of string in buffer. */
102 SString
&assign(const char* sOther
, int sSize_
= -1) {
107 sSize_
= strlen(sOther
);
109 if (sSize
> 0 && sSize_
<= sSize
) { // Does not allocate new buffer if the current is big enough
111 strncpy(s
, sOther
, sSize_
);
117 s
= StringDup(sOther
, sSize_
);
119 sSize
= sSize_
; // Allow buffer bigger than real string, thus providing space to grow
127 SString
&assign(const SString
& sOther
, int sSize_
= -1) {
128 return assign(sOther
.s
, sSize_
);
130 SString
&operator=(const char *source
) {
131 return assign(source
);
133 SString
&operator=(const SString
&source
) {
134 if (this != &source
) {
135 assign(source
.c_str());
139 bool operator==(const SString
&sOther
) const {
140 if ((s
== 0) && (sOther
.s
== 0))
142 if ((s
== 0) || (sOther
.s
== 0))
144 return strcmp(s
, sOther
.s
) == 0;
146 bool operator!=(const SString
&sOther
) const {
147 return !operator==(sOther
);
149 bool operator==(const char *sOther
) const {
150 if ((s
== 0) && (sOther
== 0))
152 if ((s
== 0) || (sOther
== 0))
154 return strcmp(s
, sOther
) == 0;
156 bool operator!=(const char *sOther
) const {
157 return !operator==(sOther
);
159 bool contains(char ch
) {
161 return strchr(s
, ch
) != 0;
165 void setsizegrowth(int sizeGrowth_
) {
166 sizeGrowth
= sizeGrowth_
;
168 const char *c_str() const {
174 /** Give ownership of buffer to caller which must use delete[] to free buffer. */
182 char operator[](int i
) const {
183 if (s
&& i
< sSize
) // Or < sLen? Depends on the use, both are OK
188 SString
&append(const char* sOther
, int sLenOther
=-1, char sep
=0) {
190 sLenOther
= strlen(sOther
);
192 if (sLen
&& sep
) // Only add a separator if not empty
194 int lenNew
= sLen
+ sLenOther
+ lenSep
;
195 if (lenNew
+ 1 < sSize
) {
196 // Conservative about growing the buffer: don't do it, unless really needed
201 strncpy(&s
[sLen
], sOther
, sLenOther
);
202 s
[sLen
+ sLenOther
] = '\0';
205 // Grow the buffer bigger than really needed, to have room for other appends
206 char *sNew
= new char[lenNew
+ sizeGrowth
+ 1];
209 memcpy(sNew
, s
, sLen
);
213 sSize
= lenNew
+ sizeGrowth
;
218 strncpy(&s
[sLen
], sOther
, sLenOther
);
219 sNew
[sLen
+ sLenOther
] = '\0';
225 SString
&operator +=(const char *sOther
) {
226 return append(sOther
, -1);
228 SString
&operator +=(const SString
&sOther
) {
229 return append(sOther
.s
, sOther
.sSize
);
231 SString
&operator +=(char ch
) {
232 return append(&ch
, 1);
234 SString
&appendwithseparator(const char* sOther
, char sep
) {
235 return append(sOther
, strlen(sOther
), sep
);
243 void substitute(char find
, char replace
) {