fix: enumparser capitalize everything to avoid issues

This commit is contained in:
Austin Horstman 2023-09-09 12:14:52 -05:00
parent 3ae2fe3272
commit 2fee12d930
No known key found for this signature in database
1 changed files with 17 additions and 3 deletions

View File

@ -12,12 +12,26 @@ template <typename EnumType>
struct EnumParser { struct EnumParser {
EnumParser() {} EnumParser() {}
// Helper function to capitalize a string
std::string capitalizeString(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c) { return std::toupper(c); });
return result;
}
EnumType parseStringToEnum(const std::string& str, EnumType parseStringToEnum(const std::string& str,
const std::map<std::string, EnumType>& enumMap) { const std::map<std::string, EnumType>& enumMap) {
// Convert the input string to uppercase // Convert the input string to uppercase
std::string uppercaseStr = str; std::string uppercaseStr = capitalizeString(str);
std::transform(uppercaseStr.begin(), uppercaseStr.end(), uppercaseStr.begin(),
[](unsigned char c) { return std::toupper(c); }); // Capitalize the map keys before searching
std::map<std::string, EnumType> capitalizedEnumMap;
std::transform(enumMap.begin(), enumMap.end(),
std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
[this](const auto& pair) {
return std::make_pair(capitalizeString(pair.first), pair.second);
});
// Return enum match of string // Return enum match of string
auto it = enumMap.find(uppercaseStr); auto it = enumMap.find(uppercaseStr);