rapidyaml  0.11.0
parse and emit YAML, and do it fast
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 #include "c4/language.hpp"
8 #endif
9 
10 #if (C4_CPP >= 17 && defined(__cpp_lib_string_view)) || defined(__DOXYGEN__)
11 
12 #ifndef C4CORE_SINGLE_HEADER
13 #include "c4/substr.hpp"
14 #endif
15 
16 #include <string_view>
17 
18 
19 namespace c4 {
20 
21 // mark std::string_view as a string type
22 template<class T> struct is_string;
23 template<> struct is_string<std::string_view> : public std::true_type {};
24 
25 
26 //-----------------------------------------------------------------------------
27 
28 /** create a csubstr from an existing std::string_view. */
29 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string_view s) noexcept
30 {
31  return c4::csubstr(s.data(), s.size());
32 }
33 
34 
35 //-----------------------------------------------------------------------------
36 
37 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) == 0; }
38 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) != 0; }
39 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) >= 0; }
40 C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) > 0; }
41 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) <= 0; }
42 C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) < 0; }
43 
44 C4_ALWAYS_INLINE bool operator== (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
45 C4_ALWAYS_INLINE bool operator!= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
46 C4_ALWAYS_INLINE bool operator<= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
47 C4_ALWAYS_INLINE bool operator< (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
48 C4_ALWAYS_INLINE bool operator>= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
49 C4_ALWAYS_INLINE bool operator> (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) < 0; }
50 
51 
52 //-----------------------------------------------------------------------------
53 
54 /** copy an std::string_view to a writeable substr */
55 inline size_t to_chars(c4::substr buf, std::string_view s)
56 {
57  size_t sz = s.size();
58  size_t len = buf.len < sz ? buf.len : sz;
59  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
60  return sz; // return the number of needed chars
61 }
62 
63 } // namespace c4
64 
65 #endif // STRING_VIEW_AVAILABLE
66 
67 #endif // _C4_STD_STRING_VIEW_HPP_
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2210
bool operator!=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2283
bool operator>(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2285
bool operator>=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2287
bool operator<=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2286
bool operator==(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2282
bool operator<(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2284
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2331
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
a traits class to mark a type as a string type (meaning c4::to_csubstr() can be used directly).
Definition: substr.hpp:2245
read+write string views