10#include <kernel/util/half.hpp>
22#ifdef FEAT_COMPILER_MICROSOFT
27#ifdef FEAT_HAVE_QUADMATH
76 return left.compare_no_case(right) < 0;
94 inline String(
const char* str, size_type count)
95 : std::string(str, count)
128 inline String(
const std::string& str, size_type offset, size_type count = npos)
129 : std::string(str, offset, count)
135 : std::string(count, c)
140 String& operator=(
const std::string& str)
142 std::string::operator=(str);
147 String& operator=(std::string&& str)
149 std::string::operator=(str);
156 std::string::operator=(str);
163 std::string::operator=(str);
168 String& operator=(
const char* s)
170 std::string::operator=(s);
174 String& append(
const std::string& str)
176 std::string::append(str);
180 String& append(
const std::string& str, size_type pos, size_type n)
182 std::string::append(str, pos, n);
186 String& append(
const char* s, size_type n)
188 std::string::append(s, n);
192 String& append(
const char* s)
194 std::string::append(s);
198 String& append(size_type n,
char c)
200 std::string::append(n, c);
204 String& operator+=(
const std::string& str)
209 String& operator+=(
const char* s)
214 String substr(size_type pos = size_type(0), size_type n = npos)
const
216 return String(std::string::substr(pos, n));
225 return " \a\b\f\n\r\t\v";
236 insert(size_type(0), size_type(1),
value);
242 erase(size_type(0), size_type(1));
248 erase(size() - size_type(1), size_type(1));
265 size_type pos = find_first_not_of(charset);
297 size_type pos = find_last_not_of(charset);
301 return substr(size_type(0), pos + size_type(1));
361 return (*
this =
trim(charset));
373 return (*
this =
trim());
395 size_type l(length());
396 return (l < len) ?
String(len - l, c).append(*
this) : *
this;
418 size_type l(length());
419 return (l < len) ?
String(*this).append(len - l, c) : *
this;
433 return length() <= len ? *this : substr(length() - len);
447 return length() <= len ? *this : substr(size_type(0), len);
470 std::deque<String> words;
471 if(empty() || charset.empty())
477 size_type off1(find_first_not_of(charset));
488 size_type off2(find_first_of(charset, off1));
494 words.push_back(substr(off1));
500 words.push_back(substr(off1, (off2 == npos ? npos : off2 - off1)));
504 off1 = find_first_not_of(charset, off2);
542 std::deque<String> words;
543 if(empty() || delimiter.empty())
547 size_type off1(find(delimiter));
548 words.push_back(substr(0, off1));
553 const size_type del_len(delimiter.size());
560 size_type off2 = find(delimiter, off1);
564 words.push_back(substr(off1));
570 words.push_back(substr(off1, off2 - off1));
601 size_type flen(find_string.size());
602 size_type rlen(replace_string.size());
607 size_type pos(find(find_string));
608 size_type counter(size_type(0));
612 replace(pos, flen, replace_string);
618 pos = find(find_string, pos + rlen);
635 for(const_iterator it(begin()); it != end(); ++it)
637#ifdef FEAT_COMPILER_MICROSOFT
638 str.push_back(std::toupper(*it, std::locale::classic()));
641 str.push_back(std::toupper<char>(*it, loc));
657 for(const_iterator it(begin()); it != end(); ++it)
659#ifdef FEAT_COMPILER_MICROSOFT
660 str.push_back(std::tolower(*it, std::locale::classic()));
663 str.push_back(std::tolower<char>(*it, loc));
682#ifdef FEAT_COMPILER_MICROSOFT
684 return _stricmp(c_str(), other.c_str());
688 size_type n1 = size();
689 size_type n2 = other.size();
690 size_type n = std::min(n1, n2);
693 for(size_type i = 0; i < n; ++i)
695 int k = int(std::tolower<char>((*
this)[i], loc)) - int(std::tolower<char>(other[i], loc));
739 if(this->size() < head.size())
743 return (this->compare(std::size_t(0), head.size(), head) == 0);
762 if(this->size() < tail.size())
766 return (this->compare(this->size() - tail.size(), tail.size(), tail) == 0);
780 return (this->empty() ?
false : this->front() == head);
794 return (this->empty() ?
false : this->back() == tail);
815 for(
const auto& s : sset)
822 else if(this->compare(s) == 0)
837 template<
typename T_>
840 std::istringstream iss(
trim());
846 bool parse(
bool& b)
const
861 bool parse(std::string& s)
const
877 bool parse(std::uint8_t& var)
const
881 std::uint64_t tmp = std::stoull(this->c_str());
884 var = std::uint8_t(tmp);
891 catch(std::invalid_argument& )
901 bool parse(std::int8_t& var)
const
905 std::int64_t tmp = std::stoll(this->c_str());
906 if(-128 <= tmp && tmp <= 127)
908 var = std::int8_t(tmp);
915 catch(std::invalid_argument& )
923 #ifdef FEAT_HAVE_HALFMATH
927 bool ret =
parse(tmp);
928 t = __float2half(tmp);
934#ifdef FEAT_HAVE_QUADMATH
935 bool parse(__float128& x)
const
940 const char* nptr(this->c_str());
941 char* endptr(
nullptr);
942 x = strtoflt128(nptr, &endptr);
946 return (nptr != endptr);
956 return String(a).append(b);
961 return String(a).append(b);
966 return String(a).append(b);
971 return String(a).append(String::size_type(1), c);
976 return String(String::size_type(1), c).append(b);
992 template<
typename T_>
995 std::ostringstream oss;
1018 return String(item ?
"true" :
"false");
1021#ifdef FEAT_HAVE_HALFMATH
1031 return String(
"nullptr");
1034#ifdef FEAT_HAVE_QUADMATH
1038 int len = ::quadmath_snprintf(
nullptr, 0,
"%Qg",
value);
1040 std::vector<char> buffer(len+16);
1042 quadmath_snprintf(buffer.data(), buffer.size(),
"%Qg",
value);
1044 return String(buffer.data());
1065 template<
typename Iterator_>
1069 const String & delimiter =
"")
1074 Iterator_ it(first);
1077 for(++it; it != last; ++it)
1078 str.append(delimiter).append(
stringify(*it));
1098 template<
typename Container_>
1100 const Container_& container,
1101 const String & delimiter =
"")
1103 return stringify_join(container.cbegin(), container.cend(), delimiter);
1136 template<
typename DataType_>
1139 std::ostringstream oss;
1140 oss << std::scientific;
1142 oss << std::setprecision(precision);
1144 oss << std::setw(width);
1146 oss << std::showpos;
1151 #ifdef FEAT_HAVE_HALFMATH
1190 template<
typename DataType_>
1193 std::ostringstream oss;
1196 oss << std::setprecision(precision);
1198 oss << std::setw(width);
1200 oss << std::showpos;
1205 #ifdef FEAT_HAVE_HALFMATH
1215#ifdef FEAT_HAVE_QUADMATH
1228 format.append(
"Qe");
1230 int len = ::quadmath_snprintf(
nullptr, 0, format.c_str(),
value);
1232 std::vector<char> buffer(len+16);
1234 quadmath_snprintf(buffer.data(), buffer.size(), format.c_str(),
value);
1236 return String(buffer.data());
1251 format.append(
"Qf");
1253 int len = ::quadmath_snprintf(
nullptr, 0, format.c_str(),
value);
1255 std::vector<char> buffer(len+16);
1257 quadmath_snprintf(buffer.data(), buffer.size(), format.c_str(),
value);
1259 return String(buffer.data());
1262 inline std::istream&
operator>>(std::istream& is, __float128& x)
1266 if(!(is >> buffer).fail())
1268 if(!buffer.parse(x))
1271 for(std::size_t i(0); i < buffer.size(); ++i)
1272 is.putback(buffer.at(i));
1275 is.setstate(std::ios_base::failbit);
1315 else if(bytes <= 1'022'976ull)
1316 return stringify_fp_fix(
double(bytes) / (1024.), precision, width) +
" KiB";
1317 else if(bytes <= 1'047'527'424ull)
1318 return stringify_fp_fix(
double(bytes) / (1048576.), precision, width) +
" MiB";
1319 else if(bytes <= 1'072'668'082'176ull)
1320 return stringify_fp_fix(
double(bytes) / (1073741824.), precision, width) +
" GiB";
1321 else if(bytes <= 1'098'412'116'148'224ull)
1322 return stringify_fp_fix(
double(bytes) / (1099511627776.), precision, width) +
" TiB";
1324 return stringify_fp_fix(
double(bytes) / (1125899906842624.), precision, width) +
" PiB";
1331#ifdef FEAT_HAVE_QUADMATH
1334 inline std::ostream& operator<<(std::ostream& os, __float128 x)
A class providing case-insensitive String comparison.
bool operator()(const String &left, const String &right) const
Compares two Strings without regard to case.
String class implementation.
String trunc_front(size_type len) const
Truncates the front of the string to a given maximum length.
void pop_front()
Removes the first character from the string.
String trim() const
Trims the string of all white-spaces.
bool parse(T_ &t) const
Parses the string and stores its value in the provided variable.
String trim_back(const String &charset) const
Trims the back of the string.
String & trim_me()
Trims this string of all white-spaces.
std::deque< String > split_by_string(const String &delimiter) const
Splits the string by a delimiter substring.
String trim_front() const
Trims the front of the string of all white-spaces.
String trim_back() const
Trims the back of the string of all white-spaces.
String trunc_back(size_type len) const
Truncates the back of the string to a given maximum length.
String(std::string &&str)
CTOR.
String(const String &str)
copy CTOR
static const char * whitespaces()
Returns a null-terminated char string containing all white-space characters.
String(const char *str, size_type count)
CTOR.
std::deque< String > split_by_whitespaces() const
Splits the string by white-spaces.
int compare_no_case(const String &other) const
Compares two strings without regard to case.
bool ends_with(const char tail) const
Checks whether this string ends with a specified character.
String trim_front(const String &charset) const
Trims the front of the string.
bool starts_with(const char head) const
Checks whether this string starts with a specified character.
String pad_back(size_type len, char c=' ') const
Pads the back of the string up to a desired length.
bool ends_with(const String &tail) const
Checks whether this string ends with another string.
std::deque< String > split_by_charset(const String &charset) const
Splits the string by a delimiter charset.
String & trim_me(const String &charset)
Trims this string.
void pop_back()
Removes the last character from the string.
bool starts_with(const String &head) const
Checks whether this string starts with another string.
String(const char *str)
CTOR.
String(const std::string &str)
CTOR.
String()
default constructor
String(String &&str)
move CTOR
String(const std::string &str, size_type offset, size_type count=npos)
CTOR.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
size_type replace_all(const String &find_string, const String &replace_string)
Replaces all occurrences of a substring by another substring.
String trim(const String &charset) const
Trims the string.
void push_front(char value)
Inserts a character at the front of the string.
String upper() const
Converts the string to upper case.
String(size_type count, char c)
CTOR.
bool is_one_of(const String &set, const String &sep=" ", bool case_insensitive=false) const
Checks whether this string is equal to one in a set of strings.
String lower() const
Converts the string to lower case.
std::istream & operator>>(std::istream &is, Pack::Type &t)
stream input operator for Pack::Type
CUDA_HOST_DEVICE Vector< T_, n_ > operator+(const Vector< T_, n_, sa_ > &a, const Vector< T_, n_, sb_ > &b)
vector addition operator
String stringify_fp_fix(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in fixed-point notation.
String stringify(const T_ &item)
Converts an item into a String.
String stringify_join(Iterator_ first, Iterator_ last, const String &delimiter="")
Joins a sequence of strings.
__half Half
Half data type.
String stringify_bytes(std::uint64_t bytes, int precision=3, int width=7)
Prints a byte size to a string using the common units Bytes, KiB, MiB, Gib, TiB or PiB.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
@ value
specifies whether the space should supply basis function values