Added some std::string functions from c++ 20

v1
Brett 2023-01-22 17:54:24 -05:00
parent 12ec6a9334
commit 69ab5d7079
3 changed files with 30 additions and 1 deletions

View File

@ -13,6 +13,31 @@
#include <vector> #include <vector>
namespace blt::String { 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 * Converts the string into lower case
* @param s string to lower case * @param s string to lower case

View File

@ -1,6 +1,10 @@
#include "binary_trees.h" #include "binary_trees.h"
#include "blt/std/string.h"
int main() { int main() {
binaryTreeTest(); binaryTreeTest();
std::string hello = "superSexyMax";
std::cout << "String starts with: " << blt::String::ends_with(hello, "Max") << "\n";
} }