From 7e27c6cf7d29913a094a23648163aa0dc3d1304e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 1 Oct 2020 23:31:06 +0900 Subject: [PATCH] Further simplify and centralize hex conversion --- src/string_util.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/string_util.cpp b/src/string_util.cpp index 9a3cf90..9286ed4 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -58,7 +58,6 @@ template std::string str(unsigned long long value); //------------------------------------------------------------------- // Functions //------------------------------------------------------------------- -static const char hexAlphabet[] = "0123456789ABCDEF"; // replacement for C++11 std::stoll off_t s3fs_strtoofft(const char* str, int base) @@ -151,7 +150,7 @@ std::string urlEncode(const std::string &s) { std::string result; for (size_t i = 0; i < s.length(); ++i) { - char c = s[i]; + unsigned char c = s[i]; if (c == '/' // Note- special case for fuse paths... || c == '.' || c == '-' @@ -164,8 +163,7 @@ std::string urlEncode(const std::string &s) result += c; }else{ result += "%"; - result += hexAlphabet[static_cast(c) / 16]; - result += hexAlphabet[static_cast(c) % 16]; + result += s3fs_hex(&c, 1); } } return result; @@ -180,7 +178,7 @@ std::string urlEncode2(const std::string &s) { std::string result; for (size_t i = 0; i < s.length(); ++i) { - char c = s[i]; + unsigned char c = s[i]; if (c == '=' // Note- special case for fuse paths... || c == '&' // Note- special case for s3... || c == '%' @@ -195,8 +193,7 @@ std::string urlEncode2(const std::string &s) result += c; }else{ result += "%"; - result += hexAlphabet[static_cast(c) / 16]; - result += hexAlphabet[static_cast(c) % 16]; + result += s3fs_hex(&c, 1); } } return result; @@ -378,11 +375,11 @@ bool convert_unixtime_from_option_arg(const char* argv, time_t& unixtime) std::string s3fs_hex(const unsigned char* input, size_t length) { + static const char hexAlphabet[] = "0123456789abcdef"; std::string hex; for(size_t pos = 0; pos < length; ++pos){ - char hexbuf[3]; - snprintf(hexbuf, 3, "%02x", input[pos]); - hex += hexbuf; + hex += hexAlphabet[input[pos] / 16]; + hex += hexAlphabet[input[pos] % 16]; } return hex; }