Initial test bootstrap.
This commit is contained in:
parent
6c618749f1
commit
4b308f6c32
94 changed files with 2543 additions and 681 deletions
232
src/base/core/data_structures/String.cpp
Normal file
232
src/base/core/data_structures/String.cpp
Normal file
|
@ -0,0 +1,232 @@
|
|||
#include "String.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
String::String()
|
||||
{
|
||||
m_data.push_back('\0');
|
||||
}
|
||||
|
||||
String::String(const Vector<Byte>& data)
|
||||
{
|
||||
append(data);
|
||||
}
|
||||
|
||||
String::String(const char* data)
|
||||
{
|
||||
append(data);
|
||||
}
|
||||
|
||||
void String::append(const char* data)
|
||||
{
|
||||
if (data == nullptr)
|
||||
{
|
||||
m_data.push_back('\0');
|
||||
return;
|
||||
}
|
||||
|
||||
auto loc = data;
|
||||
while(*loc != '\0')
|
||||
{
|
||||
m_data.push_back(*loc);
|
||||
loc++;
|
||||
}
|
||||
m_data.push_back('\0');
|
||||
}
|
||||
|
||||
const Vector<char>& String::data() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
bool String::empty() const
|
||||
{
|
||||
return m_data.empty() || m_data[0] == '\0';
|
||||
}
|
||||
|
||||
void String::append(const Vector<Byte>& data)
|
||||
{
|
||||
if (data.capacity() == 0)
|
||||
{
|
||||
m_data.push_back('\0');
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_data.size() == 1 && m_data[0] == '\0')
|
||||
{
|
||||
m_data.clear();
|
||||
}
|
||||
|
||||
for(const auto c : data)
|
||||
{
|
||||
if(c == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_data.push_back(static_cast<char>(c));
|
||||
}
|
||||
}
|
||||
m_data.push_back('\0');
|
||||
}
|
||||
|
||||
Pair<String, String> String::rsplit(char c) const
|
||||
{
|
||||
if (const auto index = rindex(c); index.valid())
|
||||
{
|
||||
String left;
|
||||
slice(0, index.value(), left);
|
||||
|
||||
String right;
|
||||
slice(index.value(), size(), right);
|
||||
return {left, right};
|
||||
}
|
||||
return {*this, {}};
|
||||
}
|
||||
|
||||
bool String::slice(std::size_t idx, String& out) const
|
||||
{
|
||||
if (idx >= m_data.size() - 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto ok = m_data.slice(idx, out.m_data);
|
||||
if (!ok)
|
||||
{
|
||||
return ok;
|
||||
}
|
||||
out.m_data.push_back('\0');
|
||||
return true;
|
||||
}
|
||||
|
||||
bool String::slice(std::size_t start, std::size_t end, String& out) const
|
||||
{
|
||||
if (end >= m_data.size() - 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto ok = m_data.slice(start, end, out.m_data);
|
||||
if (!ok)
|
||||
{
|
||||
return ok;
|
||||
}
|
||||
out.m_data.push_back('\0');
|
||||
return true;
|
||||
}
|
||||
|
||||
Index String::rindex(char c) const
|
||||
{
|
||||
if (m_data.size() <= 2)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
for(std::size_t idx=m_data.size()-2; idx >= 0; idx--)
|
||||
{
|
||||
if (m_data[idx] == c)
|
||||
{
|
||||
return Index(idx);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
const char* String::raw() const
|
||||
{
|
||||
return m_data.data();
|
||||
}
|
||||
|
||||
std::size_t String::size() const
|
||||
{
|
||||
return m_data.size() - 1;
|
||||
}
|
||||
|
||||
void String::reverse()
|
||||
{
|
||||
if (m_data.size() == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for(size_t idx=0; idx<(m_data.size()-1)/2; idx++)
|
||||
{
|
||||
const auto ridx = m_data.size() - 2 - idx;
|
||||
const auto tmp0 = m_data[idx];
|
||||
m_data[idx] = m_data[ridx];
|
||||
m_data[ridx] = tmp0;
|
||||
}
|
||||
}
|
||||
|
||||
String String::to_string(size_t input)
|
||||
{
|
||||
String conv;
|
||||
auto input_cpy = input;
|
||||
while(input_cpy > 0)
|
||||
{
|
||||
const auto rem = input_cpy % 10;
|
||||
conv += static_cast<char>(48 + rem);
|
||||
input_cpy /= 10;
|
||||
}
|
||||
conv.reverse();
|
||||
return conv;
|
||||
}
|
||||
|
||||
char String::operator[](std::size_t idx) const
|
||||
{
|
||||
return m_data[idx];
|
||||
}
|
||||
|
||||
String& String::operator<<(const char* body)
|
||||
{
|
||||
append(body);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool String::operator==(const String& other) const
|
||||
{
|
||||
return m_data == other.m_data;
|
||||
}
|
||||
|
||||
bool String::operator!=(const String& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
String& String::operator<<(size_t idx)
|
||||
{
|
||||
/*
|
||||
const auto num_digits = static_cast<unsigned>(log10(double(idx))) + 1;
|
||||
char body[num_digits+1];
|
||||
snprintf(body, num_digits+1, "%d", static_cast<int>(idx));
|
||||
append(body);
|
||||
*/
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::operator+=(const String& str)
|
||||
{
|
||||
if (m_data.empty())
|
||||
{
|
||||
m_data = str.m_data;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_data.pop_back();
|
||||
m_data.extend(str.m_data);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
String String::operator+(const String& str) const
|
||||
{
|
||||
auto ret = *this;
|
||||
ret += str;
|
||||
return ret;
|
||||
}
|
||||
|
||||
String& String::operator+=(char c)
|
||||
{
|
||||
m_data.push_back('\0');
|
||||
m_data[m_data.size()-2] = c;
|
||||
return *this;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue