Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
text.h
1#ifndef SOURCEMETA_CORE_TEXT_H_
2#define SOURCEMETA_CORE_TEXT_H_
3
4#ifndef SOURCEMETA_CORE_TEXT_EXPORT
5#include <sourcemeta/core/text_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/text_static_string.h>
10// NOLINTEND(misc-include-cleaner)
11
12#include <algorithm> // std::max
13#include <array> // std::array
14#include <charconv> // std::to_chars
15#include <concepts> // std::same_as, std::integral
16#include <cstddef> // std::size_t
17#include <cstdint> // std::int8_t, std::uint64_t
18#include <filesystem> // std::filesystem::path
19#include <ios> // std::streamsize
20#include <limits> // std::numeric_limits
21#include <optional> // std::optional
22#include <ostream> // std::ostream
23#include <span> // std::span
24#include <string> // std::string
25#include <string_view> // std::string_view
26#include <type_traits> // std::is_nothrow_copy_constructible_v, std::is_nothrow_invocable_r_v, std::remove_cv_t
27#include <utility> // std::pair
28#include <vector> // std::vector
29
38
39namespace sourcemeta::core {
40
54SOURCEMETA_CORE_TEXT_EXPORT
55auto to_title_case(std::string &value) -> void;
56
70template <typename Character>
71 requires std::same_as<Character, char> ||
72 std::same_as<Character, signed char> ||
73 std::same_as<Character, unsigned char> ||
74 std::same_as<Character, wchar_t>
75constexpr auto to_lowercase(const Character character) noexcept -> Character {
76 return (character >= 'A' && character <= 'Z')
77 ? static_cast<Character>(character + ('a' - 'A'))
78 : character;
79}
80
94template <typename Character, typename Traits, typename Allocator>
95 requires requires(Character character) {
96 { to_lowercase(character) } -> std::same_as<Character>;
97 }
98inline auto to_lowercase(std::basic_string<Character, Traits, Allocator> &value)
99 -> void {
100 for (auto &character : value) {
101 character = to_lowercase(character);
102 }
103}
104
118SOURCEMETA_CORE_TEXT_EXPORT
119auto to_lowercase(std::filesystem::path &value) -> void;
120
133template <typename Character>
134 requires std::same_as<Character, char> ||
135 std::same_as<Character, signed char> ||
136 std::same_as<Character, unsigned char> ||
137 std::same_as<Character, wchar_t>
138constexpr auto is_lowercase(const Character character) noexcept -> bool {
139 return character < 'A' || character > 'Z';
140}
141
155template <typename String>
156 requires requires(const String &value) {
157 { is_lowercase(*value.begin()) } -> std::same_as<bool>;
158 }
159inline auto is_lowercase(const String &value) noexcept -> bool {
160 for (const auto character : value) {
161 if (!is_lowercase(character)) {
162 return false;
163 }
164 }
165 return true;
166}
167
181SOURCEMETA_CORE_TEXT_EXPORT
182auto is_lowercase(const std::filesystem::path &value) noexcept -> bool;
183
196template <typename Character>
197 requires std::same_as<Character, char> ||
198 std::same_as<Character, signed char> ||
199 std::same_as<Character, unsigned char> ||
200 std::same_as<Character, wchar_t>
201constexpr auto is_alpha(const Character character) noexcept -> bool {
202 return (character >= 'a' && character <= 'z') ||
203 (character >= 'A' && character <= 'Z');
204}
205
219constexpr auto is_alpha(const std::string_view value) noexcept -> bool {
220 if (value.empty()) {
221 return false;
222 }
223 for (const auto character : value) {
224 if (!is_alpha(character)) {
225 return false;
226 }
227 }
228 return true;
229}
230
242template <typename Character>
243 requires std::same_as<Character, char> ||
244 std::same_as<Character, signed char> ||
245 std::same_as<Character, unsigned char> ||
246 std::same_as<Character, wchar_t>
247constexpr auto is_digit(const Character character) noexcept -> bool {
248 return character >= '0' && character <= '9';
249}
250
264constexpr auto is_digit(const std::string_view value) noexcept -> bool {
265 if (value.empty()) {
266 return false;
267 }
268 for (const auto character : value) {
269 if (!is_digit(character)) {
270 return false;
271 }
272 }
273 return true;
274}
275
288template <typename Character>
289 requires std::same_as<Character, char> ||
290 std::same_as<Character, signed char> ||
291 std::same_as<Character, unsigned char> ||
292 std::same_as<Character, wchar_t>
293constexpr auto is_alphanum(const Character character) noexcept -> bool {
294 return is_alpha(character) || is_digit(character);
295}
296
310constexpr auto is_alphanum(const std::string_view value) noexcept -> bool {
311 if (value.empty()) {
312 return false;
313 }
314 for (const auto character : value) {
315 if (!is_alphanum(character)) {
316 return false;
317 }
318 }
319 return true;
320}
321
337template <typename Character>
338 requires std::same_as<Character, char> ||
339 std::same_as<Character, signed char> ||
340 std::same_as<Character, unsigned char> ||
341 std::same_as<Character, wchar_t>
342constexpr auto is_punctuation(const Character character) noexcept -> bool {
343 return (character >= '!' && character <= '/') ||
344 (character >= ':' && character <= '@') ||
345 (character >= '[' && character <= '`') ||
346 (character >= '{' && character <= '~');
347}
348
364SOURCEMETA_CORE_TEXT_EXPORT
365auto truncate(std::string &input, const std::size_t maximum_length,
366 const std::string_view marker) -> void;
367
379SOURCEMETA_CORE_TEXT_EXPORT
380auto replace(const std::string_view input, const std::string_view target,
381 const std::string_view replacement) -> std::string;
382
396SOURCEMETA_CORE_TEXT_EXPORT
397auto trim(const std::string_view input) noexcept -> std::string_view;
398
411SOURCEMETA_CORE_TEXT_EXPORT
412auto strip_left(const std::string_view input, const char character) noexcept
413 -> std::string_view;
414
427SOURCEMETA_CORE_TEXT_EXPORT
428auto strip_right(const std::string_view input, const char character) noexcept
429 -> std::string_view;
430
443template <typename Predicate>
444 requires std::predicate<Predicate &, char>
445constexpr auto
446strip_left(const std::string_view input, Predicate predicate) noexcept(
447 std::is_nothrow_invocable_r_v<bool, Predicate &, char> &&
448 std::is_nothrow_copy_constructible_v<Predicate>) -> std::string_view {
449 std::string_view result{input};
450 while (!result.empty() && predicate(result.front())) {
451 result.remove_prefix(1);
452 }
453
454 return result;
455}
456
469template <typename Predicate>
470 requires std::predicate<Predicate &, char>
471constexpr auto
472strip_right(const std::string_view input, Predicate predicate) noexcept(
473 std::is_nothrow_invocable_r_v<bool, Predicate &, char> &&
474 std::is_nothrow_copy_constructible_v<Predicate>) -> std::string_view {
475 std::string_view result{input};
476 while (!result.empty() && predicate(result.back())) {
477 result.remove_suffix(1);
478 }
479
480 return result;
481}
482
496template <typename Predicate>
497 requires std::predicate<Predicate &, char>
498constexpr auto trim(const std::string_view input, Predicate predicate) noexcept(
499 std::is_nothrow_invocable_r_v<bool, Predicate &, char> &&
500 std::is_nothrow_copy_constructible_v<Predicate>) -> std::string_view {
501 return strip_right(strip_left(input, predicate), predicate);
502}
503
518constexpr auto unquote(const std::string_view input, const char quote) noexcept
519 -> std::string_view {
520 if (input.size() < 2 || input.front() != quote || input.back() != quote) {
521 return input;
522 }
523
524 auto inner{input};
525 inner.remove_prefix(1);
526 inner.remove_suffix(1);
527 return inner;
528}
529
542SOURCEMETA_CORE_TEXT_EXPORT
543auto pad_left(const std::string_view input, const std::size_t width,
544 const char character) -> std::string;
545
559SOURCEMETA_CORE_TEXT_EXPORT
560auto take_until(const std::string_view input, const char marker) noexcept
561 -> std::string_view;
562
579SOURCEMETA_CORE_TEXT_EXPORT
580auto split_once(const std::string_view input, const char delimiter) noexcept
581 -> std::optional<std::pair<std::string_view, std::string_view>>;
582
598SOURCEMETA_CORE_TEXT_EXPORT
599auto split_once(const std::string_view input,
600 const std::string_view delimiter) noexcept
601 -> std::optional<std::pair<std::string_view, std::string_view>>;
602
619SOURCEMETA_CORE_TEXT_EXPORT
620auto rsplit_once(const std::string_view input, const char delimiter) noexcept
621 -> std::optional<std::pair<std::string_view, std::string_view>>;
622
637template <typename Callback>
638auto split(const std::string_view input, const char delimiter,
639 Callback callback) -> void {
640 std::string_view rest{input};
641 while (true) {
642 const auto next{sourcemeta::core::split_once(rest, delimiter)};
643 if (!next.has_value()) {
644 callback(rest);
645 return;
646 }
647 callback(next->first);
648 rest = next->second;
649 }
650}
651
667inline auto split(const std::string_view input, const char delimiter)
668 -> std::vector<std::string_view> {
669 std::vector<std::string_view> parts;
671 input, delimiter,
672 [&parts](const std::string_view part) -> void { parts.push_back(part); });
673 return parts;
674}
675
690template <typename Range>
691auto join(const Range &items, const std::string_view separator) -> std::string {
692 std::string result;
693 bool first{true};
694 for (const auto &item : items) {
695 if (!first) {
696 result.append(separator);
697 }
698
699 result.append(item);
700 first = false;
701 }
702
703 return result;
704}
705
720template <typename Range>
721auto join_to(std::ostream &stream, const Range &items,
722 const std::string_view separator) -> void {
723 bool first{true};
724 for (const auto &item : items) {
725 if (!first) {
726 stream << separator;
727 }
728 stream << item;
729 first = false;
730 }
731}
732
738template <typename Type>
740 std::integral<Type> && !std::same_as<std::remove_cv_t<Type>, bool> &&
741 sizeof(Type) <= sizeof(std::uint64_t);
742
748template <typename Integer>
750inline constexpr std::size_t DIGITS_CAPACITY{
751 static_cast<std::size_t>(std::numeric_limits<Integer>::digits10) + 1 +
752 (std::numeric_limits<Integer>::is_signed ? 1U : 0U)};
753
759using DigitsBuffer = std::array<char, std::max(DIGITS_CAPACITY<std::uint64_t>,
761
784template <typename Integer, std::size_t Capacity>
786 (Capacity >= DIGITS_CAPACITY<Integer>)
787inline auto digits_view(const Integer value,
788 std::array<char, Capacity> &buffer) noexcept
789 -> std::string_view {
790 const auto result{
791 std::to_chars(buffer.data(), buffer.data() + buffer.size(), value)};
792 return {buffer.data(), static_cast<std::size_t>(result.ptr - buffer.data())};
793}
794
809template <typename Output, typename Integer>
810 requires text_spellable_integer<Integer>
811inline auto digits_append(Output &output, const Integer value) -> void {
812 std::array<char, DIGITS_CAPACITY<Integer>> buffer;
813 const auto digits{digits_view(value, buffer)};
814 output.append(digits.data(), digits.size());
815}
816
833template <typename CharT, typename Traits, typename Integer>
834 requires text_spellable_integer<Integer>
835inline auto digits_write(std::basic_ostream<CharT, Traits> &stream,
836 const Integer value) -> void {
837 std::array<char, DIGITS_CAPACITY<Integer>> buffer;
838 const auto digits{digits_view(value, buffer)};
839 stream.write(digits.data(), static_cast<std::streamsize>(digits.size()));
840}
841
855inline auto hex_digit_value(const char character) noexcept -> std::int8_t {
856 // Indexed by byte value: ASCII '0'-'9', 'A'-'F', and 'a'-'f' map to their
857 // hexadecimal value, everything else to -1
858 static constexpr std::array<std::int8_t, 256> TABLE{
859 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
860 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
861 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5,
862 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
863 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
864 -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1,
865 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
866 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
867 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
868 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
869 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
870 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
871 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
872 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
873 -1, -1, -1, -1}};
874 return TABLE[static_cast<unsigned char>(character)];
875}
876
889inline auto is_hex_digit(const char character) noexcept -> bool {
890 return hex_digit_value(character) >= 0;
891}
892
906inline auto is_percent_triplet(const std::string_view input,
907 const std::size_t position) noexcept -> bool {
908 return input.size() >= 3 && position < input.size() - 2 &&
909 input[position] == '%' && is_hex_digit(input[position + 1]) &&
910 is_hex_digit(input[position + 2]);
911}
912
928SOURCEMETA_CORE_TEXT_EXPORT
929auto hex_to_bytes(const std::string_view input,
930 const bool allow_odd_length = false)
931 -> std::optional<std::string>;
932
943SOURCEMETA_CORE_TEXT_EXPORT
944auto bytes_to_hex(const std::string_view input) -> std::string;
945
967SOURCEMETA_CORE_TEXT_EXPORT
968auto bytes_to_hex(const std::span<const std::uint8_t> input,
969 const std::span<char> output) noexcept -> void;
970
986inline auto equals_ignore_case(const std::string_view left,
987 const std::string_view right) noexcept -> bool {
988 if (left.size() != right.size()) {
989 return false;
990 }
991
992 for (std::size_t index{0}; index < left.size(); ++index) {
993 if (to_lowercase(left[index]) != to_lowercase(right[index])) {
994 return false;
995 }
996 }
997
998 return true;
999}
1000
1015inline auto starts_with_ignore_case(const std::string_view value,
1016 const std::string_view prefix) noexcept
1017 -> bool {
1018 if (value.size() < prefix.size()) {
1019 return false;
1020 }
1021
1022 for (std::size_t index{0}; index < prefix.size(); ++index) {
1023 if (to_lowercase(value[index]) != to_lowercase(prefix[index])) {
1024 return false;
1025 }
1026 }
1027
1028 return true;
1029}
1030
1045inline auto less_ignore_case(const std::string_view left,
1046 const std::string_view right) noexcept -> bool {
1047 const auto length{left.size() < right.size() ? left.size() : right.size()};
1048 for (std::size_t index{0}; index < length; ++index) {
1049 const auto left_lower{to_lowercase(left[index])};
1050 const auto right_lower{to_lowercase(right[index])};
1051 if (left_lower != right_lower) {
1052 return left_lower < right_lower;
1053 }
1054 }
1055
1056 return left.size() < right.size();
1057}
1058
1070SOURCEMETA_CORE_TEXT_EXPORT
1071auto squeeze(const std::string_view input, const char character) -> std::string;
1072
1088template <typename Output>
1089auto squeeze(const std::string_view input, const char character, Output &output)
1090 -> void {
1091 bool in_run{false};
1092 for (const auto value : input) {
1093 if (value == character) {
1094 if (!in_run) {
1095 output.push_back(value);
1096 }
1097
1098 in_run = true;
1099 } else {
1100 output.push_back(value);
1101 in_run = false;
1102 }
1103 }
1104}
1105
1121SOURCEMETA_CORE_TEXT_EXPORT
1122auto remove_suffix_ignore_case(const std::string_view input,
1123 const std::string_view suffix) noexcept
1124 -> std::string_view;
1125
1126} // namespace sourcemeta::core
1127
1128#endif
@ Type
The token type does not match the expected media type.
Definition jose_verify.h:216
constexpr auto is_digit(const char character) -> bool
Definition numeric_util.h:51
SOURCEMETA_CORE_TEXT_EXPORT auto truncate(std::string &input, const std::size_t maximum_length, const std::string_view marker) -> void
auto digits_write(std::basic_ostream< CharT, Traits > &stream, const Integer value) -> void
Definition text.h:835
constexpr auto is_alphanum(const Character character) noexcept -> bool
Definition text.h:293
SOURCEMETA_CORE_TEXT_EXPORT auto to_title_case(std::string &value) -> void
auto digits_append(Output &output, const Integer value) -> void
Definition text.h:811
auto join_to(std::ostream &stream, const Range &items, const std::string_view separator) -> void
Definition text.h:721
constexpr auto to_lowercase(const Character character) noexcept -> Character
Definition text.h:75
SOURCEMETA_CORE_TEXT_EXPORT auto bytes_to_hex(const std::string_view input) -> std::string
auto less_ignore_case(const std::string_view left, const std::string_view right) noexcept -> bool
Definition text.h:1045
auto is_hex_digit(const char character) noexcept -> bool
Definition text.h:889
std::array< char, std::max(DIGITS_CAPACITY< std::uint64_t >, DIGITS_CAPACITY< std::int64_t >)> DigitsBuffer
Definition text.h:759
constexpr auto is_punctuation(const Character character) noexcept -> bool
Definition text.h:342
constexpr auto is_alpha(const Character character) noexcept -> bool
Definition text.h:201
auto split(const std::string_view input, const char delimiter, Callback callback) -> void
Definition text.h:638
SOURCEMETA_CORE_TEXT_EXPORT auto hex_to_bytes(const std::string_view input, const bool allow_odd_length=false) -> std::optional< std::string >
auto digits_view(const Integer value, std::array< char, Capacity > &buffer) noexcept -> std::string_view
Definition text.h:787
auto equals_ignore_case(const std::string_view left, const std::string_view right) noexcept -> bool
Definition text.h:986
SOURCEMETA_CORE_TEXT_EXPORT auto strip_left(const std::string_view input, const char character) noexcept -> std::string_view
constexpr auto unquote(const std::string_view input, const char quote) noexcept -> std::string_view
Definition text.h:518
SOURCEMETA_CORE_TEXT_EXPORT auto replace(const std::string_view input, const std::string_view target, const std::string_view replacement) -> std::string
auto hex_digit_value(const char character) noexcept -> std::int8_t
Definition text.h:855
SOURCEMETA_CORE_TEXT_EXPORT auto remove_suffix_ignore_case(const std::string_view input, const std::string_view suffix) noexcept -> std::string_view
auto join(const Range &items, const std::string_view separator) -> std::string
Definition text.h:691
SOURCEMETA_CORE_TEXT_EXPORT auto strip_right(const std::string_view input, const char character) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto trim(const std::string_view input) noexcept -> std::string_view
constexpr auto is_lowercase(const Character character) noexcept -> bool
Definition text.h:138
SOURCEMETA_CORE_TEXT_EXPORT auto squeeze(const std::string_view input, const char character) -> std::string
SOURCEMETA_CORE_TEXT_EXPORT auto take_until(const std::string_view input, const char marker) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto rsplit_once(const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >
SOURCEMETA_CORE_TEXT_EXPORT auto pad_left(const std::string_view input, const std::size_t width, const char character) -> std::string
auto is_percent_triplet(const std::string_view input, const std::size_t position) noexcept -> bool
Definition text.h:906
SOURCEMETA_CORE_TEXT_EXPORT auto split_once(const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >
constexpr std::size_t DIGITS_CAPACITY
Definition text.h:750
auto starts_with_ignore_case(const std::string_view value, const std::string_view prefix) noexcept -> bool
Definition text.h:1015