- Removed fennec::path, see #Security Ramblings in PLANNING.md
This commit is contained in:
@@ -19,13 +19,16 @@
|
||||
#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/fproc/strings/cstring.h>
|
||||
|
||||
#include <fennec/lang/assert.h>
|
||||
|
||||
#include <fennec/memory/allocator.h>
|
||||
#include <fennec/memory/common.h>
|
||||
|
||||
#include <fennec/math/common.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
@@ -43,6 +46,7 @@ template<typename AllocT = allocator<char>>
|
||||
struct _string
|
||||
{
|
||||
public:
|
||||
static constexpr size_t npos = -1;
|
||||
using alloc_t = allocation<char, AllocT>;
|
||||
|
||||
|
||||
@@ -139,7 +143,8 @@ public:
|
||||
///
|
||||
/// \brief Implicit Dereference Cast
|
||||
constexpr operator const char*() const {
|
||||
return _str; }
|
||||
return _str;
|
||||
}
|
||||
|
||||
|
||||
// Examination =========================================================================================================
|
||||
@@ -155,24 +160,37 @@ public:
|
||||
/// \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);
|
||||
constexpr int compare(const cstring& str, size_t i = 0, size_t n = npos) const {
|
||||
if (i >= size()) { // bounds check
|
||||
return -1;
|
||||
}
|
||||
n = min(n, max(_str, str.size()) + 1);
|
||||
|
||||
return ::strncmp(_str + i, str, n);
|
||||
}
|
||||
|
||||
///
|
||||
/// \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();
|
||||
constexpr size_t find(char c, size_t i = 0) const {
|
||||
if (i >= size()) { // bounds check
|
||||
return size();
|
||||
}
|
||||
|
||||
const char* loc = ::strchr(_str + i, c); // get location using strchr
|
||||
return loc ? loc - _str : size(); // return size if not found
|
||||
}
|
||||
|
||||
///
|
||||
/// \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 {
|
||||
constexpr size_t find(const string& str, size_t i = 0) const { // bounds check
|
||||
if (i >= size()) { // bounds check
|
||||
return size();
|
||||
}
|
||||
|
||||
const char* loc = ::strstr(_str, str);
|
||||
return loc ? loc - _str : size();
|
||||
}
|
||||
@@ -181,9 +199,13 @@ public:
|
||||
/// \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();
|
||||
constexpr size_t find(const cstring& str, size_t i = 0) const {
|
||||
if (i + str.size() > size()) { // bounds check
|
||||
return size();
|
||||
}
|
||||
|
||||
const char* loc = ::strstr(_str + i, str); // get location using strstr
|
||||
return loc ? loc - _str : size(); // return size if not found
|
||||
}
|
||||
|
||||
///
|
||||
@@ -191,11 +213,15 @@ public:
|
||||
/// \param c the string to find
|
||||
/// \param i the index to start at
|
||||
/// \returns The index of `c` if it occurs in the string, otherwise returns `size()`
|
||||
constexpr size_t rfind(char c, size_t i = size()) const {
|
||||
if (size() == 0) return size();
|
||||
constexpr size_t rfind(char c, size_t i = npos) const {
|
||||
if (size() == 0) {
|
||||
return size();
|
||||
}
|
||||
i = min(i, size() - 1); // clamp i to bounds
|
||||
do {
|
||||
if (_str[i] == c) return i; // loop backwards looking for c
|
||||
if (_str[i] == c) { // loop backwards looking for c
|
||||
return i;
|
||||
}
|
||||
} while (i--);
|
||||
return size(); // base case
|
||||
}
|
||||
@@ -205,12 +231,18 @@ public:
|
||||
/// \param str the string to find
|
||||
/// \param i the index to start at
|
||||
/// \returns The index of `str` if it occurs in the string, otherwise returns `size()`
|
||||
constexpr size_t rfind(const cstring& str, size_t i = size()) const {
|
||||
constexpr size_t rfind(const cstring& str, size_t i = npos) const {
|
||||
if (size() == 0) {
|
||||
return size();
|
||||
}
|
||||
const char first = str[0];
|
||||
i = min(i, size() - str.size());
|
||||
do {
|
||||
if(_str[i] == first)
|
||||
if (compare(str, i) == 0) return i; // loop backwards looking for str
|
||||
if(_str[i] == first) {
|
||||
if (compare(str, i) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
} while (i--);
|
||||
return size(); // base case
|
||||
}
|
||||
@@ -220,12 +252,18 @@ public:
|
||||
/// \param str the string to find
|
||||
/// \param i the index to start at
|
||||
/// \returns The index of `str` if it occurs in the string, otherwise returns `size()`
|
||||
constexpr size_t rfind(const string& str, size_t i = size()) const {
|
||||
constexpr size_t rfind(const string& str, size_t i = npos) const {
|
||||
if (size() == 0) {
|
||||
return size();
|
||||
}
|
||||
const char first = str[0];
|
||||
i = min(i, size() - str.size());
|
||||
do {
|
||||
if(_str[i] == first)
|
||||
if (compare(str, i) == 0) return i; // loop backwards looking for str
|
||||
if(_str[i] == first) {
|
||||
if (compare(str, i) == 0) { // loop backwards looking for str
|
||||
return i;
|
||||
}
|
||||
}
|
||||
} while (i--);
|
||||
return size(); // base case
|
||||
}
|
||||
@@ -313,7 +351,7 @@ public:
|
||||
|
||||
constexpr string& operator+=(const string& str) {
|
||||
size_t x = size();
|
||||
string::resize(x + str.size());
|
||||
resize(x + str.size());
|
||||
fennec::memcpy(&_str[x], str, str.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user