20#include <znc/zncconfig.h>
27#include <initializer_list>
29#define _SQL(s) CString("'" + CString(s).Escape_n(CString::ESQL) + "'")
30#define _URL(s) CString(s).Escape_n(CString::EURL)
31#define _HTML(s) CString(s).Escape_n(CString::EHTML)
32#define _NAMEDFMT(s) CString(s).Escape_n(CString::ENAMEDFMT)
39typedef std::vector<std::pair<CString, CString>>
VPair;
67 explicit CString(
bool b) :
std::string(b ?
"true" :
"false") {}
78 explicit CString(
double i,
int precision = 2);
79 explicit CString(
float i,
int precision = 2);
86 CString(std::initializer_list<char> list) :
std::string(list) {}
97 std::stringstream ss(*
this);
109 template <
typename Iterator>
111 if (i_start == i_end)
return CString(
"");
112 std::ostringstream output;
116 if (i_start == i_end)
return CString(output.str());
130 CString::size_type uLen = CString::npos)
const;
138 int StrCmp(
const CString& s, CString::size_type uLen = CString::npos)
const;
151 CString::size_type uLen = CString::npos)
const;
228 bool bRemoveDelims =
false);
242 bool bRemoveDelims =
false)
const;
254 bool bRemoveDelims =
false);
294 bool bAllowEmpty =
false)
const;
302 bool bTrimQuotes =
true)
const;
324 bool bAllowEmpty =
true,
const CString& sLeft =
"",
325 const CString& sRight =
"",
bool bTrimQuotes =
true,
326 bool bTrimWhiteSpace =
false)
const;
333 bool bAllowEmpty =
true,
const CString& sLeft =
"",
334 const CString& sRight =
"",
bool bTrimQuotes =
true,
335 bool bTrimWhiteSpace =
false)
const;
565 unsigned char*
strnchr(
const unsigned char* src,
unsigned char c,
566 unsigned int iMaxBytes,
567 unsigned char* pFill =
nullptr,
568 unsigned int* piCount =
nullptr)
const;
577class MCString :
public std::map<CString, CString> {
582 MCString(std::initializer_list<std::pair<const CString, CString>> list)
658 : m_sFormat(sFormat) {}
660 : m_sFormat(
std::move(sFormat)) {}
662 template <
typename... Args>
665 apply(values, 1, args...);
670 template <
typename Arg,
typename... Rest>
671 void apply(
MCString& values,
int index,
const Arg& arg,
672 const Rest&... rest)
const {
674 apply(values, index + 1, rest...);
677 void apply(
MCString& values,
int index)
const {}
684inline void PrintTo(
const CString& s, std::ostream* os) {
std::vector< std::pair< CString, CString > > VPair
Definition ZNCString.h:39
std::set< CString > SCString
Definition ZNCString.h:37
CaseSensitivity
Definition ZNCString.h:41
std::vector< CString > VCString
Definition ZNCString.h:38
String class that is used inside ZNC.
Definition ZNCString.h:50
unsigned long long ToULongLong() const
size_type Split(const CString &sDelim, VCString &vsRet, bool bAllowEmpty=true, const CString &sLeft="", const CString &sRight="", bool bTrimQuotes=true, bool bTrimWhiteSpace=false) const
Split up this string into tokens.
size_type QuoteSplit(VCString &vsRet) const
static CString NamedFormat(const CString &sFormat, const MCString &msValues)
Build a string from a format string, replacing values from a map.
CString(unsigned short i)
unsigned long Base64Decode()
Treat this string as base64-encoded data and decode it.
CString Replace_n(const CString &sReplace, const CString &sWith, const CString &sLeft="", const CString &sRight="", bool bRemoveDelims=false) const
Replace all occurrences in the current string.
CString(bool b)
Definition ZNCString.h:67
CString RightChomp_n(size_type uLen=1) const
Remove characters from the end of this string.
CString AsLower() const
Return a copy of this string with all characters turned into lower-case.
CString Escape_n(EEscape eTo) const
CString(float i, int precision=2)
CString Join(Iterator i_start, const Iterator &i_end) const
Joins a collection of objects together, using 'this' as a delimiter.
Definition ZNCString.h:110
bool EndsWith(const CString &sSuffix, CaseSensitivity cs=CaseInsensitive) const
Check whether the string ends with a given suffix.
CString TrimLeft_n(const CString &s=" \t\r\n") const
Trim this string.
CString(std::initializer_list< char > list)
Definition ZNCString.h:86
CString(const std::string &s)
Definition ZNCString.h:84
CString & Escape(EEscape eTo)
CString Encrypt_n(const CString &sPass, const CString &sIvec="") const
unsigned short ToUShort() const
bool Equals(const CString &s, bool bCaseSensitive, CString::size_type uLen=CString::npos) const
int StrCmp(const CString &s, CString::size_type uLen=CString::npos) const
Compare this string case sensitively to some other string.
static unsigned int Replace(CString &sStr, const CString &sReplace, const CString &sWith, const CString &sLeft="", const CString &sRight="", bool bRemoveDelims=false)
Replace all occurrences in a string.
static const CaseSensitivity CaseSensitive
Definition ZNCString.h:63
static CString RandomString(unsigned int uLength)
Produces a random string.
CString Decrypt_n(const CString &sPass, const CString &sIvec="") const
bool Base64Encode(unsigned int uWrap=0)
Base64-encode the current string.
long long ToLongLong() const
static const CaseSensitivity CaseInsensitive
Definition ZNCString.h:64
CString & MakeUpper()
Turn all characters in this string into their upper-case equivalent.
static EEscape ToEscape(const CString &sEsc)
unsigned int ToUInt() const
int CaseCmp(const CString &s, CString::size_type uLen=CString::npos) const
Compare this string caselessly to some other string.
void Crypt(const CString &sPass, bool bEncrypt, const CString &sIvec="")
CString StripControls_n() const
Remove controls characters from this string.
CString FirstLine() const
Get the first line of this string.
Definition ZNCString.h:276
CString TrimRight_n(const CString &s=" \t\r\n") const
Trim this string.
~CString()
Definition ZNCString.h:87
bool LeftChomp(size_type uLen=1)
Remove characters from the beginning of this string.
CString Token(size_t uPos, bool bRest=false, const CString &sSep=" ", bool bAllowEmpty=false) const
Get a token out of this string.
CString AsUpper() const
Return a copy of this string with all characters turned into upper-case.
CString & MakeLower()
Turn all characters in this string into their lower-case equivalent.
CString & StripControls()
Remove controls characters from this string.
CString Trim_n(const CString &s=" \t\r\n") const
Trim this string.
void Encrypt(const CString &sPass, const CString &sIvec="")
size_type OptionSplit(MCString &msRet, bool bUpperKeys=false) const
bool RightChomp(size_type uLen=1)
Remove characters from the end of this string.
unsigned long Base64Decode(CString &sRet) const
Treat this string as base64-encoded data and decode it.
static CString ToTimeStr(unsigned long s)
Pretty-print a time span.
CString(const char *c, size_t l)
Definition ZNCString.h:83
bool Contains(const CString &s, CaseSensitivity cs=CaseInsensitive) const
Check whether the string contains a given string.
CString Base64Encode_n(unsigned int uWrap=0) const
Base64-encode the current string.
EEscape
Definition ZNCString.h:52
@ EHTML
Definition ZNCString.h:55
@ EURL
Definition ZNCString.h:54
@ ESQL
Definition ZNCString.h:56
@ ENAMEDFMT
Definition ZNCString.h:57
@ EHEXCOLON
Definition ZNCString.h:60
@ EDEBUG
Definition ZNCString.h:58
@ EMSGTAG
Definition ZNCString.h:59
@ EASCII
Definition ZNCString.h:53
bool Base64Encode(CString &sRet, unsigned int uWrap=0) const
Base64-encode the current string.
CString & Escape(EEscape eFrom, EEscape eTo)
CString(size_t n, char c)
Definition ZNCString.h:85
CString TrimSuffix_n(const CString &sSuffix) const
Trim a given suffix.
CString Left(size_type uCount) const
Return the left part of the string.
CString(double i, int precision=2)
size_t Find(const CString &s, CaseSensitivity cs=CaseInsensitive) const
Find the position of the given substring.
CString Token(size_t uPos, bool bRest, const CString &sSep, bool bAllowEmpty, const CString &sLeft, const CString &sRight, bool bTrimQuotes=true) const
Get a token out of this string.
size_type Split(const CString &sDelim, SCString &ssRet, bool bAllowEmpty=true, const CString &sLeft="", const CString &sRight="", bool bTrimQuotes=true, bool bTrimWhiteSpace=false) const
Split up this string into tokens.
CString Ellipsize(unsigned int uLen) const
Ellipsize the current string.
CString LeftChomp_n(size_type uLen=1) const
Remove characters from the beginning of this string.
unsigned char * strnchr(const unsigned char *src, unsigned char c, unsigned int iMaxBytes, unsigned char *pFill=nullptr, unsigned int *piCount=nullptr) const
CString Escape_n(EEscape eFrom, EEscape eTo) const
unsigned int Replace(const CString &sReplace, const CString &sWith, const CString &sLeft="", const CString &sRight="", bool bRemoveDelims=false)
Replace all occurrences in the current string.
unsigned long ToULong() const
bool Trim(const CString &s=" \t\r\n")
Trim this string.
static CString ToPercent(double d)
Pretty-print a percent value.
bool StartsWith(const CString &sPrefix, CaseSensitivity cs=CaseInsensitive) const
Check whether the string starts with a given prefix.
bool TrimLeft(const CString &s=" \t\r\n")
Trim this string.
CString(const char *c)
Definition ZNCString.h:82
static bool WildCmp(const CString &sWild, const CString &sString, CaseSensitivity cs=CaseSensitive)
Do a wildcard comparison between two strings.
bool TrimSuffix(const CString &sSuffix)
Trim a given suffix.
CString Right(size_type uCount) const
Return the right part of the string.
void Decrypt(const CString &sPass, const CString &sIvec="")
CString TrimPrefix_n(const CString &sPrefix=":") const
Trim a given prefix.
bool WildCmp(const CString &sWild, CaseSensitivity cs=CaseSensitive) const
Do a wild compare on this string.
bool Convert(T *target) const
Casts a CString to another type.
Definition ZNCString.h:96
bool Equals(const CString &s, CaseSensitivity cs=CaseInsensitive) const
Check if this string is equal to some other string.
size_type URLSplit(MCString &msRet) const
CString()
Definition ZNCString.h:81
CString Base64Decode_n() const
Treat this string as base64-encoded data and decode it.
bool TrimRight(const CString &s=" \t\r\n")
Trim this string.
CString(unsigned long long i)
static CString ToByteStr(unsigned long long d)
Pretty-print a number of bytes.
bool TrimPrefix(const CString &sPrefix=":")
Trim a given prefix.
A dictionary for strings.
Definition ZNCString.h:577
enum status_t ReadFromDisk(const CString &sPath)
Read a map from a file.
enum status_t WriteToDisk(const CString &sPath, mode_t iMode=0644) const
Write this map to a file.
virtual ~MCString()
Destruct this MCString.
Definition ZNCString.h:585
static const MCString EmptyMap
A static instance of an empty map.
Definition ZNCString.h:588
MCString()
Construct an empty MCString.
Definition ZNCString.h:580
virtual CString & Decode(CString &sValue) const
Undo the effects of Encode().
virtual CString & Encode(CString &sValue) const
Encode a value so that it can safely be parsed by ReadFromDisk().
virtual bool ReadFilter(CString &sKey, CString &sValue) const
Filter used while reading this map.
Definition ZNCString.h:636
status_t
Status codes that can be returned by WriteToDisk() and ReadFromDisk().
Definition ZNCString.h:592
@ MCS_EWRITEFIL
WriteFilter() failed.
Definition ZNCString.h:600
@ MCS_EOPEN
Opening the file failed.
Definition ZNCString.h:596
@ MCS_SUCCESS
No errors.
Definition ZNCString.h:594
@ MCS_EWRITE
Writing to the file failed.
Definition ZNCString.h:598
@ MCS_EREADFIL
ReadFilter() failed.
Definition ZNCString.h:602
MCString(std::initializer_list< std::pair< const CString, CString > > list)
Construct a MCString using an initializer list eg.
Definition ZNCString.h:582
virtual bool WriteFilter(CString &sKey, CString &sValue) const
Filter used while writing this map.
Definition ZNCString.h:626