From e1f56f8689de800a25e5caa6c55326d03a55888b Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Mon, 8 Oct 2001 04:12:11 +0000 Subject: Added methods: insert, remove, search, contains and a string version of substitute. --- include/SString.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'include/SString.h') 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= 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 -- cgit v1.2.3