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.
This commit is contained in:
parent
e2ba043b8d
commit
7a208d9fad
3 changed files with 96 additions and 0 deletions
|
@ -1217,4 +1217,43 @@ BOOST_AUTO_TEST_CASE(test_DirIsWritable)
|
|||
fs::remove(tmpdirname);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ToLower)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(ToLower('@'), '@');
|
||||
BOOST_CHECK_EQUAL(ToLower('A'), 'a');
|
||||
BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
|
||||
BOOST_CHECK_EQUAL(ToLower('['), '[');
|
||||
BOOST_CHECK_EQUAL(ToLower(0), 0);
|
||||
BOOST_CHECK_EQUAL(ToLower(255), 255);
|
||||
|
||||
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_AUTO_TEST_CASE(test_ToUpper)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(ToUpper('`'), '`');
|
||||
BOOST_CHECK_EQUAL(ToUpper('a'), 'A');
|
||||
BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
|
||||
BOOST_CHECK_EQUAL(ToUpper('{'), '{');
|
||||
BOOST_CHECK_EQUAL(ToUpper(0), 0);
|
||||
BOOST_CHECK_EQUAL(ToUpper(255), 255);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_Capitalize)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(Capitalize(""), "");
|
||||
BOOST_CHECK_EQUAL(Capitalize("bitcoin"), "Bitcoin");
|
||||
BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <tinyformat.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <errno.h>
|
||||
|
@ -584,3 +585,15 @@ bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypa
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Downcase(std::string& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){return ToLower(c);});
|
||||
}
|
||||
|
||||
std::string Capitalize(std::string str)
|
||||
{
|
||||
if (str.empty()) return str;
|
||||
str[0] = ToUpper(str.front());
|
||||
return str;
|
||||
}
|
||||
|
|
|
@ -186,4 +186,48 @@ bool ConvertBits(const O& outfn, I it, I end) {
|
|||
/** Parse an HD keypaths like "m/7/0'/2000". */
|
||||
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param[in] c the character to convert to lowercase.
|
||||
* @return the lowercase equivalent of c; or the argument
|
||||
* if no conversion is possible.
|
||||
*/
|
||||
constexpr unsigned char ToLower(unsigned char c)
|
||||
{
|
||||
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given string to its lowercase equivalent.
|
||||
* 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.
|
||||
*/
|
||||
void Downcase(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.
|
||||
* @param[in] c the character to convert to uppercase.
|
||||
* @return the uppercase equivalent of c; or the argument
|
||||
* if no conversion is possible.
|
||||
*/
|
||||
constexpr unsigned char ToUpper(unsigned char c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param[in] str the string to capitalize.
|
||||
* @return string with the first letter capitalized.
|
||||
*/
|
||||
std::string Capitalize(std::string str);
|
||||
|
||||
#endif // BITCOIN_UTILSTRENCODINGS_H
|
||||
|
|
Loading…
Add table
Reference in a new issue