Path: blob/master/dep/rapidyaml/include/c4/std/vector.hpp
4265 views
#ifndef _C4_STD_VECTOR_HPP_1#define _C4_STD_VECTOR_HPP_23/** @file vector.hpp provides conversion and comparison facilities4* from/between std::vector<char> to c4::substr and c4::csubstr.5* @todo add to_span() and friends6*/78#ifndef C4CORE_SINGLE_HEADER9#include "c4/substr.hpp"10#endif1112#include <vector>1314namespace c4 {1516//-----------------------------------------------------------------------------1718/** get a substr (writeable string view) of an existing std::vector<char> */19template<class Alloc>20c4::substr to_substr(std::vector<char, Alloc> &vec)21{22char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.23return c4::substr(data, vec.size());24}2526/** get a csubstr (read-only string) view of an existing std::vector<char> */27template<class Alloc>28c4::csubstr to_csubstr(std::vector<char, Alloc> const& vec)29{30const char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.31return c4::csubstr(data, vec.size());32}3334//-----------------------------------------------------------------------------35// comparisons between substrings and std::vector<char>3637template<class Alloc> C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss != to_csubstr(s); }38template<class Alloc> C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss == to_csubstr(s); }39template<class Alloc> C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss >= to_csubstr(s); }40template<class Alloc> C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss > to_csubstr(s); }41template<class Alloc> C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss <= to_csubstr(s); }42template<class Alloc> C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss < to_csubstr(s); }4344template<class Alloc> C4_ALWAYS_INLINE bool operator!= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss != to_csubstr(s); }45template<class Alloc> C4_ALWAYS_INLINE bool operator== (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss == to_csubstr(s); }46template<class Alloc> C4_ALWAYS_INLINE bool operator>= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss <= to_csubstr(s); }47template<class Alloc> C4_ALWAYS_INLINE bool operator> (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss < to_csubstr(s); }48template<class Alloc> C4_ALWAYS_INLINE bool operator<= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss >= to_csubstr(s); }49template<class Alloc> C4_ALWAYS_INLINE bool operator< (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss > to_csubstr(s); }5051//-----------------------------------------------------------------------------5253/** copy a std::vector<char> to a writeable string view */54template<class Alloc>55inline size_t to_chars(c4::substr buf, std::vector<char, Alloc> const& s)56{57C4_ASSERT(!buf.overlaps(to_csubstr(s)));58size_t len = buf.len < s.size() ? buf.len : s.size();59// calling memcpy with null strings is undefined behavior60// and will wreak havoc in calling code's branches.61// see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-126213363762if(len > 0)63{64memcpy(buf.str, s.data(), len);65}66return s.size(); // return the number of needed chars67}6869/** copy a string view to an existing std::vector<char> */70template<class Alloc>71inline bool from_chars(c4::csubstr buf, std::vector<char, Alloc> * s)72{73s->resize(buf.len);74C4_ASSERT(!buf.overlaps(to_csubstr(*s)));75// calling memcpy with null strings is undefined behavior76// and will wreak havoc in calling code's branches.77// see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-126213363778if(buf.len > 0)79{80memcpy(&(*s)[0], buf.str, buf.len);81}82return true;83}8485} // namespace c48687#endif // _C4_STD_VECTOR_HPP_888990