Files
core_zero/tjp/core/string/Str.inl
Timothy Prepscius 996151a56a flatten 20260225
2026-02-25 12:43:06 -05:00

465 lines
7.7 KiB
C++

// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
#include "Str.hpp"
#include "../algorithm/small_cmp.hpp"
#include <cstring>
namespace tjp {
namespace core {
namespace str {
typedef StringView compare_type;
TJP_STR_INLINE
auto cmp (const compare_type &l, const compare_type &r)
{
return small_cmp(l.data(), l.size(), r.data(), r.size());
}
TJP_STR_INLINE
auto cmp (const compare_type &l, const char *r)
{
return small_cmp(l.data(), l.size(), r);
}
TJP_STR_INLINE
auto cmp (const char *l, const compare_type &r)
{
return -cmp(r, l);
}
TJP_STR_TEMPLATE12
TJP_STR_INLINE
auto cmp(const TJP_STR &lhs, const TJP_STR2 &rhs)
{
return small_cmp(lhs.data(), lhs.size(), rhs.data(), rhs.size());
}
TJP_STR_INLINE
auto add(const compare_type &l, const compare_type &r)
{
return Str(l, r);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str()
{
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(size_t size, char c)
{
for (auto i=0; i<size; ++i)
string.push_back(c);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const String &rhs) :
string(rhs.data(), rhs.data() + rhs.size())
{
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const Vector<char> &rhs) :
string(rhs.data(), rhs.data() + rhs.size())
{
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const StringView &rhs) :
string(rhs.data(), rhs.data() + rhs.size())
{
}
TJP_STR_TEMPLATE
TJP_STR_TEMPLATE2
TJP_STR_INLINE
TJP_STR::Str(const TJP_STR2 &rhs) :
string(rhs.string)
{
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const char *begin, const char *end) :
string(begin, end)
{
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const char *rhs)
{
debug_assert(rhs);
if (!rhs)
return;
auto MAX = 16384;
for (auto i=0; i<MAX; ++i)
{
if (*rhs == 0)
break;
string.push_back(*rhs++);
}
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::Str(const StringView &v0, const StringView &v1)
{
string.size_to(v0.size() + v1.size());
string.append(v0.data(), v0.data() + v0.size());
string.append(v1.data(), v1.data() + v1.size());
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
String TJP_STR::toString() const
{
return String(string.begin(), string.size());
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR::operator const StringView () const
{
return StringView(string.begin(), string.size());
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool TJP_STR::empty() const
{
return string.empty();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
void TJP_STR::clear()
{
return string.clear();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
char TJP_STR::front() const
{
return string.front();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
char TJP_STR::back() const
{
return string.back();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
const TJP_STR_TYPENAME::value_type *TJP_STR::data() const
{
return string.data();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR_TYPENAME::const_iterator_ TJP_STR::begin() const
{
return string.begin();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR_TYPENAME::const_iterator_ TJP_STR::end() const
{
return string.end();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
size_t TJP_STR::size() const
{
return string.size();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
const char *TJP_STR::c_str()
{
string.push_back(0);
string.pop_back();
return string.data();
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
const char *TJP_STR::c_str() const
{
return const_cast<TJP_STR *>(this)->c_str();
}
TJP_STR_INLINE
bool operator ==(const compare_type &lhs, const char *rhs)
{
return cmp(lhs, rhs) == 0;
}
TJP_STR_INLINE
bool operator ==(const char *lhs, const compare_type &rhs)
{
return rhs == lhs;
}
TJP_STR_INLINE
bool operator !=(const compare_type &lhs, const char *rhs)
{
return !(lhs == rhs);
}
TJP_STR_INLINE
bool operator !=(const char *lhs, const compare_type &rhs)
{
return !(lhs == rhs);
}
TJP_STR_INLINE
bool operator <(const compare_type &lhs, const char *rhs)
{
return cmp(lhs, rhs) < 0;
}
TJP_STR_INLINE
bool operator <(const char *lhs, const compare_type &rhs)
{
return cmp(lhs, rhs) < 0;
}
TJP_STR_TEMPLATE12
TJP_STR_INLINE
bool operator ==(const TJP_STR &lhs, const TJP_STR2 &rhs)
{
return cmp(lhs, rhs) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const TJP_STR &lhs, const String &r)
{
return cmp((const compare_type &)lhs, compare_type(r)) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const TJP_STR &lhs, const StringView &r)
{
return cmp((const compare_type &)lhs, r) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const TJP_STR &lhs, const char *r)
{
return cmp((const compare_type &)lhs, r) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const String &l, const TJP_STR &rhs)
{
return cmp(compare_type(l), (const compare_type &)rhs) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const StringView &l, const TJP_STR &rhs)
{
return cmp(l, (const compare_type &)rhs) == 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator ==(const char *l, const TJP_STR &rhs)
{
return cmp(l, (const compare_type &)rhs) == 0;
}
TJP_STR_TEMPLATE12
TJP_STR_INLINE
bool operator !=(const TJP_STR &lhs, const TJP_STR2 &rhs)
{
return cmp(lhs, rhs) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const TJP_STR &lhs, const String &r)
{
return cmp((const compare_type &)lhs, compare_type(r)) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const TJP_STR &lhs, const StringView &r)
{
return cmp((const compare_type &)lhs, r) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const TJP_STR &lhs, const char *r)
{
return cmp((const compare_type &)lhs, r) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const String &l, const TJP_STR &rhs)
{
return cmp(compare_type(l), (const compare_type &)rhs) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const StringView &l, const TJP_STR &rhs)
{
return cmp(compare_type(l), (const compare_type &)rhs) != 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator !=(const char *l, const TJP_STR &rhs)
{
return cmp(l, (const compare_type &)rhs) != 0;
}
TJP_STR_TEMPLATE12
TJP_STR_INLINE
bool operator <(const TJP_STR &lhs, const TJP_STR2 &rhs)
{
return cmp(lhs, rhs) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const TJP_STR &lhs, const String &r)
{
return cmp((const compare_type &)lhs, compare_type(r)) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const TJP_STR &lhs, const StringView &r)
{
return cmp((const compare_type &)lhs, r) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const TJP_STR &lhs, const char *r)
{
return cmp((const compare_type &)lhs, r) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const String &l, const TJP_STR &rhs)
{
return cmp(compare_type(l), (const compare_type &)rhs) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const StringView &l, const TJP_STR &rhs)
{
return cmp(l, (const compare_type &)rhs) < 0;
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
bool operator <(const char *l, const TJP_STR &rhs)
{
return cmp(l, (const compare_type &)rhs) < 0;
}
TJP_STR_TEMPLATE12
TJP_STR_INLINE
TJP_STR operator+(const TJP_STR &lhs, const TJP_STR2 &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const String &lhs, const TJP_STR &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const StringView &lhs, const TJP_STR &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const char *lhs, const TJP_STR &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const TJP_STR &lhs, const String &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const TJP_STR &lhs, const StringView &rhs)
{
return add(lhs, rhs);
}
TJP_STR_TEMPLATE
TJP_STR_INLINE
TJP_STR operator+(const TJP_STR &lhs, const char *rhs)
{
return add(lhs, rhs);
}
} // namespace
} // namespace
} // namespace