add string numbers check

v1
Brett 2023-10-14 17:05:51 -04:00
parent fbd067e69e
commit f55e7d931b
1 changed files with 70 additions and 30 deletions

View File

@ -12,10 +12,13 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <cctype>
namespace blt::string { namespace blt::string
{
class StringBuffer { class StringBuffer
{
private: private:
const size_t BLOCK_SIZE = 4096; const size_t BLOCK_SIZE = 4096;
size_t front = 0; size_t front = 0;
@ -23,68 +26,84 @@ namespace blt::string {
char* characterBuffer = nullptr; char* characterBuffer = nullptr;
void expand(); void expand();
public: public:
void trim(); void trim();
std::string str(); std::string str();
StringBuffer(){ StringBuffer()
{
characterBuffer = static_cast<char*>(malloc(BLOCK_SIZE)); characterBuffer = static_cast<char*>(malloc(BLOCK_SIZE));
size = BLOCK_SIZE; size = BLOCK_SIZE;
} }
StringBuffer& operator<<(char c); StringBuffer& operator<<(char c);
StringBuffer& operator<<(const std::string& str) {
StringBuffer& operator<<(const std::string& str)
{
for (char c : str) for (char c : str)
*this << c; *this << c;
return *this; return *this;
} }
template<typename T> template<typename T>
inline StringBuffer& operator<<(T t) { inline StringBuffer& operator<<(T t)
{
*this << std::to_string(t); *this << std::to_string(t);
return *this; return *this;
} }
~StringBuffer() { ~StringBuffer()
{
free(characterBuffer); free(characterBuffer);
} }
}; };
static inline bool starts_with(const std::string& string, const std::string& search){ static inline bool starts_with(const std::string& string, const std::string& search)
{
if (search.length() > string.length()) if (search.length() > string.length())
return false; return false;
auto chars = string.c_str(); auto chars = string.c_str();
auto search_chars = search.c_str(); auto search_chars = search.c_str();
for (unsigned int i = 0; i < search.length(); i++){ for (unsigned int i = 0; i < search.length(); i++)
{
if (chars[i] != search_chars[i]) if (chars[i] != search_chars[i])
return false; return false;
} }
return true; return true;
} }
static inline bool ends_with(const std::string& string, const std::string& search){ static inline bool ends_with(const std::string& string, const std::string& search)
{
if (search.length() > string.length()) if (search.length() > string.length())
return false; return false;
auto chars = string.c_str(); auto chars = string.c_str();
auto search_chars = search.c_str(); auto search_chars = search.c_str();
auto startPosition = string.length() - search.length(); auto startPosition = string.length() - search.length();
for (unsigned int i = 0; i < search.length(); i++){ for (unsigned int i = 0; i < search.length(); i++)
{
if (chars[startPosition + i] != search_chars[i]) if (chars[startPosition + i] != search_chars[i])
return false; return false;
} }
return true; return true;
} }
static inline bool contains(const std::string& string, const std::string& search){ static inline bool contains(const std::string& string, const std::string& search)
{
if (search.length() > string.length()) if (search.length() > string.length())
return false; return false;
auto chars = string.c_str(); auto chars = string.c_str();
auto search_chars = search.c_str(); auto search_chars = search.c_str();
for (unsigned int i = 0; i < string.length(); i++){ for (unsigned int i = 0; i < string.length(); i++)
if (chars[i] == search_chars[0]) { {
if (chars[i] == search_chars[0])
{
bool correct = true; bool correct = true;
for (unsigned int j = 0; j < search.length(); j++) { for (unsigned int j = 0; j < search.length(); j++)
if (chars[i + j] != search_chars[j]) { {
if (chars[i + j] != search_chars[j])
{
correct = false; correct = false;
break; break;
} }
@ -95,12 +114,14 @@ namespace blt::string {
} }
return false; 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 std::string toLowerCase(const std::string& s) { static inline std::string toLowerCase(const std::string& s)
{
std::stringstream str; std::stringstream str;
std::for_each( std::for_each(
s.begin(), s.end(), [&str](unsigned char ch) { s.begin(), s.end(), [&str](unsigned char ch) {
@ -115,7 +136,8 @@ namespace blt::string {
* @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 std::string toUpperCase(const std::string& s) { static inline std::string toUpperCase(const std::string& s)
{
std::stringstream str; std::stringstream str;
std::for_each( std::for_each(
s.begin(), s.end(), [&str](unsigned char ch) { s.begin(), s.end(), [&str](unsigned char ch) {
@ -127,10 +149,12 @@ namespace blt::string {
// 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 std::vector<std::string> split(std::string s, const std::string& delim) { static inline std::vector<std::string> split(std::string s, const std::string& delim)
{
size_t pos = 0; size_t pos = 0;
std::vector<std::string> tokens; std::vector<std::string> tokens;
while ((pos = s.find(delim)) != std::string::npos) { while ((pos = s.find(delim)) != std::string::npos)
{
auto token = s.substr(0, pos); auto token = s.substr(0, pos);
tokens.push_back(token); tokens.push_back(token);
s.erase(0, pos + delim.length()); s.erase(0, pos + delim.length());
@ -140,19 +164,22 @@ namespace blt::string {
} }
// 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 bool replace(std::string& str, const std::string& from, const std::string& to) { static inline bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from); size_t start_pos = str.find(from);
if(start_pos == std::string::npos) if (start_pos == std::string::npos)
return false; return false;
str.replace(start_pos, from.length(), to); str.replace(start_pos, from.length(), to);
return true; return true;
} }
static inline void replaceAll(std::string& str, const std::string& from, const std::string& to) { static inline void replaceAll(std::string& str, const std::string& from, const std::string& to)
if(from.empty()) {
if (from.empty())
return; return;
size_t start_pos = 0; size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) { while ((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to); str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
} }
@ -162,7 +189,8 @@ namespace blt::string {
// 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
// trim from start (in place) // trim from start (in place)
static inline std::string& ltrim(std::string& s) { static inline std::string& ltrim(std::string& s)
{
s.erase( s.erase(
s.begin(), std::find_if( s.begin(), std::find_if(
s.begin(), s.end(), [](unsigned char ch) { s.begin(), s.end(), [](unsigned char ch) {
@ -173,7 +201,8 @@ namespace blt::string {
} }
// trim from end (in place) // trim from end (in place)
static inline std::string& rtrim(std::string& s) { static inline std::string& rtrim(std::string& s)
{
s.erase( s.erase(
std::find_if( std::find_if(
s.rbegin(), s.rend(), [](unsigned char ch) { s.rbegin(), s.rend(), [](unsigned char ch) {
@ -184,30 +213,41 @@ namespace blt::string {
} }
// trim from both ends (in place) // trim from both ends (in place)
static inline std::string& trim(std::string& s) { static inline std::string& trim(std::string& s)
{
ltrim(s); ltrim(s);
rtrim(s); rtrim(s);
return s; return s;
} }
// trim from start (copying) // trim from start (copying)
static inline std::string ltrim_copy(std::string s) { static inline std::string ltrim_copy(std::string s)
{
ltrim(s); ltrim(s);
return s; return s;
} }
// trim from end (copying) // trim from end (copying)
static inline std::string rtrim_copy(std::string s) { static inline std::string rtrim_copy(std::string s)
{
rtrim(s); rtrim(s);
return s; return s;
} }
// trim from both ends (copying) // trim from both ends (copying)
static inline std::string trim_copy(std::string s) { static inline std::string trim_copy(std::string s)
{
trim(s); trim(s);
return s; return s;
} }
static inline bool isNumeric(const std::string& s)
{
return std::ranges::all_of(s, [](char c) -> bool {
return std::isdigit(c);
});
}
} }
#endif //BLT_STRING_H #endif //BLT_STRING_H