From 7f0215de4024332fb447953cbc0b5f33da3b06c1 Mon Sep 17 00:00:00 2001 From: prozacx Date: Tue, 3 May 2005 22:12:22 +0000 Subject: [PATCH] CString class git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@239 726aef4b-f618-498e-8847-2d620e286838 --- String.cpp | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++ String.h | 54 ++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 String.cpp create mode 100644 String.h diff --git a/String.cpp b/String.cpp new file mode 100644 index 00000000..49a24f17 --- /dev/null +++ b/String.cpp @@ -0,0 +1,165 @@ +#include +#include "String.h" + +int CString::CaseCmp(const CString& s) const { + return strcasecmp(c_str(), s.c_str()); +} + +int CString::StrCmp(const CString& s) const { + return strcmp(c_str(), s.c_str()); +} + +bool CString::WildCmp(const CString& sWild, const CString& sString) { + return true; // @todo +} + +bool CString::WildCmp(const CString& sWild) const { + return CString::WildCmp(sWild, *this); +} + +CString& CString::MakeUpper() { + for (unsigned int a = 0; a < length(); a++) { + char& c = (*this)[a]; + c = toupper(c); + } + + return *this; +} + +CString& CString::MakeLower() { + for (unsigned int a = 0; a < length(); a++) { + char& c = (*this)[a]; + c = tolower(c); + } + + return *this; +} + +CString CString::Token(unsigned int uPos, bool bRest, char cSep) { + string sRet; + const char* p = c_str(); + + while (*p) { + if (uPos) { + if (*p == cSep) { + uPos--; + } + } else { + if ((*p == cSep) && (!bRest)) { + return sRet; + } + + sRet += *p; + } + + p++; + } + + return sRet; +} + +CString CString::Ellipsize(unsigned int uLen) { + if (uLen >= size()) { + return *this; + } + + string sRet; + + // @todo this looks suspect + if (uLen < 4) { + for (unsigned int a = 0; a < uLen; a++) { + sRet += "."; + } + + return sRet; + } + + sRet = substr(0, uLen -3) + "..."; + + return sRet; +} + +CString CString::Left(unsigned int uCount) const { + uCount = (uCount > length()) ? length() : uCount; + return substr(0, uCount); +} + +CString CString::Right(unsigned int uCount) const { + uCount = (uCount > length()) ? length() : uCount; + return substr(length() - uCount, uCount); +} + +CString CString::Format(const CString& sFormatStr, ...) { + return ""; +} + +CString CString::ToString(short i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(unsigned short i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(int i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(unsigned int i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(long i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(unsigned long i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(unsigned long long i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(double i) { stringstream s; s << i; return s.str(); } +CString CString::ToString(float i) { stringstream s; s << i; return s.str(); } + +CString CString::ToPercent(double d) { + char szRet[32]; + snprintf(szRet, 32, "%.02f%%", d); + return szRet; +} + +CString CString::ToKBytes(double d) { + char szRet[32]; + snprintf(szRet, 32, "%.0f K/s", d); + return szRet; +} + +bool CString::Trim(const CString& s) { + bool bLeft = LeftTrim(s); + return (RightTrim(s) || bLeft); +} + +bool CString::LeftTrim(const CString& s) { + bool bRet = false; + + while ((Left(1) == " ") || (Left(1) == "\t") || (Left(1) == "\r") || (Left(1) == "\n")) { + LeftChomp(); + bRet = true; + } + + return bRet; +} + +bool CString::RightTrim(const CString& s) { + bool bRet = false; + + while ((Right(1) == " ") || (Right(1) == "\t") || (Right(1) == "\r") || (Right(1) == "\n")) { + RightChomp(); + bRet = true; + } + + return bRet; +} + +bool CString::LeftChomp(unsigned int uLen) { + bool bRet = false; + + while ((uLen--) && (length())) { + erase(0, 1); + bRet = true; + } + + return bRet; +} + +bool CString::RightChomp(unsigned int uLen) { + bool bRet = false; + + while ((uLen--) && (length())) { + erase(length() -1); + bRet = true; + } + + return bRet; +} diff --git a/String.h b/String.h new file mode 100644 index 00000000..c2b76761 --- /dev/null +++ b/String.h @@ -0,0 +1,54 @@ +#ifndef X_STRING_H +#define X_STRING_H + +#include +#include + +using std::string; +using std::stringstream; + +class CString : public string { +public: + CString() : string() {} + CString(const char* c) : string(c) {} + CString(const string& s) : string(s) {} + virtual ~CString() {} + + int CaseCmp(const CString& s) const; + int StrCmp(const CString& s) const; + static bool WildCmp(const CString& sWild, const CString& sString); + bool WildCmp(const CString& sWild) const; + + CString& MakeUpper(); + CString& MakeLower(); + + CString Token(unsigned int uPos, bool bRest = false, char cSep = ' '); + CString Ellipsize(unsigned int uLen); + CString Left(unsigned int uCount) const; + CString Right(unsigned int uCount) const; + + static CString Format(const CString& sFormatStr, ...); + + static CString ToString(short i); + static CString ToString(unsigned short i); + static CString ToString(int i); + static CString ToString(unsigned int i); + static CString ToString(long i); + static CString ToString(unsigned long i); + static CString ToString(unsigned long long i); + static CString ToString(double i); + static CString ToString(float i); + static CString ToPercent(double d); + static CString ToKBytes(double d); + + bool Trim(const CString& s = " \t\r\n"); + bool LeftTrim(const CString& s = " \t\r\n"); + bool RightTrim(const CString& s = " \t\r\n"); + bool LeftChomp(unsigned int uLen = 1); + bool RightChomp(unsigned int uLen = 1); +private: +protected: +}; + + +#endif // !X_STRING_H