1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile bs_string_uni.cpp:
|
---|
4 | * BSUString implementation.
|
---|
5 | *
|
---|
6 | *@@header "cppbase\bs_string.h"
|
---|
7 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * This file Copyright (C) 2020 Ulrich Mller.
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
---|
15 | * file of this distribution.
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #define OS2EMX_PLAIN_CHAR
|
---|
23 | // this is needed for "os2emx.h"; if this is defined,
|
---|
24 | // emx will define PSZ as _signed_ char, otherwise
|
---|
25 | // as unsigned char
|
---|
26 |
|
---|
27 | #include <os2.h>
|
---|
28 |
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <stdarg.h>
|
---|
33 |
|
---|
34 | #include "setup.h"
|
---|
35 |
|
---|
36 | #include "helpers\stringh.h"
|
---|
37 | #include "helpers\xstring.h"
|
---|
38 |
|
---|
39 | #include "encodings\base.h"
|
---|
40 |
|
---|
41 | // base includes
|
---|
42 | #include "cppbase\bs_base.h"
|
---|
43 | #include "cppbase\bs_string.h"
|
---|
44 | #include "cppbase\bs_errors.h"
|
---|
45 |
|
---|
46 | #pragma hdrstop
|
---|
47 |
|
---|
48 | // #define dprintf printf
|
---|
49 |
|
---|
50 | DEFINE_CLASS(BSUString, BSStringBase);
|
---|
51 |
|
---|
52 | /* ******************************************************************
|
---|
53 | *
|
---|
54 | * Initialize the encoding engine
|
---|
55 | *
|
---|
56 | ********************************************************************/
|
---|
57 |
|
---|
58 | // We initialize the case engine by creating a global variable
|
---|
59 | // of this class, which just calls encInitCase.
|
---|
60 |
|
---|
61 | class BSDummyInitEncoding
|
---|
62 | {
|
---|
63 | public:
|
---|
64 | BSDummyInitEncoding()
|
---|
65 | {
|
---|
66 | encInitCase();
|
---|
67 | }
|
---|
68 | };
|
---|
69 |
|
---|
70 | static BSDummyInitEncoding G_Init;
|
---|
71 |
|
---|
72 | /* ******************************************************************
|
---|
73 | *
|
---|
74 | * BSUString
|
---|
75 | *
|
---|
76 | ********************************************************************/
|
---|
77 |
|
---|
78 | /*
|
---|
79 | *@@ BSUString:
|
---|
80 | * copy constructor to convert from a
|
---|
81 | * codepaged BSString, using the
|
---|
82 | * specified BSUniCodec for conversion.
|
---|
83 | *
|
---|
84 | *@@added V0.9.20 (2002-07-03) [umoeller]
|
---|
85 | */
|
---|
86 |
|
---|
87 | BSUString::BSUString(BSUniCodec *pCodec,
|
---|
88 | const BSString &str)
|
---|
89 | : BSStringBase(tBSUString)
|
---|
90 | {
|
---|
91 | STRINGLOCK;
|
---|
92 |
|
---|
93 | if (!pCodec)
|
---|
94 | throw BSExcptBase("pCodec is NULL.");
|
---|
95 |
|
---|
96 | size_type st;
|
---|
97 | if (st = str.length())
|
---|
98 | {
|
---|
99 | pCodec->Codepage2Uni(*this,
|
---|
100 | str.c_str(),
|
---|
101 | st);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | *@@ BSUString:
|
---|
107 | * copy constructor to convert from a
|
---|
108 | * codepaged C string, using the
|
---|
109 | * specified BSUniCodec for conversion.
|
---|
110 | *
|
---|
111 | *@@added WarpIN V1.0.25 (2020-09-05) [pr]
|
---|
112 | */
|
---|
113 |
|
---|
114 | BSUString::BSUString(BSUniCodec *pCodec,
|
---|
115 | const char *psz)
|
---|
116 | : BSStringBase(tBSUString)
|
---|
117 | {
|
---|
118 | STRINGLOCK;
|
---|
119 |
|
---|
120 | if (!pCodec)
|
---|
121 | throw BSExcptBase("pCodec is NULL.");
|
---|
122 |
|
---|
123 | size_type st;
|
---|
124 | if (st = strlen(psz))
|
---|
125 | {
|
---|
126 | pCodec->Codepage2Uni(*this,
|
---|
127 | psz,
|
---|
128 | st);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | *@@ operator[]:
|
---|
134 | * returns the ul'th character of the
|
---|
135 | * string or 0 if ul is too large.
|
---|
136 | *
|
---|
137 | * Warning: this counts bytes, not UTF-8
|
---|
138 | * characters.
|
---|
139 | */
|
---|
140 |
|
---|
141 | char BSUString::operator[](const size_type ul) // in: character offset
|
---|
142 | const
|
---|
143 | {
|
---|
144 | STRINGLOCK;
|
---|
145 | if ( (_pBuf)
|
---|
146 | && (_pBuf->_str.psz)
|
---|
147 | && (ul < _pBuf->_str.ulLength)
|
---|
148 | )
|
---|
149 | return (_pBuf->_str.psz[ul]);
|
---|
150 |
|
---|
151 | return ('\0');
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | *@@ assign:
|
---|
156 | * assigns another BSUString to this. This
|
---|
157 | * is the only direct assign that is supported
|
---|
158 | * for BSUString (and gets called thru
|
---|
159 | * BSUString::operator= as well); otherwise
|
---|
160 | * you will have to use one of the
|
---|
161 | * assignUtf8 methods.
|
---|
162 | *
|
---|
163 | * Returns *this.
|
---|
164 | */
|
---|
165 |
|
---|
166 | BSUString& BSUString::assign(const BSUString &s)
|
---|
167 | {
|
---|
168 | STRINGLOCK;
|
---|
169 | // avoid self assignment
|
---|
170 | if (_pBuf != s._pBuf)
|
---|
171 | {
|
---|
172 | FreeBuf();
|
---|
173 | CopyFrom(s);
|
---|
174 | }
|
---|
175 |
|
---|
176 | return (*this);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | *@@ assignUtf8:
|
---|
181 | * assigns a UTF-8 C string to this.
|
---|
182 | *
|
---|
183 | * This is intentionally named differently
|
---|
184 | * from assign to make sure noone accidentally
|
---|
185 | * assigns codepaged C strings. Note that
|
---|
186 | * the "=" operator won't work for C strings
|
---|
187 | * either.
|
---|
188 | *
|
---|
189 | * Returns *this.
|
---|
190 | */
|
---|
191 |
|
---|
192 | BSUString& BSUString::assignUtf8(const char *psz)
|
---|
193 | {
|
---|
194 | STRINGLOCK;
|
---|
195 | FreeBuf();
|
---|
196 | CopyFrom(psz);
|
---|
197 |
|
---|
198 | return (*this);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | *@@ assignUtf8:
|
---|
203 | * copies from a UTF-8 C string, starting with
|
---|
204 | * p1 up to p2, which is not included.
|
---|
205 | *
|
---|
206 | * This is intentionally named differently
|
---|
207 | * from assign to make sure noone accidentally
|
---|
208 | * assigns codepaged C strings.
|
---|
209 | *
|
---|
210 | * Returns *this.
|
---|
211 | */
|
---|
212 |
|
---|
213 | BSUString& BSUString::assignUtf8(const char *p1, const char *p2)
|
---|
214 | {
|
---|
215 | STRINGLOCK;
|
---|
216 | FreeBuf();
|
---|
217 | CopyFrom(p1, p2);
|
---|
218 |
|
---|
219 | return (*this);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | *@@ assignCP:
|
---|
224 | * assigns a codepaged BSString to this, using
|
---|
225 | * the specified BSUniCodec for conversion.
|
---|
226 | *
|
---|
227 | * Returns *this.
|
---|
228 | */
|
---|
229 |
|
---|
230 | BSUString& BSUString::assignCP(BSUniCodec *pCodec,
|
---|
231 | const BSString &str)
|
---|
232 | {
|
---|
233 | STRINGLOCK;
|
---|
234 | FreeBuf();
|
---|
235 |
|
---|
236 | if (!pCodec)
|
---|
237 | throw BSExcptBase("pCodec is NULL.");
|
---|
238 |
|
---|
239 | size_type st;
|
---|
240 | if (st = str.length())
|
---|
241 | {
|
---|
242 | pCodec->Codepage2Uni(*this,
|
---|
243 | str.c_str(),
|
---|
244 | st);
|
---|
245 | }
|
---|
246 |
|
---|
247 | return (*this);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | *@@ assignCP:
|
---|
252 | * assigns a codepaged C string to this, using
|
---|
253 | * the specified BSUniCodec for conversion.
|
---|
254 | *
|
---|
255 | * Returns *this.
|
---|
256 | */
|
---|
257 |
|
---|
258 | BSUString& BSUString::assignCP(BSUniCodec *pCodec,
|
---|
259 | const char *psz)
|
---|
260 | {
|
---|
261 | STRINGLOCK;
|
---|
262 | FreeBuf();
|
---|
263 |
|
---|
264 | if (!pCodec)
|
---|
265 | throw BSExcptBase("pCodec is NULL.");
|
---|
266 |
|
---|
267 | size_type st;
|
---|
268 | if (st = strlen(psz))
|
---|
269 | {
|
---|
270 | pCodec->Codepage2Uni(*this,
|
---|
271 | psz,
|
---|
272 | st);
|
---|
273 | }
|
---|
274 |
|
---|
275 | return (*this);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /*
|
---|
279 | *@@ append:
|
---|
280 | * appends the given BSUString to this.
|
---|
281 | *
|
---|
282 | * Also used internally by BSUString::operator+=.
|
---|
283 | *
|
---|
284 | * Returns *this.
|
---|
285 | */
|
---|
286 |
|
---|
287 | BSUString& BSUString::append(const BSUString &s)
|
---|
288 | {
|
---|
289 | STRINGLOCK;
|
---|
290 | size_type ulSourceLen;
|
---|
291 | if (ulSourceLen = s.size())
|
---|
292 | {
|
---|
293 | // assert that we have a modifiable
|
---|
294 | // copy with enough space
|
---|
295 | reserve(ulSourceLen + size() + 1);
|
---|
296 | xstrcats(&_pBuf->_str, &s._pBuf->_str);
|
---|
297 | }
|
---|
298 |
|
---|
299 | return (*this);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /*
|
---|
303 | *@@ appendUtf8:
|
---|
304 | * appends the given UTF-8 C string to this.
|
---|
305 | *
|
---|
306 | * Returns *this.
|
---|
307 | */
|
---|
308 |
|
---|
309 | BSUString& BSUString::appendUtf8(const char *psz)
|
---|
310 | {
|
---|
311 | STRINGLOCK;
|
---|
312 | size_type ulSourceLen;
|
---|
313 | if ( (psz)
|
---|
314 | && (ulSourceLen = strlen(psz))
|
---|
315 | )
|
---|
316 | {
|
---|
317 | // assert that we have a modifiable
|
---|
318 | // copy with enough space
|
---|
319 | reserve(ulSourceLen + size() + 1);
|
---|
320 | xstrcat(&_pBuf->_str, psz, ulSourceLen);
|
---|
321 | }
|
---|
322 |
|
---|
323 | return (*this);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*
|
---|
327 | *@@ appendCP:
|
---|
328 | * appends the given codepaged BSString to this,
|
---|
329 | * using the specified BSUniCodec for conversion
|
---|
330 | * before appending.
|
---|
331 | *
|
---|
332 | * Returns *this.
|
---|
333 | *
|
---|
334 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
335 | */
|
---|
336 |
|
---|
337 | BSUString& BSUString::appendCP(BSUniCodec *pCodec,
|
---|
338 | const BSString &str)
|
---|
339 | {
|
---|
340 | STRINGLOCK;
|
---|
341 | if (str.length())
|
---|
342 | {
|
---|
343 | BSUString ustr;
|
---|
344 | ustr.assignCP(pCodec, str);
|
---|
345 | append(ustr);
|
---|
346 | }
|
---|
347 |
|
---|
348 | return (*this);
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*
|
---|
352 | *@@ compareI:
|
---|
353 | * case-insensitive compare.
|
---|
354 | *
|
---|
355 | * Compares this against the given BSUString,
|
---|
356 | * without respect to case, with full
|
---|
357 | * Unicode support by calling encicmp().
|
---|
358 | *
|
---|
359 | * Returns 0 if the two strings are the
|
---|
360 | * same, -1 if "this" is smaller, 1 if
|
---|
361 | * "s" is smaller.
|
---|
362 | *
|
---|
363 | *@@added V0.9.20 (2002-07-03) [umoeller]
|
---|
364 | */
|
---|
365 |
|
---|
366 | int BSUString::compareI(const BSUString &s) const
|
---|
367 | {
|
---|
368 | return encicmp(_pBuf->_str.psz,
|
---|
369 | s._pBuf->_str.psz);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /*
|
---|
373 | *@@ compareUtf8I:
|
---|
374 | * case-insensitive compare.
|
---|
375 | *
|
---|
376 | * Compares this against the given UTF-8
|
---|
377 | * C string, without respect to case, with
|
---|
378 | * full Unicode support by calling encicmp().
|
---|
379 | *
|
---|
380 | * Returns 0 if the two strings are the
|
---|
381 | * same, -1 if "this" is smaller, 1 if
|
---|
382 | * "pcsz" is smaller.
|
---|
383 | *
|
---|
384 | *@@added V0.9.20 (2002-07-03) [umoeller]
|
---|
385 | */
|
---|
386 |
|
---|
387 | int BSUString::compareUtf8I(const char *psz) const
|
---|
388 | {
|
---|
389 | return encicmp(_pBuf->_str.psz,
|
---|
390 | psz);
|
---|
391 | }
|
---|
392 |
|
---|
393 | /*
|
---|
394 | *@@ operator==:
|
---|
395 | * compares two UStrings.
|
---|
396 | */
|
---|
397 |
|
---|
398 | int operator==(const BSUString &s1, const BSUString &s2)
|
---|
399 | {
|
---|
400 | return (s1.compare(s2) == 0);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /*
|
---|
404 | *@@ operator1=:
|
---|
405 | * compares two UStrings.
|
---|
406 | *
|
---|
407 | *@@added V0.9.20 (2002-07-03) [umoeller]
|
---|
408 | */
|
---|
409 |
|
---|
410 | int operator!=(const BSUString &s1, const BSUString &s2)
|
---|
411 | {
|
---|
412 | return (s1.compare(s2) != 0);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*
|
---|
416 | *@@ operator+:
|
---|
417 | *
|
---|
418 | *@@added V0.9.19 (2002-04-14) [umoeller]
|
---|
419 | */
|
---|
420 |
|
---|
421 | BSUString operator+(const BSUString &s1, const BSUString &s2)
|
---|
422 | {
|
---|
423 | BSUString str(s1);
|
---|
424 | str.append(s2);
|
---|
425 | return (str);
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|