diff --git a/include/blah/containers/str.h b/include/blah/containers/str.h index 48d02e8..02a88f5 100644 --- a/include/blah/containers/str.h +++ b/include/blah/containers/str.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace Blah { @@ -36,6 +37,9 @@ namespace Blah // append cstr Str& operator+=(const char* rhs) { return append(rhs); } + + // append char + Str& operator+=(const char& rhs) { return append(rhs); } // combine string Str operator+(const Str& rhs) { Str str; str.append(*this).append(rhs); return str; } @@ -43,6 +47,9 @@ namespace Blah // combine cstr Str operator+(const char* rhs) { Str str; str.append(*this).append(rhs); return str; } + // combine char + Str operator+(const char& rhs) { Str str; str.append(*this).append(rhs); return str; } + // implicit cast to cstr operator char* () { return cstr(); } @@ -186,10 +193,10 @@ namespace Blah private: static char empty_buffer[1]; - char* m_buffer; - int m_length; - int m_capacity; - int m_local_size; + char* m_buffer; + int m_length; + int m_capacity; + int m_local_size; }; // combine string @@ -209,8 +216,8 @@ namespace Blah StrOf(const StrOf& rhs) : Str(T) { m_local_buffer[0] = '\0'; set(rhs); } // assignment operators - StrOf& operator=(const char* rhs) { set(rhs); return *this; } - StrOf& operator=(const Str& rhs) { set(rhs); return *this; } + StrOf& operator=(const char* rhs) { set(rhs); return *this; } + StrOf& operator=(const Str& rhs) { set(rhs); return *this; } StrOf& operator=(const StrOf& rhs) { set(rhs); return *this; } // creates a string from the format @@ -243,4 +250,41 @@ namespace Blah return str; } +} + +namespace std +{ + template <> + struct hash + { + std::size_t operator()(const Blah::Str& key) const + { + std::size_t result = 2166136261U; + + for (auto& it : key) + { + result ^= static_cast(it); + result *= 16777619U; + } + + return result; + } + }; + + template + struct hash> + { + std::size_t operator()(const Blah::StrOf& key) const + { + std::size_t result = 2166136261U; + + for (auto& it : key) + { + result ^= static_cast(it); + result *= 16777619U; + } + + return result; + } + }; } \ No newline at end of file diff --git a/src/containers/str.cpp b/src/containers/str.cpp index 13898fe..3d4399c 100644 --- a/src/containers/str.cpp +++ b/src/containers/str.cpp @@ -244,15 +244,19 @@ Str& Str::append_utf16(const u16* start, const u16* end, bool swap_endian) Str& Str::trim() { - const char* s = begin(); - const char* e = end() - 1; + if (m_length > 0) + { + const char* s = begin(); + const char* e = end() - 1; - while (isspace(*s) && s != e) - s++; - while (isspace(*e) && s != e) - e--; + while (isspace(*s) && s != e) + s++; + while (isspace(*e) && s != e) + e--; + + set(s, e + 1); + } - set(s, e + 1); return *this; }