From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/ro-string.hxx | 430 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 430 insertions(+) create mode 100644 libxsd/xsd/cxx/ro-string.hxx (limited to 'libxsd/xsd/cxx/ro-string.hxx') diff --git a/libxsd/xsd/cxx/ro-string.hxx b/libxsd/xsd/cxx/ro-string.hxx new file mode 100644 index 0000000..33a70e6 --- /dev/null +++ b/libxsd/xsd/cxx/ro-string.hxx @@ -0,0 +1,430 @@ +// file : xsd/cxx/ro-string.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_RO_STRING_HXX +#define XSD_CXX_RO_STRING_HXX + +#include +#include // std::size_t +#include + +namespace xsd +{ + namespace cxx + { + // Read-only string class template. + // + template + class ro_string + { + public: + typedef std::char_traits traits_type; + typedef std::size_t size_type; + + static const size_type npos = ~(size_type (0)); + + public: + ro_string () + : data_ (0), size_ (0) + { + } + + ro_string (const C* s) + : data_ (s), size_ (traits_type::length (s)) + { + } + + ro_string (const C* s, size_type size) + : data_ (s), size_ (size) + { + } + + ro_string (const std::basic_string& s) + : data_ (s.data ()), size_ (s.size ()) + { + } + + operator std::basic_string () const + { + return std::basic_string (data (), size ()); + } + + private: + ro_string (const ro_string&); + + ro_string& + operator= (const ro_string&); + + public: + // The returned string is not necessarily terminated with '\0'. + // If size() returns 0, the returned pointer may be 0. + // + const C* + data () const + { + return data_; + } + + size_type + size () const + { + return size_; + } + + size_type + length () const + { + return size (); + } + + public: + bool + empty () const + { + return size () == 0; + } + + const C& + operator[] (size_type pos) const + { + return data ()[pos]; + } + + public: + void + assign (const C* s) + { + data_ = s; + size_ = traits_type::length (s); + } + + void + assign (const C* s, size_type size) + { + data_ = s; + size_ = size; + } + + void + assign (const std::basic_string& s) + { + data_ = s.c_str (); + size_ = s.size (); + } + + public: + int + compare (const ro_string& str) const + { + return compare (str.data (), str.size ()); + } + + int + compare (const std::basic_string& str) const + { + return compare (str.c_str (), str.size ()); + } + + int + compare (const C* str) const + { + return compare (str, traits_type::length (str)); + } + + int + compare (const C* str, size_type n) const + { + size_type s1 (size ()); + size_type s (s1 < n ? s1 : n); + + int r (s != 0 ? traits_type::compare (data (), str, s) : 0); + + if (!r && s1 != n) + r = s1 < n ? -1 : 1; + + return r; + } + + public: + size_type + find (C c, size_type pos = 0) const; + + private: + const C* data_; + size_type size_; + }; + + // operator== + // + template + inline bool + operator== (const ro_string& a, const ro_string& b) + { + return a.compare (b) == 0; + } + + template + inline bool + operator== (const ro_string& a, const std::basic_string& b) + { + return a.compare (b) == 0; + } + + template + inline bool + operator== (const std::basic_string& a, const ro_string& b) + { + return b.compare (a) == 0; + } + + template + inline bool + operator== (const ro_string& a, const C* b) + { + return a.compare (b) == 0; + } + + template + inline bool + operator== (const C* a, const ro_string& b) + { + return b.compare (a) == 0; + } + + // operator!= + // + template + inline bool + operator!= (const ro_string& a, const ro_string& b) + { + return a.compare (b) != 0; + } + + template + inline bool + operator!= (const ro_string& a, const std::basic_string& b) + { + return a.compare (b) != 0; + } + + template + inline bool + operator!= (const std::basic_string& a, const ro_string& b) + { + return b.compare (a) != 0; + } + + template + inline bool + operator!= (const ro_string& a, const C* b) + { + return a.compare (b) != 0; + } + + template + inline bool + operator!= (const C* a, const ro_string& b) + { + return b.compare (a) != 0; + } + + // operator< + // + template + inline bool + operator< (const ro_string& l, const ro_string& r) + { + return l.compare (r) < 0; + } + + template + inline bool + operator< (const ro_string& l, const std::basic_string& r) + { + return l.compare (r) < 0; + } + + template + inline bool + operator< (const std::basic_string& l, const ro_string& r) + { + return r.compare (l) > 0; + } + + template + inline bool + operator< (const ro_string& l, const C* r) + { + return l.compare (r) < 0; + } + + template + inline bool + operator< (const C* l, const ro_string& r) + { + return r.compare (l) > 0; + } + + + // operator> + // + template + inline bool + operator> (const ro_string& l, const ro_string& r) + { + return l.compare (r) > 0; + } + + template + inline bool + operator> (const ro_string& l, const std::basic_string& r) + { + return l.compare (r) > 0; + } + + template + inline bool + operator> (const std::basic_string& l, const ro_string& r) + { + return r.compare (l) < 0; + } + + template + inline bool + operator> (const ro_string& l, const C* r) + { + return l.compare (r) > 0; + } + + template + inline bool + operator> (const C* l, const ro_string& r) + { + return r.compare (l) < 0; + } + + // operator<= + // + template + inline bool + operator<= (const ro_string& l, const ro_string& r) + { + return l.compare (r) <= 0; + } + + template + inline bool + operator<= (const ro_string& l, const std::basic_string& r) + { + return l.compare (r) <= 0; + } + + template + inline bool + operator<= (const std::basic_string& l, const ro_string& r) + { + return r.compare (l) >= 0; + } + + template + inline bool + operator<= (const ro_string& l, const C* r) + { + return l.compare (r) <= 0; + } + + template + inline bool + operator<= (const C* l, const ro_string& r) + { + return r.compare (l) >= 0; + } + + + // operator>= + // + template + inline bool + operator>= (const ro_string& l, const ro_string& r) + { + return l.compare (r) >= 0; + } + + template + inline bool + operator>= (const ro_string& l, const std::basic_string& r) + { + return l.compare (r) >= 0; + } + + template + inline bool + operator>= (const std::basic_string& l, const ro_string& r) + { + return r.compare (l) <= 0; + } + + template + inline bool + operator>= (const ro_string& l, const C* r) + { + return l.compare (r) >= 0; + } + + template + inline bool + operator>= (const C* l, const ro_string& r) + { + return r.compare (l) <= 0; + } + + // operator<< + // + template + std::basic_ostream& + operator<< (std::basic_ostream& os, const ro_string& str) + { + if (str.size () != 0) + os.write (str.data (), static_cast (str.size ())); + + return os; + } + + // operator+= + // + template + std::basic_string& + operator+= (std::basic_string& l, const ro_string& r) + { + l.append (r.data (), r.size ()); + return l; + } + + // Trim leading and trailing XML whitespaces. Return the new + // string size. + // + template + typename ro_string::size_type + trim_left (ro_string&); + + template + typename ro_string::size_type + trim_right (ro_string&); + + template + typename ro_string::size_type + trim (ro_string&); + + // Trim leading and trailing XML whitespaces. + // + template + std::basic_string + trim (const std::basic_string&); + } +} + +#include + +#endif // XSD_CXX_RO_STRING_HXX -- cgit v1.1