rapidyaml  0.11.1
parse and emit YAML, and do it fast
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 
18 // mark std::string as a writeable string type
19 template<class T> struct is_writeable_string;
20 template<> struct is_writeable_string<std::string> : public std::true_type {};
21 
22 
23 //-----------------------------------------------------------------------------
24 
25 /** get a writeable view to an existing std::string.
26  * When the string is empty, the returned view will be pointing
27  * at the character with value '\0', but the size will be zero.
28  * @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at
29  */
30 C4_ALWAYS_INLINE c4::substr to_substr(std::string &s) noexcept
31 {
32  #if C4_CPP < 11
33  #error this function will have undefined behavior
34  #endif
35  // since c++11 it is legal to call s[s.size()].
36  return c4::substr(&s[0], s.size()); // NOLINT(readability-container-data-pointer)
37 }
38 
39 /** get a readonly view to an existing std::string.
40  * When the string is empty, the returned view will be pointing
41  * at the character with value '\0', but the size will be zero.
42  * @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at
43  */
44 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string const& s) noexcept
45 {
46  #if C4_CPP < 11
47  #error this function will have undefined behavior
48  #endif
49  // since c++11 it is legal to call s[s.size()].
50  return c4::csubstr(&s[0], s.size()); // NOLINT(readability-container-data-pointer)
51 }
52 
53 //-----------------------------------------------------------------------------
54 
55 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) == 0; }
56 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) != 0; }
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 
62 C4_ALWAYS_INLINE bool operator== (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) == 0; }
63 C4_ALWAYS_INLINE bool operator!= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) != 0; }
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 
69 //-----------------------------------------------------------------------------
70 
71 /** copy a std::string to a writeable substr */
72 inline size_t to_chars(c4::substr buf, std::string const& s)
73 {
74  size_t sz = s.size();
75  size_t len = buf.len < sz ? buf.len : sz;
76  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
77  return sz; // return the number of needed chars
78 }
79 
80 /** copy a csubstr to an existing std::string */
81 inline bool from_chars(c4::csubstr buf, std::string * s)
82 {
83  s->resize(buf.len);
84  substr(&(*s)[0], buf.len).copy_from(buf); // NOLINT
85  return true;
86 }
87 
88 } // namespace c4
89 
90 #endif // _C4_STD_STRING_HPP_
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2366
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2210
substr to_substr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2208
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
a traits class to mark a type as a writeable string type (meaning c4::to_substr() can be used directl...
Definition: substr.hpp:2248
read+write string views