bux API Reference 1.12.6
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
StrUtil.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts> // std::regular_invocable<>, std::convertible_to<>
4#include <functional> // std::function<>
5#include <stdexcept> // std::exception
6#include <string> // std::string
7#include <string_view> // std::string_view
8#include <type_traits> // std::type_identity_t<>
9#include <typeinfo> // return type of typeid()
10#include <utility> // std::declval(), std::forward()
11
12namespace bux {
13
14//
15// Types
16//
19template<typename S>
20using string_char_t = typename decltype(std::basic_string_view{std::declval<const S&>()})::value_type;
21
22//
23// Externs
24//
25const char *ord_suffix(size_t i);
26std::string expand_env(const char *s);
27
28std::string _HRTN(const char *originalName);
29std::string OXCPT(const std::exception &e);
30
38template<typename S, typename T = string_char_t<S>>
39void split(const S& token_list, std::type_identity_t<std::basic_string_view<T>> splitters,
40 std::regular_invocable<std::basic_string_view<T>> auto&& apply_token,
41 std::regular_invocable<std::basic_string_view<T>> auto&& apply_delim)
42{
43 using view_t = std::basic_string_view<T>;
44 const view_t src{token_list};
45 for (size_t start_off = 0; start_off < src.size();)
46 {
47 const auto first_not_of_off = src.find_first_not_of(splitters, start_off);
48 if (first_not_of_off == view_t::npos)
49 break;
50
51 if (start_off < first_not_of_off)
52 apply_delim(src.substr(start_off, first_not_of_off - start_off));
53
54 start_off = first_not_of_off;
55 const auto end_off = src.find_first_of(splitters, start_off);
56 if (end_off == view_t::npos)
57 {
58 if (start_off < src.size())
59 apply_token(src.substr(start_off));
60
61 break;
62 }
63 apply_token(src.substr(start_off, end_off - start_off));
64 start_off = end_off;
65 }
66}
67
70template<typename S, typename T = string_char_t<S>>
71void split(const S& token_list, std::type_identity_t<std::basic_string_view<T>> splitters,
72 std::regular_invocable<std::basic_string_view<T>> auto&& apply_token)
73{
74 split(token_list, splitters,
75 std::forward<decltype(apply_token)>(apply_token), [](std::basic_string_view<T>){});
76}
77
78} // namespace bux
79
82#define HRTN(t) bux::_HRTN(typeid(t).name())
83using bux::OXCPT;
std::string OXCPT(const std::exception &e)
Definition StrUtil.cpp:131
THE common namespace of bux library.
Definition AtomiX.cpp:3
const char * ord_suffix(size_t i)
Definition StrUtil.cpp:25
std::string _HRTN(const char *originalName)
Definition StrUtil.cpp:73
void split(const S &token_list, std::type_identity_t< std::basic_string_view< T > > splitters, std::regular_invocable< std::basic_string_view< T > > auto &&apply_token, std::regular_invocable< std::basic_string_view< T > > auto &&apply_delim)
Split token_list at any character in splitters, feeding each token to apply_token and each run of spl...
Definition StrUtil.h:39
std::string OXCPT(const std::exception &e)
Definition StrUtil.cpp:131
std::string expand_env(const char *s)
Definition StrUtil.cpp:46
typename decltype(std::basic_string_view{std::declval< const S & >()})::value_type string_char_t
Definition StrUtil.h:20