working version with requested funcs.

P.S: W.I.P template SFINAE protection for unwanted equations
This commit is contained in:
Metehan Tuncbilek
2024-10-16 17:10:25 +03:00
parent 6d53ff9c76
commit ed07e7117a
2 changed files with 65 additions and 6 deletions

View File

@@ -7,10 +7,12 @@
#pragma once #pragma once
#include <string> #include <string>
#include <string_view>
#include <c4/substr.hpp> #include <c4/substr.hpp>
#include <c4/substr_fwd.hpp> #include <c4/substr_fwd.hpp>
#include <c4/format.hpp> #include <c4/format.hpp>
#include <c4/charconv.hpp> #include <c4/charconv.hpp>
#include <utf8.h>
#include <type_traits> #include <type_traits>
namespace OpenVulkano namespace OpenVulkano
@@ -21,6 +23,7 @@ namespace OpenVulkano
String() = default; String() = default;
String(const char* str) : m_string(str) {} String(const char* str) : m_string(str) {}
String(const std::string& str) : m_string(str) {} String(const std::string& str) : m_string(str) {}
String(const std::string_view& str) : m_string(str) {}
String(const String& other) : m_string(other.m_string) {} String(const String& other) : m_string(other.m_string) {}
String(String&& other) noexcept : m_string(std::move(other.m_string)) {} String(String&& other) noexcept : m_string(std::move(other.m_string)) {}
~String() = default; ~String() = default;
@@ -30,17 +33,31 @@ namespace OpenVulkano
m_string = other; m_string = other;
return *this; return *this;
} }
template<typename T> String& operator=(T&& other) template<typename T> String& operator=(T&& other)
{ {
m_string = std::move(other); m_string = std::move(other);
return *this; return *this;
} }
template<typename T> String& operator=(const String& other)
{
m_string = other.m_string;
return *this;
}
template<typename T> String& operator+=(const T& other) template<typename T> String& operator+=(const T& other)
{ {
m_string += other; m_string += other;
return *this; return *this;
} }
template<typename T> String& operator+=(const String& other)
{
m_string += other.m_string;
return *this;
}
template<typename T> String operator+(const T& other) const { return m_string + other; } template<typename T> String operator+(const T& other) const { return m_string + other; }
String operator+(const String& other) const { return m_string + other.m_string; } String operator+(const String& other) const { return m_string + other.m_string; }
@@ -50,19 +67,28 @@ namespace OpenVulkano
operator std::string() const { return m_string; } operator std::string() const { return m_string; }
explicit operator std::string_view() const { return m_string; } explicit operator std::string_view() const { return m_string; }
const char& operator[](size_t index) const noexcept { return m_string[index]; }
const char& At(size_t index) const { return m_string.at(index); }
const char* CharString() const { return m_string.c_str(); } const char* CharString() const { return m_string.c_str(); }
const char* Data() { return m_string.data(); } const char* Data() { return m_string.data(); }
size_t Length() const { return m_string.length(); } size_t Length() const { return m_string.length(); }
size_t Size() const { return m_string.size(); } size_t Size() const { return m_string.size(); }
bool Empty() const { return m_string.empty(); }
size_t Capacity() const { return m_string.capacity(); }
size_t CharCount() const { return utf8::distance(m_string.begin(), m_string.end()); }
char& Front() { return m_string.front(); } char& Front() { return m_string.front(); }
char& Back() { return m_string.back(); } char& Back() { return m_string.back(); }
void PopBack() { m_string.pop_back(); } void PopBack() { m_string.pop_back(); }
void Clear() { m_string.clear(); } void Clear() { m_string.clear(); }
void Shrink() { m_string.shrink_to_fit(); } void Shrink() { m_string.shrink_to_fit(); }
String Substring(size_t start, size_t end) const { return m_string.substr(start, end); } String SubString(size_t start, size_t end) const { return m_string.substr(start, end); }
bool StartsWith(const String& str) const { return m_string.find(str) == 0; }
bool EndsWith(const String& str) const { return m_string.rfind(str) == m_string.size() - str.Size(); }
bool Contains(const String& str) const { return m_string.find(str) != std::string::npos; }
size_t FindStartIndexOf(const String& str) const { return m_string.find(str); } size_t FindStartIndexOf(const String& str) const { return m_string.find(str); }
size_t FindEndIndexOf(const String& str) const { return m_string.rfind(str); }
String& Trim() noexcept String& Trim() noexcept
{ {
@@ -72,6 +98,20 @@ namespace OpenVulkano
return *this; return *this;
} }
String& TrimFront()
{
size_t start = m_string.find_first_not_of(" \t\n");
m_string = m_string.substr(start);
return *this;
}
String& TrimBack()
{
size_t end = m_string.find_last_not_of(" \t\n");
m_string = m_string.substr(0, end + 1);
return *this;
}
void ToUpper() noexcept void ToUpper() noexcept
{ {
for (char& c: m_string) for (char& c: m_string)
@@ -95,7 +135,7 @@ namespace OpenVulkano
} }
// Makes "i am a string" to "I Am A String" also need a better name for the function // Makes "i am a string" to "I Am A String" also need a better name for the function
void ToUpperStarters() void FirstLettersUpper()
{ {
for (size_t i = 0; i < m_string.size(); ++i) for (size_t i = 0; i < m_string.size(); ++i)
{ {
@@ -114,9 +154,19 @@ namespace OpenVulkano
return value; return value;
} }
// iterators
std::string::iterator begin() { return m_string.begin(); }
std::string::iterator end() { return m_string.end(); }
std::string::const_iterator cbegin() const { return m_string.cbegin(); }
std::string::const_iterator cend() const { return m_string.cend(); }
std::string::reverse_iterator rbegin() { return m_string.rbegin(); }
std::string::reverse_iterator rend() { return m_string.rend(); }
std::reverse_iterator<std::string::const_iterator> crbegin() const { return m_string.crbegin(); }
std::reverse_iterator<std::string::const_iterator> crend() const { return m_string.crend(); }
private: private:
std::string m_string; std::string m_string;
}; };
template<typename T> String operator+(const T& lhs, const String& rhs) noexcept { return lhs + rhs; } template<typename T> String operator+(const T& lhs, const String& rhs) noexcept { return String(lhs) + rhs; }
} }

