ZNC trunk
Loading...
Searching...
No Matches
ZNCString.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ZNCSTRING_H
18#define ZNCSTRING_H
19
20#include <znc/zncconfig.h>
21#include <map>
22#include <set>
23#include <string>
24#include <vector>
25#include <sstream>
26#include <sys/types.h>
27#include <initializer_list>
28
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)
33
34class CString;
35class MCString;
36
37typedef std::set<CString> SCString;
38typedef std::vector<CString> VCString;
39typedef std::vector<std::pair<CString, CString>> VPair;
40
42
50class CString : public std::string {
51 public:
62
66
67 explicit CString(bool b) : std::string(b ? "true" : "false") {}
68 explicit CString(char c);
69 explicit CString(unsigned char c);
70 explicit CString(short i);
71 explicit CString(unsigned short i);
72 explicit CString(int i);
73 explicit CString(unsigned int i);
74 explicit CString(long i);
75 explicit CString(unsigned long i);
76 explicit CString(long long i);
77 explicit CString(unsigned long long i);
78 explicit CString(double i, int precision = 2);
79 explicit CString(float i, int precision = 2);
80
81 CString() : std::string() {}
82 CString(const char* c) : std::string(c) {}
83 CString(const char* c, size_t l) : std::string(c, l) {}
84 CString(const std::string& s) : std::string(s) {}
85 CString(size_t n, char c) : std::string(n, c) {}
86 CString(std::initializer_list<char> list) : std::string(list) {}
88
95 template <typename T>
96 bool Convert(T* target) const {
97 std::stringstream ss(*this);
98 ss >> *target;
99 return (bool)ss; // we don't care why it failed, only whether it failed
100 }
101
109 template <typename Iterator>
110 CString Join(Iterator i_start, const Iterator& i_end) const {
111 if (i_start == i_end) return CString("");
112 std::ostringstream output;
113 output << *i_start;
114 while (true) {
115 ++i_start;
116 if (i_start == i_end) return CString(output.str());
117 output << *this;
118 output << *i_start;
119 }
120 }
121
129 int CaseCmp(const CString& s,
130 CString::size_type uLen = CString::npos) const;
138 int StrCmp(const CString& s, CString::size_type uLen = CString::npos) const;
146 bool Equals(const CString& s, CaseSensitivity cs = CaseInsensitive) const;
150 bool Equals(const CString& s, bool bCaseSensitive,
151 CString::size_type uLen = CString::npos) const;
163 static bool WildCmp(const CString& sWild, const CString& sString,
173 bool WildCmp(const CString& sWild,
174 CaseSensitivity cs = CaseSensitive) const;
175
198
199 static EEscape ToEscape(const CString& sEsc);
200 CString Escape_n(EEscape eFrom, EEscape eTo) const;
204
225 static unsigned int Replace(CString& sStr, const CString& sReplace,
226 const CString& sWith, const CString& sLeft = "",
227 const CString& sRight = "",
228 bool bRemoveDelims = false);
229
240 CString Replace_n(const CString& sReplace, const CString& sWith,
241 const CString& sLeft = "", const CString& sRight = "",
242 bool bRemoveDelims = false) const;
252 unsigned int Replace(const CString& sReplace, const CString& sWith,
253 const CString& sLeft = "", const CString& sRight = "",
254 bool bRemoveDelims = false);
261 CString Ellipsize(unsigned int uLen) const;
266 CString Left(size_type uCount) const;
271 CString Right(size_type uCount) const;
272
276 CString FirstLine() const { return Token(0, false, "\n"); }
277
293 CString Token(size_t uPos, bool bRest = false, const CString& sSep = " ",
294 bool bAllowEmpty = false) const;
295
300 CString Token(size_t uPos, bool bRest, const CString& sSep,
301 bool bAllowEmpty, const CString& sLeft, const CString& sRight,
302 bool bTrimQuotes = true) const;
303
304 size_type URLSplit(MCString& msRet) const;
305 size_type OptionSplit(MCString& msRet, bool bUpperKeys = false) const;
306 size_type QuoteSplit(VCString& vsRet) const;
307
323 size_type Split(const CString& sDelim, VCString& vsRet,
324 bool bAllowEmpty = true, const CString& sLeft = "",
325 const CString& sRight = "", bool bTrimQuotes = true,
326 bool bTrimWhiteSpace = false) const;
327
332 size_type Split(const CString& sDelim, SCString& ssRet,
333 bool bAllowEmpty = true, const CString& sLeft = "",
334 const CString& sRight = "", bool bTrimQuotes = true,
335 bool bTrimWhiteSpace = false) const;
336
345 static CString NamedFormat(const CString& sFormat,
346 const MCString& msValues);
347
352 static CString RandomString(unsigned int uLength);
353
355 CString MD5() const;
358
363 unsigned long Base64Decode(CString& sRet) const;
368 unsigned long Base64Decode();
381 bool Base64Encode(CString& sRet, unsigned int uWrap = 0) const;
386 bool Base64Encode(unsigned int uWrap = 0);
391 CString Base64Encode_n(unsigned int uWrap = 0) const;
392
393#ifdef HAVE_LIBSSL
394 CString Encrypt_n(const CString& sPass, const CString& sIvec = "") const;
395 CString Decrypt_n(const CString& sPass, const CString& sIvec = "") const;
396 void Encrypt(const CString& sPass, const CString& sIvec = "");
397 void Decrypt(const CString& sPass, const CString& sIvec = "");
398 void Crypt(const CString& sPass, bool bEncrypt, const CString& sIvec = "");
399#endif
400
405 static CString ToPercent(double d);
410 static CString ToByteStr(unsigned long long d);
415 static CString ToTimeStr(unsigned long s);
416
418 bool ToBool() const;
420 short ToShort() const;
422 unsigned short ToUShort() const;
424 int ToInt() const;
426 long ToLong() const;
428 unsigned int ToUInt() const;
430 unsigned long ToULong() const;
432 unsigned long long ToULongLong() const;
434 long long ToLongLong() const;
436 double ToDouble() const;
437
443 bool Trim(const CString& s = " \t\r\n");
449 bool TrimLeft(const CString& s = " \t\r\n");
455 bool TrimRight(const CString& s = " \t\r\n");
461 CString Trim_n(const CString& s = " \t\r\n") const;
467 CString TrimLeft_n(const CString& s = " \t\r\n") const;
473 CString TrimRight_n(const CString& s = " \t\r\n") const;
474
479 bool TrimPrefix(const CString& sPrefix = ":");
484 bool TrimSuffix(const CString& sSuffix);
489 CString TrimPrefix_n(const CString& sPrefix = ":") const;
494 CString TrimSuffix_n(const CString& sSuffix) const;
495
502 size_t Find(const CString& s, CaseSensitivity cs = CaseInsensitive) const;
509 bool StartsWith(const CString& sPrefix,
517 bool EndsWith(const CString& sSuffix,
526
531 bool LeftChomp(size_type uLen = 1);
536 bool RightChomp(size_type uLen = 1);
542 CString LeftChomp_n(size_type uLen = 1) const;
548 CString RightChomp_n(size_type uLen = 1) const;
562
563 private:
564 protected:
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;
569};
570
577class MCString : public std::map<CString, CString> {
578 public:
580 MCString() : std::map<CString, CString>() {}
582 MCString(std::initializer_list<std::pair<const CString, CString>> list)
583 : std::map<CString, CString>(list) {}
585 virtual ~MCString() { clear(); }
586
588 static const MCString EmptyMap;
589
604
611 enum status_t WriteToDisk(const CString& sPath, mode_t iMode = 0644) const;
617 enum status_t ReadFromDisk(const CString& sPath);
618
626 virtual bool WriteFilter(CString& sKey, CString& sValue) const {
627 return true;
628 }
636 virtual bool ReadFilter(CString& sKey, CString& sValue) const {
637 return true;
638 }
639
643 virtual CString& Encode(CString& sValue) const;
645 virtual CString& Decode(CString& sValue) const;
646};
647
648namespace std {
649template <>
650struct hash<CString> : hash<std::string> {};
651}
652
653// Make translateable messages easy to write:
654// t_f("Foo is {1}")(foo)
656 public:
657 explicit CInlineFormatMessage(const CString& sFormat)
658 : m_sFormat(sFormat) {}
659 explicit CInlineFormatMessage(CString&& sFormat)
660 : m_sFormat(std::move(sFormat)) {}
661
662 template <typename... Args>
663 CString operator()(const Args&... args) const {
664 MCString values;
665 apply(values, 1, args...);
666 return CString::NamedFormat(m_sFormat, values);
667 }
668
669 private:
670 template <typename Arg, typename... Rest>
671 void apply(MCString& values, int index, const Arg& arg,
672 const Rest&... rest) const {
673 values[CString(index)] = CString(arg);
674 apply(values, index + 1, rest...);
675 }
676
677 void apply(MCString& values, int index) const {}
678
679 CString m_sFormat;
680};
681
682// For gtest
683#ifdef GTEST_FAIL
684inline void PrintTo(const CString& s, std::ostream* os) {
685 *os << '"' << s.Escape_n(CString::EDEBUG) << '"';
686}
687#endif
688
689#endif // !ZNCSTRING_H
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
Definition ZNCString.h:655
CString operator()(const Args &... args) const
Definition ZNCString.h:663
CInlineFormatMessage(CString &&sFormat)
Definition ZNCString.h:659
CInlineFormatMessage(const CString &sFormat)
Definition ZNCString.h:657
String class that is used inside ZNC.
Definition ZNCString.h:50
unsigned long long ToULongLong() const
CString(unsigned long i)
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)
CString(long long 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(char c)
int ToInt() const
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 ToBool() 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.
CString(unsigned char c)
static EEscape ToEscape(const CString &sEsc)
unsigned int ToUInt() const
long ToLong() const
CString(unsigned int i)
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(int i)
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.
double ToDouble() const
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(long i)
CString(double i, int precision=2)
CString(short i)
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.
CString SHA256() const
bool TrimLeft(const CString &s=" \t\r\n")
Trim this string.
CString(const char *c)
Definition ZNCString.h:82
CString MD5() const
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.
short ToShort() const
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
STL namespace.