Merge remote-tracking branch 'github/main'
commit
aa97660877
|
@ -1,4 +1,4 @@
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
|
|
||||||
set(BLT_VERSION 0.16.6)
|
set(BLT_VERSION 0.16.6)
|
||||||
|
|
|
@ -91,22 +91,7 @@ namespace blt::string
|
||||||
#endif
|
#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, 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, char search)
|
static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, char search)
|
||||||
{
|
{
|
||||||
|
@ -119,166 +104,38 @@ namespace blt::string
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline std::optional<std::vector<size_t>> containsAll(std::string_view string, const std::unordered_set<char>& search)
|
std::optional<std::vector<size_t>> containsAll(std::string_view string, const std::unordered_set<char>& search);
|
||||||
{
|
|
||||||
std::vector<size_t> 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 {};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline size_t contains(std::string_view string, const std::unordered_set<char>& search)
|
size_t contains(std::string_view string, const std::unordered_set<char>& search);
|
||||||
{
|
|
||||||
for (size_t i = 0; i < string.length(); i++)
|
|
||||||
{
|
|
||||||
if (BLT_CONTAINS(search, string[i]))
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
BLT_CPP20_CONSTEXPR bool contains(std::string_view string, char search);
|
||||||
{
|
|
||||||
if (search.length() > string.length())
|
BLT_CPP20_CONSTEXPR bool contains(std::string_view string, std::string_view search);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the string into lower case
|
* Converts the string into lower case
|
||||||
* @param s string to lower case
|
* @param s string to lower case
|
||||||
* @return a string copy that is all lower case
|
* @return a string copy that is all lower case
|
||||||
*/
|
*/
|
||||||
static inline BLT_CPP20_CONSTEXPR std::string toLowerCase(std::string_view s)
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the string into upper case
|
* Converts the string into upper case
|
||||||
* @param s string to upper case
|
* @param s string to upper case
|
||||||
* @return a string copy that is all upper case
|
* @return a string copy that is all upper case
|
||||||
*/
|
*/
|
||||||
static inline BLT_CPP20_CONSTEXPR std::string toUpperCase(std::string_view s)
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
|
// taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
|
||||||
// extended to return a vector
|
// extended to return a vector
|
||||||
static inline BLT_CPP20_CONSTEXPR std::vector<std::string> split(std::string_view s, std::string_view delim)
|
BLT_CPP20_CONSTEXPR std::vector<std::string> split(std::string_view s, std::string_view delim);
|
||||||
{
|
|
||||||
size_t pos = 0;
|
|
||||||
size_t from = 0;
|
|
||||||
std::vector<std::string> 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<std::string> split(std::string_view s, char delim)
|
BLT_CPP20_CONSTEXPR std::vector<std::string> split(std::string_view s, char delim);
|
||||||
{
|
|
||||||
size_t pos = 0;
|
|
||||||
size_t from = 0;
|
|
||||||
std::vector<std::string> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline BLT_CPP20_CONSTEXPR std::vector<std::string_view> split_sv(std::string_view s, std::string_view delim)
|
BLT_CPP20_CONSTEXPR std::vector<std::string_view> split_sv(std::string_view s, std::string_view delim);
|
||||||
{
|
|
||||||
size_t pos = 0;
|
|
||||||
size_t from = 0;
|
|
||||||
std::vector<std::string_view> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline BLT_CPP20_CONSTEXPR std::vector<std::string_view> split_sv(std::string_view s, char delim)
|
BLT_CPP20_CONSTEXPR std::vector<std::string_view> split_sv(std::string_view s, char delim);
|
||||||
{
|
|
||||||
size_t pos = 0;
|
|
||||||
size_t from = 0;
|
|
||||||
std::vector<std::string_view> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
|
// 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)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline BLT_CPP20_CONSTEXPR void replaceAll(std::string& str, std::string_view from, std::string_view to)
|
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'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// taken from https://stackoverflow.com/questions/216823/how-to-trim-an-stdstring
|
// 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
|
// would've preferred to use boost lib but instructions said to avoid external libs
|
||||||
|
@ -396,6 +243,14 @@ namespace blt::string
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class match
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_STRING_H
|
#endif //BLT_STRING_H
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5
|
Subproject commit 67c24619e4f5ab2097b74cc397732c17a25d6944
|
|
@ -3,27 +3,199 @@
|
||||||
//
|
//
|
||||||
#include <blt/std/string.h>
|
#include <blt/std/string.h>
|
||||||
|
|
||||||
void blt::string::StringBuffer::expand() {
|
namespace blt
|
||||||
|
{
|
||||||
|
|
||||||
|
void blt::string::StringBuffer::expand()
|
||||||
|
{
|
||||||
size_t multiplier = size / BLOCK_SIZE;
|
size_t multiplier = size / BLOCK_SIZE;
|
||||||
auto newSize = BLOCK_SIZE * (multiplier * 2);
|
auto newSize = BLOCK_SIZE * (multiplier * 2);
|
||||||
characterBuffer = static_cast<char*>(realloc(characterBuffer, newSize));
|
characterBuffer = static_cast<char*>(realloc(characterBuffer, newSize));
|
||||||
size = newSize;
|
size = newSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void blt::string::StringBuffer::trim() {
|
void blt::string::StringBuffer::trim()
|
||||||
characterBuffer = static_cast<char*>(realloc(characterBuffer, front+1));
|
{
|
||||||
size = front+1;
|
characterBuffer = static_cast<char*>(realloc(characterBuffer, front + 1));
|
||||||
|
size = front + 1;
|
||||||
characterBuffer[front] = '\0';
|
characterBuffer[front] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
blt::string::StringBuffer& blt::string::StringBuffer::operator<<(char c) {
|
blt::string::StringBuffer& blt::string::StringBuffer::operator<<(char c)
|
||||||
|
{
|
||||||
characterBuffer[front++] = c;
|
characterBuffer[front++] = c;
|
||||||
if (front > size)
|
if (front > size)
|
||||||
expand();
|
expand();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string blt::string::StringBuffer::str() {
|
std::string blt::string::StringBuffer::str()
|
||||||
|
{
|
||||||
trim();
|
trim();
|
||||||
return std::string{characterBuffer};
|
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<char>& search)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < string.length(); i++)
|
||||||
|
{
|
||||||
|
if (BLT_CONTAINS(search, string[i]))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<size_t>> string::containsAll(std::string_view string, const std::unordered_set<char>& search)
|
||||||
|
{
|
||||||
|
std::vector<size_t> 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<std::string> string::split(std::string_view s, std::string_view delim)
|
||||||
|
{
|
||||||
|
size_t pos = 0;
|
||||||
|
size_t from = 0;
|
||||||
|
std::vector<std::string> 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<std::string> string::split(std::string_view s, char delim)
|
||||||
|
{
|
||||||
|
size_t pos = 0;
|
||||||
|
size_t from = 0;
|
||||||
|
std::vector<std::string> 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<std::string_view> string::split_sv(std::string_view s, std::string_view delim)
|
||||||
|
{
|
||||||
|
size_t pos = 0;
|
||||||
|
size_t from = 0;
|
||||||
|
std::vector<std::string_view> 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<std::string_view> string::split_sv(std::string_view s, char delim)
|
||||||
|
{
|
||||||
|
size_t pos = 0;
|
||||||
|
size_t from = 0;
|
||||||
|
std::vector<std::string_view> 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'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue