diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a98d4f..e437735 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) set(BLT_VERSION 0.16.6) diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 43ea9c9..25e688a 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -91,22 +91,7 @@ namespace blt::string #endif } - static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, std::string_view search) - { -#ifdef BLT_USE_CPP20 - return string.ends_with(search); -#else - if (search.length() > string.length()) - return false; - auto startPosition = string.length() - search.length(); - for (unsigned int i = 0; i < search.length(); i++) - { - if (string[startPosition + i] != search[i]) - return false; - } - return true; -#endif - } + static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, std::string_view search); static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, char search) { @@ -119,166 +104,38 @@ namespace blt::string #endif } - static inline std::optional> containsAll(std::string_view string, const std::unordered_set& search) - { - std::vector pos; - for (size_t i = 0; i < string.length(); i++) - { - if (BLT_CONTAINS(search, string[i])) - pos.push_back(i); - } - if (!pos.empty()) - return pos; - return {}; - } + std::optional> containsAll(std::string_view string, const std::unordered_set& search); - static inline size_t contains(std::string_view string, const std::unordered_set& search) - { - for (size_t i = 0; i < string.length(); i++) - { - if (BLT_CONTAINS(search, string[i])) - return i; - } - return false; - } + size_t contains(std::string_view string, const std::unordered_set& search); - static inline BLT_CPP20_CONSTEXPR bool contains(std::string_view string, const char search) - { -#if __cplusplus >= 202002L - return std::ranges::any_of(string, [search](const char c) -> bool { - return c == search; - }); -#else - for (const char c : string) - { - if (c == search) - return true; - } - return false; -#endif - } - static inline BLT_CPP20_CONSTEXPR bool contains(std::string_view string, std::string_view search) - { - if (search.length() > string.length()) - return false; - for (unsigned int i = 0; i < string.length(); i++) - { - if (string[i] == search[0]) - { - bool correct = true; - for (unsigned int j = 0; j < search.length(); j++) - { - if (string[i + j] != search[j]) - { - correct = false; - break; - } - } - if (correct) - return true; - } - } - return false; - } + BLT_CPP20_CONSTEXPR bool contains(std::string_view string, char search); + + BLT_CPP20_CONSTEXPR bool contains(std::string_view string, std::string_view search); /** * Converts the string into lower case * @param s string to lower case * @return a string copy that is all lower case */ - static inline BLT_CPP20_CONSTEXPR std::string toLowerCase(std::string_view s) - { - std::string str; - std::for_each( - s.begin(), s.end(), [&str](unsigned char ch) { - str += (char) std::tolower(ch); - } - ); - return str; - } + BLT_CPP20_CONSTEXPR std::string toLowerCase(std::string_view s); /** * Converts the string into upper case * @param s string to upper case * @return a string copy that is all upper case */ - static inline BLT_CPP20_CONSTEXPR std::string toUpperCase(std::string_view s) - { - std::string str; - std::for_each( - s.begin(), s.end(), [&str](unsigned char ch) { - str += (char) std::toupper(ch); - } - ); - return str; - } + BLT_CPP20_CONSTEXPR std::string toUpperCase(std::string_view s); // taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c // extended to return a vector - static inline BLT_CPP20_CONSTEXPR std::vector split(std::string_view s, std::string_view delim) - { - size_t pos = 0; - size_t from = 0; - std::vector tokens; - while ((pos = s.find(delim, from)) != std::string::npos) - { - auto size = pos - from; - auto token = s.substr(from, size); - tokens.emplace_back(token); - from += size + delim.length(); - } - tokens.emplace_back(s.substr(from)); - return tokens; - } - - static inline BLT_CPP20_CONSTEXPR std::vector split(std::string_view s, char delim) - { - size_t pos = 0; - size_t from = 0; - std::vector tokens; - while ((pos = s.find(delim, from)) != std::string::npos) - { - auto size = pos - from; - auto token = s.substr(from, size); - tokens.emplace_back(token); - from += size + 1; - } - tokens.emplace_back(s.substr(from)); - return tokens; - } + BLT_CPP20_CONSTEXPR std::vector split(std::string_view s, std::string_view delim); - static inline BLT_CPP20_CONSTEXPR std::vector split_sv(std::string_view s, std::string_view delim) - { - size_t pos = 0; - size_t from = 0; - std::vector tokens; - while ((pos = s.find(delim, from)) != std::string::npos) - { - auto size = pos - from; - auto token = s.substr(from, size); - tokens.push_back(token); - from += size + delim.length(); - } - tokens.push_back(s.substr(from)); - return tokens; - } + BLT_CPP20_CONSTEXPR std::vector split(std::string_view s, char delim); - static inline BLT_CPP20_CONSTEXPR std::vector split_sv(std::string_view s, char delim) - { - size_t pos = 0; - size_t from = 0; - std::vector tokens; - while ((pos = s.find(delim, from)) != std::string::npos) - { - auto size = pos - from; - auto token = s.substr(from, size); - tokens.push_back(token); - from += size + 1; - } - tokens.push_back(s.substr(from)); - return tokens; - } + BLT_CPP20_CONSTEXPR std::vector split_sv(std::string_view s, std::string_view delim); + + BLT_CPP20_CONSTEXPR std::vector split_sv(std::string_view s, char delim); // https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string static inline BLT_CPP20_CONSTEXPR bool replace(std::string& str, std::string_view from, std::string_view to) @@ -290,17 +147,7 @@ namespace blt::string return true; } - static inline BLT_CPP20_CONSTEXPR void replaceAll(std::string& str, std::string_view from, std::string_view to) - { - if (from.empty()) - return; - size_t start_pos = 0; - while ((start_pos = str.find(from, start_pos)) != std::string::npos) - { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' - } - } + BLT_CPP20_CONSTEXPR void replaceAll(std::string& str, std::string_view from, std::string_view to); // taken from https://stackoverflow.com/questions/216823/how-to-trim-an-stdstring // would've preferred to use boost lib but instructions said to avoid external libs @@ -396,6 +243,14 @@ namespace blt::string #endif } + class match + { + private: + + public: + + }; + } #endif //BLT_STRING_H diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 7ef2e73..67c2461 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 +Subproject commit 67c24619e4f5ab2097b74cc397732c17a25d6944 diff --git a/src/blt/std/string.cpp b/src/blt/std/string.cpp index 2ce5dd7..c8c125c 100644 --- a/src/blt/std/string.cpp +++ b/src/blt/std/string.cpp @@ -3,27 +3,199 @@ // #include -void blt::string::StringBuffer::expand() { - size_t multiplier = size / BLOCK_SIZE; - auto newSize = BLOCK_SIZE * (multiplier * 2); - characterBuffer = static_cast(realloc(characterBuffer, newSize)); - size = newSize; -} - -void blt::string::StringBuffer::trim() { - characterBuffer = static_cast(realloc(characterBuffer, front+1)); - size = front+1; - characterBuffer[front] = '\0'; -} - -blt::string::StringBuffer& blt::string::StringBuffer::operator<<(char c) { - characterBuffer[front++] = c; - if (front > size) - expand(); - return *this; -} - -std::string blt::string::StringBuffer::str() { - trim(); - return std::string{characterBuffer}; +namespace blt +{ + + void blt::string::StringBuffer::expand() + { + size_t multiplier = size / BLOCK_SIZE; + auto newSize = BLOCK_SIZE * (multiplier * 2); + characterBuffer = static_cast(realloc(characterBuffer, newSize)); + size = newSize; + } + + void blt::string::StringBuffer::trim() + { + characterBuffer = static_cast(realloc(characterBuffer, front + 1)); + size = front + 1; + characterBuffer[front] = '\0'; + } + + blt::string::StringBuffer& blt::string::StringBuffer::operator<<(char c) + { + characterBuffer[front++] = c; + if (front > size) + expand(); + return *this; + } + + std::string blt::string::StringBuffer::str() + { + trim(); + return std::string{characterBuffer}; + } + + + bool string::contains(std::string_view string, std::string_view search) + { + if (search.length() > string.length()) + return false; + for (unsigned int i = 0; i < string.length(); i++) + { + if (string[i] == search[0]) + { + bool correct = true; + for (unsigned int j = 0; j < search.length(); j++) + { + if (string[i + j] != search[j]) + { + correct = false; + break; + } + } + if (correct) + return true; + } + } + return false; + } + + std::string string::toLowerCase(std::string_view s) + { + std::string str; + std::for_each( + s.begin(), s.end(), [&str](unsigned char ch) { + str += (char) std::tolower(ch); + } + ); + return str; + } + + bool string::contains(std::string_view string, const char search) + { +#if __cplusplus >= 202002L + return std::ranges::any_of(string, [search](const char c) -> bool { + return c == search; + }); +#else + for (const char c : string) + { + if (c == search) + return true; + } + return false; +#endif + } + + size_t string::contains(std::string_view string, const std::unordered_set& search) + { + for (size_t i = 0; i < string.length(); i++) + { + if (BLT_CONTAINS(search, string[i])) + return i; + } + return false; + } + + std::optional> string::containsAll(std::string_view string, const std::unordered_set& search) + { + std::vector pos; + for (size_t i = 0; i < string.length(); i++) + { + if (BLT_CONTAINS(search, string[i])) + pos.push_back(i); + } + if (!pos.empty()) + return pos; + return {}; + } + + std::string string::toUpperCase(std::string_view s) + { + std::string str; + std::for_each( + s.begin(), s.end(), [&str](unsigned char ch) { + str += (char) std::toupper(ch); + } + ); + return str; + } + + std::vector string::split(std::string_view s, std::string_view delim) + { + size_t pos = 0; + size_t from = 0; + std::vector tokens; + while ((pos = s.find(delim, from)) != std::string::npos) + { + auto size = pos - from; + auto token = s.substr(from, size); + tokens.emplace_back(token); + from += size + delim.length(); + } + tokens.emplace_back(s.substr(from)); + return tokens; + } + + std::vector string::split(std::string_view s, char delim) + { + size_t pos = 0; + size_t from = 0; + std::vector tokens; + while ((pos = s.find(delim, from)) != std::string::npos) + { + auto size = pos - from; + auto token = s.substr(from, size); + tokens.emplace_back(token); + from += size + 1; + } + tokens.emplace_back(s.substr(from)); + return tokens; + } + + std::vector string::split_sv(std::string_view s, std::string_view delim) + { + size_t pos = 0; + size_t from = 0; + std::vector tokens; + while ((pos = s.find(delim, from)) != std::string::npos) + { + auto size = pos - from; + auto token = s.substr(from, size); + tokens.push_back(token); + from += size + delim.length(); + } + tokens.push_back(s.substr(from)); + return tokens; + } + + std::vector string::split_sv(std::string_view s, char delim) + { + size_t pos = 0; + size_t from = 0; + std::vector tokens; + while ((pos = s.find(delim, from)) != std::string::npos) + { + auto size = pos - from; + auto token = s.substr(from, size); + tokens.push_back(token); + from += size + 1; + } + tokens.push_back(s.substr(from)); + return tokens; + } + + void string::replaceAll(std::string& str, std::string_view from, std::string_view to) + { + if (from.empty()) + return; + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) + { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' + } + } + + }