more features on String
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <c4/substr.hpp>
|
||||
#include <c4/substr_fwd.hpp>
|
||||
#include <c4/format.hpp>
|
||||
@@ -93,21 +94,10 @@ namespace OpenVulkano
|
||||
size_t FindStartIndexOf(const std::string_view& str) const { return m_string.find(str); }
|
||||
size_t FindEndIndexOf(const std::string_view& str) const { return m_string.rfind(str); }
|
||||
|
||||
String& Trim() noexcept
|
||||
String Trim() noexcept
|
||||
{
|
||||
size_t start = m_string.find_first_not_of(" \t\n\r");
|
||||
if (start == std::string::npos)
|
||||
{
|
||||
m_string.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t end = m_string.find_last_not_of(" \t\n\r");
|
||||
|
||||
m_string.erase(0, start);
|
||||
m_string.resize(end - start + 1);
|
||||
|
||||
return *this;
|
||||
String trimmed = TrimBack();
|
||||
return trimmed.TrimFront();
|
||||
}
|
||||
|
||||
String& TrimFront() noexcept
|
||||
@@ -119,7 +109,7 @@ namespace OpenVulkano
|
||||
}
|
||||
else
|
||||
{
|
||||
m_string.erase(0, start);
|
||||
m_string.erase(0, start);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -138,6 +128,143 @@ namespace OpenVulkano
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<String> Split(const std::string_view& delimiter) const
|
||||
{
|
||||
std::vector<String> result;
|
||||
size_t start = 0;
|
||||
size_t end = m_string.find(delimiter);
|
||||
while (end != std::string::npos)
|
||||
{
|
||||
result.push_back(m_string.substr(start, end - start));
|
||||
start = end + delimiter.size();
|
||||
end = m_string.find(delimiter, start);
|
||||
}
|
||||
result.push_back(m_string.substr(start, end));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<String, String> SplitAtLastOccurrence(const std::string_view& delimiter) const
|
||||
{
|
||||
size_t end = m_string.rfind(delimiter);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
return { m_string, "" };
|
||||
}
|
||||
return { m_string.substr(0, end), m_string.substr(end + delimiter.size()) };
|
||||
}
|
||||
|
||||
std::pair<String, String> SplitAtFirstOccurrence(const std::string_view& delimiter) const
|
||||
{
|
||||
size_t end = m_string.find(delimiter);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
return { m_string, "" };
|
||||
}
|
||||
return { m_string.substr(0, end), m_string.substr(end + delimiter.size()) };
|
||||
}
|
||||
|
||||
std::vector<std::string_view> SplitAsStringViews(const std::string_view& delimiter) const
|
||||
{
|
||||
std::vector<std::string_view> result;
|
||||
size_t start = 0;
|
||||
size_t end = m_string.find(delimiter);
|
||||
while (end != std::string::npos)
|
||||
{
|
||||
result.push_back(std::string_view(m_string.c_str() + start, end - start));
|
||||
start = end + delimiter.size();
|
||||
end = m_string.find(delimiter, start);
|
||||
}
|
||||
result.push_back(
|
||||
std::string_view(m_string.c_str() + start, m_string.size() - (start + end + delimiter.size())));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<std::string_view, std::string_view>
|
||||
SplitAtLastOccurenceAsStringViews(const std::string_view& delimiter) const
|
||||
{
|
||||
size_t end = m_string.rfind(delimiter);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
return { m_string, "" };
|
||||
}
|
||||
return { std::string_view(m_string.c_str(), end),
|
||||
std::string_view(m_string.c_str() + end + delimiter.size(),
|
||||
m_string.size() - end - delimiter.size()) };
|
||||
}
|
||||
|
||||
std::pair<std::string_view, std::string_view>
|
||||
SplitAtFirstOccurenceAsStringViews(const std::string_view& delimiter) const
|
||||
{
|
||||
size_t end = m_string.find(delimiter);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
return { m_string, "" };
|
||||
}
|
||||
return { std::string_view(m_string.c_str(), end),
|
||||
std::string_view(m_string.c_str() + end + delimiter.size(),
|
||||
m_string.size() - end - delimiter.size()) };
|
||||
}
|
||||
|
||||
static inline constexpr int64_t OctToInt(std::string_view string)
|
||||
{
|
||||
int64_t result = 0;
|
||||
for (int i = 0; i < static_cast<int>(string.length()); i++)
|
||||
{
|
||||
char c = string[i];
|
||||
if (c == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c == ' ')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (c < '0' || c > '7')
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
result = result * 8 + c - '0';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr int64_t OctToInt() { return OctToInt(m_string); }
|
||||
|
||||
static inline constexpr int64_t HexToInt(std::string string)
|
||||
{
|
||||
int64_t result = 0;
|
||||
for (char c: string)
|
||||
{
|
||||
if (c == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c == ' ')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
result = result * 16 + c - '0';
|
||||
}
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
result = result * 16 + c - 'A' + 10;
|
||||
}
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
result = result * 16 + c - 'a' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr int64_t HexToInt() { return HexToInt(m_string); }
|
||||
|
||||
void ToUpper() noexcept
|
||||
{
|
||||
std::transform(m_string.begin(), m_string.end(), m_string.begin(),
|
||||
|
||||
@@ -215,3 +215,67 @@ TEST_CASE("Clear")
|
||||
str1.Clear();
|
||||
REQUIRE(str1.Empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Split")
|
||||
{
|
||||
String str1("Hello,World");
|
||||
auto split = str1.Split(",");
|
||||
REQUIRE(split.size() == 2);
|
||||
REQUIRE(split[0] == "Hello");
|
||||
REQUIRE(split[1] == "World");
|
||||
}
|
||||
|
||||
TEST_CASE("SplitAtLastOccurrence")
|
||||
{
|
||||
String str1 = "Hello,World,Georg";
|
||||
auto split = str1.SplitAtLastOccurrence(",");
|
||||
REQUIRE(split.first == "Hello,World");
|
||||
REQUIRE(split.second == "Georg");
|
||||
}
|
||||
|
||||
TEST_CASE("SplitAtFirstOccurrence")
|
||||
{
|
||||
String str1 = "Hello,World,Georg";
|
||||
auto split = str1.SplitAtFirstOccurrence(",");
|
||||
REQUIRE(split.first == "Hello");
|
||||
REQUIRE(split.second == "World,Georg");
|
||||
}
|
||||
|
||||
TEST_CASE("SplitAsStringViews")
|
||||
{
|
||||
String str1("Hello,World");
|
||||
auto split = str1.SplitAsStringViews(",");
|
||||
REQUIRE(split.size() == 2);
|
||||
REQUIRE(split[0] == "Hello");
|
||||
REQUIRE(split[1] == "World");
|
||||
}
|
||||
|
||||
TEST_CASE("SplitAtFirstOccurrenceAsStringViews")
|
||||
{
|
||||
String str1 = "Hello,World,Georg";
|
||||
auto split = str1.SplitAtFirstOccurenceAsStringViews(",");
|
||||
REQUIRE(split.first == "Hello");
|
||||
REQUIRE(split.second == "World,Georg");
|
||||
}
|
||||
|
||||
TEST_CASE("SplitAtLastOccurenceAsStringViews")
|
||||
{
|
||||
String str1 = "Hello,World,Georg";
|
||||
auto split = str1.SplitAtLastOccurenceAsStringViews(",");
|
||||
REQUIRE(split.first == "Hello,World");
|
||||
REQUIRE(split.second == "Georg");
|
||||
}
|
||||
|
||||
TEST_CASE("OctToInt")
|
||||
{
|
||||
String str1("47");
|
||||
REQUIRE(str1.OctToInt() == 39);
|
||||
REQUIRE(String::OctToInt("552") == 362);
|
||||
}
|
||||
|
||||
TEST_CASE("HexToInt")
|
||||
{
|
||||
String str1("2A");
|
||||
REQUIRE(str1.HexToInt() == 42);
|
||||
REQUIRE(String::HexToInt("FF") == 255);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user