diff options
author | nyamatongwe <devnull@localhost> | 2001-10-08 04:12:11 +0000 |
---|---|---|
committer | nyamatongwe <devnull@localhost> | 2001-10-08 04:12:11 +0000 |
commit | 84aba19d1c62a3b2b81860fea6a129e2a25a56c2 (patch) | |
tree | 4486aea0482673da29b3a02dd5d6cb2058db7194 | |
parent | e7eabb12a989770977828b131a8ca1fdd710833f (diff) | |
download | scintilla-mirror-84aba19d1c62a3b2b81860fea6a129e2a25a56c2.tar.gz |
Added methods: insert, remove, search, contains and a string version of
substitute.
-rw-r--r-- | include/SString.h | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/include/SString.h b/include/SString.h index aeb594050..64f702b66 100644 --- a/include/SString.h +++ b/include/SString.h @@ -234,22 +234,71 @@ public: SString &appendwithseparator(const char* sOther, char sep) { return append(sOther, strlen(sOther), sep); } + SString &insert(int pos, const char* sOther, int sLenOther=-1) { + if (sLenOther < 0) + sLenOther = strlen(sOther); + int lenNew = sLen + sLenOther; + if (lenNew + 1 >= sSize) { + // Conservative about growing the buffer: don't do it, unless really needed + char *sNew = new char[lenNew + sizeGrowth + 1]; + if (!sNew) + return *this; + if (s) { + memcpy(sNew, s, sLen+1); + delete []s; + } + s = sNew; + sSize = lenNew + sizeGrowth; + } + int moveChars = sLen-pos; + for (int i=moveChars; i>=0; i--) + s[lenNew-moveChars+i] = s[pos+i]; + memcpy(s + pos, sOther, sLenOther); + sLen += sLenOther; + return *this; + } + void remove(int pos, int len=1) { + for (int i=pos; i<sLen-len+1; i++) + s[i] = s[i+len]; + sLen -= len; + } int value() const { if (s) return atoi(s); else return 0; } - void substitute(char find, char replace) { + int search(const char *sFind, int start=0) { + if ((start >= 0) && (start < sLen)) { + const char *sFound = strstr(s+start, sFind); + if (sFound) + return sFound - s; + } + return -1; + } + bool contains(const char *sFind) { + return search(sFind) >= 0; + } + void substitute(char chFind, char chReplace) { char *t = s; while (t) { - t = strchr(t, find); + t = strchr(t, chFind); if (t) { - *t = replace; + *t = chReplace; t++; } } } + void substitute(const char *sFind, const char *sReplace) { + int lenFind = strlen(sFind); + int lenReplace = strlen(sReplace); + int posFound = search(sFind); + while (posFound >= 0) { + remove(posFound, lenFind); + insert(posFound, sReplace, lenReplace); + posFound = search(sFind, posFound + lenReplace); + } + } }; #endif |