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;
208 for (
hilet c : str) {
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 :
'_';
257 for (
hilet c : str) {
258 if (is_alpha_num(c)) {
261 }
else if (dash_count++ == 0) {
269[[nodiscard]]
constexpr bool is_slug(std::string_view str)
noexcept
271 for (
hilet c : str) {
272 if (not (is_alpha_num(c) or c ==
'-')) {
293 for (
hilet c : str) {
294 if (is_alpha_num(c)) {
297 }
else if (letter_count++ == 0) {
304 }
else if (space_count++ == 0) {
317template<
typename T,
size_t N>
318[[nodiscard]]
constexpr uint32_t fourcc(T
const (&txt)[N])
noexcept requires(
sizeof(T) == 1 and (N == 4 or N == 5))
321 r |= truncate<uint8_t>(txt[0]);
323 r |= truncate<uint8_t>(txt[1]);
325 r |= truncate<uint8_t>(txt[2]);
327 r |= truncate<uint8_t>(txt[3]);
329 if constexpr (N == 5) {
335[[nodiscard]]
constexpr uint32_t fourcc_from_cstr(
char const *txt)
noexcept
339 (char_cast<uint32_t>(txt[0]) << 24) |
340 (char_cast<uint32_t>(txt[1]) << 16) |
341 (char_cast<uint32_t>(txt[2]) << 8) |
342 char_cast<uint32_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];
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#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 make_slug(std::string_view str) noexcept
Create a slug from a string.
Definition strings.hpp:251
constexpr std::string to_title(std::string_view rhs) noexcept
Convert the current string to using title case.
Definition strings.hpp:156
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:320
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:285
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:490
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:36