#pragma once /** * @author Francois-R.Boyer@PolyMtl.ca * @date March, 2013 * @copyright GPL; see license.txt in Notepad++ source for complete license text. * @file */ #include /// Simple string class with length and pointer to const array of some type, without allocation. template class ConstString { public: typedef size_t size_type; ConstString() : _length(0), _str(0) { } ConstString(const CharT* str, size_type length) : _length(length), _str(str) { } ConstString(const ConstString& source) { *this = source; } ConstString(const std::basic_string& source) { *this = source; } template // string_type can by a ConstString or std::basic_string; anything that has length() and c_str() of same type. ConstString& operator=(const string_type& source) { _length = source.length(); _str = source.c_str(); return *this; } size_type length() const { return _length; } const CharT* c_str() const { return _str; } const CharT& operator[](size_type i) const { return _str[i]; } std::basic_string toString() const { return std::basic_string(_str, _length); } private: size_type _length; const CharT* _str; }; /// Generic comparison of strings of different character sizes. /// @note Characters are not encoded/decoded, the comparison is value by value. Thus it can compare latin-1, UCS-2, and UTF-32 strings, but not UTF-8, UTF-16 and UTF-32. template bool operator==(const ConstString& a, const ConstString& b) { size_t a_length = a.length(); if (a_length != b.length()) return false; for (size_t i = 0; i < a_length; i++) if (a[i] != b[i]) return false; return true; } template bool operator!=(const ConstString& a, const ConstString& b) { return !(a == b); } /// Specialized comparison when both strings have same character type. template bool operator==(const ConstString& a, const ConstString& b) { return a.length() == b.length() && memcmp(a.c_str(), b.c_str(), a.length() * sizeof(CharT)) == 0; } // Comparison with other string types: template bool operator==(const ConstString& a, const std::basic_string& b) { return a == ConstString(b.c_str(), b.length()); } template bool operator==(const ConstString& a, const CharT2* b) { return a == ConstString(b, std::char_traits::length(b)); } template bool operator!=(const ConstString& a, const std::basic_string& b) { return !(a == b); } template bool operator!=(const ConstString& a, const CharT2* b) { return !(a == b); }