diff options
Diffstat (limited to 'src/include/convert.h')
-rw-r--r-- | src/include/convert.h | 68 |
1 files changed, 18 insertions, 50 deletions
diff --git a/src/include/convert.h b/src/include/convert.h index 030c1b5..cc5f053 100644 --- a/src/include/convert.h +++ b/src/include/convert.h @@ -1,8 +1,8 @@ /** - * @file convert.h - * @brief Declaration of miscellaneous convertion functions. - * @author Andreas Aardal Hanssen - * @date 2002-2005 + * @file convert.h + * @brief Declaration of miscellaneous convertion functions. + * @author Andreas Aardal Hanssen + * @date 2002-2005 */ #ifndef convert_h_included @@ -18,50 +18,19 @@ #include <string_view> #include <vector> -#include <stdio.h> - #include <sys/stat.h> namespace Binc { - inline std::string toString(int i_in) - { - char intbuf[16]; - snprintf(intbuf, sizeof(intbuf), "%d", i_in); - return std::string(intbuf); - } - - inline std::string toString(unsigned int i_in) - { - char intbuf[16]; - snprintf(intbuf, sizeof(intbuf), "%u", i_in); - return std::string(intbuf); - } - - inline std::string toString(unsigned long i_in) - { - char longbuf[40]; - snprintf(longbuf, sizeof(longbuf), "%lu", i_in); - return std::string(longbuf); - } - - inline std::string toString(const char *i_in) + inline std::string toHex(std::string_view s) { - 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_view hexchars = "0123456789abcdef"; std::string tmp; - for (auto i : s) { - unsigned char c = static_cast<unsigned char>(i); - tmp += hexchars[((c & 0xf0) >> 4)]; + tmp.reserve(s.length() * 2); + + for (char i : s) { + auto c = static_cast<unsigned char>(i); + tmp += hexchars[(c & 0xf0) >> 4]; tmp += hexchars[c & 0x0f]; } @@ -70,15 +39,14 @@ namespace Binc { inline std::string fromHex(const std::string &s) { - // const - char hexchars[] = "0123456789abcdef"; + const char hexchars[] = "0123456789abcdef"; std::string tmp; 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; + const char *t; if ((t = strchr(hexchars, c)) == nullptr) return "out of range"; n = (t - hexchars) << 4; @@ -99,7 +67,7 @@ namespace Binc { 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; + return "{" + std::to_string(s_in.length()) + "}\r\n" + s_in; } return "\"" + s_in + "\""; @@ -117,14 +85,14 @@ namespace Binc { i = tolower(i); } - inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n") + inline void chomp(std::string &s_in, std::string_view chars = " \t\r\n") { int n = s_in.length(); while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos) s_in.resize(n-- - 1); } - inline void trim(std::string &s_in, const std::string &chars = " \t\r\n") + inline void trim(std::string &s_in, std::string_view chars = " \t\r\n") { while (s_in != "" && chars.find(s_in[0]) != std::string::npos) s_in = s_in.substr(1); @@ -275,7 +243,7 @@ namespace Binc { BincStream &operator<<(int t); BincStream &operator<<(char t); - std::string popString(unsigned int size); + std::string popString(size_t size); char popChar(); void unpopChar(char c); @@ -283,7 +251,7 @@ namespace Binc { const std::string &str() const; - unsigned int getSize() const; + size_t getSize() const; void clear(); |