AuROXTL/Include/auROXTL/auStringUtils.hpp
Reece cb3b1a582b [*] AuArraySize should return a uint
[*] AuReplaceAll should return a ref equal to arg 0
2022-06-14 17:21:10 +01:00

124 lines
3.1 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auStringUtils.hpp
Date: 2022-2-1
File: AuroraUtils.hpp
File: auROXTLUtils.hpp
Date: 2021-6-9
Author: Reece
***/
#pragma once
static auline bool AuStringContains(const AuString &value, const AuString &subpattern)
{
return value.find(subpattern) != AuString::npos;
}
static auline bool AuEndsWith(AuString const &value, AuString const &ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
static auline bool AuStartsWith(AuString const &value, AuString const &starting)
{
#if defined(AU_STRING_IS_TINYUTF_EXPERIMENT)
return value.starts_with(starting);
#else
return value.rfind(starting, 0) == 0;
#endif
}
#if defined(AU_STRING_IS_TINYUTF_EXPERIMENT)
static AuString AuStringTransform(const AuString &in, const AuSupplierConsumer<AuUInt32, AuUInt32> &out)
{
AuString cpy = in;
for (int i = 0; i < cpy.length(); i++)
{
cpy[i] = out(cpy[i]);
}
return cpy;
}
static auline AuString AuToUpper(const AuString &in)
{
return AuStringTransform(in, std::toupper);
}
static auline AuString AuToLower(const AuString &in)
{
return AuStringTransform(in, std::toupper);
}
#else
template <class T>
static auline AuString AuToStringASCIIOp(T op, const AuString &in)
{
AuString ret;
ret.resize(in.size());
std::transform(in.begin(), in.end(), ret.begin(), [=](const char &c)
{
return op(c);
});
return ret;
}
static auline AuString AuToLower(const AuString &in)
{
return AuToStringASCIIOp<int(*)(int)>(std::tolower, in);
}
static auline AuString AuToUpper(const AuString &in)
{
return AuToStringASCIIOp<int(*)(int)>(std::toupper, in);
}
#endif
static auline AuString &AuReplaceAll(AuString &str, const AuString &from, const AuString &to)
{
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(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
// i told myself not to copy this, required a split function twice, now here we are :D
static auline AuList<AuString> AuSplitString(const AuString &str, const AuString &delim, bool ignoreEmpty = true)
{
AuList<AuString> tokens;
AuUInt prev = 0, pos = 0;
tokens.reserve(str.size() / 16);
do
{
pos = str.find(delim, prev);
if (pos == AuString::npos) pos = str.length();
auto token = str.substr(prev, pos - prev);
if ((!token.empty()) && ignoreEmpty) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
#if !defined(AURORA_RUNTIME_TO_STRING)
#define AURORA_RUNTIME_TO_STRING std::to_string
#endif
template <class T>
static auline AuString AuToString(const T &obj)
{
#if defined(_AUHAS_FMT)
// locale independent and better optimized!
return fmt::format("{}", obj);
#else
// TODO: to_chars (locale independent)
return AURORA_RUNTIME_TO_STRING(obj);
#endif
}