rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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
20namespace c4 {
21
22template<class T> struct is_string;
23template<class T> struct is_writeable_string;
24
25// mark std::span<const char> as a string type
26template<> struct is_string<std::span<const char>> : public std::true_type {};
27template<> struct is_string<const std::span<const char>> : public std::true_type {};
28
29// mark std::span<char> as a string type
30template<> struct is_string<std::span<char>> : public std::true_type {};
31template<> struct is_string<const std::span<char>> : public std::true_type {};
32template<> struct is_writeable_string<std::span<char>> : public std::true_type {};
33template<> 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> */
39C4_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> */
45C4_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> */
50C4_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
58C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) == 0; }
59C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) != 0; }
60C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) >= 0; }
61C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) > 0; }
62C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) <= 0; }
63C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::span<char> s) { return ss.compare(s.data(), s.size()) < 0; }
64
65C4_ALWAYS_INLINE bool operator== (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
66C4_ALWAYS_INLINE bool operator!= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
67C4_ALWAYS_INLINE bool operator<= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
68C4_ALWAYS_INLINE bool operator< (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
69C4_ALWAYS_INLINE bool operator>= (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
70C4_ALWAYS_INLINE bool operator> (std::span<char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) < 0; }
71
72
73C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) == 0; }
74C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) != 0; }
75C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) >= 0; }
76C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) > 0; }
77C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) <= 0; }
78C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::span<const char> s) { return ss.compare(s.data(), s.size()) < 0; }
79
80C4_ALWAYS_INLINE bool operator== (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
81C4_ALWAYS_INLINE bool operator!= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
82C4_ALWAYS_INLINE bool operator<= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
83C4_ALWAYS_INLINE bool operator< (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
84C4_ALWAYS_INLINE bool operator>= (std::span<const char> s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
85C4_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 */
91C4_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 */
100inline 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> */
109inline 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 from_chars(csubstr buf, uint8_t *v) noexcept
substr to_substr(char(&s)[N]) noexcept
Definition substr.hpp:2376
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2380
bool operator<(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2440
bool operator!=(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2439
bool operator==(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2438
bool operator>(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2441
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:2442
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
size_t to_chars(substr buf, uint8_t v) noexcept
int compare(C const c) const noexcept
Definition substr.hpp:385
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
a traits class to mark a type as a string type, meaning c4::to_csubstr() can be used directly instead...
Definition substr.hpp:134
a traits class to mark a type as a writeable string type, meaning c4::to_substr() can be used directl...
Definition substr.hpp:143
read+write string views