3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_STRINGS_H
7#define ELEMENTA_BASE_STRINGS_H
67struct is_charlike<char> {
static constexpr bool value =
true; };
71struct is_charlike<wchar_t> {
static constexpr bool value =
true; };
75struct is_charlike<char16_t> {
static constexpr bool value =
true; };
79struct is_charlike<char32_t> {
static constexpr bool value =
true; };
91template <
typename... T>
100template <
typename T,
class Enable =
void>
117 typename T::value_type,
118 typename T::traits_type,
119 typename T::allocator_type
124 typename T::value_type,
125 typename T::traits_type,
126 typename T::allocator_type
153 {
return(((c >=
'a') && (c <=
'f')) ||
154 ((c >=
'A') && (c <=
'F')) ||
171 bool onlyint =
false);
175 {
return((c ==
'\r') || (c ==
'\n')); }
179 {
return((c ==
'\t') || (c ==
' ')); }
208template <
typename NumType>
211 static_assert(std::is_integral<NumType>::value,
212 "to_string_dec only works with integral types");
214 std::stringstream stream;
217 stream << std::setw(std::numeric_limits<NumType>::digits10 + 1);
218 stream << std::setfill(
'0');
221 return(stream.str());
228std::string to_string_dec<>(uint8_t n,
bool padding);
233template <
typename NumType>
236 static_assert(std::is_integral<NumType>::value,
237 "to_string_hex only works with integral types");
239 std::stringstream stream;
241 stream << std::hex <<
242 std::setw(
sizeof(NumType)*2) << std::setfill(
'0') <<
245 else stream << std::hex << static_cast<LongestUnsigned>(n);
246 return(stream.str());
252template <
typename NumType>
255 static_assert(std::is_integral<NumType>::value,
256 "to_string_bin only works with integral types");
258 const std::string result = std::bitset<sizeof(NumType)*8>(n).to_string();
259 if (padding)
return(result);
260 return(result.substr(result.find(
"1", 0)));
266template <
typename NumType>
269 static_assert(std::is_integral<NumType>::value,
270 "to_string_oct only works with integral types");
272 std::stringstream stream;
274 stream << std::oct <<
275 std::setw(
sizeof(NumType)*2) << std::setfill(
'0') <<
277 else stream << std::oct << static_cast<LongestUnsigned>(n);
278 return(stream.str());
285template <
typename NumType>
288 static_assert(std::is_unsigned<NumType>::value,
289 "from_string_hex only works with integral unsigned types");
294 for (
int f =
static_cast<int>(txt.size() - 1); f >= 0; --f)
296 if (std::isdigit(txt[f])) c = txt[f] -
'0';
297 else if (std::isalpha(txt[f]))
299 if (std::isupper(txt[f])) c = txt[f] -
'A' + 10;
300 else c = txt[f] -
'a' + 10;
314template <
typename NumType>
317 static_assert(std::is_unsigned<NumType>::value,
318 "from_string_bin only works with integral unsigned types");
323 for (
int f =
static_cast<int>(txt.size() - 1); f >= 0; --f)
325 if ((txt[f] ==
'0') || (txt[f] ==
'1')) c = txt[f] -
'0';
348template <
typename Po
intedType>
349std::string
hexdump(
const PointedType * data,
size_t howmany,
bool toupp =
true,
350 bool sepelems =
true)
353 if ((data !=
nullptr) && (howmany > 0))
355 const auto * bytes =
reinterpret_cast<const uint8_t *
>(data);
356 for (
size_t f = 0; f < howmany; ++f)
359 if (sepelems && (f < howmany - 1)) res.push_back(
' ');
363 std::transform(res.begin(),res.end(),res.begin(),
365 { return(std::toupper(c)); });
385std::string
toCase(
const std::string & s,
char to);
402 auto f = [](
int c) {
return(!std::isspace(c)); };
403 s.erase(s.begin(), std::find_if(s.begin(),s.end(),f) );
413inline std::string
ltrim(
const std::string & s)
427 auto f = [](
int c) {
return(!std::isspace(c)); };
428 s.erase(std::find_if(s.rbegin(),s.rend(),f).base() , s.end() );
433inline std::string
rtrim(
const std::string & s)
447inline std::string
trim(
const std::string & s)
456void pad(std::string & s,
size_t len,
457 bool leftorright =
false,
char padding =
' ');
462void chop(std::string & s,
size_t len,
char excess = 0x00,
463 bool leftorright =
false,
char padding =
' ');
478std::vector<std::string>
split(
const std::string & s,
char delim);
489 const std::string & s2,
490 const std::string & m =
". ");
542 bool atstarttoo =
false,
543 std::string::size_type maxlinelength = 80,
544 const std::string & newlinepreffix =
" ",
545 const std::string & newlinetext =
"\n");
567 using Base = std::vector<std::string>;
576 const std::string & beg =
"{",
577 const std::string & end =
"}")
const;
unsigned long long LongestUnsigned
Longest unsigned integral type in the target machine.
long double LongestReal
Longest real type.
std::string to_string(const std::string &preffix="", const std::string &beg="{", const std::string &end="}") const
Return a string formed by all the strings as a set.
std::string & trim_mut(std::string &s)
Remove both right and left space-like characters, directly on input string.
const std::vector< uint8_t > & bytes_for_eol(void)
Return a vector containing the bytes that form std::endl in this machine.
std::string rtrim(const std::string &s)
Remove right space-like characters, creating a new string with the result.
std::string char_to_string(char c)
Convert a char to a string of 1 char.
std::string to_string_bin(NumType n, bool padding=true)
Convert an integral number to string in binary format.
bool is_all_lower(const std::string &s) noexcept
Return TRUE if all characters in S are lowercase.
std::string hexdump(const PointedType *data, size_t howmany, bool toupp=true, bool sepelems=true)
Return a string that represents all hex codes of the pointed bytes.
std::string concatWithMiddle(const std::string &s1, const std::string &s2, const std::string &m=". ")
Concatenate two strings putting a middle one only if the second is not empty.
constexpr bool is_blank(char c)
Return TRUE if C is a tab or space character.
bool is_identifier(const std::string &s)
Return TRUE if S is a standard identifier.
constexpr bool is_hex_digit(char c) noexcept
Return TRUE if C is an hexadecimal digit, either in lower or uppercase.
std::string & ltrim_mut(std::string &s)
Remove left space-like characters, modifying the input string directly.
void chop(std::string &s, size_t len, char excess=0x00, bool leftorright=false, char padding=' ')
Chop S to a string of length LEN, padding it if necessary.
std::string real_to_string(LongestReal n)
Convert a N to string with high precision.
std::vector< std::string > split(const std::string &s, char delim)
Split a string through a given delimiter.
std::string indentlinedtext(const std::string &txt, bool atstarttoo=false, std::string::size_type maxlinelength=80, const std::string &newlinepreffix=" ", const std::string &newlinetext="\n")
Return a string after splitting it into a number of indented lines.
std::string ltrim(const std::string &s)
Remove left space-like characters, creating a new string with the result.
constexpr bool is_linechanger(char c)
Return TRUE if C is a character involved in changing to new lines of text.
bool is_all_upper(const std::string &s) noexcept
Return TRUE if all characters in S are uppercase.
std::string real_to_string_fixdec(LongestReal n, unsigned ds)
Convert N to string with a fixed number of decimal digits. */.
std::string to_string_dec(NumType n, bool padding=true)
Convert an integral number to string in decimal format.
bool is_number(const std::string &s, uint8_t base=10, bool onlyint=false)
Return TRUE if S contains a number (possibly with sign) in some base.
std::string to_string_hex(NumType n, bool padding=true)
Convert an integral number to string in hexadecimal format, without preffix.
std::string to_string_oct(NumType n, bool padding=true)
Convert an integral number to string in octal format.
std::string toCase(const std::string &s, char to)
Convert a string into upper ('U') or lower ('L') case.
void pad(std::string &s, size_t len, bool leftorright=false, char padding=' ')
Pad S with character PADDING if shorter than LEN.
NumType from_string_bin(const std::string &txt)
Return the unsigned int. value of a number expressed in binary form.
std::string & rtrim_mut(std::string &s)
Remove right space-like characters, modifying the input string directly.
constexpr bool is_separator(char c)
Return TRUE if C is a separator character.
std::string trim(const std::string &s)
Remove both right and left space-like characters, creating new string.
NumType from_string_hex(const std::string &txt)
Return the unsigned int. value of a number expressed in hexadecimal form.
A struct with a void member.
Return TRUE if type T is char, wchar_t, char16_t or char32_t.
Template class to check whether T is derived from std::basic_string.