Fixed Str constructor not to check for a null-terminator if given a size of zero.

This commit is contained in:
Karutoh 2025-05-02 07:01:40 -07:00
parent c5b281d73c
commit 63eba0d2db

View File

@ -48,8 +48,11 @@ namespace ehs
/// @param [in] str The C-style string. /// @param [in] str The C-style string.
/// @param [in] size The size of the given C-style string. /// @param [in] size The size of the given C-style string.
Str(const T* const str, const N size) Str(const T* const str, const N size)
: size((size) ? size : Len(str)), data(nullptr) : size(size), data(nullptr)
{ {
if (!size)
return;
data = new T[this->size + 1]; data = new T[this->size + 1];
Util::Copy(data, str, Size(true)); Util::Copy(data, str, Size(true));
@ -914,10 +917,10 @@ namespace ehs
} }
/// Splits a string into a Vector with the given separator. /// Splits a string into a Vector with the given separator.
/// @param [in] ide The given string as the separator. /// @param [in] delimeter The given string as the separator.
/// @param [in] max The max amount of times to split the string. /// @param [in] max The max amount of times to split the string.
/// @returns The resulting string object. /// @returns The resulting string object.
Vector<Str<T, N>, N> Split(const Str<T, N>& ide, const N max = 0) const Vector<Str<T, N>, N> Split(const Str<T, N>& delimeter, const N max = 0) const
{ {
Vector<Str<T, N>, N> result(0, 5); Vector<Str<T, N>, N> result(0, 5);
@ -925,9 +928,9 @@ namespace ehs
for (N i = 0, c = 0; i < size; ++i) for (N i = 0, c = 0; i < size; ++i)
{ {
if (data[i] == ide[c]) if (data[i] == delimeter[c])
{ {
if (++c == ide.Size()) if (++c == delimeter.Size())
{ {
N r = i - (c - 1) - b; N r = i - (c - 1) - b;
if (!r) if (!r)
@ -955,17 +958,17 @@ namespace ehs
} }
/// Removes all instances of the ide. /// Removes all instances of the ide.
/// @param [in] ide The string to look for. /// @param [in] delimeter The string to look for.
/// @returns The resulting string object. /// @returns The resulting string object.
Str<T, N> RemoveAll(const Str<T, N>& ide) const Str<T, N> RemoveAll(const Str<T, N>& delimeter) const
{ {
Str<T, N> result(size); Str<T, N> result(size);
for (N i = 0, b = 0, c = 0; i < size; ++i) for (N i = 0, b = 0, c = 0; i < size; ++i)
{ {
if (data[i] == ide[c]) if (data[i] == delimeter[c])
{ {
if (++c == ide.Size()) if (++c == delimeter.Size())
c = 0; c = 0;
} }
else else
@ -984,18 +987,18 @@ namespace ehs
} }
/// Replaces all instances of ide with the replacer. /// Replaces all instances of ide with the replacer.
/// @param [in] ide The string to look for. /// @param [in] delimeter The string to look for.
/// @param [in] replacer The string placed. /// @param [in] replacer The string placed.
/// @returns The resulting string object. /// @returns The resulting string object.
Str ReplaceAll(const Str& ide, const Str& replacer) const Str ReplaceAll(const Str& delimeter, const Str& replacer) const
{ {
Str<T, N> result; Str<T, N> result;
for (N i = 0, b = 0; i < size; ++i) for (N i = 0, b = 0; i < size; ++i)
{ {
if (data[i] == ide[b]) if (data[i] == delimeter[b])
{ {
if (++b == ide.Size()) if (++b == delimeter.Size())
{ {
result.Push(replacer); result.Push(replacer);
b = 0; b = 0;
@ -1011,20 +1014,20 @@ namespace ehs
} }
/// Finds the first instance of the given string object. /// Finds the first instance of the given string object.
/// @param [in] ide The string to look for. /// @param [in] delimeter The string to look for.
/// @param [out] index The index of the string found. Can be a nullptr. /// @param [out] index The index of the string found. Can be a nullptr.
/// @param [in] pattern The search pattern for optimization. /// @param [in] pattern The search pattern for optimization.
/// @param [in] result What index to return where the first instance is found. /// @param [in] result What index to return where the first instance is found.
/// @returns The index where the instance was found with the result varying from the result parameter. /// @returns The index where the instance was found with the result varying from the result parameter.
bool Find(const Str<T, N> &ide, N* const index = nullptr, const SearchPattern pattern = SearchPattern::LEFT_RIGHT, const IndexResult result = IndexResult::BEGINNING) const bool Find(const Str<T, N> &delimeter, N* const index = nullptr, const SearchPattern pattern = SearchPattern::LEFT_RIGHT, const IndexResult result = IndexResult::BEGINNING) const
{ {
if (pattern == SearchPattern::LEFT_RIGHT) if (pattern == SearchPattern::LEFT_RIGHT)
{ {
for (N i = 0, c = 0; i < size; ++i) for (N i = 0, c = 0; i < size; ++i)
{ {
if (data[i] == ide[c]) if (data[i] == delimeter[c])
{ {
if (++c == ide.Size()) if (++c == delimeter.Size())
{ {
if (result == IndexResult::BEGINNING) if (result == IndexResult::BEGINNING)
{ {
@ -1046,16 +1049,16 @@ namespace ehs
} }
else if (pattern == SearchPattern::RIGHT_LEFT) else if (pattern == SearchPattern::RIGHT_LEFT)
{ {
for (N i = size, c = ide.Size(); i > 0; --i) for (N i = size, c = delimeter.Size(); i > 0; --i)
{ {
if (data[i - 1] == ide[c - 1]) if (data[i - 1] == delimeter[c - 1])
{ {
if (--c == 0) if (--c == 0)
{ {
if (result == IndexResult::BEGINNING) if (result == IndexResult::BEGINNING)
{ {
if (index) if (index)
*index = i - (ide.Size() - 1); *index = i - (delimeter.Size() - 1);
return true; return true;
} }
@ -1075,18 +1078,18 @@ namespace ehs
} }
/// Checks if the current string contains the given ide. /// Checks if the current string contains the given ide.
/// @param [in] ide The given ide to check for. /// @param [in] delimeter The given ide to check for.
/// @param [in] pattern The search pattern to use. /// @param [in] pattern The search pattern to use.
/// @returns True if the current string does contain the ide. /// @returns True if the current string does contain the ide.
bool Contains(const Str<T, N>& ide, const SearchPattern pattern = SearchPattern::LEFT_RIGHT) const bool Contains(const Str<T, N>& delimeter, const SearchPattern pattern = SearchPattern::LEFT_RIGHT) const
{ {
if (pattern == SearchPattern::LEFT_RIGHT) if (pattern == SearchPattern::LEFT_RIGHT)
{ {
for (N i = 0, c = 0; i < size; ++i) for (N i = 0, c = 0; i < size; ++i)
{ {
if (data[i] == ide[c]) if (data[i] == delimeter[c])
{ {
if (++c == ide.Size()) if (++c == delimeter.Size())
{ {
return true; return true;
} }
@ -1095,9 +1098,9 @@ namespace ehs
} }
else if (pattern == SearchPattern::RIGHT_LEFT) else if (pattern == SearchPattern::RIGHT_LEFT)
{ {
for (N i = size, c = ide.Size(); i > 0; --i) for (N i = size, c = delimeter.Size(); i > 0; --i)
{ {
if (data[i - 1] == ide[c - 1]) if (data[i - 1] == delimeter[c - 1])
{ {
if (--c == 0) if (--c == 0)
{ {