diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-07 22:33:50 +0200 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-08 11:35:51 +0200 |
commit | 1978c49bea5b439d993067c055cec47e70db8fd6 (patch) | |
tree | 255caea96a13f95564e6b631be9a4ac55ce33cd9 /src/include/convert.h | |
parent | 3b1278f5459514a6d6364f068ff97b8a0432057b (diff) |
minor refactoring
Diffstat (limited to 'src/include/convert.h')
-rw-r--r-- | src/include/convert.h | 103 |
1 files changed, 42 insertions, 61 deletions
diff --git a/src/include/convert.h b/src/include/convert.h index 9e51027..2342a70 100644 --- a/src/include/convert.h +++ b/src/include/convert.h @@ -1,12 +1,13 @@ -/** -------------------------------------------------------------------- +/** * @file convert.h * @brief Declaration of miscellaneous convertion functions. * @author Andreas Aardal Hanssen * @date 2002-2005 - * ----------------------------------------------------------------- **/ + */ #ifndef convert_h_included #define convert_h_included + #include "address.h" #include "depot.h" @@ -22,7 +23,6 @@ namespace Binc { - //---------------------------------------------------------------------- inline std::string toString(int i_in) { char intbuf[16]; @@ -30,7 +30,6 @@ namespace Binc { return std::string(intbuf); } - //---------------------------------------------------------------------- inline std::string toString(unsigned int i_in) { char intbuf[16]; @@ -38,7 +37,6 @@ namespace Binc { return std::string(intbuf); } - //---------------------------------------------------------------------- inline std::string toString(unsigned long i_in) { char longbuf[40]; @@ -46,25 +44,22 @@ namespace Binc { return std::string(longbuf); } - //---------------------------------------------------------------------- inline std::string toString(const char *i_in) { return std::string(i_in); } - //---------------------------------------------------------------------- inline int atoi(const std::string &s_in) { return ::atoi(s_in.c_str()); } - //---------------------------------------------------------------------- inline std::string toHex(const std::string &s) { const char hexchars[] = "0123456789abcdef"; std::string tmp; - for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { - unsigned char c = (unsigned char)*i; + for (auto i : s) { + unsigned char c = static_cast<unsigned char>(i); tmp += hexchars[((c & 0xf0) >> 4)]; tmp += hexchars[c & 0x0f]; } @@ -72,22 +67,21 @@ namespace Binc { return tmp; } - //---------------------------------------------------------------------- inline std::string fromHex(const std::string &s) { // const char hexchars[] = "0123456789abcdef"; std::string tmp; - for (std::string::const_iterator i = s.begin(); i != s.end() && i + 1 != s.end(); i += 2) { + for (auto i = s.cbegin(); i != s.cend() && i + 1 != s.cend(); i += 2) { int n; unsigned char c = *i; unsigned char d = *(i + 1); char *t; - if ((t = strchr(hexchars, c)) == 0) return "out of range"; + if ((t = strchr(hexchars, c)) == nullptr) return "out of range"; n = (t - hexchars) << 4; - if ((t = strchr(hexchars, d)) == 0) return "out of range"; + if ((t = strchr(hexchars, d)) == nullptr) return "out of range"; n += (t - hexchars); if (n >= 0 && n <= 255) @@ -99,11 +93,10 @@ namespace Binc { return tmp; } - //---------------------------------------------------------------------- inline std::string toImapString(const std::string &s_in) { - for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) { - unsigned char c = (unsigned char)*i; + for (const auto i : s_in) { + auto c = static_cast<unsigned char>(i); if (c <= 31 || c >= 127 || c == '\"' || c == '\\') return "{" + toString(s_in.length()) + "}\r\n" + s_in; } @@ -111,21 +104,18 @@ namespace Binc { return "\"" + s_in + "\""; } - //---------------------------------------------------------------------- inline void uppercase(std::string &input) { - for (std::string::iterator i = input.begin(); i != input.end(); ++i) - *i = toupper(*i); + for (auto &i : input) + i = toupper(i); } - //---------------------------------------------------------------------- inline void lowercase(std::string &input) { - for (std::string::iterator i = input.begin(); i != input.end(); ++i) - *i = tolower(*i); + for (char &i : input) + i = tolower(i); } - //---------------------------------------------------------------------- inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n") { int n = s_in.length(); @@ -133,7 +123,6 @@ namespace Binc { s_in.resize(n-- - 1); } - //---------------------------------------------------------------------- inline void trim(std::string &s_in, const std::string &chars = " \t\r\n") { while (s_in != "" && chars.find(s_in[0]) != std::string::npos) @@ -141,14 +130,15 @@ namespace Binc { chomp(s_in, chars); } - //---------------------------------------------------------------------- inline const std::string unfold(const std::string &a, bool removecomment = true) { std::string tmp; + bool incomment = false; bool inquotes = false; - for (std::string::const_iterator i = a.begin(); i != a.end(); ++i) { - unsigned char c = (unsigned char)*i; + + for (const char i : a) { + unsigned char c = (unsigned char)i; if (!inquotes && removecomment) { if (c == '(') { incomment = true; @@ -156,14 +146,14 @@ namespace Binc { } else if (c == ')') { incomment = false; } else if (c != 0x0a && c != 0x0d) { - tmp += *i; + tmp += i; } } else if (c != 0x0a && c != 0x0d) { - tmp += *i; + tmp += i; } if (!incomment) { - if (*i == '\"') inquotes = !inquotes; + if (i == '\"') inquotes = !inquotes; } } @@ -171,46 +161,45 @@ namespace Binc { return tmp; } - //---------------------------------------------------------------------- inline void split(const std::string &s_in, const std::string &delim, std::vector<std::string> &dest, bool skipempty = true) { std::string token; - for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) { - if (delim.find(*i) != std::string::npos) { + for (const char i : s_in) { + if (delim.find(i) != std::string::npos) { if (!skipempty || token != "") dest.push_back(token); token = ""; - } else - token += *i; + } else { + token += i; + } } if (token != "") dest.push_back(token); } - //---------------------------------------------------------------------- inline void splitAddr(const std::string &s_in, std::vector<std::string> &dest, bool skipempty = true) { static const std::string delim = ","; std::string token; bool inquote = false; - for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) { - if (inquote && *i == '\"') + for (const char i : s_in) { + if (inquote && i == '\"') inquote = false; - else if (!inquote && *i == '\"') + else if (!inquote && i == '\"') inquote = true; - if (!inquote && delim.find(*i) != std::string::npos) { + if (!inquote && delim.find(i) != std::string::npos) { if (!skipempty || token != "") dest.push_back(token); token = ""; - } else - token += *i; + } else { + token += i; + } } if (token != "") dest.push_back(token); } - //---------------------------------------------------------------------- inline std::string toCanonMailbox(const std::string &s_in) { if (s_in.find("..") != std::string::npos) return ""; @@ -224,27 +213,27 @@ namespace Binc { return s_in; } - //------------------------------------------------------------------------ inline std::string toRegex(const std::string &s_in, char delimiter) { std::string regex = "^"; - for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) { - if (*i == '.' || *i == '[' || *i == ']' || *i == '{' || *i == '}' || *i == '(' || *i == ')' - || *i == '^' || *i == '$' || *i == '?' || *i == '+' || *i == '\\') + for (const char i : s_in) { + if (i == '.' || i == '[' || i == ']' || i == '{' || i == '}' || i == '(' || i == ')' || i == '^' + || i == '$' || i == '?' || i == '+' || i == '\\') { regex += "\\"; - regex += *i; - } else if (*i == '*') + regex += i; + } else if (i == '*') { regex += ".*?"; - else if (*i == '%') { + } else if (i == '%') { regex += "(\\"; regex += delimiter; regex += "){0,1}"; regex += "[^\\"; regex += delimiter; regex += "]*?"; - } else - regex += *i; + } else { + regex += i; + } } if (regex[regex.length() - 1] == '?') @@ -255,13 +244,11 @@ namespace Binc { return regex; } - //------------------------------------------------------------------------ class BincStream { private: std::string nstr; public: - //-- BincStream &operator<<(std::ostream &(*)(std::ostream &)); BincStream &operator<<(const std::string &t); BincStream &operator<<(unsigned long t); @@ -269,24 +256,18 @@ namespace Binc { BincStream &operator<<(int t); BincStream &operator<<(char t); - //-- std::string popString(unsigned int size); - //-- char popChar(void); void unpopChar(char c); void unpopStr(const std::string &s); - //-- const std::string &str(void) const; - //-- unsigned int getSize(void) const; - //-- void clear(void); - //-- BincStream(void); ~BincStream(void); }; |