1
/*********************************************************
2
* Copyright (C) 2008 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
22
* A string wrapper for bora/lib/unicode. This class is intended to provide
23
* more c++ features such as operator overloading, automatic string conversion
24
* between different types of string classes.
26
* This class uses glib::ustring as the underlying storage for its data,
27
* we chose this object because of its internal support for unicode.
42
#include <glibmm/ustring.h>
52
#include "libExport.hh"
54
#include "unicodeTypes.h"
59
* Disabling Windows warning 4251
60
* This is a warning msg about requiring Glib::ustring to be DLL
64
#pragma warning(disable:4251)
70
/* utf8string should be replaced with an opaque type. It is temporarily used
71
* to replace the std::string in our codebase.
73
typedef std::string utf8string;
74
typedef std::basic_string<utf16_t> utf16string;
76
class VMSTRING_EXPORT string
80
typedef Glib::ustring::size_type size_type;
81
typedef Glib::ustring::value_type value_type;
82
typedef Glib::ustring::iterator iterator;
83
typedef Glib::ustring::const_iterator const_iterator;
85
// constant definitions
86
static const size_type npos;
88
// Normalize mode map to Glib::NormalizeMode
90
NORMALIZE_DEFAULT = Glib::NORMALIZE_DEFAULT,
91
NORMALIZE_NFD = Glib::NORMALIZE_NFD,
92
NORMALIZE_DEFAULT_COMPOSE = Glib::NORMALIZE_DEFAULT_COMPOSE,
93
NORMALIZE_NFC = Glib::NORMALIZE_NFC,
94
NORMALIZE_ALL = Glib::NORMALIZE_ALL,
95
NORMALIZE_NFKD = Glib::NORMALIZE_NFKD,
96
NORMALIZE_ALL_COMPOSE = Glib::NORMALIZE_ALL_COMPOSE,
97
NORMALIZE_NFKC = Glib::NORMALIZE_NFKC
101
string(ConstUnicode s);
104
string(const ubstr_t &s);
105
explicit string(const _bstr_t &s);
106
string(const uvariant_t &v);
107
explicit string(const _variant_t &v);
110
string(const utf16string &s);
111
string(const utf16_t *s);
112
string(const char *s, StringEncoding encoding);
113
string(const Glib::ustring &s);
114
string(const string &s);
118
// Implicit conversions
119
operator const Glib::ustring& () const;
121
operator const ubstr_t() const;
124
// Conversions to other i18n types (utf8, utf16, unicode)
125
const char *c_str() const;
126
const utf16_t *w_str() const;
127
const Glib::ustring& ustr() const;
129
// Mapping functions to Glib::ustring
130
void swap(string &s);
131
void resize(size_type n, value_type c = '\0');
133
size_type size() const;
134
size_type w_size() const;
135
size_type length() const;
136
size_type bytes() const;
137
string foldCase() const;
139
string trimLeft() const;
140
string trimRight() const;
141
string normalize(NormalizeMode mode = NORMALIZE_DEFAULT_COMPOSE) const;
143
string toLower(const char *locale) const;
144
string toUpper(const char *locale) const;
146
string toTitle(const char *locale) const;
149
// String-level member methods.
150
string& append(const string &s);
151
string& append(const string &s, size_type i, size_type n);
152
string& append(const char *s, size_type n);
153
string& assign(const string &s);
154
void push_back(value_type uc);
157
string& insert(size_type i, const string& s);
158
string& insert(size_type i, size_type n, value_type uc);
159
string& erase(size_type i, size_type n = npos);
160
iterator erase(iterator p);
161
iterator erase(iterator pbegin, iterator pend);
162
string& replace(size_type i, size_type n, const string& s);
163
string& replace(const string &from, const string &to);
164
string replace_copy(const string& from, const string& to) const;
166
int compare(const string &s, bool ignoreCase = false) const;
167
int compare(size_type i, size_type n, const string &s) const;
168
int compareLength(const string &s, size_type len, bool ignoreCase = false) const;
169
int compareRange(size_type thisStart, size_type thisLength, const string &str,
170
size_type strStart, size_type strLength,
171
bool ignoreCase = false) const;
172
size_type find(const string &s, size_type pos = 0) const;
173
size_type rfind(const string &s, size_type pos = npos) const;
174
size_type find_first_of(const string &s, size_type i = 0) const;
175
size_type find_first_not_of(const string &s, size_type i = 0) const;
176
size_type find_last_of(const string &s, size_type i = npos) const;
177
size_type find_last_not_of(const string &s, size_type i = npos) const;
178
string substr(size_type start = 0, size_type len = npos) const;
180
// Character-level member methods.
181
value_type operator[](size_type i) const;
182
size_type find(value_type uc, size_type pos = 0) const;
183
size_type rfind(value_type uc, size_type pos = npos) const;
184
size_type find_first_of(value_type uc, size_type i = 0) const;
185
size_type find_first_not_of(value_type uc, size_type i = 0) const;
186
size_type find_last_of(value_type uc, size_type i = npos) const;
187
size_type find_last_not_of(value_type uc, size_type i = npos) const;
189
// Sequence accessor.
192
const_iterator begin() const;
193
const_iterator end() const;
195
// Operator overloads
196
string& operator=(string copy);
197
string& operator+=(const string &s);
198
string& operator+=(value_type uc);
200
// Some helper functions that are nice to have
201
bool startsWith(const string &s, bool ignoreCase = false) const;
202
bool endsWith(const string &s, bool ignoreCase = false) const;
203
std::vector<string> split(const string &sep) const;
205
// Overloaded operators
206
string operator+(const string &rhs) const;
207
string operator+(value_type uc) const;
208
bool operator==(const string &rhs) const;
209
bool operator!=(const string &rhs) const;
210
bool operator<(const string &rhs) const;
211
bool operator>(const string &rhs) const;
212
bool operator<=(const string &rhs) const;
213
bool operator>=(const string &rhs) const;
217
void InvalidateCache();
220
const utf16_t *GetUtf16Cache() const;
223
// Private utility constructor.
224
void init_bstr_t(const _bstr_t &s);
227
// utf::string is internally backed by Glib::ustring.
230
// Cached representations.
231
mutable utf16_t *mUtf16Cache;
232
mutable size_type mUtf16Length;
235
* All added members need to be initialized in all constructors and need
236
* to be handled in swap().
243
operator+(ConstUnicode lhs, const string &rhs) {
244
return string(lhs) + rhs;
248
operator+(const string& lhs, ConstUnicode rhs) {
249
return lhs + string(rhs);
253
operator==(ConstUnicode lhs, const string &rhs) {
254
return string(lhs) == rhs;
258
operator!=(ConstUnicode lhs, const string &rhs) {
259
return string(lhs) != rhs;
263
operator<(ConstUnicode lhs, const string &rhs) {
264
return string(lhs) < rhs;
268
operator>(ConstUnicode lhs, const string &rhs) {
269
return string(lhs) > rhs;
273
operator<=(ConstUnicode lhs, const string &rhs) {
274
return string(lhs) <= rhs;
278
operator>=(ConstUnicode lhs, const string &rhs) {
279
return string(lhs) >= rhs;
282
// This lets a utf::string appear on the right side of a stream insertion operator.
284
operator<<(std::ostream& strm, const string& s)
290
// ConversionError class for exception
292
class ConversionError {};
297
bool VMSTRING_EXPORT Validate(const Glib::ustring& s);
299
string VMSTRING_EXPORT
300
CreateWithLength(const void *buffer, ssize_t lengthInBytes, StringEncoding encoding);
302
string VMSTRING_EXPORT
303
CreateWithBOMBuffer(const void *buffer, ssize_t lengthInBytes);
305
string VMSTRING_EXPORT
308
void VMSTRING_EXPORT CreateWritableBuffer(const string& s,
309
std::vector<char>& buf);
310
void VMSTRING_EXPORT CreateWritableBuffer(const string& s,
311
std::vector<utf16_t>& buf);
318
// Template specializations for utf::string.
323
swap<utf::string>(utf::string& s1, // IN/OUT
324
utf::string& s2) // IN/OUT