From 69ab5d70797aa5be549e4136ab31a798d752a05a Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 22 Jan 2023 17:54:24 -0500 Subject: [PATCH] Added some std::string functions from c++ 20 --- include/blt/std/map.h | 2 +- include/blt/std/string.h | 25 +++++++++++++++++++++++++ src/tests/main.cpp | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/blt/std/map.h b/include/blt/std/map.h index 5f96993..1b9a17e 100644 --- a/include/blt/std/map.h +++ b/include/blt/std/map.h @@ -9,7 +9,7 @@ template> class hashmap { - + }; #endif //BLT_TESTS_MAP_H diff --git a/include/blt/std/string.h b/include/blt/std/string.h index d7b2950..1056438 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -13,6 +13,31 @@ #include namespace blt::String { + + static inline bool starts_with(const std::string& string, const std::string& search){ + if (search.length() > string.length()) + return false; + auto chars = string.c_str(); + auto search_chars = search.c_str(); + for (int i = 0; i < search.length(); i++){ + if (chars[i] != search_chars[i]) + return false; + } + return true; + } + + static inline bool ends_with(const std::string& string, const std::string& search){ + if (search.length() > string.length()) + return false; + auto chars = string.c_str(); + auto search_chars = search.c_str(); + auto startPosition = string.length() - search.length(); + for (int i = 0; i < search.length(); i++){ + if (chars[startPosition + i] != search_chars[i]) + return false; + } + return true; + } /** * Converts the string into lower case * @param s string to lower case diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 749b61c..1002e5e 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -1,6 +1,10 @@ #include "binary_trees.h" +#include "blt/std/string.h" int main() { binaryTreeTest(); + + std::string hello = "superSexyMax"; + std::cout << "String starts with: " << blt::String::ends_with(hello, "Max") << "\n"; } \ No newline at end of file