7#include "algorithm.hpp"
12#include "concepts.hpp"
13#include "exception.hpp"
14#include "unicode/UTF.hpp"
23namespace hi::inline v1 {
25[[nodiscard]]
constexpr bool is_upper(
char c)
noexcept
27 return c >=
'A' && c <=
'Z';
30[[nodiscard]]
constexpr bool is_lower(
char c)
noexcept
32 return c >=
'a' && c <=
'z';
35[[nodiscard]]
constexpr bool is_alpha(
char c)
noexcept
37 return is_upper(c) || is_lower(c);
40[[nodiscard]]
constexpr bool is_digit(
char c)
noexcept
42 return c >=
'0' && c <=
'9';
45[[nodiscard]]
constexpr bool is_alpha_num(
char c)
noexcept
47 return is_alpha(c) || is_digit(c);
50[[nodiscard]]
constexpr bool is_line_feed(
char c)
noexcept
52 return c ==
'\r' || c ==
'\n' || c ==
'\f' || c ==
'\v';
55[[nodiscard]]
constexpr bool is_white_space(
char c)
noexcept
57 return c ==
' ' || c ==
'\t' || is_line_feed(c);
60[[nodiscard]]
constexpr bool is_number_first(
char c)
noexcept
62 return is_digit(c) || c ==
'+' || c ==
'-';
65[[nodiscard]]
constexpr bool is_name_first(
char c)
noexcept
67 return is_alpha(c) or c ==
'_' or c ==
'$' or (c & 0x80);
70[[nodiscard]]
constexpr bool is_name_next(
char c)
noexcept
72 return is_alpha_num(c) or c ==
'_' or c ==
'$' or (c & 0x80);
75[[nodiscard]]
constexpr bool is_quote(
char c)
noexcept
77 return c ==
'"' || c ==
'\'' || c ==
'`';
80[[nodiscard]]
constexpr bool is_open_bracket(
char c)
noexcept
82 return c ==
'(' || c ==
'{' || c ==
'[';
85[[nodiscard]]
constexpr bool is_close_bracket(
char c)
noexcept
87 return c ==
')' || c ==
'}' || c ==
']';
90[[nodiscard]]
constexpr bool is_operator(
char c)
noexcept
92 return !is_alpha_num(c) && c !=
'_' && !is_white_space(c) && !is_quote(c) && !is_open_bracket(c) && !is_close_bracket(c);
95[[nodiscard]]
constexpr bool is_digit(std::string_view str)
noexcept
98 if (not is_digit(c)) {
105[[nodiscard]]
constexpr bool is_alpha(std::string_view str)
noexcept
107 for (
hilet c : str) {
108 if (not is_alpha(c)) {
115[[nodiscard]]
constexpr char to_lower(
char c)
noexcept
117 return (c >=
'A' and c <=
'Z') ? (c -
'A') +
'a' : c;
120[[nodiscard]]
constexpr char to_upper(
char c)
noexcept
122 return (c >=
'a' and c <=
'z') ? (c -
'a') +
'A' : c;
125[[nodiscard]]
inline std::string to_lower(std::string_view str)
noexcept
130 for (
hilet c : str) {
137[[nodiscard]]
inline std::string to_upper(std::string_view str)
noexcept
142 for (
hilet c : str) {
151[[nodiscard]]
inline std::string normalize_lf(std::string_view str)
noexcept
156 auto found_cr =
false;
157 for (
hilet c : str) {
161 [[unlikely]] r +=
'\n';
162 if (c !=
'\r' && c !=
'\n') {
166 }
else if (c !=
'\r') {
171 found_cr = c ==
'\r';
183[[nodiscard]]
inline std::string make_identifier(std::string_view str)
noexcept
188 r += is_name_first(str.front()) ? str.front() :
'_';
189 for (
hilet c : str.substr(1)) {
190 r += is_name_next(c) ? c :
'_';
200[[nodiscard]]
inline std::string make_slug(std::string_view str)
noexcept
206 for (
hilet c : str) {
207 if (is_alpha_num(c)) {
210 }
else if (dash_count++ == 0) {
224[[nodiscard]]
inline std::string make_title(std::string_view str)
noexcept
232 for (
hilet c : str) {
233 if (is_alpha_num(c)) {
236 }
else if (letter_count++ == 0) {
243 }
else if (space_count++ == 0) {
256[[nodiscard]]
constexpr uint32_t fourcc(
char const txt[5])
noexcept
259 (
static_cast<uint32_t
>(txt[0]) << 24) | (
static_cast<uint32_t
>(txt[1]) << 16) | (
static_cast<uint32_t
>(txt[2]) << 8) |
260 static_cast<uint32_t
>(txt[3]));
263[[nodiscard]]
constexpr uint32_t fourcc(uint8_t
const *txt)
noexcept
266 (
static_cast<uint32_t
>(txt[0]) << 24) | (
static_cast<uint32_t
>(txt[1]) << 16) | (
static_cast<uint32_t
>(txt[2]) << 8) |
267 static_cast<uint32_t
>(txt[3]));
270[[nodiscard]]
inline std::string fourcc_to_string(uint32_t x)
noexcept
273 c_str[0] =
static_cast<char>((x >> 24) & 0xff);
274 c_str[1] =
static_cast<char>((x >> 16) & 0xff);
275 c_str[2] =
static_cast<char>((x >> 8) & 0xff);
276 c_str[3] =
static_cast<char>(x & 0xff);
282constexpr std::size_t string_size(sizeable
auto str)
noexcept
287constexpr std::size_t string_size(
auto str)
noexcept
292template<
typename FirstNeedle,
typename... Needles>
294string_find_any(std::string_view haystack,
std::size_t pos, FirstNeedle
const &first_needle, Needles
const &...needles)
noexcept
298 std::size_t first = haystack.find(first_needle, pos);
299 std::size_t last = first + string_size(first_needle);
301 if (first == std::string_view::npos) {
302 first = size(haystack);
303 last = size(haystack);
306 if constexpr (
sizeof...(Needles) != 0) {
307 hilet[other_first, other_last] = string_find_any(haystack, pos, needles...);
308 if (other_first < first) {
314 return {first, last};
317template<
typename StringType,
typename... Needles>
322 std::string_view::size_type current_pos = 0;
324 while (current_pos < size(haystack)) {
325 hilet[needle_first, needle_last] = string_find_any(haystack, current_pos, needles...);
326 r.
push_back(StringType{haystack.substr(current_pos, needle_first - current_pos)});
327 current_pos = needle_last;
333template<
typename... Needles>
336 return _split<std::string>(haystack, needles...);
341 return split(haystack,
' ');
344template<
typename... Needles>
347 return _split<std::string_view>(haystack, needles...);
352 return split_view(haystack,
' ');
355template<
typename CharT>
361 if (list.size() > 1) {
362 std::size_t final_size = (list.size() - 1) * joiner.size();
363 for (
hilet &item : list) {
364 final_size += item.size();
370 for (
hilet &item : list) {
379template<
typename CharT>
383 return join(list, std::basic_string_view<CharT>{joiner});
386template<
typename CharT>
389 return join(list, std::basic_string_view<CharT>{joiner});
396 if (list.
size() > 1) {
398 for (
hilet &item : list) {
399 final_size += item.size();
405 for (
hilet &item : list) {
424 case '\n': line++; [[fallthrough]];
425 case '\r': column = 1;
break;
426 case '\t': column = ((((column - 1) / 8) + 1) * 8) + 1;
break;
430 return {line, column};
436template<
typename T, std::
size_t N>
437constexpr auto to_array_without_last(T (&rhs)[N])
noexcept
449template<
typename T, std::
size_t N>
450constexpr auto to_array_without_last(T(&&rhs)[N])
noexcept
459[[nodiscard]]
inline std::string lstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
461 auto first = front_strip(
begin(haystack),
end(haystack),
begin(needle),
end(needle));
465[[nodiscard]]
inline std::string rstrip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
467 auto last = back_strip(
begin(haystack),
end(haystack),
begin(needle),
end(needle));
471[[nodiscard]]
inline std::string strip(std::string_view haystack,
std::string needle =
" \t\r\n\f") noexcept
473 auto first = front_strip(
begin(haystack),
end(haystack),
begin(needle),
end(needle));
474 auto last = back_strip(first,
end(haystack),
begin(needle),
end(needle));
485[[nodiscard]]
inline std::vector<std::string> ZZWSTR_to_string(
wchar_t *first,
wchar_t *last, ssize_t nr_strings = -1)
489 while (first != last) {
490 auto it_zero =
std::find(first, last,
wchar_t{0});
491 if (it_zero == last) {
492 throw parse_error(
"Could not find terminating zero of a string.");
495 auto ws = std::wstring_view{first, narrow_cast<std::size_t>(it_zero - first)};
507 if (nr_strings != -1 && ssize(r) != nr_strings) {
508 throw parse_error(
"Unexpected number of string in list.");
517[[nodiscard]]
inline char *make_cstr(
char const *c_str,
std::size_t size = -1) noexcept
523 auto r =
new char[size + 1];
531[[nodiscard]]
inline char *make_cstr(
std::string const &s)
noexcept
533 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...