mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
adding a few utility methods to blah string
This commit is contained in:
parent
1e2a009778
commit
916ddc2020
|
@ -105,33 +105,43 @@ namespace Blah
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
StackString operator+(const BaseString& rhs)
|
|
||||||
{
|
|
||||||
StackString str(*this);
|
|
||||||
str += rhs;
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
StackString operator+(const char* rhs)
|
|
||||||
{
|
|
||||||
StackString str(*this);
|
|
||||||
str += rhs;
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
StackString operator+(const char& rhs)
|
|
||||||
{
|
|
||||||
StackString str(*this);
|
|
||||||
str += rhs;
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
StackString substr(int start, int len = 0) const
|
StackString substr(int start, int len = 0) const
|
||||||
{
|
{
|
||||||
if (len == 0) len = length() - start;
|
if (len == 0) len = length() - start;
|
||||||
return StackString(cstr() + start, cstr() + start + len);
|
return StackString(cstr() + start, cstr() + start + len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StackString replace(const StackString& term, const StackString& replacement) const
|
||||||
|
{
|
||||||
|
StackString result;
|
||||||
|
int term_length = term.length();
|
||||||
|
for (int i = 0, n = length() - term_length; i < n; i ++)
|
||||||
|
{
|
||||||
|
bool match = false;
|
||||||
|
for (int j = 0; j < term_length; j ++)
|
||||||
|
if (operator[](i) != term[j]) { match = true; break; }
|
||||||
|
if (match)
|
||||||
|
{
|
||||||
|
result.append(replacement);
|
||||||
|
i += term_length - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
result.append(operator[](i));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_length(int new_length)
|
||||||
|
{
|
||||||
|
new_length++;
|
||||||
|
if (s_len() < new_length)
|
||||||
|
append('\0', new_length - s_len());
|
||||||
|
else if (m_heap_buffer.size() > 0)
|
||||||
|
m_heap_buffer.erase(new_length, s_len() - new_length);
|
||||||
|
else
|
||||||
|
m_stack_buffer.erase(new_length, s_len() - new_length);
|
||||||
|
}
|
||||||
|
|
||||||
StackString trim() const
|
StackString trim() const
|
||||||
{
|
{
|
||||||
if (length() > 0)
|
if (length() > 0)
|
||||||
|
@ -256,30 +266,37 @@ namespace Blah
|
||||||
StackVector<char, StackSize> m_stack_buffer;
|
StackVector<char, StackSize> m_stack_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t StackSize>
|
template<size_t StackSizeLeft, size_t StackSizeRight>
|
||||||
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const BaseString& rhs)
|
StackString<StackSizeLeft> operator+(const StackString<StackSizeLeft>& lhs, const StackString<StackSizeRight>& rhs)
|
||||||
{
|
{
|
||||||
StackString str(lhs);
|
StackString<StackSizeLeft> str(lhs); str.append(rhs); return str;
|
||||||
str += rhs;
|
}
|
||||||
return str;
|
|
||||||
|
template<size_t StackSize>
|
||||||
|
StackString<StackSize> operator+(const char* lhs, const StackString<StackSize>& rhs)
|
||||||
|
{
|
||||||
|
StackString<StackSize> str(lhs); str.append(rhs); return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t StackSize>
|
template<size_t StackSize>
|
||||||
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char* rhs)
|
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char* rhs)
|
||||||
{
|
{
|
||||||
StackString str(lhs);
|
StackString<StackSize> str(lhs); str.append(rhs); return str;
|
||||||
str += rhs;
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t StackSize>
|
template<size_t StackSize>
|
||||||
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char& rhs)
|
StackString<StackSize> operator+(const StackString<StackSize>& lhs, const char& rhs)
|
||||||
{
|
{
|
||||||
StackString str(lhs);
|
StackString<StackSize> str(lhs); str.append(rhs); return str;
|
||||||
str += rhs;
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<size_t StackSize>
|
||||||
|
StackString<StackSize> operator+(const char& lhs, const StackString<StackSize>& rhs)
|
||||||
|
{
|
||||||
|
StackString<StackSize> str(lhs); str.append(rhs); return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Stores enough for an empty string on the stack, and afterwards allocates on the heap
|
// Stores enough for an empty string on the stack, and afterwards allocates on the heap
|
||||||
using HeapString = StackString<1>;
|
using HeapString = StackString<1>;
|
||||||
|
|
||||||
|
|
|
@ -20,30 +20,36 @@ namespace
|
||||||
|
|
||||||
constexpr bool blah_compare_ignore_case(char a, char b) { return blah_to_lower(a) == blah_to_lower(b); };
|
constexpr bool blah_compare_ignore_case(char a, char b) { return blah_to_lower(a) == blah_to_lower(b); };
|
||||||
constexpr bool blah_compare_with_case(char a, char b) { return a == b; };
|
constexpr bool blah_compare_with_case(char a, char b) { return a == b; };
|
||||||
|
|
||||||
|
#define BLAH_ASSERT_INPUT_STRING(input) \
|
||||||
|
BLAH_ASSERT(!(input >= cstr() && input < cstr() + length()), "Assigning string to itself is bad news!")
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseString::assign(const char* cstr, const char* cstr_end)
|
void BaseString::assign(const char* cstr_start, const char* cstr_end)
|
||||||
{
|
{
|
||||||
|
BLAH_ASSERT_INPUT_STRING(cstr_start);
|
||||||
s_clear();
|
s_clear();
|
||||||
append(cstr, cstr_end);
|
append(cstr_start, cstr_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseString::append(const char* cstr, const char* cstr_end)
|
void BaseString::append(const char* cstr_start, const char* cstr_end)
|
||||||
{
|
{
|
||||||
|
BLAH_ASSERT_INPUT_STRING(cstr_start);
|
||||||
|
|
||||||
// make sure values are valid
|
// make sure values are valid
|
||||||
if (cstr == nullptr || *cstr == '\0')
|
if (cstr_start == nullptr || *cstr_start == '\0')
|
||||||
return;
|
return;
|
||||||
if (cstr_end == nullptr)
|
if (cstr_end == nullptr)
|
||||||
cstr_end = cstr + blah_strlen(cstr);
|
cstr_end = cstr_start + blah_strlen(cstr_start);
|
||||||
|
|
||||||
// reserve (+1 for null-terminator)
|
// reserve (+1 for null-terminator)
|
||||||
auto len = length();
|
auto len = length();
|
||||||
s_ensure(len + (cstr_end - cstr) + 1);
|
s_ensure(len + (cstr_end - cstr_start) + 1);
|
||||||
|
|
||||||
// copy value over to our buffer
|
// copy value over to our buffer
|
||||||
char* dst = s_ptr() + len;
|
char* dst = s_ptr() + len;
|
||||||
while (cstr < cstr_end)
|
while (cstr_start < cstr_end)
|
||||||
*(dst++) = *(cstr++);
|
*(dst++) = *(cstr_start++);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseString::append(const u16* u16_cstr, const u16* u16_cstr_end, bool swap_endian)
|
void BaseString::append(const u16* u16_cstr, const u16* u16_cstr_end, bool swap_endian)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user