Skip to content

Commit

Permalink
Updated StringUtils
Browse files Browse the repository at this point in the history
added string to T conversion
  • Loading branch information
baderouaich committed Dec 27, 2023
1 parent 7639fb2 commit e965d8e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/tgbotxx/utils/StringUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ namespace tgbotxx {
return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

/// Replaces a token within a string with another string
/// @param str String that contains tokens to replace
/// @param search Token to replace
/// @param replace New value to replace token with
static void replace(std::string& str, const std::string& search, const std::string& replace) {
if (search.empty()) return; // Avoid infinite loops
std::size_t pos{0};
Expand All @@ -142,6 +146,12 @@ namespace tgbotxx {
}
}


/// Replaces a token within a string with another string and return a new copy
/// @param str String that contains tokens to replace
/// @param search Token to replace
/// @param replace New value to replace token with
/// @return new replaced tokens string
static std::string replaceCopy(std::string str, const std::string& search, const std::string& replace) {
if (search.empty()) return str; // Avoid infinite loops
std::size_t pos{0};
Expand All @@ -151,5 +161,18 @@ namespace tgbotxx {
}
return str;
}

/// Convert a string to T type
/// @tparam T desired type
/// @param str string to convert
/// @return converted string to T
template<typename T>
static T to(const std::string& str) {
T v{};
std::istringstream oss{str};
oss >> v;
return v;
}

};
}

0 comments on commit e965d8e

Please sign in to comment.