Merge #16566: util: refactor upper/lowercase functions

0481fa2584 util: refactor upper/lowercase functions (Karl-Johan Alm)

Pull request description:

  This includes renaming Downcase() to ToLower() and make it return a string rather than modify referenced arg.
  Also adds ToUpper() string version.

  Additionally, it clarifies that the locale independency of the case functions is a *feature* and not a limitation. I interpreted it as the latter and rewrote code to be locale-aware before realizing this.

  This is done in preparation for #11413 and as a general refactor. I don't think the optimization that the pre-refactor state gave warrants the unwieldy usage.

ACKs for top commit:
  laanwj:
    ACK 0481fa2584 from me
  practicalswift:
    ACK 0481fa2584 -- diff looks correct
  l2a5b1:
    ACK 0481fa2 - Although, I think @luke-jr's [feedback](https://github.com/bitcoin/bitcoin/pull/16566#issuecomment-519580760) is spot on; `Downcase` is just an artifact of `ParseNetwork`, which has been happily downcasing `net` via a string argument for over 7 years; and I do recommend to add `ToLower` *when* somebody actually needs it in new work, there is no point in keeping a trivial utility function if it is not appreciated.
  promag:
    ACK 0481fa2584.

Tree-SHA512: 9b834ecc1b97db043e261bcbc59e42372e11e2fb9a6943688f18a835bf5c9205f68e4614f58e90ba260d1b8f0e060c6f67b390b62436c21b56891db23bc41628
This commit is contained in:
fanquake 2019-08-13 12:07:18 +08:00
commit b799ebcc17
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
6 changed files with 48 additions and 24 deletions

View file

@ -37,8 +37,8 @@ bool fNameLookup = DEFAULT_NAME_LOOKUP;
static const int SOCKS5_RECV_TIMEOUT = 20 * 1000;
static std::atomic<bool> interruptSocks5Recv(false);
enum Network ParseNetwork(std::string net) {
Downcase(net);
enum Network ParseNetwork(const std::string& net_in) {
std::string net = ToLower(net_in);
if (net == "ipv4") return NET_IPV4;
if (net == "ipv6") return NET_IPV6;
if (net == "onion") return NET_ONION;

View file

@ -37,7 +37,7 @@ public:
bool randomize_credentials;
};
enum Network ParseNetwork(std::string net);
enum Network ParseNetwork(const std::string& net);
std::string GetNetworkName(enum Network net);
bool SetProxy(enum Network net, const proxyType &addrProxy);
bool GetProxy(enum Network net, proxyType &proxyInfoOut);

View file

@ -1532,17 +1532,9 @@ BOOST_AUTO_TEST_CASE(test_ToLower)
BOOST_CHECK_EQUAL(ToLower(0), 0);
BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
std::string testVector;
Downcase(testVector);
BOOST_CHECK_EQUAL(testVector, "");
testVector = "#HODL";
Downcase(testVector);
BOOST_CHECK_EQUAL(testVector, "#hodl");
testVector = "\x00\xfe\xff";
Downcase(testVector);
BOOST_CHECK_EQUAL(testVector, "\x00\xfe\xff");
BOOST_CHECK_EQUAL(ToLower(""), "");
BOOST_CHECK_EQUAL(ToLower("#HODL"), "#hodl");
BOOST_CHECK_EQUAL(ToLower("\x00\xfe\xff"), "\x00\xfe\xff");
}
BOOST_AUTO_TEST_CASE(test_ToUpper)
@ -1553,6 +1545,10 @@ BOOST_AUTO_TEST_CASE(test_ToUpper)
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
BOOST_CHECK_EQUAL(ToUpper(0), 0);
BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
BOOST_CHECK_EQUAL(ToUpper(""), "");
BOOST_CHECK_EQUAL(ToUpper("#hodl"), "#HODL");
BOOST_CHECK_EQUAL(ToUpper("\x00\xfe\xff"), "\x00\xfe\xff");
}
BOOST_AUTO_TEST_CASE(test_Capitalize)

View file

@ -546,9 +546,18 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
return true;
}
void Downcase(std::string& str)
std::string ToLower(const std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), [](char c){return ToLower(c);});
std::string r;
for (auto ch : str) r += ToLower((unsigned char)ch);
return r;
}
std::string ToUpper(const std::string& str)
{
std::string r;
for (auto ch : str) r += ToUpper((unsigned char)ch);
return r;
}
std::string Capitalize(std::string str)

View file

@ -199,6 +199,8 @@ bool ConvertBits(const O& outfn, I it, I end) {
* Converts the given character to its lowercase equivalent.
* This function is locale independent. It only converts uppercase
* characters in the standard 7-bit ASCII range.
* This is a feature, not a limitation.
*
* @param[in] c the character to convert to lowercase.
* @return the lowercase equivalent of c; or the argument
* if no conversion is possible.
@ -209,17 +211,22 @@ constexpr char ToLower(char c)
}
/**
* Converts the given string to its lowercase equivalent.
* Returns the lowercase equivalent of the given string.
* This function is locale independent. It only converts uppercase
* characters in the standard 7-bit ASCII range.
* @param[in,out] str the string to convert to lowercase.
* This is a feature, not a limitation.
*
* @param[in] str the string to convert to lowercase.
* @returns lowercased equivalent of str
*/
void Downcase(std::string& str);
std::string ToLower(const std::string& str);
/**
* Converts the given character to its uppercase equivalent.
* This function is locale independent. It only converts lowercase
* characters in the standard 7-bit ASCII range.
* This is a feature, not a limitation.
*
* @param[in] c the character to convert to uppercase.
* @return the uppercase equivalent of c; or the argument
* if no conversion is possible.
@ -229,13 +236,25 @@ constexpr char ToUpper(char c)
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
}
/**
* Returns the uppercase equivalent of the given string.
* This function is locale independent. It only converts lowercase
* characters in the standard 7-bit ASCII range.
* This is a feature, not a limitation.
*
* @param[in] str the string to convert to uppercase.
* @returns UPPERCASED EQUIVALENT OF str
*/
std::string ToUpper(const std::string& str);
/**
* Capitalizes the first character of the given string.
* This function is locale independent. It only capitalizes the
* first character of the argument if it has an uppercase equivalent
* in the standard 7-bit ASCII range.
* This function is locale independent. It only converts lowercase
* characters in the standard 7-bit ASCII range.
* This is a feature, not a limitation.
*
* @param[in] str the string to capitalize.
* @return string with the first letter capitalized.
* @returns string with the first letter capitalized.
*/
std::string Capitalize(std::string str);

View file

@ -388,7 +388,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
key.erase(is_index);
}
#ifdef WIN32
std::transform(key.begin(), key.end(), key.begin(), ToLower);
key = ToLower(key);
if (key[0] == '/')
key[0] = '-';
#endif