30template <
typename CharTy>
31using BasicString = std::basic_string<CharTy>;
35using String = BasicString<char>;
39using WideString = BasicString<wchar_t>;
43template <
typename CharTy>
48using StringView = BasicStringView<char>;
52using WideStringView = BasicStringView<wchar_t>;
59String Format(
const char* format, ...);
63WideString Format(
const wchar_t* format, ...);
67String FormatArgs(
const char* format, va_list args);
71WideString FormatArgs(
const wchar_t* format, va_list args);
75String WideToNarrow(WideStringView str);
79WideString NarrowToWide(StringView str);
83String WideToUTF8(WideStringView str);
87WideString UTF8ToWide(StringView str);
93template <
typename CharTy>
97 using value_type = CharTy;
98 using pointer = CharTy*;
99 using const_pointer =
const CharTy*;
100 using reference = CharTy&;
101 using const_reference =
const CharTy&;
102 using traits_type = std::char_traits<CharTy>;
103 using size_type = std::size_t;
104 using string_type = BasicString<CharTy>;
117 count_ = traits_type::length(c_str);
140 , count_(str.length())
150 inline const value_type* data()
const
155 inline bool empty()
const
157 return !ptr_ || !count_;
160 inline size_type find(value_type ch)
const
162 const auto ptr = traits_type::find(ptr_, count_, ch);
167 return string_type::npos;
170 inline BasicStringView substr(size_type pos, size_type count = string_type::npos)
const
175 if (count == string_type::npos)
178 KGE_ASSERT(pos + count <= count_);
182 inline size_type size()
const
187 inline value_type at(size_type index)
const
189 return operator[](index);
192 inline value_type operator[](size_type index)
const
194 if (empty() || index >= count_)
195 throw std::out_of_range(
"operator[] out of index");
208 return lhs.size() == rhs.size() ? std::char_traits<value_type>::compare(lhs.data(), rhs.data(), lhs.size()) == 0
212 inline operator string_type()
const
214 return string_type(ptr_, count_);
223 const value_type* ptr_;
228 using iterator_category = std::random_access_iterator_tag;
229 using value_type = value_type;
230 using pointer = value_type*;
231 using reference = value_type&;
232 using difference_type = ptrdiff_t;
234 inline Iterator(pointer ptr, size_type pos, size_type count)
256 inline const value_type& operator*()
const
258 KGE_ASSERT(pos_ < count_);
262 inline const value_type* operator->()
const
264 return std::pointer_traits<pointer>::pointer_to(**
this);
267 inline Iterator& operator+=(size_type count)
269 KGE_ASSERT(pos_ + count >= 0 && pos_ + count <= count_);
274 inline Iterator& operator-=(size_type count)
276 KGE_ASSERT(pos_ - count >= 0 && pos_ - count <= count_);
281 inline const Iterator operator+(size_type count)
const
288 inline const Iterator& operator-(size_type count)
const
297 KGE_ASSERT(pos_ < count_);
311 KGE_ASSERT(pos_ > 0);
323 inline const value_type& operator[](size_type index)
const
326 return iter.ptr_[iter.pos_];
329 inline difference_type operator-(
const Iterator& other)
const
331 KGE_ASSERT(ptr_ == other.ptr_ && count_ == other.count_);
332 return static_cast<difference_type
>(pos_ - other.pos_);
335 inline bool operator==(
const Iterator& other)
const
337 return ptr_ == other.ptr_ && pos_ == other.pos_ && count_ == other.count_;
340 inline bool operator!=(
const Iterator& other)
const
342 return !(*
this == other);
345 inline bool operator<(
const Iterator& other)
const
347 return ptr_ < other.ptr_ || pos_ < other.pos_ || count_ < other.count_;
350 inline bool operator<=(
const Iterator& other)
const
352 return (*
this < other) || (*
this == other);
355 inline bool operator>(
const Iterator& other)
const
357 return !(*
this <= other);
360 inline bool operator>=(
const Iterator& other)
const
362 return !(*
this < other);
365 inline operator bool()
const
367 return ptr_ !=
nullptr && pos_ != count_;
373 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
374 using reverse_iterator = const_reverse_iterator;
381 inline const_iterator cbegin()
const
386 inline const_iterator end()
const
388 return const_iterator(ptr_, count_, count_);
391 inline const_iterator cend()
const
396 inline const_reverse_iterator rbegin()
const
398 return const_reverse_iterator(end());
401 inline const_reverse_iterator crbegin()
const
406 inline const_reverse_iterator rend()
const
408 return const_reverse_iterator(begin());
411 inline const_reverse_iterator crend()
const
416 inline const value_type& front()
const
419 throw std::out_of_range(
"front() called on empty list");
423 inline const value_type& back()
const
426 throw std::out_of_range(
"back() called on empty list");
431 const value_type* ptr_;
440template <
class _Char>
443 inline size_t operator()(const ::kiwano::BasicStringView<_Char>& v)
const
445 std::hash<_Char> hasher;
448 for (
size_t i = 0; i < v.size(); ++i)
449 result = (result << 1) ^ hasher(v[i]);
基础字符串视图
Definition: String.h:95