View File

@@ -74,6 +74,15 @@ TEST_CASE("String")
String str8("Hello"); String str8("Hello");
String str9 = str8 + String(" World"); String str9 = str8 + String(" World");
REQUIRE(str9 == "Hello World"); REQUIRE(str9 == "Hello World");
String str10 = String("Hello") + " World";
REQUIRE(str10 == "Hello World");
String str11 = String("Hello") + std::string(" World");
REQUIRE(str11 == "Hello World");
String str12 = std::string("Hello") + String(" World");
REQUIRE(str12 == "Hello World");
} }
SECTION("Comparison") SECTION("Comparison")
@@ -110,15 +119,15 @@ TEST_CASE("String")
REQUIRE(str2 == "hello world"); REQUIRE(str2 == "hello world");
String str3("Georg will save us all from our doom"); String str3("Georg will save us all from our doom");
str3.ToUpperStarters(); str3.FirstLettersUpper();
REQUIRE(str3 == "Georg Will Save Us All From Our Doom"); REQUIRE(str3 == "Georg Will Save Us All From Our Doom");
} }
SECTION("Substring") SECTION("Substring")
{ {
String str1("Hello World"); String str1("Hello World");
REQUIRE(str1.Substring(0, 5) == "Hello"); REQUIRE(str1.SubString(0, 5) == "Hello");
REQUIRE(str1.Substring(6, 11) == "World"); REQUIRE(str1.SubString(6, 11) == "World");
} }
SECTION("FromString") SECTION("FromString")