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
#ifdef SUPPORT_UNICODE
144
string toLower(const char *locale) const;
145
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& assign(const string &s);
153
void push_back(value_type uc);
156
string& insert(size_type i, const string& s);
157
string& insert(size_type i, size_type n, value_type uc);
158
string& erase(size_type i, size_type n = npos);
159
iterator erase(iterator p);
160
iterator erase(iterator pbegin, iterator pend);
161
string& replace(size_type i, size_type n, const string& s);
162
string& replace(const string &from, const string &to);
163
string replace_copy(const string& from, const string& to) const;
165
int compare(const string &s, bool ignoreCase = false) const;
166
int compare(size_type i, size_type n, const string &s) const;
167
int compareLength(const string &s, size_type len, bool ignoreCase = false) const;
168
int compareRange(size_type thisStart, size_type thisLength, const string &str,
169
size_type strStart, size_type strLength,
170
bool ignoreCase = false) const;
171
size_type find(const string &s, size_type pos = 0) const;
172
size_type rfind(const string &s, size_type pos = npos) const;
173
size_type find_first_of(const string &s, size_type i = 0) const;
174
size_type find_first_not_of(const string &s, size_type i = 0) const;
175
size_type find_last_of(const string &s, size_type i = npos) const;
176
size_type find_last_not_of(const string &s, size_type i = npos) const;
177
string substr(size_type start = 0, size_type len = npos) const;
179
// Character-level member methods.
180
value_type operator[](size_type i) const;
181
size_type find(value_type uc, size_type pos = 0) const;
182
size_type rfind(value_type uc, size_type pos = npos) const;
183
size_type find_first_of(value_type uc, size_type i = 0) const;
184
size_type find_first_not_of(value_type uc, size_type i = 0) const;
185
size_type find_last_of(value_type uc, size_type i = npos) const;
186
size_type find_last_not_of(value_type uc, size_type i = npos) const;
188
// Sequence accessor.
191
const_iterator begin() const;
192
const_iterator end() const;
194
// Operator overloads
195
string& operator=(string copy);
196
string& operator+=(const string &s);
197
string& operator+=(value_type uc);
199
// Some helper functions that are nice to have
200
bool startsWith(const string &s, bool ignoreCase = false) const;
201
bool endsWith(const string &s, bool ignoreCase = false) const;
202
std::vector<string> split(const string &sep) const;
204
// Overloaded operators
205
string operator+(const string &rhs) const;
206
string operator+(value_type uc) const;
207
bool operator==(const string &rhs) 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;
216
void InvalidateCache();
219
const utf16_t *GetUtf16Cache() const;
222
// Private utility constructor.
223
void init_bstr_t(const _bstr_t &s);
226
// utf::string is internally backed by Glib::ustring.
229
// Cached representations.
230
mutable utf16_t *mUtf16Cache;
231
mutable size_type mUtf16Length;
234
* All added members need to be initialized in all constructors and need
235
* to be handled in swap().
242
operator+(ConstUnicode lhs, const string &rhs) {
243
return string(lhs) + rhs;
247
operator+(const string& lhs, ConstUnicode rhs) {
248
return lhs + string(rhs);
252
operator==(ConstUnicode lhs, const string &rhs) {
253
return string(lhs) == rhs;
257
operator!=(ConstUnicode lhs, const string &rhs) {
258
return string(lhs) != rhs;
262
operator<(ConstUnicode lhs, const string &rhs) {
263
return string(lhs) < rhs;
267
operator>(ConstUnicode lhs, const string &rhs) {
268
return string(lhs) > rhs;
272
operator<=(ConstUnicode lhs, const string &rhs) {
273
return string(lhs) <= rhs;
277
operator>=(ConstUnicode lhs, const string &rhs) {
278
return string(lhs) >= rhs;
282
// ConversionError class for exception
284
class ConversionError {};
289
bool VMSTRING_EXPORT Validate(const Glib::ustring& s);
291
string VMSTRING_EXPORT
292
CreateWithLength(const void *buffer, ssize_t lengthInBytes, StringEncoding encoding);
294
string VMSTRING_EXPORT
295
CreateWithBOMBuffer(const void *buffer, ssize_t lengthInBytes);
297
string VMSTRING_EXPORT
300
void VMSTRING_EXPORT CreateWritableBuffer(const string& s,
301
std::vector<char>& buf);
302
void VMSTRING_EXPORT CreateWritableBuffer(const string& s,
303
std::vector<utf16_t>& buf);
310
// Template specializations for utf::string.
315
swap<utf::string>(utf::string& s1, // IN/OUT
316
utf::string& s2) // IN/OUT