rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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)
14C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wnull-dereference")
15#endif
16#include <vector>
17#if defined(__GNUC__) && (__GNUC__ >= 6)
18C4_SUPPRESS_WARNING_GCC_POP
19#endif
20
21namespace c4 {
22
23// mark std::string as a string type
24template<class T> struct is_string;
25template<class Alloc> struct is_string<std::vector<char,Alloc>> : public std::true_type {};
26template<class Alloc> struct is_string<const std::vector<char,Alloc>> : public std::true_type {};
27
28// mark std::string as a writeable string type
29template<class T> struct is_writeable_string;
30template<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> */
36template<class Alloc>
37c4::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> */
44template<class Alloc>
45c4::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
55template<class Alloc> C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss != to_csubstr(s); }
56template<class Alloc> C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss == to_csubstr(s); }
57template<class Alloc> C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss >= to_csubstr(s); }
58template<class Alloc> C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss > to_csubstr(s); }
59template<class Alloc> C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss <= to_csubstr(s); }
60template<class Alloc> C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss < to_csubstr(s); }
61
62template<class Alloc> C4_ALWAYS_INLINE bool operator!= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss != to_csubstr(s); }
63template<class Alloc> C4_ALWAYS_INLINE bool operator== (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss == to_csubstr(s); }
64template<class Alloc> C4_ALWAYS_INLINE bool operator>= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss <= to_csubstr(s); }
65template<class Alloc> C4_ALWAYS_INLINE bool operator> (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss < to_csubstr(s); }
66template<class Alloc> C4_ALWAYS_INLINE bool operator<= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss >= to_csubstr(s); }
67template<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 */
73template<class Alloc>
74inline 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> */
83template<class Alloc>
84inline 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 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
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
bool empty() const noexcept
Definition substr.hpp:356
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