rapidyaml  0.16.0
parseandemitYAML,anddoitfast
string_view.hpp
Go to the documentation of this file.
1 #ifndef C4_STD_STRING_VIEW_HPP_
2 #define C4_STD_STRING_VIEW_HPP_
3 
4 /** @file string_view.hpp */
5 
6 #ifndef C4CORE_SINGLE_HEADER
7 #ifndef C4_LANGUAGE_HPP_
8 #include "c4/language.hpp"
9 #endif
10 #endif
11 
12 #if (C4_CPP >= 17 && defined(__cpp_lib_string_view)) || defined(__DOXYGEN__)
13 
14 #ifndef C4CORE_SINGLE_HEADER
15 #ifndef C4_SUBSTR_HPP_
16 #include "c4/substr.hpp"
17 #endif
18 #endif
19 
20 #include <string_view>
21 
22 
23 namespace c4 {
24 
25 // mark std::string_view as a string type
26 template<class T> struct is_string;
27 template<> struct is_string<std::string_view> : public std::true_type {};
28 template<> struct is_string<const std::string_view> : public std::true_type {};
29 
30 
31 //-----------------------------------------------------------------------------
32 
33 /** create a csubstr from an existing std::string_view. */
34 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string_view s) noexcept
35 {
36  return c4::csubstr(s.data(), s.size());
37 }
38 
39 
40 //-----------------------------------------------------------------------------
41 
42 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) == 0; }
43 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) != 0; }
44 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) >= 0; }
45 C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) > 0; }
46 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) <= 0; }
47 C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) < 0; }
48 
49 C4_ALWAYS_INLINE bool operator== (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
50 C4_ALWAYS_INLINE bool operator!= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
51 C4_ALWAYS_INLINE bool operator<= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
52 C4_ALWAYS_INLINE bool operator< (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
53 C4_ALWAYS_INLINE bool operator>= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
54 C4_ALWAYS_INLINE bool operator> (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) < 0; }
55 
56 
57 //-----------------------------------------------------------------------------
58 
59 /** copy an std::string_view to a writeable substr */
60 inline size_t to_chars(c4::substr buf, std::string_view s)
61 {
62  size_t sz = s.size();
63  size_t len = buf.len < sz ? buf.len : sz;
64  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
65  return sz; // return the number of needed chars
66 }
67 
68 } // namespace c4
69 
70 #endif // STRING_VIEW_AVAILABLE
71 
72 #endif // C4_STD_STRING_VIEW_HPP_
bool operator!=(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2439
read+write string views
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< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
bool operator<(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2440
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
bool operator<=(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2442
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