From ec9e0b5614d0f3661f18a4343a4c483dd020d53c Mon Sep 17 00:00:00 2001 From: prozacx Date: Sun, 22 Jan 2006 06:02:29 +0000 Subject: [PATCH] Added Split() for splitting into a set git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@597 726aef4b-f618-498e-8847-2d620e286838 --- String.cpp | 34 +++++++++++++++++++++++++++++++--- String.h | 7 +++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/String.cpp b/String.cpp index c2f493e6..3914c277 100644 --- a/String.cpp +++ b/String.cpp @@ -386,10 +386,17 @@ unsigned int CString::Replace(CString& sStr, const CString& sReplace, const CStr } CString CString::Token(unsigned int uPos, bool bRest, const CString& sSep) const { - string sRet; + CString sRet; + Token(sRet, uPos, bRest, sSep); + return sRet; +} + +bool CString::Token(CString& sRet, unsigned int uPos, bool bRest, const CString& sSep) const { const char* p = c_str(); unsigned int uSepLen = sSep.length(); + sRet.clear(); + if (uSepLen) { uSepLen--; } @@ -403,7 +410,7 @@ CString CString::Token(unsigned int uPos, bool bRest, const CString& sSep) const } else { if (strncmp(p, sSep.c_str(), sSep.length()) == 0) { if (!bRest) { - return sRet; + return true; } } @@ -413,7 +420,7 @@ CString CString::Token(unsigned int uPos, bool bRest, const CString& sSep) const p++; } - return sRet; + return !sRet.empty(); } CString CString::Ellipsize(unsigned int uLen) const { @@ -447,6 +454,17 @@ CString CString::Right(unsigned int uCount) const { return substr(length() - uCount, uCount); } +void CString::Split(SCString& ssRet, const CString& sDelim) { + ssRet.clear(); + + unsigned int a = 0; + CString sTok; + + while (Token(sTok, a++, false, sDelim)) { + ssRet.insert(sTok); + } +} + VCString CString::Split(const CString& sDelim, bool bAllowEmpty) const { VCString vsRet; Split(sDelim, vsRet, bAllowEmpty); @@ -475,6 +493,16 @@ unsigned int CString::Split(const CString& sDelim, VCString& vsRet, bool bAllowE return vsRet.size(); } +CString CString::RandomString(unsigned int uLength) { + CString sRet; + + for (unsigned int a = 0; a < uLength; a++) { + sRet += (char) (rand() % 26) + 65; + } + + return sRet; +} + CString CString::Format(const CString& sFormatStr, ...) { return ""; } diff --git a/String.h b/String.h index 5e24f616..a8192a9f 100644 --- a/String.h +++ b/String.h @@ -4,15 +4,18 @@ #include #include #include +#include #include using std::map; using std::string; +using std::set; using std::vector; using std::stringstream; class CString; typedef vector VCString; +typedef set SCString; typedef map MVCString; static const unsigned char XX = 0xff; @@ -73,10 +76,14 @@ public: CString Left(unsigned int uCount) const; CString Right(unsigned int uCount) const; + static CString RandomString(unsigned int uLength); + CString Token(unsigned int uPos, bool bRest = false, const CString& sSep = " ") const; + bool Token(CString& sRet, unsigned int uPos, bool bRest = false, const CString& sSep = " ") const; VCString Split(const CString& sDelim, bool bKeepEmpty = true) const; unsigned int Split(const CString& sDelim, VCString& vsRet, bool bAllowEmpty = true) const; static CString Format(const CString& sFormatStr, ...); + void Split(SCString& ssRet, const CString& sDelim = " "); CString MD5() const; unsigned long Base64Decode(CString& sRet) const;