Skip to content

Commit 0481fa2

Browse files
committed
util: refactor upper/lowercase functions
This includes renaming Downcase() to ToLower() and make it return a string rather than modify referenced arg. Also adds ToUpper() string version.
1 parent e5fdda6 commit 0481fa2

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

src/netbase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ bool fNameLookup = DEFAULT_NAME_LOOKUP;
3737
static const int SOCKS5_RECV_TIMEOUT = 20 * 1000;
3838
static std::atomic<bool> interruptSocks5Recv(false);
3939

40-
enum Network ParseNetwork(std::string net) {
41-
Downcase(net);
40+
enum Network ParseNetwork(const std::string& net_in) {
41+
std::string net = ToLower(net_in);
4242
if (net == "ipv4") return NET_IPV4;
4343
if (net == "ipv6") return NET_IPV6;
4444
if (net == "onion") return NET_ONION;

src/netbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class proxyType
3737
bool randomize_credentials;
3838
};
3939

40-
enum Network ParseNetwork(std::string net);
40+
enum Network ParseNetwork(const std::string& net);
4141
std::string GetNetworkName(enum Network net);
4242
bool SetProxy(enum Network net, const proxyType &addrProxy);
4343
bool GetProxy(enum Network net, proxyType &proxyInfoOut);

src/test/util_tests.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,17 +1532,9 @@ BOOST_AUTO_TEST_CASE(test_ToLower)
15321532
BOOST_CHECK_EQUAL(ToLower(0), 0);
15331533
BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
15341534

1535-
std::string testVector;
1536-
Downcase(testVector);
1537-
BOOST_CHECK_EQUAL(testVector, "");
1538-
1539-
testVector = "#HODL";
1540-
Downcase(testVector);
1541-
BOOST_CHECK_EQUAL(testVector, "#hodl");
1542-
1543-
testVector = "\x00\xfe\xff";
1544-
Downcase(testVector);
1545-
BOOST_CHECK_EQUAL(testVector, "\x00\xfe\xff");
1535+
BOOST_CHECK_EQUAL(ToLower(""), "");
1536+
BOOST_CHECK_EQUAL(ToLower("#HODL"), "#hodl");
1537+
BOOST_CHECK_EQUAL(ToLower("\x00\xfe\xff"), "\x00\xfe\xff");
15461538
}
15471539

15481540
BOOST_AUTO_TEST_CASE(test_ToUpper)
@@ -1553,6 +1545,10 @@ BOOST_AUTO_TEST_CASE(test_ToUpper)
15531545
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
15541546
BOOST_CHECK_EQUAL(ToUpper(0), 0);
15551547
BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
1548+
1549+
BOOST_CHECK_EQUAL(ToUpper(""), "");
1550+
BOOST_CHECK_EQUAL(ToUpper("#hodl"), "#HODL");
1551+
BOOST_CHECK_EQUAL(ToUpper("\x00\xfe\xff"), "\x00\xfe\xff");
15561552
}
15571553

15581554
BOOST_AUTO_TEST_CASE(test_Capitalize)

src/util/strencodings.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,18 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
546546
return true;
547547
}
548548

549-
void Downcase(std::string& str)
549+
std::string ToLower(const std::string& str)
550550
{
551-
std::transform(str.begin(), str.end(), str.begin(), [](char c){return ToLower(c);});
551+
std::string r;
552+
for (auto ch : str) r += ToLower((unsigned char)ch);
553+
return r;
554+
}
555+
556+
std::string ToUpper(const std::string& str)
557+
{
558+
std::string r;
559+
for (auto ch : str) r += ToUpper((unsigned char)ch);
560+
return r;
552561
}
553562

554563
std::string Capitalize(std::string str)

src/util/strencodings.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ bool ConvertBits(const O& outfn, I it, I end) {
199199
* Converts the given character to its lowercase equivalent.
200200
* This function is locale independent. It only converts uppercase
201201
* characters in the standard 7-bit ASCII range.
202+
* This is a feature, not a limitation.
203+
*
202204
* @param[in] c the character to convert to lowercase.
203205
* @return the lowercase equivalent of c; or the argument
204206
* if no conversion is possible.
@@ -209,17 +211,22 @@ constexpr char ToLower(char c)
209211
}
210212

211213
/**
212-
* Converts the given string to its lowercase equivalent.
214+
* Returns the lowercase equivalent of the given string.
213215
* This function is locale independent. It only converts uppercase
214216
* characters in the standard 7-bit ASCII range.
215-
* @param[in,out] str the string to convert to lowercase.
217+
* This is a feature, not a limitation.
218+
*
219+
* @param[in] str the string to convert to lowercase.
220+
* @returns lowercased equivalent of str
216221
*/
217-
void Downcase(std::string& str);
222+
std::string ToLower(const std::string& str);
218223

219224
/**
220225
* Converts the given character to its uppercase equivalent.
221226
* This function is locale independent. It only converts lowercase
222227
* characters in the standard 7-bit ASCII range.
228+
* This is a feature, not a limitation.
229+
*
223230
* @param[in] c the character to convert to uppercase.
224231
* @return the uppercase equivalent of c; or the argument
225232
* if no conversion is possible.
@@ -229,13 +236,25 @@ constexpr char ToUpper(char c)
229236
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
230237
}
231238

239+
/**
240+
* Returns the uppercase equivalent of the given string.
241+
* This function is locale independent. It only converts lowercase
242+
* characters in the standard 7-bit ASCII range.
243+
* This is a feature, not a limitation.
244+
*
245+
* @param[in] str the string to convert to uppercase.
246+
* @returns UPPERCASED EQUIVALENT OF str
247+
*/
248+
std::string ToUpper(const std::string& str);
249+
232250
/**
233251
* Capitalizes the first character of the given string.
234-
* This function is locale independent. It only capitalizes the
235-
* first character of the argument if it has an uppercase equivalent
236-
* in the standard 7-bit ASCII range.
252+
* This function is locale independent. It only converts lowercase
253+
* characters in the standard 7-bit ASCII range.
254+
* This is a feature, not a limitation.
255+
*
237256
* @param[in] str the string to capitalize.
238-
* @return string with the first letter capitalized.
257+
* @returns string with the first letter capitalized.
239258
*/
240259
std::string Capitalize(std::string str);
241260

src/util/system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
388388
key.erase(is_index);
389389
}
390390
#ifdef WIN32
391-
std::transform(key.begin(), key.end(), key.begin(), ToLower);
391+
key = ToLower(key);
392392
if (key[0] == '/')
393393
key[0] = '-';
394394
#endif

0 commit comments

Comments
 (0)