7#include "char_maps/module.hpp"
8#include "algorithm.hpp"
9#include "utility/module.hpp"
23hi_warning_ignore_msvc(26409);
25namespace hi::inline
v1 {
27[[nodiscard]]
constexpr bool is_upper(
char c)
noexcept
29 return c >=
'A' &&
c <=
'Z';
32[[nodiscard]]
constexpr bool is_lower(
char c)
noexcept
34 return c >=
'a' &&
c <=
'z';
37[[nodiscard]]
constexpr bool is_alpha(
char c)
noexcept
39 return is_upper(c) || is_lower(c);
42[[nodiscard]]
constexpr bool is_digit(
char c)
noexcept
44 return c >=
'0' &&
c <=
'9';
47[[nodiscard]]
constexpr bool is_alpha_num(
char c)
noexcept
49 return is_alpha(c) || is_digit(c);
52[[nodiscard]]
constexpr bool is_line_feed(
char c)
noexcept
54 return c ==
'\r' ||
c ==
'\n' ||
c ==
'\f' ||
c ==
'\v';
57[[nodiscard]]
constexpr bool is_white_space(
char c)
noexcept
59 return c ==
' ' ||
c ==
'\t' || is_line_feed(c);
62[[nodiscard]]
constexpr bool is_number_first(
char c)
noexcept
64 return is_digit(c) ||
c ==
'+' ||
c ==
'-';
67[[nodiscard]]
constexpr bool is_name_first(
char c)
noexcept
69 return is_alpha(c) or
c ==
'_' or
c ==
'$' or (c & 0x80);
72[[nodiscard]]
constexpr bool is_name_next(
char c)
noexcept
74 return is_alpha_num(c) or
c ==
'_' or
c ==
'$' or (c & 0x80);
77[[nodiscard]]
constexpr bool is_quote(
char c)
noexcept
79 return c ==
'"' ||
c ==
'\'' ||
c ==
'`';
82[[nodiscard]]
constexpr bool is_open_bracket(
char c)
noexcept
84 return c ==
'(' ||
c ==
'{' ||
c ==
'[';
87[[nodiscard]]
constexpr bool is_close_bracket(
char c)
noexcept
89 return c ==
')' ||
c ==
'}' ||
c ==
']';
92[[nodiscard]]
constexpr bool is_operator(
char c)
noexcept
94 return !is_alpha_num(c) &&
c !=
'_' && !is_white_space(c) && !is_quote(c) && !is_open_bracket(c) && !is_close_bracket(c);
97[[nodiscard]]
constexpr bool is_digit(std::string_view str)
noexcept
100 if (not is_digit(c)) {
107[[nodiscard]]
constexpr bool is_alpha(std::string_view str)
noexcept
109 for (
hilet c : str) {
110 if (not is_alpha(c)) {
117[[nodiscard]]
constexpr char to_lower(
char c)
noexcept
119 return (c >=
'A' and c <=
'Z') ? (
c -
'A') +
'a' :
c;
122[[nodiscard]]
constexpr char to_upper(
char c)
noexcept
124 return (c >=
'a' and c <=
'z') ? (
c -
'a') +
'A' :
c;
127[[nodiscard]]
inline std::string to_lower(std::string_view str)
noexcept
132 for (
hilet c : str) {
139[[nodiscard]]
inline std::string to_upper(std::string_view str)
noexcept
144 for (
hilet c : str) {
165 }
else if (
c ==
' ') {
190 }
else if (
c ==
' ') {
207 auto found_cr =
false;
212 [[unlikely]] r +=
'\n';
213 if (
c !=
'\r' &&
c !=
'\n') {
217 }
else if (
c !=
'\r') {
222 found_cr =
c ==
'\r';
239 r += is_name_first(str.front()) ? str.front() :
'_';
240 for (
hilet c : str.substr(1)) {
241 r += is_name_next(
c) ?
c :
'_';
258 if (is_alpha_num(
c)) {
261 }
else if (dash_count++ == 0) {
284 if (is_alpha_num(
c)) {
287 }
else if (letter_count++ == 0) {
294 }
else if (space_count++ == 0) {
307template<
typename T,
size_t N>
308[[nodiscard]]
constexpr uint32_t fourcc(T
const (&txt)[N])
noexcept requires(
sizeof(T) == 1 and (N == 4 or N == 5))
311 r |= truncate<uint8_t>(txt[0]);
313 r |= truncate<uint8_t>(txt[1]);
315 r |= truncate<uint8_t>(txt[2]);
317 r |= truncate<uint8_t>(txt[3]);
319 if constexpr (N == 5) {
325[[nodiscard]]
constexpr uint32_t fourcc_from_cstr(
char const *txt)
noexcept
329 (char_cast<uint32_t>(txt[0]) << 24) |
330 (char_cast<uint32_t>(txt[1]) << 16) |
331 (char_cast<uint32_t>(txt[2]) << 8) |
332 char_cast<uint32_t>(txt[3]);
335[[nodiscard]]
inline std::string fourcc_to_string(uint32_t x)
noexcept
338 r += truncate<char>((x >> 24) & 0xff);
339 r += truncate<char>((x >> 16) & 0xff);
340 r += truncate<char>((x >> 8) & 0xff);
341 r += truncate<char>(x & 0xff);
345constexpr std::size_t string_size(sizeable
auto str)
noexcept
350constexpr std::size_t string_size(
auto str)
noexcept
355template<
typename FirstNeedle,
typename... Needles>
357string_find_any(std::string_view haystack,
std::size_t pos, FirstNeedle
const& first_needle, Needles
const&...needles)
noexcept
361 std::size_t first = haystack.find(first_needle, pos);
362 std::size_t last = first + string_size(first_needle);
364 if (first == std::string_view::npos) {
365 first = size(haystack);
366 last = size(haystack);
369 if constexpr (
sizeof...(Needles) != 0) {
370 hilet[other_first, other_last] = string_find_any(haystack, pos, needles...);
371 if (other_first < first) {
377 return {first, last};
380template<
typename StringType,
typename... Needles>
385 std::string_view::size_type current_pos = 0;
387 while (current_pos < size(haystack)) {
388 hilet[needle_first, needle_last] = string_find_any(haystack, current_pos, needles...);
389 r.
push_back(StringType{haystack.substr(current_pos, needle_first - current_pos)});
390 current_pos = needle_last;
396template<
typename... Needles>
399 return _split<std::string>(haystack, needles...);
404 return split(haystack,
' ');
407template<
typename... Needles>
410 return _split<std::string_view>(haystack, needles...);
415 return split_view(haystack,
' ');
418template<
typename CharT>
424 if (list.size() > 1) {
425 std::size_t final_size = (list.size() - 1) * joiner.size();
426 for (
hilet& item : list) {
427 final_size += item.size();
433 for (
hilet& item : list) {
442template<
typename CharT>
446 return join(list, std::basic_string_view<CharT>{joiner});
449template<
typename CharT>
452 return join(list, std::basic_string_view<CharT>{joiner});
459 if (list.
size() > 1) {
461 for (
hilet item : list) {
462 final_size += item.size();
468 for (
hilet item : list) {
485 for (; begin != end; begin++) {
494 column = ((((column - 1) / 8) + 1) * 8) + 1;
500 return {line, column};
506template<
typename T, std::
size_t N>
519template<
typename T, std::
size_t N>
529[[nodiscard]]
inline std::string lstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
531 auto first = front_strip(begin(haystack), end(haystack), begin(needle), end(needle));
535[[nodiscard]]
inline std::string rstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
541[[nodiscard]]
inline std::string strip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
559 while (first != last) {
560 auto it_zero =
std::find(first, last,
wchar_t{0});
561 if (it_zero == last) {
562 throw parse_error(
"Could not find terminating zero of a string.");
565 hilet ws = std::wstring_view{first, narrow_cast<std::size_t>(it_zero - first)};
577 if (nr_strings != -1 && ssize(r) != nr_strings) {
578 throw parse_error(
"Unexpected number of string in list.");
593 auto r =
new char[size + 1];
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
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:13
constexpr std::string to_title(std::string_view rhs) noexcept
Convert the current string to using title case.
Definition strings.hpp:156
std::string make_slug(std::string_view str) noexcept
Create a slug from a string.
Definition strings.hpp:251
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:507
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:320
char * make_cstr(char const *c_str, std::size_t size=-1) noexcept
Copy a std::string to new memory.
Definition strings.hpp:587
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:555
std::string make_title(std::string_view str) noexcept
Create a title from a string.
Definition strings.hpp:275
std::string make_identifier(std::string_view str) noexcept
Encode a string to be usable as an id.
Definition strings.hpp:234
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:341
std::string normalize_lf(std::string_view str) noexcept
Normalize string to use only line-feeds.
Definition strings.hpp:202
std::pair< int, int > count_line_and_columns(It begin, It const end)
Definition strings.hpp:480
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition utility.hpp:189
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:36