- Stacktrace generation with failed asserts
This commit is contained in:
297
include/fennec/fproc/strings/string.h
Normal file
297
include/fennec/fproc/strings/string.h
Normal file
@@ -0,0 +1,297 @@
|
||||
// =====================================================================================================================
|
||||
// fennec, a free and open source game engine
|
||||
// Copyright © 2025 Medusa Slockbower
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// =====================================================================================================================
|
||||
|
||||
#ifndef FENNEC_FPROC_STRINGS_STRING_H
|
||||
#define FENNEC_FPROC_STRINGS_STRING_H
|
||||
|
||||
#include <fennec/fproc/strings/detail/__ctype.h>
|
||||
|
||||
#include <fennec/lang/assert.h>
|
||||
#include <fennec/memory/allocator.h>
|
||||
#include <fennec/memory/memory.h>
|
||||
|
||||
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
|
||||
template<typename AllocT = allocator<char>>
|
||||
struct string
|
||||
{
|
||||
public:
|
||||
using alloc_t = allocation<char, AllocT>;
|
||||
|
||||
///
|
||||
/// \brief Default Constructor, initializes empty string
|
||||
constexpr string()
|
||||
: _str()
|
||||
{ }
|
||||
|
||||
///
|
||||
/// \brief Sized Constructor, initializes a null-terminated string of size `n` with null characters
|
||||
/// \param n the number of characters
|
||||
///
|
||||
/// \details adds additional character for null termination.
|
||||
constexpr string(size_t n)
|
||||
: string('\0', n)
|
||||
{ }
|
||||
|
||||
///
|
||||
/// \brief Sized Constructor, initializes a null-terminated string of size `n` filled with the character `c`
|
||||
/// \param c the character to fill with
|
||||
/// \param n the number of characters
|
||||
///
|
||||
/// \details adds additional character for null termination.
|
||||
constexpr string(char c, size_t n)
|
||||
: _str(n + 1)
|
||||
{ fennec::memset(_str, c, n); _str[n] = '\0'; }
|
||||
|
||||
///
|
||||
/// \brief Buffer Copy Constructor
|
||||
/// \param str the buffer to copy
|
||||
/// \param len number of characters in the buffer
|
||||
///
|
||||
/// \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);
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief String Copy Constructor
|
||||
/// \param str the string to copy
|
||||
constexpr string(const string& str)
|
||||
: string(str, str.size() - 1)
|
||||
{ }
|
||||
|
||||
constexpr string(string&& str) noexcept
|
||||
: _str(fennec::move(str._str))
|
||||
{ }
|
||||
|
||||
///
|
||||
/// \brief String Destructor, cleans up the underlying allocation
|
||||
constexpr ~string() = default; // allocation cleans up itself
|
||||
|
||||
// Properties ==========================================================================================================
|
||||
|
||||
///
|
||||
/// \returns The size of the string excluding null terminator
|
||||
constexpr size_t size() const { return _str.capacity() - 1; }
|
||||
|
||||
|
||||
// Access ==============================================================================================================
|
||||
|
||||
///
|
||||
/// \brief Array Access Operator
|
||||
/// \param i the index to access
|
||||
/// \returns a reference to the character
|
||||
constexpr char& operator[](int i) {
|
||||
assert(i >= 0 && i < size(), "Array Out of Bounds");
|
||||
return _str[i];
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Const-Array Access Operator
|
||||
/// \param i the index to access
|
||||
/// \returns a copy of the character
|
||||
constexpr char operator[](int i) const {
|
||||
assert(i >= 0 && i < size(), "Array Out of Bounds");
|
||||
return _str[i];
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Dereference Operator
|
||||
/// \returns A const qualified pointer to the underlying allocation
|
||||
constexpr const char* operator*() const { return _str; }
|
||||
|
||||
///
|
||||
/// \brief Implicit Dereference Cast
|
||||
constexpr operator const char*() const { return _str; }
|
||||
|
||||
|
||||
// Examination =========================================================================================================
|
||||
|
||||
///
|
||||
/// \returns The length of the string to the first null-terminator
|
||||
constexpr size_t length() const
|
||||
{ return find('\0'); }
|
||||
|
||||
///
|
||||
/// \brief String Comparison
|
||||
/// \param ostr the string to compare against
|
||||
/// \returns Zero if both strings are equal, otherwise a negative value if lhs appears before rhs according to the
|
||||
/// current locale, otherwise a positive value.
|
||||
constexpr int compare(const string& ostr) const
|
||||
{ return ::strcoll(_str, ostr); }
|
||||
|
||||
///
|
||||
/// \brief Finds the index of the first occurrence of `x` in the string
|
||||
/// \param x the character to find
|
||||
/// \returns The index of `x` if it occurs in the string, otherwise returns `size()`
|
||||
constexpr size_t find(char x) const
|
||||
{
|
||||
const char* loc = ::strchr(_str, x);
|
||||
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 string& 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
|
||||
/// \returns The index of `x` if it occurs in the string, otherwise returns `size()`
|
||||
constexpr size_t rfind(char x) const
|
||||
{
|
||||
const char* loc = ::strrchr(_str, x);
|
||||
return loc ? loc - _str : size();
|
||||
}
|
||||
|
||||
// TODO: constexpr size_t rfind(const string& str) const;
|
||||
|
||||
// Manipulation ========================================================================================================
|
||||
|
||||
///
|
||||
/// \brief Resize the string, filling additional bytes with `'\0'`
|
||||
/// \param n the new size of the string
|
||||
constexpr void resize(size_t n)
|
||||
{
|
||||
size_t i = size();
|
||||
_str.reallocate(n + 1);
|
||||
if (n > i) fennec::memset(*_str + i, '\0', n - i);
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Copy Assignment Operator
|
||||
/// \param str the string to copy
|
||||
/// \returns a reference to `this`
|
||||
constexpr string& operator=(const string& str)
|
||||
{
|
||||
if (str.size() > size()) string::resize(str.size());
|
||||
fennec::memcpy(_str, str, str.size());
|
||||
_str[str.size()] = '\0';
|
||||
return *this;
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Move Assignment Operator
|
||||
/// \param str the string to move
|
||||
/// \returns a reference to `this`
|
||||
constexpr string& operator=(string&& str) noexcept
|
||||
{
|
||||
_str = fennec::move(str._str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Retrieve a substring of a string
|
||||
/// \param i the start index
|
||||
/// \param n the number of characters
|
||||
/// \return
|
||||
constexpr string substring(size_t i, size_t n = size())
|
||||
{
|
||||
assert(i < size(), "Array Out of Bounds");
|
||||
n = min(n, size() - i);
|
||||
return string(&_str[i], n);
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Returns a string with `c` appended to it
|
||||
/// \param c
|
||||
/// \returns
|
||||
constexpr string operator+(char c)
|
||||
{
|
||||
// Copy contents with one additional byte.
|
||||
string res(_str, _str.size());
|
||||
res[size()] = c; // Set the last character to c
|
||||
return res;
|
||||
}
|
||||
|
||||
constexpr string operator+(const string& 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();
|
||||
resize(x + 1);
|
||||
_str[x] = c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr string& operator+=(const string& str)
|
||||
{
|
||||
size_t x = size();
|
||||
string::resize(x + str.size());
|
||||
fennec::memcpy(&_str[x], str, str.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
alloc_t _str;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // FENNEC_FPROC_STRINGS_STRING_H
|
||||
Reference in New Issue
Block a user