8#include "algorithm.hpp"
13#include "concepts.hpp"
14#include "exception.hpp"
28hi_warning_ignore_msvc(26409);
30namespace hi::inline
v1 {
32[[nodiscard]]
constexpr bool is_upper(
char c)
noexcept
34 return c >=
'A' &&
c <=
'Z';
37[[nodiscard]]
constexpr bool is_lower(
char c)
noexcept
39 return c >=
'a' &&
c <=
'z';
42[[nodiscard]]
constexpr bool is_alpha(
char c)
noexcept
44 return is_upper(c) || is_lower(c);
47[[nodiscard]]
constexpr bool is_digit(
char c)
noexcept
49 return c >=
'0' &&
c <=
'9';
52[[nodiscard]]
constexpr bool is_alpha_num(
char c)
noexcept
54 return is_alpha(c) || is_digit(c);
57[[nodiscard]]
constexpr bool is_line_feed(
char c)
noexcept
59 return c ==
'\r' ||
c ==
'\n' ||
c ==
'\f' ||
c ==
'\v';
62[[nodiscard]]
constexpr bool is_white_space(
char c)
noexcept
64 return c ==
' ' ||
c ==
'\t' || is_line_feed(c);
67[[nodiscard]]
constexpr bool is_number_first(
char c)
noexcept
69 return is_digit(c) ||
c ==
'+' ||
c ==
'-';
72[[nodiscard]]
constexpr bool is_name_first(
char c)
noexcept
74 return is_alpha(c) or
c ==
'_' or
c ==
'$' or (c & 0x80);
77[[nodiscard]]
constexpr bool is_name_next(
char c)
noexcept
79 return is_alpha_num(c) or
c ==
'_' or
c ==
'$' or (c & 0x80);
82[[nodiscard]]
constexpr bool is_quote(
char c)
noexcept
84 return c ==
'"' ||
c ==
'\'' ||
c ==
'`';
87[[nodiscard]]
constexpr bool is_open_bracket(
char c)
noexcept
89 return c ==
'(' ||
c ==
'{' ||
c ==
'[';
92[[nodiscard]]
constexpr bool is_close_bracket(
char c)
noexcept
94 return c ==
')' ||
c ==
'}' ||
c ==
']';
97[[nodiscard]]
constexpr bool is_operator(
char c)
noexcept
99 return !is_alpha_num(c) &&
c !=
'_' && !is_white_space(c) && !is_quote(c) && !is_open_bracket(c) && !is_close_bracket(c);
102[[nodiscard]]
constexpr bool is_digit(std::string_view str)
noexcept
104 for (
hilet c : str) {
105 if (not is_digit(c)) {
112[[nodiscard]]
constexpr bool is_alpha(std::string_view str)
noexcept
114 for (
hilet c : str) {
115 if (not is_alpha(c)) {
122[[nodiscard]]
constexpr char to_lower(
char c)
noexcept
124 return (c >=
'A' and c <=
'Z') ? (
c -
'A') +
'a' :
c;
127[[nodiscard]]
constexpr char to_upper(
char c)
noexcept
129 return (c >=
'a' and c <=
'z') ? (
c -
'a') +
'A' :
c;
132[[nodiscard]]
inline std::string to_lower(std::string_view str)
noexcept
137 for (
hilet c : str) {
144[[nodiscard]]
inline std::string to_upper(std::string_view str)
noexcept
149 for (
hilet c : str) {
170 }
else if (
c ==
' ') {
195 }
else if (
c ==
' ') {
212 auto found_cr =
false;
217 [[unlikely]] r +=
'\n';
218 if (
c !=
'\r' &&
c !=
'\n') {
222 }
else if (
c !=
'\r') {
227 found_cr =
c ==
'\r';
244 r += is_name_first(str.front()) ? str.front() :
'_';
245 for (
hilet c : str.substr(1)) {
246 r += is_name_next(
c) ?
c :
'_';
263 if (is_alpha_num(
c)) {
266 }
else if (dash_count++ == 0) {
289 if (is_alpha_num(
c)) {
292 }
else if (letter_count++ == 0) {
299 }
else if (space_count++ == 0) {
312template<
typename T,
size_t N>
313[[nodiscard]]
constexpr uint32_t fourcc(T
const (&txt)[N])
noexcept requires(
sizeof(T) == 1 and (N == 4 or N == 5))
316 r |= truncate<uint8_t>(txt[0]);
318 r |= truncate<uint8_t>(txt[1]);
320 r |= truncate<uint8_t>(txt[2]);
322 r |= truncate<uint8_t>(txt[3]);
324 if constexpr (N == 5) {
331[[nodiscard]]
constexpr uint32_t fourcc_from_cstr(T
const *txt)
noexcept requires(
sizeof(T) == 1)
335 r |= truncate<uint8_t>(txt[0]);
337 r |= truncate<uint8_t>(txt[1]);
339 r |= truncate<uint8_t>(txt[2]);
341 r |= truncate<uint8_t>(txt[3]);
345[[nodiscard]]
inline std::string fourcc_to_string(uint32_t x)
noexcept
348 r += truncate<char>((x >> 24) & 0xff);
349 r += truncate<char>((x >> 16) & 0xff);
350 r += truncate<char>((x >> 8) & 0xff);
351 r += truncate<char>(x & 0xff);
355constexpr std::size_t string_size(sizeable
auto str)
noexcept
360constexpr std::size_t string_size(
auto str)
noexcept
365template<
typename FirstNeedle,
typename... Needles>
367string_find_any(std::string_view haystack,
std::size_t pos, FirstNeedle
const& first_needle, Needles
const&...needles)
noexcept
371 std::size_t first = haystack.find(first_needle, pos);
372 std::size_t last = first + string_size(first_needle);
374 if (first == std::string_view::npos) {
375 first = size(haystack);
376 last = size(haystack);
379 if constexpr (
sizeof...(Needles) != 0) {
380 hilet[other_first, other_last] = string_find_any(haystack, pos, needles...);
381 if (other_first < first) {
387 return {first, last};
390template<
typename StringType,
typename... Needles>
395 std::string_view::size_type current_pos = 0;
397 while (current_pos < size(haystack)) {
398 hilet[needle_first, needle_last] = string_find_any(haystack, current_pos, needles...);
399 r.
push_back(StringType{haystack.substr(current_pos, needle_first - current_pos)});
400 current_pos = needle_last;
406template<
typename... Needles>
409 return _split<std::string>(haystack, needles...);
414 return split(haystack,
' ');
417template<
typename... Needles>
420 return _split<std::string_view>(haystack, needles...);
425 return split_view(haystack,
' ');
428template<
typename CharT>
434 if (list.size() > 1) {
435 std::size_t final_size = (list.size() - 1) * joiner.size();
436 for (
hilet& item : list) {
437 final_size += item.size();
443 for (
hilet& item : list) {
452template<
typename CharT>
456 return join(list, std::basic_string_view<CharT>{joiner});
459template<
typename CharT>
462 return join(list, std::basic_string_view<CharT>{joiner});
469 if (list.
size() > 1) {
471 for (
hilet item : list) {
472 final_size += item.size();
478 for (
hilet item : list) {
495 for (; begin != end; begin++) {
504 column = ((((column - 1) / 8) + 1) * 8) + 1;
510 return {line, column};
516template<
typename T, std::
size_t N>
529template<
typename T, std::
size_t N>
539[[nodiscard]]
inline std::string lstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
541 auto first = front_strip(begin(haystack), end(haystack), begin(needle), end(needle));
545[[nodiscard]]
inline std::string rstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
551[[nodiscard]]
inline std::string strip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
569 while (first != last) {
570 auto it_zero =
std::find(first, last,
wchar_t{0});
571 if (it_zero == last) {
572 throw parse_error(
"Could not find terminating zero of a string.");
575 hilet ws = std::wstring_view{first, narrow_cast<std::size_t>(it_zero - first)};
587 if (nr_strings != -1 && ssize(r) != nr_strings) {
588 throw parse_error(
"Unexpected number of string in list.");
603 auto r =
new char[size + 1];
Utilities to assert and bound check.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:118
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
String conversion functions.
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr std::string to_title(std::string_view rhs) noexcept
Convert the current string to using title case.
Definition strings.hpp:161
std::string make_slug(std::string_view str) noexcept
Create a slug from a string.
Definition strings.hpp:256
constexpr auto to_array_without_last(T(&rhs)[N]) noexcept
Create an std::array from a one dimensional array, without the last element.
Definition strings.hpp:517
DataIt front_strip(DataIt data_first, DataIt data_last, ValueIt value_first, ValueIt value_last) noexcept
Strip data from the front side.
Definition algorithm.hpp:322
char * make_cstr(char const *c_str, std::size_t size=-1) noexcept
Copy a std::string to new memory.
Definition strings.hpp:597
std::vector< std::string > ZZWSTR_to_string(wchar_t *first, wchar_t *last, ssize_t nr_strings=-1)
Convert a win32 zero terminated list of zero terminated strings.
Definition strings.hpp:565
std::string make_title(std::string_view str) noexcept
Create a title from a string.
Definition strings.hpp:280
std::string make_identifier(std::string_view str) noexcept
Encode a string to be usable as an id.
Definition strings.hpp:239
DataIt back_strip(DataIt data_first, DataIt data_last, ValueIt value_first, ValueIt value_last) noexcept
Strip data from the back side.
Definition algorithm.hpp:343
std::string normalize_lf(std::string_view str) noexcept
Normalize string to use only line-feeds.
Definition strings.hpp:207
std::pair< int, int > count_line_and_columns(It begin, It const end)
Definition strings.hpp:490
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition utility.hpp:173
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:33