rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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
12namespace c4 {
13
14// mark std::string as a string type
15template<class T> struct is_string;
16template<> struct is_string<std::string> : public std::true_type {};
17template<> struct is_string<const std::string> : public std::true_type {};
18
19// mark std::string as a writeable string type
20template<class T> struct is_writeable_string;
21template<> 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 */
31C4_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 */
45C4_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
57C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) == 0; }
58C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) != 0; }
59C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) >= 0; }
60C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) > 0; }
61C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) <= 0; }
62C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) < 0; }
63
64C4_ALWAYS_INLINE bool operator== (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) == 0; }
65C4_ALWAYS_INLINE bool operator!= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) != 0; }
66C4_ALWAYS_INLINE bool operator>= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) <= 0; }
67C4_ALWAYS_INLINE bool operator> (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) < 0; }
68C4_ALWAYS_INLINE bool operator<= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) >= 0; }
69C4_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 */
75inline 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 */
84inline 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 from_chars(csubstr buf, uint8_t *v) noexcept
substr to_substr(char(&s)[N]) noexcept
Definition substr.hpp:2377
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
a traits class to mark a type as a writeable string type, meaning c4::to_substr() can be used directl...
Definition substr.hpp:143
read+write string views