refactor: move capitalize string helper

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

View File

@ -6,32 +6,24 @@
#include <stdexcept>
#include <string>
#include "util/string.hpp"
namespace waybar::util {
template <typename EnumType>
struct 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,
const std::map<std::string, EnumType>& enumMap) {
// Convert the input string to uppercase
std::string uppercaseStr = capitalizeString(str);
std::string uppercaseStr = capitalize(str);
// 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);
});
std::transform(
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
[this](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
// Return enum match of string
auto it = enumMap.find(uppercaseStr);

View File

@ -1,5 +1,6 @@
#pragma once
#include <iostream>
#include <string>
const std::string WHITESPACE = " \n\r\t\f\v";
@ -15,3 +16,10 @@ inline std::string rtrim(const std::string& s) {
}
inline std::string trim(const std::string& s) { return rtrim(ltrim(s)); }
inline std::string capitalize(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;
}