duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

string_view.hpp (2877B)


      1 #ifndef _C4_STD_STRING_VIEW_HPP_
      2 #define _C4_STD_STRING_VIEW_HPP_
      3 
      4 /** @file string_view.hpp */
      5 
      6 #ifndef C4CORE_SINGLE_HEADER
      7 #include "c4/language.hpp"
      8 #endif
      9 
     10 #if (C4_CPP >= 17 && defined(__cpp_lib_string_view))
     11 
     12 #ifndef C4CORE_SINGLE_HEADER
     13 #include "c4/substr.hpp"
     14 #endif
     15 
     16 #include <string_view>
     17 
     18 
     19 namespace c4 {
     20 
     21 //-----------------------------------------------------------------------------
     22 
     23 /** create a csubstr from an existing std::string_view. */
     24 C4_ALWAYS_INLINE c4::csubstr to_csubstr(std::string_view s) noexcept
     25 {
     26     return c4::csubstr(s.data(), s.size());
     27 }
     28 
     29 
     30 //-----------------------------------------------------------------------------
     31 
     32 C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) == 0; }
     33 C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) != 0; }
     34 C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) >= 0; }
     35 C4_ALWAYS_INLINE bool operator>  (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) >  0; }
     36 C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) <= 0; }
     37 C4_ALWAYS_INLINE bool operator<  (c4::csubstr ss, std::string_view s) { return ss.compare(s.data(), s.size()) <  0; }
     38 
     39 C4_ALWAYS_INLINE bool operator== (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) == 0; }
     40 C4_ALWAYS_INLINE bool operator!= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) != 0; }
     41 C4_ALWAYS_INLINE bool operator<= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >= 0; }
     42 C4_ALWAYS_INLINE bool operator<  (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) >  0; }
     43 C4_ALWAYS_INLINE bool operator>= (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <= 0; }
     44 C4_ALWAYS_INLINE bool operator>  (std::string_view s, c4::csubstr ss) { return ss.compare(s.data(), s.size()) <  0; }
     45 
     46 
     47 //-----------------------------------------------------------------------------
     48 
     49 /** copy an std::string_view to a writeable substr */
     50 inline size_t to_chars(c4::substr buf, std::string_view s)
     51 {
     52     C4_ASSERT(!buf.overlaps(to_csubstr(s)));
     53     size_t sz = s.size();
     54     size_t len = buf.len < sz ? buf.len : sz;
     55     // calling memcpy with null strings is undefined behavior
     56     // and will wreak havoc in calling code's branches.
     57     // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
     58     if(len)
     59     {
     60         C4_ASSERT(s.data() != nullptr);
     61         C4_ASSERT(buf.str != nullptr);
     62         memcpy(buf.str, s.data(), len);
     63     }
     64     return sz; // return the number of needed chars
     65 }
     66 
     67 } // namespace c4
     68 
     69 #endif // C4_STRING_VIEW_AVAILABLE
     70 
     71 #endif // _C4_STD_STRING_VIEW_HPP_