Skip to content

Commit 7a208d9

Browse files
committed
Implements custom tolower and toupper functions.
This commit implements custom equivalents for the C and C++ `tolower` and `toupper` Standard Library functions. In addition it implements a utility function to capitalize the first letter of a string.
1 parent e2ba043 commit 7a208d9

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/test/util_tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,4 +1217,43 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable)
12171217
fs::remove(tmpdirname);
12181218
}
12191219

1220+
BOOST_AUTO_TEST_CASE(test_ToLower)
1221+
{
1222+
BOOST_CHECK_EQUAL(ToLower('@'), '@');
1223+
BOOST_CHECK_EQUAL(ToLower('A'), 'a');
1224+
BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
1225+
BOOST_CHECK_EQUAL(ToLower('['), '[');
1226+
BOOST_CHECK_EQUAL(ToLower(0), 0);
1227+
BOOST_CHECK_EQUAL(ToLower(255), 255);
1228+
1229+
std::string testVector;
1230+
Downcase(testVector);
1231+
BOOST_CHECK_EQUAL(testVector, "");
1232+
1233+
testVector = "#HODL";
1234+
Downcase(testVector);
1235+
BOOST_CHECK_EQUAL(testVector, "#hodl");
1236+
1237+
testVector = "\x00\xfe\xff";
1238+
Downcase(testVector);
1239+
BOOST_CHECK_EQUAL(testVector, "\x00\xfe\xff");
1240+
}
1241+
1242+
BOOST_AUTO_TEST_CASE(test_ToUpper)
1243+
{
1244+
BOOST_CHECK_EQUAL(ToUpper('`'), '`');
1245+
BOOST_CHECK_EQUAL(ToUpper('a'), 'A');
1246+
BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
1247+
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
1248+
BOOST_CHECK_EQUAL(ToUpper(0), 0);
1249+
BOOST_CHECK_EQUAL(ToUpper(255), 255);
1250+
}
1251+
1252+
BOOST_AUTO_TEST_CASE(test_Capitalize)
1253+
{
1254+
BOOST_CHECK_EQUAL(Capitalize(""), "");
1255+
BOOST_CHECK_EQUAL(Capitalize("bitcoin"), "Bitcoin");
1256+
BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
1257+
}
1258+
12201259
BOOST_AUTO_TEST_SUITE_END()

src/utilstrencodings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <tinyformat.h>
99

10+
#include <algorithm>
1011
#include <cstdlib>
1112
#include <cstring>
1213
#include <errno.h>
@@ -584,3 +585,15 @@ bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypa
584585
}
585586
return true;
586587
}
588+
589+
void Downcase(std::string& str)
590+
{
591+
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){return ToLower(c);});
592+
}
593+
594+
std::string Capitalize(std::string str)
595+
{
596+
if (str.empty()) return str;
597+
str[0] = ToUpper(str.front());
598+
return str;
599+
}

src/utilstrencodings.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,48 @@ bool ConvertBits(const O& outfn, I it, I end) {
186186
/** Parse an HD keypaths like "m/7/0'/2000". */
187187
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
188188

189+
/**
190+
* Converts the given character to its lowercase equivalent.
191+
* This function is locale independent. It only converts uppercase
192+
* characters in the standard 7-bit ASCII range.
193+
* @param[in] c the character to convert to lowercase.
194+
* @return the lowercase equivalent of c; or the argument
195+
* if no conversion is possible.
196+
*/
197+
constexpr unsigned char ToLower(unsigned char c)
198+
{
199+
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
200+
}
201+
202+
/**
203+
* Converts the given string to its lowercase equivalent.
204+
* This function is locale independent. It only converts uppercase
205+
* characters in the standard 7-bit ASCII range.
206+
* @param[in,out] str the string to convert to lowercase.
207+
*/
208+
void Downcase(std::string& str);
209+
210+
/**
211+
* Converts the given character to its uppercase equivalent.
212+
* This function is locale independent. It only converts lowercase
213+
* characters in the standard 7-bit ASCII range.
214+
* @param[in] c the character to convert to uppercase.
215+
* @return the uppercase equivalent of c; or the argument
216+
* if no conversion is possible.
217+
*/
218+
constexpr unsigned char ToUpper(unsigned char c)
219+
{
220+
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
221+
}
222+
223+
/**
224+
* Capitalizes the first character of the given string.
225+
* This function is locale independent. It only capitalizes the
226+
* first character of the argument if it has an uppercase equivalent
227+
* in the standard 7-bit ASCII range.
228+
* @param[in] str the string to capitalize.
229+
* @return string with the first letter capitalized.
230+
*/
231+
std::string Capitalize(std::string str);
232+
189233
#endif // BITCOIN_UTILSTRENCODINGS_H

0 commit comments

Comments
 (0)