opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
string_helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <optional>
6#include <ostream>
7#include <sstream>
8#include <string>
9#include <string_view>
10#include <utility>
11
12namespace osc
13{
14 // returns true if `sv` contains `substr` (case-insensitive)
15 bool contains_case_insensitive(std::string_view sv, std::string_view substr);
16
17 // returns true if `b` is lexicographically greater than `a`, ignoring case
18 bool is_string_case_insensitive_greater_than(std::string_view a, std::string_view b);
19
20 // returns true if `a` is equal to `b` (case-insensitive)
21 bool is_equal_case_insensitive(std::string_view a, std::string_view b);
22
23 // returns true if:
24 //
25 // - `sv` is nonempty
26 // - the first character of `sv` is in the set [_a-zA-Z]
27 // - the remaining characters of `sv` are in the set [_a-zA-Z0-9]
28 // - (sorry UTF8ers)
29 //
30 // i.e. it would be a valid identifier in, say, a scripting language or tree
31 bool is_valid_identifier(std::string_view sv);
32
33 // returns a substring of `sv` without leading/trailing whitespace
34 std::string_view strip_whitespace(std::string_view sv);
35
36 // (tries to) convert `sv` to a floating point number
37 //
38 // - strips leading and trailing whitespace
39 //
40 // - parses the remaining characters as a locale-dependent floating point
41 // number, internally using something like `std::strtof` (which depends
42 // on C locale - careful)
43 //
44 // returns the resulting float if successful, or `std::nullopt` if it fails
45 std::optional<float> from_chars_strip_whitespace(std::string_view sv);
46
47 // returns a string that *may* be truncated with ellipsis (...) if the length
48 // of `sv` exceeds `max_length`
49 std::string truncate_with_ellipsis(std::string_view sv, size_t max_length);
50
51 // returns the end of the string between the last occurrence of `delimiter` and
52 // the end of `sv`, or `sv` if `delimiter` does not occur within `sv`.
53 std::string_view substring_after_last(std::string_view sv, std::string_view::value_type delimiter);
54
55 // Splits `sv` on the last occurrence of `delimiter` and returns the prefix
56 // before `delimiter` and suffix after `delimiter`.
57 std::optional<std::pair<std::string_view, std::string_view>> rsplit_once(
58 std::string_view sv,
59 std::string_view::value_type delimiter
60 );
61
62 // converts the given byte into a 2-length hex character representation
63 //
64 // e.g. 0x00 --> ('0', '0')
65 // 0xf0 --> ('f', '0')
66 // 0x02 --> ('0', '2')
67 std::pair<char, char> to_hex_chars(uint8_t);
68 std::optional<uint8_t> try_parse_hex_chars_as_byte(char, char);
69
70 // satisfied if `T` can be written to a `std::ostream`
71 template<typename T>
72 concept OutputStreamable = requires (T v, std::ostream& o) {
73 { o << v } -> std::same_as<std::ostream&>;
74 };
75
76 // returns a string representation of `v` by first streaming it to a `std::stringstream`
77 template<OutputStreamable T>
78 std::string stream_to_string(const T& v)
79 {
80 std::stringstream ss;
81 ss << v;
82 return std::move(ss).str();
83 }
84
85 // returns a string that contains a string-ified version of each element in `r` joined
86 // with a given `delimiter`.
87 template<std::ranges::input_range R>
89 std::string join(R&& r, std::string_view delimiter)
90 {
91 std::stringstream ss;
92 std::string_view prefix_delimiter;
93 for (auto&& el : r) {
95 ss << el;
97 }
98 return std::move(ss).str();
99 }
100
101 // Returns a copy of `str`'s content, but with the first instance of `from` replaced
102 // with `to` (if any).
103 std::string replace(std::string_view str, std::string_view from, std::string_view to);
104}
Definition string_helpers.h:72
Definition custom_decoration_generator.h:5
std::string_view substring_after_last(std::string_view sv, std::string_view::value_type delimiter)
std::optional< std::pair< std::string_view, std::string_view > > rsplit_once(std::string_view sv, std::string_view::value_type delimiter)
bool is_valid_identifier(std::string_view sv)
bool is_string_case_insensitive_greater_than(std::string_view a, std::string_view b)
std::optional< uint8_t > try_parse_hex_chars_as_byte(char, char)
std::string_view strip_whitespace(std::string_view sv)
constexpr U to(T &&value)
Definition conversion.h:56
std::string join(R &&r, std::string_view delimiter)
Definition string_helpers.h:89
std::optional< float > from_chars_strip_whitespace(std::string_view sv)
std::string replace(std::string_view str, std::string_view from, std::string_view to)
std::string truncate_with_ellipsis(std::string_view sv, size_t max_length)
bool contains_case_insensitive(std::string_view sv, std::string_view substr)
std::pair< char, char > to_hex_chars(uint8_t)
bool is_equal_case_insensitive(std::string_view a, std::string_view b)
std::string stream_to_string(const T &v)
Definition string_helpers.h:78