From 6fa77292530c16d04a1ca3e2e34a9ad0c6423ba3 Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 1 Feb 2024 17:04:57 -0800 Subject: [PATCH] Added and adjusted docs for Str class. --- CMakeLists.txt | 2 +- include/ehs/Str.h | 214 ++++++++++++++++++++++++++++++---------------- 2 files changed, 141 insertions(+), 75 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6cc834..befac3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,7 +221,7 @@ add_executable(StrToHash src/StrToHash.cpp) target_include_directories(EHS PUBLIC ${PROJECT_SOURCE_DIR}/include) -set(CMAKE_INSTALL_PREFIX "${USER_HOME_DIRECTORY}/Libraries/EHS") +set(CMAKE_INSTALL_PREFIX "/usr/local") install(TARGETS EHS DESTINATION lib) install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION include) diff --git a/include/ehs/Str.h b/include/ehs/Str.h index dc05684..e549792 100644 --- a/include/ehs/Str.h +++ b/include/ehs/Str.h @@ -33,7 +33,7 @@ namespace ehs public: /// Frees any data created on the heap. - ~Str() + ~Str() override { delete[] data; } @@ -84,6 +84,7 @@ namespace ehs AddType("Str"); } + /// A move constructor. Str(Str&& str) noexcept : BaseObj((BaseObj&&)str), size(str.size), data(str.data) { @@ -158,7 +159,7 @@ namespace ehs /// Concatenates with the given C-style string. /// @param [in] str The given C-style string. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const T* const str) { N inputSize = Len(str); @@ -180,7 +181,7 @@ namespace ehs /// Concatenates with the given string object. /// @param [in] str The given string object. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const Str& str) { T* result = new T[size + str.size + 1]; @@ -200,7 +201,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const SInt_64 num) { return operator+=(FromNum(num)); @@ -208,7 +209,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const UInt_64 num) { return operator+=(FromNum(num)); @@ -216,7 +217,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const SInt_32 num) { return operator+=(FromNum(num)); @@ -224,7 +225,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const UInt_32 num) { return operator+=(FromNum(num)); @@ -232,7 +233,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const SInt_16 num) { return operator+=(FromNum(num)); @@ -240,7 +241,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const UInt_16 num) { return operator+=(FromNum(num)); @@ -248,7 +249,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const SInt_8 num) { return operator+=(FromNum(num)); @@ -256,7 +257,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const UInt_8 num) { return operator+=(FromNum(num)); @@ -282,7 +283,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const float num) { return operator+=(FromNum(num)); @@ -290,7 +291,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const double num) { return operator+=(FromNum(num)); @@ -298,7 +299,7 @@ namespace ehs /// Concatenates with the given number. /// @param [in] num The given number to concatenate. - /// @returns The result. + /// @returns The resulting string object. Str& operator+=(const long double num) { return operator+=(FromNum(num)); @@ -306,7 +307,7 @@ namespace ehs /// Concatenates with the given C-style string. /// @param [in] str The given C-style string. - /// @returns The result. + /// @returns The resulting string object. Str operator+(const T* const str) const { N inSize = Len(str); @@ -324,7 +325,7 @@ namespace ehs /// Concatenates with the given string object. /// @param [in] str The given string object. - /// @returns The result. + /// @returns The resulting string object. Str operator+(const Str& str) const { Str result(size + str.size); @@ -339,64 +340,64 @@ namespace ehs } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const SInt_64 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const UInt_64 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const SInt_32 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const UInt_32 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const SInt_16 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const UInt_16 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const SInt_8 num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const UInt_8 num) const { return operator+(FromNum(num)); @@ -421,29 +422,32 @@ namespace ehs #endif /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const float num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const double num) const { return operator+(FromNum(num)); } /// Concatenates with the given number. - /// @param [in] str The given number to Concatenate. - /// @returns The result. + /// @param [in] num The given number to Concatenate. + /// @returns The resulting string object. Str operator+(const long double num) const { return operator+(FromNum(num)); } + /// Compares with a another string. First comparing sizes. + /// @param [in] str The string object to compare with. + /// @returns Whether or not they are equal. bool operator==(T* str) const { if (size != Len(str)) @@ -535,7 +539,7 @@ namespace ehs return size; } - /// Finds a null terminator in the string and makes it the exact size if greater than. + /// Finds the null terminator in the string and makes it the exact size if greater than. void ExactSize() { size = Len(data); @@ -594,6 +598,9 @@ namespace ehs Util::Copy(data[dstOffset], src, srcSize * sizeof(T)); } + /// Inserts a string at a specified index. + /// @param [in] index The index to insert the string at. + /// @param [in] value The string to insert. void Insert(const N index, const Str& value) { if (!value.size) @@ -616,6 +623,9 @@ namespace ehs size = newSize; } + /// Inserts a character at a specified index. + /// @param [in] index The index to insert the character at. + /// @param [in] value The character to insert. void Insert(const N index, const T value) { N newSize = 0; @@ -641,6 +651,10 @@ namespace ehs size = newSize; } + /// Removes characters withing the given range. + /// @param [in] start The index to start. + /// @param [in] end The index to end. + /// @returns The removed string object. Str Remove(const N start, const N end) { if (!size || start >= size || end > size || end <= start) @@ -664,6 +678,9 @@ namespace ehs return popped; } + /// Removes a character at the given index. + /// @param [in] index The index to remove a character. + /// @returns The character removed. T Remove(const N index) { T popped = {}; @@ -691,7 +708,6 @@ namespace ehs /// Adds a value at the end of the string. /// @param [in] value The character to push to the end of the string. - /// @note Automatically moves the null terminator after the value is pushed. void Push(const Str &value) { T* result = new T[size + value.size + 1]; @@ -709,7 +725,6 @@ namespace ehs /// Adds a value at the end of the string. /// @param [in] value The C-style string to push to the end of the string. /// @param [in] size The size of the given C-style string. - /// @note Automatically moves the null terminator after the value is pushed. void Push(const T* const value, const N size) { T* result = new T[this->size + size + 1]; @@ -726,7 +741,6 @@ namespace ehs /// Adds a value at the end of the string. /// @param [in] value The C-style string to push to the end of the string. - /// @note Automatically moves the null terminator after the value is pushed. void Push(const T* const value) { N inSize = Len(value); @@ -745,7 +759,6 @@ namespace ehs /// Adds a value at the end of the string. /// @param [in] value The character to push to the end of the string. - /// @note Automatically moves the null terminator after the value is pushed. void Push(const T value) { T* result = new T[size + 2]; @@ -761,7 +774,7 @@ namespace ehs ++size; } - /// Removes a value at the end of the array. + /// Removes the value at the end of the array. /// @returns The value that was popped. T Pop() { @@ -794,7 +807,7 @@ namespace ehs return (Byte*)data; } - /// Changes all upper-case ASCII characters to lower-case. + /// Converts all upper-case ASCII characters to lower-case. void ToLower() { for (N i = 0; i < size; ++i) @@ -802,8 +815,8 @@ namespace ehs data[i] += 32; } - /// Changes all upper-case ASCII characters to lower-case. - /// @returns The result. + /// Converts all upper-case ASCII characters to lower-case. + /// @returns The resulting string object. Str GetLower() const { Str result(size); @@ -817,7 +830,7 @@ namespace ehs return result; } - /// Changes all lower-case ASCII characters to upper-case. + /// Converts all lower-case ASCII characters to upper-case. void ToUpper() { for (N i = 0; i < size; ++i) @@ -825,8 +838,8 @@ namespace ehs data[i] -= 32; } - /// Changes all lower-case ASCII characters to upper-case. - /// @returns The result. + /// Converts all lower-case ASCII characters to upper-case. + /// @returns The resulting string object. Str GetUpper() const { Str result(size); @@ -840,7 +853,7 @@ namespace ehs return result; } - /// Reverses the entire referenced string object. + /// Reverses the entire string object. void Reverse() { if (size <= 1 || !data) @@ -859,7 +872,7 @@ namespace ehs } /// Reverses the entire string object. - /// @returns The result. + /// @returns The resulting string object. Str GetReverse() { if (size <= 1 || !data) @@ -876,7 +889,7 @@ namespace ehs /// Clips the string at the given index and with the given size. /// @param [in] index The index to clip at. /// @param [in] size The size for the clip starting from the index. - /// @returns The result. + /// @returns The resulting string object. Str Sub(const N index, const N size = 0) const { if (index >= this->size) @@ -911,10 +924,10 @@ namespace ehs } } - /// Splits a string into an array 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] max The max amount of times to split the string. - /// @returns The result. + /// @returns The resulting string object. Vector, N> Split(const Str& ide, const N max = 0) const { Vector, N> result(0, 5); @@ -952,9 +965,9 @@ namespace ehs return result; } - /// Removes all instances of the given string object. + /// Removes all instances of the ide. /// @param [in] ide The string to look for. - /// @returns The result. + /// @returns The resulting string object. Str RemoveAll(const Str& ide) const { Str result(size); @@ -981,6 +994,10 @@ namespace ehs return result; } + /// Replaces all instances of ide with the replacer. + /// @param [in] ide The string to look for. + /// @param [in] replacer The string placed. + /// @returns The resulting string object. Str ReplaceAll(const Str& ide, const Str& replacer) const { Str result; @@ -1068,6 +1085,10 @@ namespace ehs return false; } + /// Checks if the current string contains the given ide. + /// @param [in] ide The given ide to check for. + /// @param [in] pattern The search pattern to use. + /// @returns True if the current string does contain the ide. bool Contains(const Str& ide, const SearchPattern pattern = SearchPattern::LEFT_RIGHT) const { if (pattern == SearchPattern::LEFT_RIGHT) @@ -1117,6 +1138,10 @@ namespace ehs return true; } + /// Converts a number into hexadecimal string representation. + /// @tparam I The data type of the number given. + /// @param [in] num The number to convert. + /// @returns The resulting hexadecimal. template static Str NumToHex(const I num) { @@ -1133,6 +1158,10 @@ namespace ehs return result; } + /// Converts a string hexadecimal into a number. + /// @tparam I The data type of the number outputted. + /// @param [in] in The string to convert. + /// @returns The resulting number. template static I HexToNum(const Str& in) { @@ -1173,6 +1202,9 @@ namespace ehs return neg ? -acc : acc; } + /// Converts the current string from hexadecimal into a number. + /// @tparam I The data type of the number outputted. + /// @returns The resulting number. template I HexToNum() const { @@ -1216,7 +1248,7 @@ namespace ehs /// Converts the string into a number. /// @tparam I The resulting number's data type. /// @returns The result. - /// @note Use "IsNum" before this if the referenced string object will not always be a number. + /// @note Use "IsNum" before this if the string object is not guaranteed to be a number. template I ToDecimal() const { @@ -1234,6 +1266,9 @@ namespace ehs return r; } + /// Converts the string into a floating point number. + /// @returns The resulting float. + /// @note Use "IsNum" before this if the string object is not guaranteed to be a number. float ToFloat() const { N decPoint = size; @@ -1257,6 +1292,9 @@ namespace ehs return result; } + /// Converts the string into a double floating point number. + /// @returns The resulting double. + /// @note Use "IsNum" before this if the string object is not guaranteed to be a number. double ToDouble() const { N decPoint = size; @@ -1280,6 +1318,9 @@ namespace ehs return result; } + /// Converts the string into a long double floating point number. + /// @returns The resulting long double. + /// @note Use "IsNum" before this if the string object is not guaranteed to be a number. long double ToLDouble() const { N decPoint = size; @@ -1304,7 +1345,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const SInt_64 num) { if (num == 0) @@ -1339,7 +1381,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const UInt_64 num) { if (num == 0) @@ -1367,7 +1410,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const SInt_32 num) { if (num == 0) @@ -1402,7 +1446,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const UInt_32 num) { if (num == 0) @@ -1430,7 +1475,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const SInt_16 num) { if (num == 0) @@ -1465,7 +1511,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const UInt_16 num) { if (num == 0) @@ -1493,7 +1540,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const SInt_8 num) { if (num == 0) @@ -1528,7 +1576,8 @@ namespace ehs } /// Converts the given number into a string. - /// @returns The result. + /// @param [in] num The given number to convert. + /// @returns The resulting string representation. static Str FromNum(const UInt_8 num) { if (num == 0) @@ -1620,8 +1669,10 @@ namespace ehs } #endif - /// Converts the given float into a string. - /// @returns The result. + /// Converts the given floating point into a string. + /// @param [in] num The given floating point to convert. + /// @param [in] maxDecimals The max decimal places to add. + /// @returns The resulting string representation. static Str FromNum(const float num, const UInt_8 maxDecimals = 5) { SInt_64 whole = (SInt_64)num; @@ -1660,8 +1711,10 @@ namespace ehs return result; } - /// Converts the given double into a string. - /// @returns The result. + /// Converts the given double floating point into a string. + /// @param [in] num The given double floating point to convert. + /// @param [in] maxDecimals The max decimal places to add. + /// @returns The resulting string representation. static Str FromNum(const double num, const UInt_8 maxDecimals = 5) { SInt_64 whole = (SInt_64)num; @@ -1700,8 +1753,10 @@ namespace ehs return result; } - /// Converts the given long double into a string. - /// @returns The result. + /// Converts the given long double floating point into a string. + /// @param [in] num The given long double floating point to convert. + /// @param [in] maxDecimals The max decimal places to add. + /// @returns The resulting string representation. static Str FromNum(const long double num, const UInt_8 maxDecimals = 5) { SInt_64 whole = (SInt_64)num; @@ -1742,7 +1797,7 @@ namespace ehs /// A 32-bit FNV-1a hash algorithm. /// @param [in] str The string to hash. - /// @returns The resulting hash. + /// @returns The resulting hash. Zero if string does not contain any characters. static UInt_32 Hash_32(const Str& str) { if (!str.Size()) @@ -1758,6 +1813,8 @@ namespace ehs return hash; } + /// A 32-bit FNV-1a hash algorithm. + /// @returns The resulting hash. Zero if string does not contain any characters. UInt_32 Hash_32() const { if (!size) @@ -1775,7 +1832,7 @@ namespace ehs /// A 64-bit FNV-1a hash algorithm. /// @param [in] str The string to hash. - /// @returns The resulting hash. + /// @returns The resulting hash. Zero if string does not contain any characters. static UInt_64 Hash_64(const Str& str) { if (!str.Size()) @@ -1791,6 +1848,8 @@ namespace ehs return hash; } + /// A 64-bit FNV-1a hash algorithm. + /// @returns The resulting hash. Zero if string does not contain any characters. UInt_64 Hash_64() const { if (!size) @@ -1806,6 +1865,9 @@ namespace ehs return hash; } + /// Calculates the length of a C-Style string. + /// @param [in] str The C-Style string to calculate. + /// @returns The character count. static N Len(const T* const str) { N count = 0; @@ -1814,6 +1876,10 @@ namespace ehs return count; } + /// Compares two C-style string with each other. + /// @param [in] a The first C-style string to compare. + /// @param [in] b The second C-style string to compare. + /// @returns True if both C-style strings are equal. static bool Cmp(const T* const a, const T* const b) { N aSize = Len(a);