- Setup Basic Implementation for String Library

This commit is contained in:
2025-07-06 19:29:28 -04:00
parent a33bf5206f
commit 012052641d
17 changed files with 646 additions and 41 deletions

View File

@@ -19,6 +19,7 @@
#ifndef FENNEC_FPROC_STRINGS_STRING_H
#define FENNEC_FPROC_STRINGS_STRING_H
#include <fennec/fproc/strings/cstring.h>
#include <fennec/fproc/strings/detail/__ctype.h>
#include <fennec/lang/assert.h>
@@ -28,35 +29,19 @@
namespace fennec
{
// TODO: Document
using ::isalnum;
using ::isalpha;
using ::islower;
using ::isupper;
using ::isdigit;
using ::isxdigit;
using ::iscntrl;
using ::isgraph;
using ::isspace;
using ::isblank;
using ::isprint;
using ::ispunct;
using ::tolower;
using ::toupper;
///
/// \brief Struct for wrapping c-style strings
///
/// \details fennec will use this class over `const char*` for memory safety.
/// behaviour guarantees that the underlying string is null-terminated
/// \details behaviour guarantees that the underlying string is null-terminated
template<typename AllocT = allocator<char>>
struct string
{
public:
using alloc_t = allocation<char, AllocT>;
// Constructors ========================================================================================================
///
/// \brief Default Constructor, initializes empty string
constexpr string()
@@ -89,22 +74,9 @@ public:
///
/// \details adds additional character for null termination. Ignores whether str is null-terminated.
/// This constructor makes the assumption that `len` is the intended number of characters.
constexpr string(const char* str, size_t len)
: _str(str, len + 1)
{ _str[len] = '\0'; }
///
/// \brief Array Copy Constructor
/// \tparam N the number of characters
/// \param str the array to copy
///
/// \details if str is not null-terminated, adds additional character for null-termination.
template<size_t N>
constexpr string(const char str[N])
{
if (str[N - 1] == '\0') string(str, N - 1);
else string(str, N);
}
constexpr string(const cstring& str)
: _str(str, str.size() + 1)
{ _str[str.size()] = '\0'; }
///
/// \brief String Copy Constructor
@@ -155,7 +127,7 @@ public:
///
/// \brief Implicit Dereference Cast
constexpr operator const char*() const { return _str; }
constexpr operator const char*() const { return _str; }
// Examination =========================================================================================================
@@ -193,6 +165,16 @@ public:
return loc ? loc - _str : size();
}
///
/// \brief Finds the index of the first occurrence of `str` in the string.
/// \param str the string to find
/// \returns The index of `str` if it occurs in the string, otherwise returns `size()`
constexpr size_t find(const cstring& str) const
{
const char* loc = ::strstr(_str, str);
return loc ? loc - _str : size();
}
///
/// \brief Finds the index of the last occurrence of `x` in the string.
/// \param x the string to find
@@ -271,6 +253,14 @@ public:
return res;
}
constexpr string operator+(const cstring& str)
{
string res(size() + str.size()); // Make a new string with the size of this + str
fennec::memcpy(res, _str, size()); // Copy the contents of this
fennec::memcpy(&res[size()], str, str.size()); // Append the contents of str
return res;
}
constexpr string& operator+=(char c)
{
size_t x = size();
@@ -287,6 +277,14 @@ public:
return *this;
}
constexpr string& operator+=(const cstring& str)
{
size_t x = size();
resize(x + str.size());
fennec::memcpy(&_str[x], str, str.size());
return *this;
}
private:
alloc_t _str;
};