rapidyaml  0.16.0
parseandemitYAML,anddoitfast
vector.hpp
Go to the documentation of this file.
1 #ifndef C4_STD_VECTOR_HPP_
2 #define C4_STD_VECTOR_HPP_
3 
4 /** @file vector.hpp provides conversion and comparison facilities
5  * from/between std::vector<char> to c4::substr and c4::csubstr.
6  * @todo add to_span() and friends
7  */
8 
9 #ifndef C4CORE_SINGLE_HEADER
10 #include "c4/substr.hpp"
11 #endif
12 
13 #if defined(__GNUC__) && (__GNUC__ >= 6)
14 C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wnull-dereference")
15 #endif
16 #include <vector>
17 #if defined(__GNUC__) && (__GNUC__ >= 6)
18 C4_SUPPRESS_WARNING_GCC_POP
19 #endif
20 
21 namespace c4 {
22 
23 // mark std::string as a string type
24 template<class T> struct is_string;
25 template<class Alloc> struct is_string<std::vector<char,Alloc>> : public std::true_type {};
26 template<class Alloc> struct is_string<const std::vector<char,Alloc>> : public std::true_type {};
27 
28 // mark std::string as a writeable string type
29 template<class T> struct is_writeable_string;
30 template<class Alloc> struct is_writeable_string<std::vector<char,Alloc>> : public std::true_type {};
31 
32 
33 //-----------------------------------------------------------------------------
34 
35 /** get a substr (writeable string view) of an existing std::vector<char> */
36 template<class Alloc>
37 c4::substr to_substr(std::vector<char, Alloc> &vec)
38 {
39  char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.
40  return c4::substr(data, vec.size());
41 }
42 
43 /** get a csubstr (read-only string) view of an existing std::vector<char> */
44 template<class Alloc>
45 c4::csubstr to_csubstr(std::vector<char, Alloc> const& vec)
46 {
47  const char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.
48  return c4::csubstr(data, vec.size());
49 }
50 
51 
52 //-----------------------------------------------------------------------------
53 // comparisons between substrings and std::vector<char>
54 
55 template<class Alloc> C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss != to_csubstr(s); }
56 template<class Alloc> C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss == to_csubstr(s); }
57 template<class Alloc> C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss >= to_csubstr(s); }
58 template<class Alloc> C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss > to_csubstr(s); }
59 template<class Alloc> C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss <= to_csubstr(s); }
60 template<class Alloc> C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss < to_csubstr(s); }
61 
62 template<class Alloc> C4_ALWAYS_INLINE bool operator!= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss != to_csubstr(s); }
63 template<class Alloc> C4_ALWAYS_INLINE bool operator== (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss == to_csubstr(s); }
64 template<class Alloc> C4_ALWAYS_INLINE bool operator>= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss <= to_csubstr(s); }
65 template<class Alloc> C4_ALWAYS_INLINE bool operator> (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss < to_csubstr(s); }
66 template<class Alloc> C4_ALWAYS_INLINE bool operator<= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss >= to_csubstr(s); }
67 template<class Alloc> C4_ALWAYS_INLINE bool operator< (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss > to_csubstr(s); }
68 
69 
70 //-----------------------------------------------------------------------------
71 
72 /** copy a std::vector<char> to a substr */
73 template<class Alloc>
74 inline size_t to_chars(c4::substr buf, std::vector<char, Alloc> const& s)
75 {
76  size_t sz = s.size();
77  size_t len = buf.len < sz ? buf.len : sz;
78  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
79  return sz; // return the number of needed chars
80 }
81 
82 /** copy a csubstr to an existing std::vector<char> */
83 template<class Alloc>
84 inline bool from_chars(c4::csubstr buf, std::vector<char, Alloc> * s)
85 {
86  s->resize(buf.len);
87  substr(s->data(), buf.len).copy_from(buf);
88  return true;
89 }
90 
91 } // namespace c4
92 
93 #endif // C4_STD_VECTOR_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
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
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 empty() const noexcept
Definition: substr.hpp:356
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