diff options
author | nyamatongwe <unknown> | 2004-01-12 11:18:26 +0000 |
---|---|---|
committer | nyamatongwe <unknown> | 2004-01-12 11:18:26 +0000 |
commit | 60590dd936b2cbbdce7d620e1fa52a0e10ecfb9c (patch) | |
tree | 1ec7b0e57f38b2868dd6ab79672130c106ea4d22 | |
parent | 2b2c0414f2a278eb669a830dd8b8fa998e6ff757 (diff) | |
download | scintilla-mirror-60590dd936b2cbbdce7d620e1fa52a0e10ecfb9c.tar.gz |
Patch from Bruce Dodson to move length string methods
out of the header and into an implementation file.
-rw-r--r-- | include/SString.h | 220 | ||||
-rw-r--r-- | src/PropSet.cxx | 227 |
2 files changed, 249 insertions, 198 deletions
diff --git a/include/SString.h b/include/SString.h index 01602df78..7714d6d50 100644 --- a/include/SString.h +++ b/include/SString.h @@ -38,47 +38,9 @@ private: lenpos_t sLen; ///< The size of the string in s lenpos_t sizeGrowth; ///< Minimum growth size when appending strings enum { sizeGrowthDefault = 64 }; - bool grow(lenpos_t lenNew) { - while (sizeGrowth * 6 < lenNew) { - sizeGrowth *= 2; - } - char *sNew = new char[lenNew + sizeGrowth + 1]; - if (sNew) { - if (s) { - memcpy(sNew, s, sLen); - delete []s; - } - s = sNew; - s[sLen] = '\0'; - sSize = lenNew + sizeGrowth; - } - return sNew != 0; - } - SString &assign(const char *sOther, lenpos_t sSize_=measure_length) { - if (!sOther) { - sSize_ = 0; - } else if (sSize_ == measure_length) { - sSize_ = strlen(sOther); - } - if (sSize > 0 && sSize_ <= sSize) { // Does not allocate new buffer if the current is big enough - if (s && sSize_) { - memcpy(s, sOther, sSize_); - } - s[sSize_] = '\0'; - sLen = sSize_; - } else { - delete []s; - s = StringAllocate(sOther, sSize_); - if (s) { - sSize = sSize_; // Allow buffer bigger than real string, thus providing space to grow - sLen = strlen(s); - } else { - sSize = sLen = 0; - } - } - return *this; - } + bool grow(lenpos_t lenNew); + SString &assign(const char *sOther, lenpos_t sSize_=measure_length); public: SString() : s(0), sSize(0), sLen(0), sizeGrowth(sizeGrowthDefault) { @@ -140,40 +102,22 @@ public: } return *this; } - bool operator==(const SString &sOther) const { - if ((s == 0) && (sOther.s == 0)) - return true; - if ((s == 0) || (sOther.s == 0)) - return false; - return strcmp(s, sOther.s) == 0; - } + bool operator==(const SString &sOther) const; bool operator!=(const SString &sOther) const { return !operator==(sOther); } - bool operator==(const char *sOther) const { - if ((s == 0) && (sOther == 0)) - return true; - if ((s == 0) || (sOther == 0)) - return false; - return strcmp(s, sOther) == 0; - } + bool operator==(const char *sOther) const; bool operator!=(const char *sOther) const { return !operator==(sOther); } bool contains(char ch) { - if (s && *s) - return strchr(s, ch) != 0; - else - return false; + return (s && *s) ? strchr(s, ch) != 0 : false; } void setsizegrowth(lenpos_t sizeGrowth_) { sizeGrowth = sizeGrowth_; } const char *c_str() const { - if (s) - return s; - else - return ""; + return s ? s : ""; } /** Give ownership of buffer to caller which must use delete[] to free buffer. */ char *detach() { @@ -184,56 +128,12 @@ public: return sRet; } char operator[](lenpos_t i) const { - if (s && i < sSize) // Or < sLen? Depends on the use, both are OK - return s[i]; - else - return '\0'; - } - SString substr(lenpos_t subPos, lenpos_t subLen=measure_length) const { - if (subPos >= sLen) { - return SString(); // return a null string if start index is out of bounds - } - if ((subLen == measure_length) || (subPos + subLen > sLen)) { - subLen = sLen - subPos; // can't substr past end of source string - } - return SString(s, subPos, subPos + subLen); - } - SString &lowercase(lenpos_t subPos = 0, lenpos_t subLen=measure_length) { - if ((subLen == measure_length) || (subPos + subLen > sLen)) { - subLen = sLen - subPos; // don't apply past end of string - } - for (lenpos_t i = subPos; i < subPos + subLen; i++) { - if (s[i] < 'A' || s[i] > 'Z') - continue; - else - s[i] = static_cast<char>(s[i] - 'A' + 'a'); - } - return *this; - } - SString &append(const char *sOther, lenpos_t sLenOther=measure_length, char sep = '\0') { - if (!sOther) { - return *this; - } - if (sLenOther == measure_length) { - sLenOther = strlen(sOther); - } - int lenSep = 0; - if (sLen && sep) { // Only add a separator if not empty - lenSep = 1; - } - lenpos_t lenNew = sLen + sLenOther + lenSep; - // Conservative about growing the buffer: don't do it, unless really needed - if ((lenNew + 1 < sSize) || (grow(lenNew))) { - if (lenSep) { - s[sLen] = sep; - sLen++; - } - memcpy(&s[sLen], sOther, sLenOther); - sLen += sLenOther; - s[sLen] = '\0'; - } - return *this; + return (s && i < sSize) ? s[i] : '\0'; } + SString substr(lenpos_t subPos, lenpos_t subLen=measure_length) const; + SString &lowercase(lenpos_t subPos = 0, lenpos_t subLen=measure_length); + SString &uppercase(lenpos_t subPos = 0, lenpos_t subLen=measure_length); + SString &append(const char *sOther, lenpos_t sLenOther=measure_length, char sep = '\0'); SString &operator+=(const char *sOther) { return append(sOther, static_cast<lenpos_t>(measure_length)); } @@ -246,93 +146,31 @@ public: SString &appendwithseparator(const char *sOther, char sep) { return append(sOther, strlen(sOther), sep); } - SString &insert(lenpos_t pos, const char *sOther, lenpos_t sLenOther=measure_length) { - if (!sOther) { - return *this; - } - if (sLenOther == measure_length) { - sLenOther = strlen(sOther); - } - lenpos_t lenNew = sLen + sLenOther; - // Conservative about growing the buffer: don't do it, unless really needed - if ((lenNew + 1 < sSize) || grow(lenNew)) { - lenpos_t moveChars = sLen - pos + 1; - for (lenpos_t i = moveChars; i > 0; i--) { - s[pos + sLenOther + i - 1] = s[pos + i - 1]; - } - memcpy(s + pos, sOther, sLenOther); - sLen = lenNew; - } - return *this; - } + SString &insert(lenpos_t pos, const char *sOther, lenpos_t sLenOther=measure_length); + /** Remove @a len characters from the @a pos position, included. * Characters at pos + len and beyond replace characters at pos. * If @a len is 0, or greater than the length of the string * starting at @a pos, the string is just truncated at @a pos. */ - void remove(lenpos_t pos, lenpos_t len) { - if (len < 1 || pos + len >= sLen) { - s[pos] = '\0'; - sLen = pos; - } else { - for (lenpos_t i = pos; i < sLen - len + 1; i++) { - s[i] = s[i+len]; - } - sLen -= len; - } - } + void remove(lenpos_t pos, lenpos_t len); + SString &change(lenpos_t pos, char ch) { - if (pos >= sLen) { // character changed must be in string bounds - return *this; + if (pos < sLen) { // character changed must be in string bounds + *(s + pos) = ch; } - *(s + pos) = ch; return *this; } /** Read an integral numeric value from the string. */ int value() const { - if (s) - return atoi(s); - else - return 0; - } - int search(const char *sFind, lenpos_t start=0) const { - if (start < sLen) { - const char *sFound = strstr(s + start, sFind); - if (sFound) { - return sFound - s; - } - } - return -1; + return s ? atoi(s) : 0; } + int search(const char *sFind, lenpos_t start=0) const; bool contains(const char *sFind) { return search(sFind) >= 0; } - int substitute(char chFind, char chReplace) { - int c = 0; - char *t = s; - while (t) { - t = strchr(t, chFind); - if (t) { - *t = chReplace; - t++; - c++; - } - } - return c; - } - int substitute(const char *sFind, const char *sReplace) { - int c = 0; - lenpos_t lenFind = strlen(sFind); - lenpos_t lenReplace = strlen(sReplace); - int posFound = search(sFind); - while (posFound >= 0) { - remove(posFound, lenFind); - insert(posFound, sReplace, lenReplace); - posFound = search(sFind, posFound + lenReplace); - c++; - } - return c; - } + int substitute(char chFind, char chReplace); + int substitute(const char *sFind, const char *sReplace); int remove(const char *sFind) { return substitute(sFind, ""); } @@ -344,21 +182,7 @@ public: */ static char *StringAllocate( const char *s, ///< The string to duplicate - lenpos_t len=measure_length) ///< The length of memory to allocate. Optional. - { - if (s == 0) { - return 0; - } - if (len == measure_length) { - len = strlen(s); - } - char *sNew = new char[len + 1]; - if (sNew) { - memcpy(sNew, s, len); - sNew[len] = '\0'; - } - return sNew; - } + lenpos_t len=measure_length); ///< The length of memory to allocate. Optional. }; /** diff --git a/src/PropSet.cxx b/src/PropSet.cxx index 658d5046a..a12e074cc 100644 --- a/src/PropSet.cxx +++ b/src/PropSet.cxx @@ -71,6 +71,233 @@ bool EqualCaseInsensitive(const char *a, const char *b) { return 0 == CompareCaseInsensitive(a, b); } +// Since the CaseInsensitive functions declared in SString +// are implemented here, I will for now put the non-inline +// implementations of the SString members here as well, so +// that I can quickly see what effect this has. + +bool SString::grow(lenpos_t lenNew) { + while (sizeGrowth * 6 < lenNew) { + sizeGrowth *= 2; + } + char *sNew = new char[lenNew + sizeGrowth + 1]; + if (sNew) { + if (s) { + memcpy(sNew, s, sLen); + delete []s; + } + s = sNew; + s[sLen] = '\0'; + sSize = lenNew + sizeGrowth; + } + return sNew != 0; +} + +SString &SString::assign(const char *sOther, lenpos_t sSize_) { + if (!sOther) { + sSize_ = 0; + } else if (sSize_ == measure_length) { + sSize_ = strlen(sOther); + } + if (sSize > 0 && sSize_ <= sSize) { // Does not allocate new buffer if the current is big enough + if (s && sSize_) { + memcpy(s, sOther, sSize_); + } + s[sSize_] = '\0'; + sLen = sSize_; + } else { + delete []s; + s = StringAllocate(sOther, sSize_); + if (s) { + sSize = sSize_; // Allow buffer bigger than real string, thus providing space to grow + sLen = strlen(s); + } else { + sSize = sLen = 0; + } + } + return *this; +} + +bool SString::operator==(const SString &sOther) const { + if ((s == 0) && (sOther.s == 0)) + return true; + if ((s == 0) || (sOther.s == 0)) + return false; + return strcmp(s, sOther.s) == 0; +} + +bool SString::operator==(const char *sOther) const { + if ((s == 0) && (sOther == 0)) + return true; + if ((s == 0) || (sOther == 0)) + return false; + return strcmp(s, sOther) == 0; +} + +SString SString::substr(lenpos_t subPos, lenpos_t subLen) const { + if (subPos >= sLen) { + return SString(); // return a null string if start index is out of bounds + } + if ((subLen == measure_length) || (subPos + subLen > sLen)) { + subLen = sLen - subPos; // can't substr past end of source string + } + return SString(s, subPos, subPos + subLen); +} + +SString &SString::lowercase(lenpos_t subPos, lenpos_t subLen) { + if ((subLen == measure_length) || (subPos + subLen > sLen)) { + subLen = sLen - subPos; // don't apply past end of string + } + for (lenpos_t i = subPos; i < subPos + subLen; i++) { + if (s[i] < 'A' || s[i] > 'Z') + continue; + else + s[i] = static_cast<char>(s[i] - 'A' + 'a'); + } + return *this; +} + +SString &SString::uppercase(lenpos_t subPos, lenpos_t subLen) { + if ((subLen == measure_length) || (subPos + subLen > sLen)) { + subLen = sLen - subPos; // don't apply past end of string + } + for (lenpos_t i = subPos; i < subPos + subLen; i++) { + if (s[i] < 'a' || s[i] > 'z') + continue; + else + s[i] = static_cast<char>(s[i] - 'a' + 'A'); + } + return *this; +} + +SString &SString::append(const char *sOther, lenpos_t sLenOther, char sep) { + if (!sOther) { + return *this; + } + if (sLenOther == measure_length) { + sLenOther = strlen(sOther); + } + int lenSep = 0; + if (sLen && sep) { // Only add a separator if not empty + lenSep = 1; + } + lenpos_t lenNew = sLen + sLenOther + lenSep; + // Conservative about growing the buffer: don't do it, unless really needed + if ((lenNew + 1 < sSize) || (grow(lenNew))) { + if (lenSep) { + s[sLen] = sep; + sLen++; + } + memcpy(&s[sLen], sOther, sLenOther); + sLen += sLenOther; + s[sLen] = '\0'; + } + return *this; +} + +SString &SString::insert(lenpos_t pos, const char *sOther, lenpos_t sLenOther) { + if (!sOther) { + return *this; + } + if (sLenOther == measure_length) { + sLenOther = strlen(sOther); + } + lenpos_t lenNew = sLen + sLenOther; + // Conservative about growing the buffer: don't do it, unless really needed + if ((lenNew + 1 < sSize) || grow(lenNew)) { + lenpos_t moveChars = sLen - pos + 1; + for (lenpos_t i = moveChars; i > 0; i--) { + s[pos + sLenOther + i - 1] = s[pos + i - 1]; + } + memcpy(s + pos, sOther, sLenOther); + sLen = lenNew; + } + return *this; +} + +/** Remove @a len characters from the @a pos position, included. + * Characters at pos + len and beyond replace characters at pos. + * If @a len is 0, or greater than the length of the string + * starting at @a pos, the string is just truncated at @a pos. + */ +void SString::remove(lenpos_t pos, lenpos_t len) { + if (len < 1 || pos + len >= sLen) { + s[pos] = '\0'; + sLen = pos; + } else { + for (lenpos_t i = pos; i < sLen - len + 1; i++) { + s[i] = s[i+len]; + } + sLen -= len; + } +} + +int SString::search(const char *sFind, lenpos_t start) const { + if (start < sLen) { + const char *sFound = strstr(s + start, sFind); + if (sFound) { + return sFound - s; + } + } + return -1; +} + +int SString::substitute(char chFind, char chReplace) { + int c = 0; + char *t = s; + while (t) { + t = strchr(t, chFind); + if (t) { + *t = chReplace; + t++; + c++; + } + } + return c; +} + +int SString::substitute(const char *sFind, const char *sReplace) { + int c = 0; + lenpos_t lenFind = strlen(sFind); + lenpos_t lenReplace = strlen(sReplace); + int posFound = search(sFind); + while (posFound >= 0) { + remove(posFound, lenFind); + insert(posFound, sReplace, lenReplace); + posFound = search(sFind, posFound + lenReplace); + c++; + } + return c; +} + +/** + * Duplicate a C string. + * Allocate memory of the given size, or big enough to fit the string if length isn't given; + * then copy the given string in the allocated memory. + * @return the pointer to the new string + */ +char *SString::StringAllocate( + const char *s, ///< The string to duplicate + lenpos_t len) ///< The length of memory to allocate. Optional. +{ + if (s == 0) { + return 0; + } + if (len == measure_length) { + len = strlen(s); + } + char *sNew = new char[len + 1]; + if (sNew) { + memcpy(sNew, s, len); + sNew[len] = '\0'; + } + return sNew; +} + +// End SString functions + + + PropSet::PropSet() { superPS = 0; for (int root = 0; root < hashRoots; root++) |