Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/utility/src/string_utils.cpp

55 lines
1.7 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#include "string_utils.hpp"
std::string remove_substring(const std::string_view string, const std::string_view substring) {
std::string result(string);
const auto substring_length = substring.length();
for(auto i = result.find(substring); i != std::string::npos; i = result.find(substring))
result.erase(i, substring_length);
return result;
}
std::string replace_substring(const std::string_view string, const std::string_view substring, const std::string_view replacement) {
std::string result(string);
const auto substring_length = substring.length();
size_t i = 0;
while((i = result.find(substring, i)) != std::string::npos) {
result.erase(i, substring_length);
result.insert(i, replacement);
i += replacement.length();
}
return result;
}
bool string_contains(const std::string_view a, const std::string_view b) {
return a.find(b) != std::string::npos;
}
bool string_starts_with(const std::string_view haystack, const std::string_view needle) {
return needle.length() <= haystack.length()
&& std::equal(needle.begin(), needle.end(), haystack.begin());
}
std::vector<std::string> tokenize(const std::string_view string, const std::string_view& delimiters) {
std::vector<std::string> tokens;
const size_t length = string.length();
size_t lastPos = 0;
while(lastPos < length + 1) {
size_t pos = string.find_first_of(delimiters, lastPos);
if(pos == std::string_view::npos)
pos = length;
if(pos != lastPos)
tokens.emplace_back(string.data() + lastPos, pos - lastPos);
lastPos = pos + 1;
}
return tokens;
}