7#include "char_maps/to_string.hpp"
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) {
161[[nodiscard]]
constexpr std::string to_title(std::string_view rhs)
noexcept
170 }
else if (c ==
' ') {
186[[nodiscard]]
constexpr basic_fixed_string<char, N> to_title(basic_fixed_string<char, N>
const &rhs)
noexcept
195 }
else if (c ==
' ') {
207[[nodiscard]]
inline std::string normalize_lf(std::string_view str)
noexcept
212 auto found_cr =
false;
213 for (
hilet c : str) {
217 [[unlikely]] r +=
'\n';
218 if (c !=
'\r' && c !=
'\n') {
222 }
else if (c !=
'\r') {
227 found_cr = c ==
'\r';
239[[nodiscard]]
inline std::string make_identifier(std::string_view str)
noexcept
244 r += is_name_first(str.front()) ? str.front() :
'_';
245 for (
hilet c : str.substr(1)) {
246 r += is_name_next(c) ? c :
'_';
256[[nodiscard]]
inline std::string make_slug(std::string_view str)
noexcept
262 for (
hilet c : str) {
263 if (is_alpha_num(c)) {
266 }
else if (dash_count++ == 0) {
280[[nodiscard]]
inline std::string make_title(std::string_view str)
noexcept
288 for (
hilet c : str) {
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) {
325 hi_axiom(txt[4] == 0);
331[[nodiscard]]
constexpr uint32_t fourcc_from_cstr(T
const *txt)
noexcept requires(
sizeof(T) == 1)
333 hi_axiom(txt !=
nullptr);
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) {
504 column = ((((column - 1) / 8) + 1) * 8) + 1;
510 return {line, column};
516template<
typename T, std::
size_t N>
517constexpr auto to_array_without_last(T (&rhs)[N])
noexcept
529template<
typename T, std::
size_t N>
530constexpr auto to_array_without_last(T(&&rhs)[N])
noexcept
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
547 auto last = back_strip(
begin(haystack),
end(haystack),
begin(needle),
end(needle));
551[[nodiscard]]
inline std::string strip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
553 auto first = front_strip(
begin(haystack),
end(haystack),
begin(needle),
end(needle));
554 auto last = back_strip(first,
end(haystack),
begin(needle),
end(needle));
565[[nodiscard]]
inline std::vector<std::string> ZZWSTR_to_string(
wchar_t *first,
wchar_t *last, ssize_t nr_strings = -1)
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.");
597[[nodiscard]]
inline char *make_cstr(
char const *c_str,
std::size_t size = -1) noexcept
603 auto r =
new char[size + 1];
611[[nodiscard]]
inline char *make_cstr(
std::string const& s)
noexcept
613 return make_cstr(s.c_str(), s.size());
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...