Path: blob/master/dep/rapidyaml/include/c4/std/string.hpp
4265 views
#ifndef _C4_STD_STRING_HPP_1#define _C4_STD_STRING_HPP_23/** @file string.hpp */45#ifndef C4CORE_SINGLE_HEADER6#include "c4/substr.hpp"7#endif89#include <string>1011namespace c4 {1213//-----------------------------------------------------------------------------1415/** get a writeable view to an existing std::string.16* When the string is empty, the returned view will be pointing17* at the character with value '\0', but the size will be zero.18* @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at19*/20C4_ALWAYS_INLINE c4::substr to_substr(std::string &s) noexcept21{22#if C4_CPP < 1123#error this function will have undefined behavior24#endif25// since c++11 it is legal to call s[s.size()].26return c4::substr(&s[0], s.size());27}2829/** get a readonly view to an existing std::string.30* When the string is empty, the returned view will be pointing31* at the character with value '\0', but the size will be zero.32* @see https://en.cppreference.com/w/cpp/string/basic_string/operator_at33*/34C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string const& s) noexcept35{36#if C4_CPP < 1137#error this function will have undefined behavior38#endif39// since c++11 it is legal to call s[s.size()].40return c4::csubstr(&s[0], s.size());41}4243//-----------------------------------------------------------------------------4445C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) == 0; }46C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) != 0; }47C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) >= 0; }48C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) > 0; }49C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) <= 0; }50C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::string const& s) { return ss.compare(to_csubstr(s)) < 0; }5152C4_ALWAYS_INLINE bool operator== (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) == 0; }53C4_ALWAYS_INLINE bool operator!= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) != 0; }54C4_ALWAYS_INLINE bool operator>= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) <= 0; }55C4_ALWAYS_INLINE bool operator> (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) < 0; }56C4_ALWAYS_INLINE bool operator<= (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) >= 0; }57C4_ALWAYS_INLINE bool operator< (std::string const& s, c4::csubstr ss) { return ss.compare(to_csubstr(s)) > 0; }5859//-----------------------------------------------------------------------------6061/** copy an std::string to a writeable string view */62inline size_t to_chars(c4::substr buf, std::string const& s)63{64C4_ASSERT(!buf.overlaps(to_csubstr(s)));65size_t len = buf.len < s.size() ? buf.len : s.size();66// calling memcpy with null strings is undefined behavior67// and will wreak havoc in calling code's branches.68// see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-126213363769if(len)70{71C4_ASSERT(s.data() != nullptr);72C4_ASSERT(buf.str != nullptr);73memcpy(buf.str, s.data(), len);74}75return s.size(); // return the number of needed chars76}7778/** copy a string view to an existing std::string */79inline bool from_chars(c4::csubstr buf, std::string * s)80{81s->resize(buf.len);82C4_ASSERT(!buf.overlaps(to_csubstr(*s)));83// calling memcpy with null strings is undefined behavior84// and will wreak havoc in calling code's branches.85// see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-126213363786if(buf.len)87{88C4_ASSERT(buf.str != nullptr);89memcpy(&(*s)[0], buf.str, buf.len);90}91return true;92}9394} // namespace c49596#endif // _C4_STD_STRING_HPP_979899