rapidyaml  0.16.0
parseandemitYAML,anddoitfast
span.hpp
Go to the documentation of this file.
1 #ifndef C4_STD_SPAN_HPP_
2 #define C4_STD_SPAN_HPP_
3 
4 /** @file span.hpp */
5 
6 #ifndef C4CORE_SINGLE_HEADER
7 #include "c4/language.hpp"
8 #endif
9 
10 
11 #if (C4_CPP >= 20) || defined(__DOXYGEN__)
12 
13 #ifndef C4CORE_SINGLE_HEADER
14 #include "c4/substr.hpp"
15 #endif
16 
17 #include <span>
18 
19 
20 namespace c4 {
21 
22 template<class T> struct is_string;
23 template<class T> struct is_writeable_string;
24 
25 // mark std::span<const char> as a string type
26 template<> struct is_string<std::span<const char>> : public std::true_type {};
27 template<> struct is_string<const std::span<const char>> : public std::true_type {};
28 
29 // mark std::span<char> as a string type
30 template<> struct is_string<std::span<char>> : public std::true_type {};
31 template<> struct is_string<const std::span<char>> : public std::true_type {};
32 template<> struct is_writeable_string<std::span<char>> : public std::true_type {};
33 template<> struct is_writeable_string<const std::span<char>> : public std::true_type {};
34 
35 
36 //-----------------------------------------------------------------------------
37 
38 /** create a csubstr from an existing std::span<const char> */
39 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::span<const char> s) noexcept
40 {
41  return c4::csubstr(s.data(), s.size());
42 }
43 
44 /** create a csubstr from an existing std::span<char> */
45 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::span<char> s) noexcept
46 {
47  return c4::csubstr(s.data(), s.size());
48 }
49 /** create a substr from an existing std::span<char> */
50 C4_ALWAYS_INLINE c4::substr to_substr(std::span<char> s) noexcept
51 {
52  return c4::substr(s.data(), s.size());
53 }
54 
55 
56 //-----------------------------------------------------------------------------
57 
58 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) == 0; }
59 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) != 0; }
60 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) >= 0; }
61 C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) > 0; }
62 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) <= 0; }
63 C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) < 0; }
64 
65 C4_ALWAYS_INLINE bool operator== (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
66 C4_ALWAYS_INLINE bool operator!= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
67 C4_ALWAYS_INLINE bool operator<= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
68 C4_ALWAYS_INLINE bool operator< (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
69 C4_ALWAYS_INLINE bool operator>= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
70 C4_ALWAYS_INLINE bool operator> (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) < 0; }
71 
72 
73 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) == 0; }
74 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) != 0; }
75 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) >= 0; }
76 C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) > 0; }
77 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) <= 0; }
78 C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) < 0; }
79 
80 C4_ALWAYS_INLINE bool operator== (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
81 C4_ALWAYS_INLINE bool operator!= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
82 C4_ALWAYS_INLINE bool operator<= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
83 C4_ALWAYS_INLINE bool operator< (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
84 C4_ALWAYS_INLINE bool operator>= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
85 C4_ALWAYS_INLINE bool operator> (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) < 0; }
86 
87 
88 //-----------------------------------------------------------------------------
89 
90 /** copy a std::span<const char> to a writeable substr */
91 C4_ALWAYS_INLINE size_t to_chars(c4::substr buf, std::span<const char> s)
92 {
93  size_t sz = s.size();
94  size_t len = buf.len < sz ? buf.len : sz;
95  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
96  return sz; // return the number of needed chars
97 }
98 
99 /** copy a std::span<char> to a writeable substr */
100 inline size_t to_chars(c4::substr buf, std::span<char> s)
101 {
102  size_t sz = s.size();
103  size_t len = buf.len < sz ? buf.len : sz;
104  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
105  return sz; // return the number of needed chars
106 }
107 
108 /** copy a csubstr to an existing std::span<char> */
109 inline bool from_chars(c4::csubstr buf, std::span<char> * s)
110 {
111  if(buf.len <= s->size())
112  {
113  substr(s->data(), buf.len).copy_from(buf);
114  *s = s->first(buf.len);
115  return true;
116  }
117  return false;
118 }
119 
120 } // namespace c4
121 
122 #endif // SPAN_AVAILABLE
123 
124 #endif // C4_STD_SPAN_HPP_
bool operator!=(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2439
a traits class to mark a type as a writeable string type, meaning c4::to_substr() can be used directl...
Definition: substr.hpp:140
substr to_substr(char(&s)[N]) noexcept
Definition: substr.hpp:2376
read+write string views
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2383
bool operator>=(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2443
bool operator>(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2441
a traits class to mark a type as a string type, meaning c4::to_csubstr() can be used directly instead...
Definition: substr.hpp:131
int compare(C const c) const noexcept
Definition: substr.hpp:385
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition: substr.hpp:2380
basic_substring< char > substr
a mutable string view
Definition: substr.hpp:2355
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
auto copy_from(ro_substr that) -> typename std::enable_if< !std::is_const< U >::value, void >::type
copy a string to this substr, starting at 0
Definition: substr.hpp:2165
size_t len
the length of the substring
Definition: substr.hpp:218
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2342
bool operator==(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2438