rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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
23namespace c4 {
24
25// mark std::string_view as a string type
26template<class T> struct is_string;
27template<> struct is_string<std::string_view> : public std::true_type {};
28template<> 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. */
34C4_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
42C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) == 0; }
43C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) != 0; }
44C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) >= 0; }
45C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) > 0; }
46C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) <= 0; }
47C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) < 0; }
48
49C4_ALWAYS_INLINE bool operator== (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
50C4_ALWAYS_INLINE bool operator!= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
51C4_ALWAYS_INLINE bool operator<= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
52C4_ALWAYS_INLINE bool operator< (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) > 0; }
53C4_ALWAYS_INLINE bool operator>= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
54C4_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 */
60inline 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_
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2381
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: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:2442
bool operator>=(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2444
bool operator<=(const char c, basic_substring< C > const that) noexcept
Definition substr.hpp:2443
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
size_t to_chars(substr buf, uint8_t v) noexcept
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
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:2166
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
read+write string views