rapidyaml  0.16.0
parseandemitYAML,anddoitfast
string.hpp
Go to the documentation of this file.
1 #ifndef C4_STD_STRING_HPP_
2 #define C4_STD_STRING_HPP_
3 
4 /** @file string.hpp */
5 
6 #ifndef C4CORE_SINGLE_HEADER
7 #include "c4/substr.hpp"
8 #endif
9 
10 #include <string>
11 
12 namespace c4 {
13 
14 // mark std::string as a string type
15 template<class T> struct is_string;
16 template<> struct is_string<std::string> : public std::true_type {};
17 template<> struct is_string<const std::string> : public std::true_type {};
18 
19 // mark std::string as a writeable string type
20 template<class T> struct is_writeable_string;
21 template<> struct is_writeable_string<std::string> : public std::true_type {};
22 
23 
24 //-----------------------------------------------------------------------------
25 
26 /** get a writeable view to an existing std::string.
27  * When the string is empty, the returned view will be pointing
28  * at the character with value '\0', but the size will be zero.
29  * @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at
30  */
31 C4_ALWAYS_INLINE c4::substr to_substr(std::string &s) noexcept
32 {
33  #if C4_CPP < 11
34  #error this function requires c++11
35  #endif
36  // since c++11 it is legal to call s[s.size()].
37  return c4::substr(&s[0], s.size()); // NOLINT
38 }
39 
40 /** get a readonly view to an existing std::string.
41  * When the string is empty, the returned view will be pointing
42  * at the character with value '\0', but the size will be zero.
43  * @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at
44  */
45 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string const& s) noexcept
46 {
47  #if C4_CPP < 11
48  #error this function requires c++11
49  #endif
50  // since c++11 it is legal to call s[s.size()].
51  return c4::csubstr(&s[0], s.size()); // NOLINT
52 }
53 
54 
55 //-----------------------------------------------------------------------------
56 
57 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) == 0; }
58 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) != 0; }
59 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) >= 0; }
60 C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) > 0; }
61 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) <= 0; }
62 C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) < 0; }
63 
64 C4_ALWAYS_INLINE bool operator== (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) == 0; }
65 C4_ALWAYS_INLINE bool operator!= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) != 0; }
66 C4_ALWAYS_INLINE bool operator>= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) <= 0; }
67 C4_ALWAYS_INLINE bool operator> (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) < 0; }
68 C4_ALWAYS_INLINE bool operator<= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) >= 0; }
69 C4_ALWAYS_INLINE bool operator< (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) > 0; }
70 
71 
72 //-----------------------------------------------------------------------------
73 
74 /** copy a std::string to a writeable substr */
75 inline size_t to_chars(c4::substr buf, std::string const& s)
76 {
77  size_t sz = s.size();
78  size_t len = buf.len < sz ? buf.len : sz;
79  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
80  return sz; // return the number of needed chars
81 }
82 
83 /** copy a csubstr to an existing std::string */
84 inline bool from_chars(c4::csubstr buf, std::string * s)
85 {
86  s->resize(buf.len);
87  substr(&(*s)[0], buf.len).copy_from(buf); // NOLINT
88  return true;
89 }
90 
91 } // namespace c4
92 
93 #endif // C4_STD_STRING_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
a non-owning string-view, consisting of a character pointer and a length.
Definition: substr.hpp:211
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
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