Honeycomb  0.1
Component-Model Framework
String.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Misc/Debug.h"
5 
6 namespace honey
7 {
8 
11 
13 typedef char16_t Char;
14 
16 
23 class String : public std::basic_string<Char>
24 {
25  typedef std::basic_string<Char> Super;
26 
27 public:
29  typedef vector<String> List;
30 
31  using Super::basic_string;
32  String() = default;
34  String(const std::basic_string<Char>& str) : Super(str) {}
36  String(std::basic_string<Char>&& str) : Super(move(str)) {}
38  String(const std::string& str) : Super(str.begin(), str.end()) {}
40  String(const ostream& os) : String(static_cast<const ostringstream&>(os).str()) {}
42  String(const char* str, szt len = npos) { assign(str, 0, len); }
44  String(szt n, char c) { assign(n, c); }
45 
47  template<class T>
48  String& operator=(T&& rhs) { return assign(forward<T>(rhs)); }
50  template<class T>
51  String& operator+=(T&& rhs) { return append(forward<T>(rhs)); }
52  String& operator+=(Char rhs) { return append(1, rhs); }
53  String& operator+=(char rhs) { return append(1, rhs); }
54 
55  String& clear() { Super::clear(); return *this; }
56 
59  String& append(const String& str, szt subpos = 0, szt sublen = npos) { return insert(length(), str, subpos, sublen); }
60  String& append(const std::string& str, szt subpos = 0, szt sublen = npos) { return insert(length(), str, subpos, sublen); }
61  String& append(const Char* str, szt subpos = 0, szt sublen = npos) { return insert(length(), str, subpos, sublen); }
62  String& append(const char* str, szt subpos = 0, szt sublen = npos) { return insert(length(), str, subpos, sublen); }
63  String& append(szt n, Char c) { return insert(length(), n, c); }
64  String& append(szt n, char c) { return insert(length(), n, c); }
65  template<class InputIterator>
66  String& append(InputIterator first, InputIterator last) { insert(begin() + length(), first, last); return *this; }
68 
71  String& assign(const String& str, szt subpos = 0, szt sublen = npos) { clear(); return append(str, subpos, sublen); }
72  String& assign(const std::string& str, szt subpos = 0, szt sublen = npos) { clear(); return append(str, subpos, sublen); }
73  String& assign(const Char* str, szt subpos = 0, szt sublen = npos) { clear(); return append(str, subpos, sublen); }
74  String& assign(const char* str, szt subpos = 0, szt sublen = npos) { clear(); return append(str, subpos, sublen); }
75  String& assign(szt n, Char c) { clear(); return append(n, c); }
76  String& assign(szt n, char c) { clear(); return append(n, c); }
77  template<class InputIterator>
78  String& assign(InputIterator first, InputIterator last) { clear(); return append<InputIterator>(first, last); }
80 
81  String& insert(szt pos, const String& str, szt subpos = 0, szt sublen = npos);
82  String& insert(szt pos, const std::string& str, szt subpos = 0, szt sublen = npos);
83  String& insert(szt pos, const Char* str, szt subpos = 0, szt sublen = npos);
84  String& insert(szt pos, const char* str, szt subpos = 0, szt sublen = npos);
85  String& insert(szt pos, szt n, Char c) { Super::insert(pos, n, c); return *this; }
86  String& insert(szt pos, szt n, char c) { return insert(pos, n, static_cast<Char>(c)); }
87  iterator insert(const_iterator p, Char c) { return Super::insert(p, c); }
88  iterator insert(const_iterator p, szt n, Char c) { return Super::insert(p, n, c); }
89  template<class InputIterator>
90  iterator insert(const_iterator p, InputIterator first, InputIterator last) { return Super::insert(p, first, last); }
91 
92  String& erase(szt pos = 0, szt len = npos) { Super::erase(pos, len); return *this; }
93  iterator erase(const_iterator position) { return Super::erase(position); }
94  iterator erase(const_iterator first, const_iterator last) { return Super::erase(first, last); }
95 
96  String& replace(szt pos, szt len, const String& str, szt subpos = 0, szt sublen = npos);
97  String& replace(const_iterator i1, const_iterator i2, const String& str) { Super::replace(i1, i2, str); return *this; }
98  template<class InputIterator>
99  String& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last) { Super::replace(i1, i2, first, last); return *this; }
100 
101  szt copy(Char* s, szt len, szt pos = 0) const { return Super::copy(s, len, pos); }
102  szt copy(char* s, szt len, szt pos = 0) const { std::string str(begin() + pos, begin() + pos + len); return str.copy(s, str.length()); }
103 
104  String substr(szt pos = 0, szt len = npos) const { return Super::substr(pos, len); }
105 
107  int icompare(const String& str) const { return icompare(0, npos, str); }
108  int icompare(szt pos, szt len, const String& str, szt subpos = 0, szt sublen = npos) const;
109 
111  List split(const String& delim = String(1, ' '), szt pos = 0, szt count = npos) const;
112 
114  static String join(const List& strings, const String& delim = String(1, ' '), szt start = npos, szt end = npos);
115 
117  String toLower() const;
119  String toUpper() const;
121  std::string u8() const { return std::string(begin(), end()); }
122 
124  operator std::string() const { return u8(); }
125 
126  friend ostream& operator<<(ostream& os, const String& str) { return os << str.u8(); }
127  friend istream& operator>>(istream& is, String& str) { std::string str_; is >> str_; str = str_; return is; }
128 };
129 
132 inline String operator+(const String& lhs, const String& rhs) { return String(lhs).append(rhs); }
133 inline String operator+(const String& lhs, const std::string& rhs) { return String(lhs).append(rhs); }
134 inline String operator+(const String& lhs, const Char* rhs) { return String(lhs).append(rhs); }
135 inline String operator+(const String& lhs, const char* rhs) { return String(lhs).append(rhs); }
136 inline String operator+(const std::string& lhs, const String& rhs) { return String(lhs).append(rhs); }
137 inline String operator+(const Char* lhs, const String& rhs) { return String(lhs).append(rhs); }
138 inline String operator+(const char* lhs, const String& rhs) { return String(lhs).append(rhs); }
139 inline bool operator==(const String& lhs, const String& rhs) { return !lhs.compare(rhs); }
140 inline bool operator==(const String& lhs, const Char* rhs) { return !lhs.compare(rhs); }
141 inline bool operator==(const Char* lhs, const String& rhs) { return !rhs.compare(lhs); }
142 inline bool operator!=(const String& lhs, const String& rhs) { return lhs.compare(rhs); }
143 inline bool operator!=(const String& lhs, const Char* rhs) { return lhs.compare(rhs); }
144 inline bool operator!=(const Char* lhs, const String& rhs) { return rhs.compare(lhs); }
145 inline bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
146 inline bool operator< (const String& lhs, const Char* rhs) { return lhs.compare(rhs) < 0; }
147 inline bool operator< (const Char* lhs, const String& rhs) { return rhs.compare(lhs) > 0; }
148 inline bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
149 inline bool operator> (const String& lhs, const Char* rhs) { return lhs.compare(rhs) > 0; }
150 inline bool operator> (const Char* lhs, const String& rhs) { return rhs.compare(lhs) < 0; }
151 inline bool operator<=(const String& lhs, const String& rhs) { return lhs.compare(rhs) <= 0; }
152 inline bool operator<=(const String& lhs, const Char* rhs) { return lhs.compare(rhs) <= 0; }
153 inline bool operator<=(const Char* lhs, const String& rhs) { return rhs.compare(lhs) >= 0; }
154 inline bool operator>=(const String& lhs, const String& rhs) { return lhs.compare(rhs) >= 0; }
155 inline bool operator>=(const String& lhs, const Char* rhs) { return lhs.compare(rhs) >= 0; }
156 inline bool operator>=(const Char* lhs, const String& rhs) { return rhs.compare(lhs) <= 0; }
157 
159 inline const Char* c_str(const Char* str) { return str ? str : u""; }
160 inline const char* c_str(const char* str) { return str ? str : ""; }
162 
164 
165 }
166 
167 namespace std
168 {
171 
173  inline ostream& operator<<(ostream& os, const honey::Char* str) { assert(str); return os << std::string(str, str + std::char_traits<honey::Char>::length(str)); }
174  inline ostream& operator<<(ostream& os, const honey::Char val) { honey::Char str[] = {val, 0}; return os << str; }
176 }
int icompare(const String &str) const
Case-insensitive compare.
Definition: String.h:107
String & append(const String &str, szt subpos=0, szt sublen=npos)
Definition: String.h:59
String & assign(const String &str, szt subpos=0, szt sublen=npos)
Definition: String.h:71
ostream & operator<<(ostream &os, const honey::Char val)
Definition: String.h:174
String & append(szt n, char c)
Definition: String.h:64
String(const char *str, szt len=npos)
Convert from UTF-8 string, pointer must not be null.
Definition: String.h:42
String & insert(szt pos, szt n, char c)
Definition: String.h:86
String & append(const Char *str, szt subpos=0, szt sublen=npos)
Definition: String.h:61
bool operator>(const String &lhs, const String &rhs)
Definition: String.h:148
String & assign(InputIterator first, InputIterator last)
Definition: String.h:78
iterator erase(const_iterator first, const_iterator last)
Definition: String.h:94
bool operator!=(const String &lhs, const String &rhs)
Definition: String.h:142
String & append(const char *str, szt subpos=0, szt sublen=npos)
Definition: String.h:62
List::reverse_iterator erase(List &list, const typename List::reverse_iterator &iter)
Erase using reverse iterator. Returns reverse iterator to element after erased element.
Definition: StdUtil.h:45
String(const ostream &os)
Convert from UTF-8 stringstream.
Definition: String.h:40
STL namespace.
String & insert(szt pos, const String &str, szt subpos=0, szt sublen=npos)
Definition: String.cpp:9
String toUpper() const
Convert string to upper case.
Definition: String.cpp:77
const Char * c_str(const Char *str)
Ensures that str points to a valid C-string. If str is null then the result is an empty C-string (ie...
Definition: String.h:159
iterator insert(const_iterator p, InputIterator first, InputIterator last)
Definition: String.h:90
String toLower() const
Convert string to lower case.
Definition: String.cpp:70
String & assign(const Char *str, szt subpos=0, szt sublen=npos)
Definition: String.h:73
iterator erase(const_iterator position)
Definition: String.h:93
String substr(szt pos=0, szt len=npos) const
Definition: String.h:104
String & assign(const char *str, szt subpos=0, szt sublen=npos)
Definition: String.h:74
String & insert(szt pos, szt n, Char c)
Definition: String.h:85
String & replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last)
Definition: String.h:99
vector< String > List
List of strings.
Definition: String.h:29
friend istream & operator>>(istream &is, String &str)
Definition: String.h:127
bool operator<(const String &lhs, const String &rhs)
Definition: String.h:145
List split(const String &delim=String(1, ' '), szt pos=0, szt count=npos) const
Split a string into a list of separate substrings delimited by delim.
Definition: String.cpp:140
String(std::basic_string< Char > &&str)
Move UTF-16 string.
Definition: String.h:36
char16_t Char
Represents a single code unit (not code point) for class String.
Definition: String.h:13
String & operator+=(T &&rhs)
Forwards to append()
Definition: String.h:51
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
std::string u8() const
Convert to UTF-8 string.
Definition: String.h:121
String & erase(szt pos=0, szt len=npos)
Definition: String.h:92
String & operator=(T &&rhs)
Forwards to assign()
Definition: String.h:48
String operator+(const String &lhs, const String &rhs)
Definition: String.h:132
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
String & assign(const std::string &str, szt subpos=0, szt sublen=npos)
Definition: String.h:72
String & operator+=(Char rhs)
Definition: String.h:52
String(szt n, char c)
Convert from UTF-8 char repeated n times.
Definition: String.h:44
iterator insert(const_iterator p, Char c)
Definition: String.h:87
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
friend ostream & operator<<(ostream &os, const String &str)
Definition: String.h:126
String & append(szt n, Char c)
Definition: String.h:63
iterator insert(const_iterator p, szt n, Char c)
Definition: String.h:88
String & append(InputIterator first, InputIterator last)
Definition: String.h:66
bool operator<=(const String &lhs, const String &rhs)
Definition: String.h:151
szt copy(char *s, szt len, szt pos=0) const
Definition: String.h:102
String & assign(szt n, char c)
Definition: String.h:76
String & assign(szt n, Char c)
Definition: String.h:75
String & replace(const_iterator i1, const_iterator i2, const String &str)
Definition: String.h:97
String(const std::string &str)
Convert from UTF-8 string.
Definition: String.h:38
bool operator==(const String &lhs, const String &rhs)
Definition: String.h:139
String & operator+=(char rhs)
Definition: String.h:53
szt copy(Char *s, szt len, szt pos=0) const
Definition: String.h:101
bool operator>=(const String &lhs, const String &rhs)
Definition: String.h:154
static String join(const List &strings, const String &delim=String(1, ' '), szt start=npos, szt end=npos)
Join list into one string, separated by delim.
Definition: String.cpp:176
Global Honeycomb namespace.
String & append(const std::string &str, szt subpos=0, szt sublen=npos)
Definition: String.h:60
String & replace(szt pos, szt len, const String &str, szt subpos=0, szt sublen=npos)
Definition: String.cpp:39
String(const std::basic_string< Char > &str)
Copy UTF-16 string.
Definition: String.h:34
String & clear()
Definition: String.h:55
String()=default