31#ifndef KBLIB_STRINGOPS_H
32#define KBLIB_STRINGOPS_H
42#include <initializer_list>
48# include <string_view>
53#if true or KBLIB_USE_CXX17
86 template <typename T, bool = std::is_arithmetic<T>::value>
103 template <
typename T>
113 template <
typename T,
typename = arithmetic_type_t<T>>
140 template <
typename T>
151 return std::forward<T>(in);
195 struct str_type<char8_t, char8_t> {
196 using type = char8_t;
197 KBLIB_NODISCARD static auto convert(
char8_t in) ->
char8_t {
return in; }
203 template <
typename T>
216template <
typename Str>
218 if constexpr (std::is_array_v<std::remove_reference_t<Str>>) {
220 }
else if constexpr (std::is_pointer_v<std::decay_t<Str>>) {
222 }
else if constexpr (is_character_v<std::decay_t<Str>>) {
224 }
else if constexpr (std::is_integral_v<std::decay_t<Str>>) {
226 }
else if constexpr (std::is_floating_point_v<std::decay_t<Str>>) {
233template <
typename CharT>
250template <
typename string,
typename F,
typename... S>
251auto append(
string&& out, F&& f, S&&... tail) ->
void {
252 if constexpr (is_character_v<std::decay_t<F>>) {
254 }
else if constexpr (std::is_arithmetic_v<std::decay_t<F>>) {
259 if constexpr (
sizeof...(S) > 0) {
267 template <std::
size_t I,
typename T>
272 template <
class Idxs,
class... Ts>
275 template <std::size_t... Idxs,
typename... Ts>
278 template <
typename string,
typename... S, std::size_t... I>
304template <
typename string = std::string,
typename F,
typename... S>
306 return detail::concat_impl<string>(
307 std::make_index_sequence<1 +
sizeof...(S)>{}, std::forward<F>(f),
308 std::forward<S>(ins)...);
318template <
typename string = std::
string,
typename str>
322 ins.begin(), ins.end(), std::size_t{0},
323 [](std::size_t z,
const str& s) { return z + strsize(s); }));
324 for (
auto&& s : ins) {
344 for (
auto v :
" \t\r\n\f\v") {
352 for (
auto v : L
" \t\r\n\f\v") {
369template <
typename range,
typename string = std::
string>
374 begin(in), end(in), std::size_t{},
375 [](std::size_t l,
const auto& x) {
return l +
strsize(x); });
376 auto ret = *begin(in);
395template <
typename Container = std::vector<std::
string>,
typename Predicate,
400 typename Container::value_type::value_type>::value,
403 bool delim_run =
true;
404 const char* begpos{};
405 auto endpos = begpos;
406 for (
const auto& c : in) {
413 ret.emplace_back(begpos, &c - begpos);
414 }
else if (not spacer(c)) {
421 ret.emplace_back(begpos, endpos + 1);
423 if (not delim_run and begpos != endpos) {
424 ret.emplace_back(begpos, endpos - begpos + 1);
435template <
typename Container = std::vector<std::
string>,
typename String>
447template <
typename Container = std::vector<std::
string>,
typename String>
449 const String& in,
typename Container::value_type::value_type delim)
452 bool delim_run =
true;
453 using CharT =
typename Container::value_type::value_type;
454 const CharT* begpos{};
455 auto endpos = begpos;
456 for (
const CharT& c : in) {
463 ret.emplace_back(begpos, &c - begpos);
464 }
else if (c != delim) {
470 if (not delim_run and begpos != endpos) {
471 ret.emplace_back(&*begpos, endpos - begpos + 1);
476template <
typename Container = std::vector<std::
string>,
typename String>
479 bool delim_run =
true;
484 }
else if (c != delim) {
487 ret.back().push_back(c);
490 if (ret.back().empty()) {
503template <
typename Container = std::vector<std::
string>,
typename String>
506 for (std::size_t pos1{}, pos2{str.find(delim)}; pos1 != str.npos;) {
507 ret.emplace_back(str, pos1, pos2 - pos1);
509 if (pos1 != str.npos) {
523template <
typename Container = std::vector<std::
string>,
typename String,
528 typename Container::value_type::value_type>::value,
531 for (std::size_t pos1{}, pos2{str.find(delim)}; pos1 != str.npos;) {
532 ret.emplace_back(str, pos1, pos2 - pos1);
535 if (pos1 != str.npos) {
554template <
typename string>
556 std::reverse(val.begin(), val.end());
562 template <
typename CharT>
566 template <
typename CharT,
typename IntT>
593template <
typename string>
596 [](
auto c) { return detail::tolower(c); });
606template <
typename string>
609 [](
auto c) { return detail::toupper(c); });
624template <
typename string>
628 for (std::size_t i = 0; i < count; ++i) {
643 return std::string(count, val);
646#if KBLIB_USE_STRING_VIEW
655 std::string_view needle) ->
bool {
656 return haystack.size() >= needle.size()
657 and haystack.compare(haystack.size() - needle.size(),
658 std::string_view::npos, needle)
670 return not haystack.empty() and haystack.back() == needle;
680 std::string_view needle) ->
bool {
681 return haystack.size() >= needle.size()
682 and haystack.compare(0, needle.size(), needle) == 0;
693 return not haystack.empty() and haystack.front() == needle;
Provides general-purpose algorithms, similar to the <algorithms> header.
auto to_char_type(IntT ch)
auto towlower(wchar_t ch)
typename str_type< T >::type str_type_t
Provides the natural stringlike type for representing a T.
typename arithmetic_type< T >::type arithmetic_type_t
Equivalent to typename arithmetic_type<T>::type.
auto to_int_type(CharT ch)
auto concat_impl(std::index_sequence< I... >, S &&... ins) -> string
auto towupper(wchar_t ch)
constexpr auto size(const C &c) -> decltype(c.size())
auto starts_with(std::string_view haystack, char needle) -> bool
Checks if a given string starts with a particular string.
constexpr auto exchange(T &obj, U &&new_value) -> T
auto consumer(F f) -> consume_iterator< F >
Creates a consume_iterator of deduced type F.
auto ends_with(std::string_view haystack, char needle) -> bool
Checks if a given string ends with a particular string.
auto isspace(wchar_t c) -> bool
constexpr auto isAspace(wchar_t c) -> bool
auto strsize(Str &&str) -> std::size_t
Determines the size in characters of any valid argument to concat or append.
auto split_tokens(const String &in, typename Container::value_type::value_type delim) -> Container
Split a string on all instances of a delimiter.
auto kbsplit2(const String &in, char delim=' ') -> Container
constexpr auto tolower(string str) -> string
Folds all characters in a string using the default execution character set to lowercase.
auto join(const range &in, const string &joiner="")
Concatenates all elements of a range together with an optional joiner.
auto try_reserve(C &c, std::size_t s) noexcept(noexcept(c.reserve(s))) -> void
Attempt to reserve capacity in a container. No-op if unsupported.
constexpr bool is_character_v
Equivalent to is_character<C>::value.
constexpr auto range(Value min, Value max, Delta step=0) -> range_t< Value, Delta >
Constructs a range from beginning, end, and step amount. The range is half-open, that is min is in th...
constexpr auto length(const CharT *str) noexcept -> std::size_t
auto reverse_str(string val) -> string
Reverses all the elements of its input.
auto to_string(Int num) -> std::string
typename std::decay< T >::type decay_t
auto append(string &&out, F &&f, S &&... tail) -> void
Given an object out of resizable stringlike type string, appends all other arguments to it.
constexpr auto count_digits(Number val) -> enable_if_t< std::is_floating_point< Number >::value, int >
Calculates the number of decimal digits needed to represent a number, plus one for negative numbers.
constexpr auto accumulate(InputIt first, InputIt last, T init) -> T
A constexpr version of std::accumulate.
auto concat(std::initializer_list< str > ins) -> string
Returns a string consisting of the concatenation of all elements of an initializer list.
auto toupper(string str) -> string
Folds all characters in a string using the default execution character set to uppercase.
constexpr auto copy(InputIt first, EndIt last, OutputIt out) -> OutputIt
Copies all elements of [first, last) to out. It also allows for a sentinel end iterator.
auto split_dsv(const String &str, Predicate delim) -> return_assert_t< is_callable< Predicate, typename Container::value_type::value_type >::value, Container >
Split a string on all instances of delim.
typename return_assert< V, T >::type return_assert_t
auto repeat(char val, std::size_t count) -> std::string
Construct a string consisting of count copies of val.
constexpr auto find_in_if(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> size_t
Find the offset of the first element for which p returns true. It also allows for a sentinel end iter...
constexpr auto to_unsigned(I x) -> std::make_unsigned_t< I >
Cast integral argument to corresponding unsigned type.
constexpr auto transform(InputIt first, EndIt last, OutputIt d_first, UnaryOperation unary_op) -> OutputIt
transform applies the given function to a range and stores the result in another range,...
Determines if T is a type in Tuple, which must be a std::tuple.
Filter only arithmetic types.
static auto convert(T &&in) -> type
Returns the argument unchanged.
T type
Non-arithmetic types are either already stringlike, or have no natural conversion to std::string.
static auto convert(char16_t in) -> char16_t
static auto convert(char32_t in) -> char32_t
static auto convert(char in) -> char
static auto convert(wchar_t in) -> wchar_t
Converts arithmetic types to strings, but provides the identity transformation for all other types.
std::string type
Arithmetic types can be converted into strings using the standard library.
static auto convert(T in) -> std::string
Forwards to std::to_string.
Determine if the given type, ignoring const or reference qualifiers, is a character type.
auto operator()(wchar_t c) -> bool
auto operator()(char c) -> bool
Provides macros and basic templates used by the rest of kblib.
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.
Contains some type traits not in the standard library that are useful in the implementation of kblib.