1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile bs_string.cpp:
|
---|
4 | * BSString implementation.
|
---|
5 | *
|
---|
6 | *@@header "cppbase\bs_string.h"
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * This file Copyright (C) 1999-2001 Ulrich Mller.
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
---|
14 | * file of this distribution.
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #define OS2EMX_PLAIN_CHAR
|
---|
22 | // this is needed for "os2emx.h"; if this is defined,
|
---|
23 | // emx will define PSZ as _signed_ char, otherwise
|
---|
24 | // as unsigned char
|
---|
25 |
|
---|
26 | #define INCL_DOSSEMAPHORES
|
---|
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 | // base includes
|
---|
40 | #include "cppbase\bs_base.h"
|
---|
41 | #include "cppbase\bs_string.h"
|
---|
42 | #include "cppbase\bs_errors.h"
|
---|
43 |
|
---|
44 | #pragma hdrstop
|
---|
45 |
|
---|
46 | // #define dprintf printf
|
---|
47 |
|
---|
48 | DEFINE_CLASS(BSString, BSStringBase);
|
---|
49 |
|
---|
50 | /* ******************************************************************
|
---|
51 | *
|
---|
52 | * BSString implementation
|
---|
53 | *
|
---|
54 | ********************************************************************/
|
---|
55 |
|
---|
56 | /*
|
---|
57 | *@@ BSString:
|
---|
58 | * copy constructor to copy from
|
---|
59 | * another BSString. As opposed to the
|
---|
60 | * first version, you can specify a substring
|
---|
61 | * here.
|
---|
62 | *
|
---|
63 | * Characters are copied from s starting
|
---|
64 | * at ulPos. Copying is stopped at the end
|
---|
65 | * of s or if n characters have been copied.
|
---|
66 | */
|
---|
67 |
|
---|
68 | BSString::BSString(const BSString &s, // in: source string
|
---|
69 | size_type ulPos, // in: first pos to copy
|
---|
70 | size_type n) // in: maximum no. of chars to copy, defaults to npos
|
---|
71 | : BSStringBase(tBSString)
|
---|
72 | {
|
---|
73 | STRINGLOCK;
|
---|
74 | // Init(); is called by parent already V0.9.20 (2002-07-03) [umoeller]
|
---|
75 | CopyFrom(s, ulPos, n);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /*
|
---|
79 | *@@ BSString:
|
---|
80 | * copy constructor to copy from a C string.
|
---|
81 | */
|
---|
82 |
|
---|
83 | BSString::BSString(const char *psz) // in: string to copy
|
---|
84 | : BSStringBase(tBSString)
|
---|
85 | {
|
---|
86 | STRINGLOCK;
|
---|
87 | // Init(); is called by parent already V0.9.20 (2002-07-03) [umoeller]
|
---|
88 | CopyFrom(psz);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | *@@ BSString:
|
---|
93 | * copy constructor to extract a new
|
---|
94 | * BSString from a codepaged C string.
|
---|
95 | *
|
---|
96 | * Characters are copied starting at p1.
|
---|
97 | * Copying is stopped at p2, which is not
|
---|
98 | * included.
|
---|
99 | *
|
---|
100 | * Note: as far as I know, something like this
|
---|
101 | * is NOT defined with the C++ string class.
|
---|
102 | */
|
---|
103 |
|
---|
104 | BSString::BSString(const char *p1, const char *p2)
|
---|
105 | : BSStringBase(tBSString)
|
---|
106 | {
|
---|
107 | STRINGLOCK;
|
---|
108 | // Init(); is called by parent already V0.9.20 (2002-07-03) [umoeller]
|
---|
109 | CopyFrom(p1, p2);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | *@@ BSString:
|
---|
114 | * copy constructor to convert from a
|
---|
115 | * UTF-8 BSUString, using the
|
---|
116 | * specified BSUniCodec for conversion.
|
---|
117 | *
|
---|
118 | *@@added V0.9.20 (2002-07-03) [umoeller]
|
---|
119 | */
|
---|
120 |
|
---|
121 | BSString::BSString(BSUniCodec *pCodec,
|
---|
122 | const BSUString &ustr)
|
---|
123 | : BSStringBase(tBSString)
|
---|
124 | {
|
---|
125 | STRINGLOCK;
|
---|
126 |
|
---|
127 | if (!pCodec)
|
---|
128 | throw BSExcptBase("pCodec is NULL.");
|
---|
129 |
|
---|
130 | size_type st;
|
---|
131 | if (st = ustr.length())
|
---|
132 | {
|
---|
133 | pCodec->Uni2Codepage(*this,
|
---|
134 | ustr.GetBuffer(),
|
---|
135 | st);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | *@@ operator[]:
|
---|
141 | * returns the ul'th character of the
|
---|
142 | * string or 0 if ul is too large.
|
---|
143 | */
|
---|
144 |
|
---|
145 | char BSString::operator[](const size_type ul) // in: character offset
|
---|
146 | const
|
---|
147 | {
|
---|
148 | STRINGLOCK;
|
---|
149 | if ( (_pBuf)
|
---|
150 | && (_pBuf->_str.psz)
|
---|
151 | && (ul < _pBuf->_str.ulLength)
|
---|
152 | )
|
---|
153 | return (_pBuf->_str.psz[ul]);
|
---|
154 |
|
---|
155 | return ('\0');
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | *@@ assign:
|
---|
160 | * assigns a codepaged BSString to this.
|
---|
161 | *
|
---|
162 | * This is also used internally for
|
---|
163 | * BSString::operator=.
|
---|
164 | *
|
---|
165 | * Returns *this.
|
---|
166 | *
|
---|
167 | *@@added V0.9.7 (2001-01-07) [umoeller]
|
---|
168 | */
|
---|
169 |
|
---|
170 | BSString& BSString::assign(const BSString &s)
|
---|
171 | {
|
---|
172 | STRINGLOCK;
|
---|
173 | // avoid self assignment
|
---|
174 | if (_pBuf != s._pBuf)
|
---|
175 | {
|
---|
176 | FreeBuf();
|
---|
177 | CopyFrom(s);
|
---|
178 | }
|
---|
179 |
|
---|
180 | return (*this);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*
|
---|
184 | *@@ assign:
|
---|
185 | * assigns a part of the given codepaged
|
---|
186 | * BSString to this.
|
---|
187 | *
|
---|
188 | * Characters are copied from s starting
|
---|
189 | * at ulPos. Copying is stopped at the end
|
---|
190 | * of s or if n characters have been copied.
|
---|
191 | *
|
---|
192 | * Returns *this.
|
---|
193 | *
|
---|
194 | *@@added V0.9.7 (2001-01-07) [umoeller]
|
---|
195 | */
|
---|
196 |
|
---|
197 | BSString& BSString::assign(const BSString &s,
|
---|
198 | size_type ulPos, // in: start copy, defaults to 0
|
---|
199 | size_type n) // in: copy count, defaults to npos
|
---|
200 | {
|
---|
201 | STRINGLOCK;
|
---|
202 | FreeBuf();
|
---|
203 | CopyFrom(s, ulPos, n);
|
---|
204 |
|
---|
205 | return (*this);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*
|
---|
209 | *@@ assign:
|
---|
210 | * assigns a codepaged C string to this.
|
---|
211 | *
|
---|
212 | * Returns *this.
|
---|
213 | *
|
---|
214 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
215 | */
|
---|
216 |
|
---|
217 | BSString& BSString::assign(const char *psz)
|
---|
218 | {
|
---|
219 | STRINGLOCK;
|
---|
220 | FreeBuf();
|
---|
221 | CopyFrom(psz);
|
---|
222 |
|
---|
223 | return (*this);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | *@@ assign:
|
---|
228 | * copies from a C string.
|
---|
229 | *
|
---|
230 | * Characters are copied starting at p1.
|
---|
231 | * Copying is stopped at p2, which is not
|
---|
232 | * included.
|
---|
233 | *
|
---|
234 | * Returns *this.
|
---|
235 | *
|
---|
236 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
237 | */
|
---|
238 |
|
---|
239 | BSString& BSString::assign(const char *p1, const char *p2)
|
---|
240 | {
|
---|
241 | STRINGLOCK;
|
---|
242 | FreeBuf();
|
---|
243 | CopyFrom(p1, p2);
|
---|
244 |
|
---|
245 | return (*this);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /*
|
---|
249 | *@@ assign:
|
---|
250 | * assigns the given single character to this.
|
---|
251 | *
|
---|
252 | * Returns *this.
|
---|
253 | *
|
---|
254 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
255 | */
|
---|
256 |
|
---|
257 | BSString& BSString::assign(char c) // in: character to copy
|
---|
258 | {
|
---|
259 | STRINGLOCK;
|
---|
260 | FreeBuf();
|
---|
261 | CopyFrom(c);
|
---|
262 |
|
---|
263 | return (*this);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*
|
---|
267 | *@@ assignUtf8:
|
---|
268 | * assigns the given UTF-8 BSUString to this,
|
---|
269 | * using the specified BSUniCodec for conversion.
|
---|
270 | *
|
---|
271 | * Returns *this.
|
---|
272 | *
|
---|
273 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
274 | */
|
---|
275 |
|
---|
276 | BSString& BSString::assignUtf8(BSUniCodec *pCodec,
|
---|
277 | const BSUString &ustr)
|
---|
278 | {
|
---|
279 | STRINGLOCK;
|
---|
280 | FreeBuf();
|
---|
281 |
|
---|
282 | if (!pCodec)
|
---|
283 | throw BSExcptBase("pCodec is NULL.");
|
---|
284 |
|
---|
285 | size_type st;
|
---|
286 | if (st = ustr.length())
|
---|
287 | {
|
---|
288 | pCodec->Uni2Codepage(*this,
|
---|
289 | ustr.GetBuffer(),
|
---|
290 | st);
|
---|
291 | }
|
---|
292 |
|
---|
293 | return (*this);
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | *@@ append:
|
---|
298 | * appends the given codepaged BSString to this.
|
---|
299 | *
|
---|
300 | * Returns *this.
|
---|
301 | *
|
---|
302 | *@@changed V0.9.7 (2001-01-15) [umoeller]: ClearShiftTable was missing
|
---|
303 | */
|
---|
304 |
|
---|
305 | BSString& BSString::append(const BSString &s) // in: string to append
|
---|
306 | {
|
---|
307 | STRINGLOCK;
|
---|
308 | size_type ulSourceLen;
|
---|
309 | if (ulSourceLen = s.size())
|
---|
310 | {
|
---|
311 | // assert that we have a modifiable
|
---|
312 | // copy with enough space
|
---|
313 | reserve(ulSourceLen + size() + 1);
|
---|
314 | xstrcats(&_pBuf->_str, &s._pBuf->_str);
|
---|
315 | }
|
---|
316 |
|
---|
317 | return (*this);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /*
|
---|
321 | *@@ append:
|
---|
322 | * appends the given codepaged C string to this.
|
---|
323 | *
|
---|
324 | * Returns *this.
|
---|
325 | */
|
---|
326 |
|
---|
327 | BSString& BSString::append(const char *psz) // in: string to append
|
---|
328 | {
|
---|
329 | STRINGLOCK;
|
---|
330 | size_type ulSourceLen;
|
---|
331 | if ( (psz)
|
---|
332 | && (ulSourceLen = strlen(psz))
|
---|
333 | )
|
---|
334 | {
|
---|
335 | // assert that we have a modifiable
|
---|
336 | // copy with enough space
|
---|
337 | reserve(ulSourceLen + size() + 1);
|
---|
338 | xstrcat(&_pBuf->_str, psz, ulSourceLen);
|
---|
339 | }
|
---|
340 |
|
---|
341 | return (*this);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*
|
---|
345 | *@@ append:
|
---|
346 | * appends the given single character to this.
|
---|
347 | *
|
---|
348 | * Returns *this.
|
---|
349 | *
|
---|
350 | *@@changed V0.9.7 (2001-01-15) [umoeller]: now using xstrcatc
|
---|
351 | */
|
---|
352 |
|
---|
353 | BSString& BSString::append(char c) // in: character to append
|
---|
354 | {
|
---|
355 | STRINGLOCK;
|
---|
356 | if (c)
|
---|
357 | {
|
---|
358 | // assert that we have a modifiable
|
---|
359 | // copy with enough space
|
---|
360 | reserve(1 + size() + 1);
|
---|
361 | xstrcatc(&_pBuf->_str, c);
|
---|
362 | }
|
---|
363 |
|
---|
364 | return (*this);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*
|
---|
368 | *@@ appendCP:
|
---|
369 | * assigns the given UTF-8 BSUString to this,
|
---|
370 | * using the specified BSUniCodec for conversion
|
---|
371 | * before appending.
|
---|
372 | *
|
---|
373 | * Returns *this.
|
---|
374 | *
|
---|
375 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
376 | */
|
---|
377 |
|
---|
378 | BSString& BSString::appendUtf8(BSUniCodec *pCodec,
|
---|
379 | const BSUString &s)
|
---|
380 | {
|
---|
381 | STRINGLOCK;
|
---|
382 | if (!pCodec)
|
---|
383 | throw BSExcptBase("pCodec is NULL.");
|
---|
384 |
|
---|
385 | size_type st;
|
---|
386 | if (st = s.length())
|
---|
387 | {
|
---|
388 | BSString strTemp;
|
---|
389 | pCodec->Uni2Codepage(strTemp,
|
---|
390 | s.GetBuffer(),
|
---|
391 | st);
|
---|
392 | append(strTemp);
|
---|
393 | }
|
---|
394 |
|
---|
395 | return (*this);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | *@@ compare:
|
---|
400 | * returns 0 if the two strings are the
|
---|
401 | * same, -1 if "this" is smaller, 1 if
|
---|
402 | * "s" is smaller.
|
---|
403 | *
|
---|
404 | * As opposed to the standard compare,
|
---|
405 | * this only compares the first "n"
|
---|
406 | * characters of the member string,
|
---|
407 | * counting from ulPos.
|
---|
408 | *
|
---|
409 | * If ulPos is specified, the returned offset
|
---|
410 | * is from the beginning of the string, not
|
---|
411 | * from the offset.
|
---|
412 | */
|
---|
413 |
|
---|
414 | int BSString::compare(size_type ulPos,
|
---|
415 | size_type n,
|
---|
416 | const BSString &s)
|
---|
417 | const
|
---|
418 | {
|
---|
419 | STRINGLOCK;
|
---|
420 | const BSString strTemp(*this, ulPos, n);
|
---|
421 | return (strTemp.compare(s));
|
---|
422 | }
|
---|
423 |
|
---|
424 | /*
|
---|
425 | *@@ find:
|
---|
426 | * first version of find.
|
---|
427 | *
|
---|
428 | * Finds the first position of a substring in the string.
|
---|
429 | *
|
---|
430 | * The search is started at iPos, which defaults to 0.
|
---|
431 | * Returns BSString::npos if not found.
|
---|
432 | *
|
---|
433 | *@@changed V0.9.18 (2002-03-08) [umoeller]: now using strhmemfind
|
---|
434 | */
|
---|
435 |
|
---|
436 | size_type BSString::find(const BSString &strFind, // in: string to find
|
---|
437 | size_type ulPos) // in: offset to start at (defaults to 0)
|
---|
438 | const
|
---|
439 | {
|
---|
440 | STRINGLOCK;
|
---|
441 | if ( (ulPos < size())
|
---|
442 | && (strFind.size())
|
---|
443 | )
|
---|
444 | {
|
---|
445 | // if this is the first time strFind is used as
|
---|
446 | // a "find" string, allocate a shift table...
|
---|
447 | if (strFind._pBuf->_pShiftTable == NULL)
|
---|
448 | {
|
---|
449 | strFind._pBuf->_pShiftTable = (size_t*)malloc(sizeof(size_t) * 256);
|
---|
450 | strFind._pBuf->_fRepeat = FALSE;
|
---|
451 | }
|
---|
452 |
|
---|
453 | char *p;
|
---|
454 | if (p = (char*)strhmemfind(_pBuf->_str.psz + ulPos, // in: haystack
|
---|
455 | _pBuf->_str.ulLength - ulPos,
|
---|
456 | strFind._pBuf->_str.psz,
|
---|
457 | strFind._pBuf->_str.ulLength,
|
---|
458 | strFind._pBuf->_pShiftTable,
|
---|
459 | &strFind._pBuf->_fRepeat))
|
---|
460 | return (p - _pBuf->_str.psz);
|
---|
461 |
|
---|
462 | /* char *p;
|
---|
463 | if (p = strstr(_pBuf->_str.psz + ulPos,
|
---|
464 | strFind._pBuf->_str.psz))
|
---|
465 | // found: calculate offset
|
---|
466 | return (p - _pBuf->_str.psz); */
|
---|
467 | }
|
---|
468 |
|
---|
469 | return (npos);
|
---|
470 | }
|
---|
471 |
|
---|
472 | /*
|
---|
473 | *@@ find:
|
---|
474 | * second version of find.
|
---|
475 | *
|
---|
476 | * Finds the first position of a substring in the string.
|
---|
477 | *
|
---|
478 | * The search is started at iPos, which defaults to 0.
|
---|
479 | * Returns BSString::npos if not found.
|
---|
480 | */
|
---|
481 |
|
---|
482 | size_type BSString::find(const char *pszFind, // in: string to find
|
---|
483 | size_type ulPos) // in: offset to start at (defaults to 0)
|
---|
484 | const
|
---|
485 | {
|
---|
486 | STRINGLOCK;
|
---|
487 | if ( (ulPos < size())
|
---|
488 | && (pszFind)
|
---|
489 | && (*pszFind)
|
---|
490 | )
|
---|
491 | {
|
---|
492 | char *p;
|
---|
493 | if (p = strstr(_pBuf->_str.psz + ulPos,
|
---|
494 | pszFind))
|
---|
495 | // found: calculate offset
|
---|
496 | return (p - _pBuf->_str.psz);
|
---|
497 | }
|
---|
498 |
|
---|
499 | return (npos);
|
---|
500 | }
|
---|
501 |
|
---|
502 | /*
|
---|
503 | *@@ find:
|
---|
504 | * third version of find.
|
---|
505 | *
|
---|
506 | * Finds the first position of a character in the string.
|
---|
507 | * The search is started at iPos, which defaults to 0.
|
---|
508 | *
|
---|
509 | * Returns BSString::npos if not found.
|
---|
510 | */
|
---|
511 |
|
---|
512 | size_type BSString::find(char c, // in: character to find
|
---|
513 | size_type ulPos) // in: offset to start at (defaults to 0)
|
---|
514 | const
|
---|
515 | {
|
---|
516 | STRINGLOCK;
|
---|
517 | if ( (ulPos < size())
|
---|
518 | && (c)
|
---|
519 | )
|
---|
520 | {
|
---|
521 | char *p;
|
---|
522 | if (p = strchr(_pBuf->_str.psz + ulPos, c))
|
---|
523 | // found: calculate offset
|
---|
524 | return (p - _pBuf->_str.psz);
|
---|
525 | }
|
---|
526 |
|
---|
527 | return (npos);
|
---|
528 | }
|
---|
529 |
|
---|
530 | /*
|
---|
531 | *@@ rfind:
|
---|
532 | * finds the last position of a character in the string.
|
---|
533 | *
|
---|
534 | * Returns BSString::npos if not found.
|
---|
535 | *
|
---|
536 | *@@changed V0.9.18 (2002-03-08) [umoeller]: rewritten using strrchr
|
---|
537 | */
|
---|
538 |
|
---|
539 | size_type BSString::rfind(char c, // in: character to find
|
---|
540 | size_type ulPos) // in: position to search from (from the right);
|
---|
541 | // defaults to npos (means end of string)
|
---|
542 | const
|
---|
543 | {
|
---|
544 | STRINGLOCK;
|
---|
545 | size_type Size;
|
---|
546 |
|
---|
547 | if ( (Size = size())
|
---|
548 | && (c)
|
---|
549 | )
|
---|
550 | {
|
---|
551 | const char *p;
|
---|
552 | if ( (ulPos == npos)
|
---|
553 | || (ulPos > Size)
|
---|
554 | )
|
---|
555 | p = _pBuf->_str.psz;
|
---|
556 | else
|
---|
557 | p = _pBuf->_str.psz + ulPos;
|
---|
558 |
|
---|
559 | if (p = strrchr(p, c))
|
---|
560 | // found: calculate offset
|
---|
561 | return (p - _pBuf->_str.psz);
|
---|
562 |
|
---|
563 | /* char *p;
|
---|
564 |
|
---|
565 | if ( (ulPos == npos)
|
---|
566 | || (ulPos > Size)
|
---|
567 | )
|
---|
568 | p = _str.psz + Size - 1;
|
---|
569 | else
|
---|
570 | p = _str.psz + ulPos;
|
---|
571 |
|
---|
572 | // search backwards
|
---|
573 | for (;
|
---|
574 | p >= _str.psz;
|
---|
575 | p--)
|
---|
576 | if (*p == c)
|
---|
577 | return (p - _str.psz);
|
---|
578 | */
|
---|
579 | }
|
---|
580 |
|
---|
581 | return (npos);
|
---|
582 | }
|
---|
583 |
|
---|
584 | /*
|
---|
585 | *@@ find_first_of:
|
---|
586 | * finds the first character in the member
|
---|
587 | * string which is one of the chars in achChars.
|
---|
588 | *
|
---|
589 | * The search is started at ulPos, which
|
---|
590 | * defaults to 0.
|
---|
591 | *
|
---|
592 | * Returns BSString::npos if not found.
|
---|
593 | *
|
---|
594 | *@@added V0.9.7 (2001-01-07) [umoeller]
|
---|
595 | */
|
---|
596 |
|
---|
597 | size_type BSString::find_first_of(const char *achChars, // in: chars array to look for
|
---|
598 | size_type ulPos) // in: start of search (defaults to 0)
|
---|
599 | const
|
---|
600 | {
|
---|
601 | STRINGLOCK;
|
---|
602 | size_type Size = size();
|
---|
603 |
|
---|
604 | if ( (ulPos < Size)
|
---|
605 | && (achChars)
|
---|
606 | && (*achChars)
|
---|
607 | )
|
---|
608 | {
|
---|
609 | char *p;
|
---|
610 | size_type ul;
|
---|
611 | for (p = _pBuf->_str.psz + ulPos, ul = ulPos;
|
---|
612 | ul < Size;
|
---|
613 | ++p, ++ul)
|
---|
614 | {
|
---|
615 | if (strchr(achChars, *p))
|
---|
616 | // *p is in achChars:
|
---|
617 | return (ul);
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 | // not found:
|
---|
622 | return (npos);
|
---|
623 | }
|
---|
624 |
|
---|
625 | /*
|
---|
626 | *@@ find_first_not_of:
|
---|
627 | * finds the first character in the member
|
---|
628 | * string which is different from "c".
|
---|
629 | *
|
---|
630 | * The search is started at ulPos, which
|
---|
631 | * defaults to 0.
|
---|
632 | *
|
---|
633 | * Returns BSString::npos if not found.
|
---|
634 | */
|
---|
635 |
|
---|
636 | size_type BSString::find_first_not_of(char c, // in: character to ignore
|
---|
637 | size_type ulPos) // in: start of search (defaults to 0)
|
---|
638 | const
|
---|
639 | {
|
---|
640 | STRINGLOCK;
|
---|
641 | size_type Size = size();
|
---|
642 |
|
---|
643 | if ( (ulPos < Size)
|
---|
644 | && (c)
|
---|
645 | )
|
---|
646 | {
|
---|
647 | char *p;
|
---|
648 | size_type ul;
|
---|
649 | for (p = _pBuf->_str.psz + ulPos, ul = ulPos;
|
---|
650 | ul < Size;
|
---|
651 | ++p, ++ul)
|
---|
652 | {
|
---|
653 | if (*p != c)
|
---|
654 | return (ul);
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | // not found:
|
---|
659 | return (npos);
|
---|
660 | }
|
---|
661 |
|
---|
662 | /*
|
---|
663 | *@@ find_first_not_of:
|
---|
664 | * finds the first character in the member
|
---|
665 | * string which is NOT one of the chars in achChars.
|
---|
666 | *
|
---|
667 | * The search is started at ulPos, which
|
---|
668 | * defaults to 0.
|
---|
669 | *
|
---|
670 | * Returns BSString::npos if not found.
|
---|
671 | *
|
---|
672 | *@@added V0.9.7 (2001-01-07) [umoeller]
|
---|
673 | */
|
---|
674 |
|
---|
675 | size_type BSString::find_first_not_of(const char *achChars, // in: characters to ignore
|
---|
676 | size_type ulPos) // in: start of search (defaults to 0)
|
---|
677 | const
|
---|
678 | {
|
---|
679 | STRINGLOCK;
|
---|
680 | size_type Size = size();
|
---|
681 |
|
---|
682 | if ( (ulPos < Size)
|
---|
683 | && (achChars)
|
---|
684 | && (*achChars)
|
---|
685 | )
|
---|
686 | {
|
---|
687 | char *p;
|
---|
688 | size_type ul;
|
---|
689 | for (p = _pBuf->_str.psz + ulPos, ul = ulPos;
|
---|
690 | ul < Size;
|
---|
691 | ++p, ++ul)
|
---|
692 | {
|
---|
693 | if (!strchr(achChars, *p))
|
---|
694 | // *p is NOT in achChars:
|
---|
695 | return (ul);
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | // not found:
|
---|
700 | return (npos);
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*
|
---|
704 | *@@ replace:
|
---|
705 | * replaces "nThis" characters, starting at
|
---|
706 | * position ulPosThis, in the member string
|
---|
707 | * with "strReplace".
|
---|
708 | *
|
---|
709 | * Returns *this.
|
---|
710 | *
|
---|
711 | *@@added V0.9.7 (2001-01-15) [umoeller]
|
---|
712 | */
|
---|
713 |
|
---|
714 | BSString& BSString::replace(size_type ulPosThis, // in: ofs of first char to replace
|
---|
715 | size_type nThis, // in: char count to replace
|
---|
716 | const BSString &strReplace) // in: string to replace with
|
---|
717 | {
|
---|
718 | STRINGLOCK;
|
---|
719 | if (size())
|
---|
720 | {
|
---|
721 | reserve(0);
|
---|
722 |
|
---|
723 | if (nThis == npos)
|
---|
724 | // I'm not sure if the C++ string allows setting nThis to npos...
|
---|
725 | // but we'll just support it, it won't hurt
|
---|
726 | nThis = _pBuf->_str.ulLength - ulPosThis;
|
---|
727 |
|
---|
728 | xstrrpl(&_pBuf->_str,
|
---|
729 | ulPosThis,
|
---|
730 | nThis,
|
---|
731 | strReplace._pBuf->_str.psz,
|
---|
732 | strReplace._pBuf->_str.ulLength);
|
---|
733 | }
|
---|
734 |
|
---|
735 | return (*this);
|
---|
736 | }
|
---|
737 |
|
---|
738 | /*
|
---|
739 | *@@ substr:
|
---|
740 | * creates a new string containing parts of
|
---|
741 | * the member string.
|
---|
742 | *
|
---|
743 | * Copying is started at offset ulPos, from
|
---|
744 | * which n characters are copied. If
|
---|
745 | * (n == BSString::npos), all remaining characters
|
---|
746 | * are copied.
|
---|
747 | *
|
---|
748 | * This always returns a string, but it may be
|
---|
749 | * empty if invalid parameters are specified.
|
---|
750 | */
|
---|
751 |
|
---|
752 | BSString BSString::substr(size_type ulPos, // in: start pos, defaults to 0
|
---|
753 | size_type n) // in: no. of characters to copy,
|
---|
754 | // defaults to npos (== up to rest of string)
|
---|
755 | const
|
---|
756 | {
|
---|
757 | STRINGLOCK;
|
---|
758 | return BSString(*this, ulPos, n);
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*
|
---|
762 | *@@ _find_word:
|
---|
763 | * searches for strWord in the string, whose offset
|
---|
764 | * is returned if found (or BSString::npos if not).
|
---|
765 | *
|
---|
766 | * As opposed to BSString::find, this finds strWord
|
---|
767 | * only if it is a "word". A search string is
|
---|
768 | * considered a word if the character _before_
|
---|
769 | * it is in pcszBeginChars and the char _after_
|
---|
770 | * it is in pcszEndChars.
|
---|
771 | *
|
---|
772 | * Example:
|
---|
773 | + BSString str("This is an example.");
|
---|
774 | + str._find_word(, "is");
|
---|
775 | *
|
---|
776 | * returns the offset of "is", but not the "is" in "This".
|
---|
777 | *
|
---|
778 | * Note: as far as I know, something like this
|
---|
779 | * is NOT defined with the C++ string class.
|
---|
780 | */
|
---|
781 |
|
---|
782 | size_type BSString::_find_word(BSString &strFind,
|
---|
783 | size_type ulPos, // defaults to 0
|
---|
784 | const char *pcszBeginChars, // = "\x0d\x0a ()/\\-,.",
|
---|
785 | const char *pcszEndChars) // = "\x0d\x0a ()/\\-,.:;")
|
---|
786 | const
|
---|
787 | {
|
---|
788 | STRINGLOCK;
|
---|
789 | if ( (ulPos < size())
|
---|
790 | && (strFind.size())
|
---|
791 | )
|
---|
792 | {
|
---|
793 | // if this is the first time strFind is used as
|
---|
794 | // a "find" string, allocate a shift table...
|
---|
795 | if (strFind._pBuf->_pShiftTable == NULL)
|
---|
796 | {
|
---|
797 | strFind._pBuf->_pShiftTable = (size_t*)malloc(sizeof(size_t) * 256);
|
---|
798 | strFind._pBuf->_fRepeat = FALSE;
|
---|
799 | }
|
---|
800 |
|
---|
801 | char *p;
|
---|
802 | if (p = xstrFindWord(&_pBuf->_str,
|
---|
803 | ulPos,
|
---|
804 | &strFind._pBuf->_str,
|
---|
805 | strFind._pBuf->_pShiftTable,
|
---|
806 | (PBOOL)&strFind._pBuf->_fRepeat,
|
---|
807 | pcszBeginChars,
|
---|
808 | pcszEndChars))
|
---|
809 | // found: calculate offset
|
---|
810 | return (p - _pBuf->_str.psz);
|
---|
811 | }
|
---|
812 |
|
---|
813 | return (npos);
|
---|
814 | }
|
---|
815 |
|
---|
816 | /*
|
---|
817 | *@@ _find_word:
|
---|
818 | * second version of _find_word, which takes
|
---|
819 | * a const PSZ as input.
|
---|
820 | *
|
---|
821 | * Note: as far as I know, something like this
|
---|
822 | * is NOT defined with the C++ string class.
|
---|
823 | */
|
---|
824 |
|
---|
825 | size_type BSString::_find_word(const char *pszFind,
|
---|
826 | size_type ulPos, // defaults to 0
|
---|
827 | const char *pcszBeginChars, // = "\x0d\x0a ()/\\-,.",
|
---|
828 | const char *pcszEndChars) // = "\x0d\x0a ()/\\-,.:;")
|
---|
829 | const
|
---|
830 | {
|
---|
831 | STRINGLOCK;
|
---|
832 | if ( (ulPos < size())
|
---|
833 | && (pszFind)
|
---|
834 | && (*pszFind)
|
---|
835 | )
|
---|
836 | {
|
---|
837 | char *p;
|
---|
838 | if (p = strhFindWord(_pBuf->_str.psz + ulPos,
|
---|
839 | pszFind,
|
---|
840 | pcszBeginChars,
|
---|
841 | pcszEndChars))
|
---|
842 | // found: calculate offset
|
---|
843 | return (p - _pBuf->_str.psz);
|
---|
844 | }
|
---|
845 |
|
---|
846 | return (npos);
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*
|
---|
850 | *@@ _extract_word:
|
---|
851 | * extracts a word from the member string,
|
---|
852 | * which is written into strTarget.
|
---|
853 | *
|
---|
854 | * Returns 1 (TRUE) if the specified word still
|
---|
855 | * existed or 0 otherwise.
|
---|
856 | *
|
---|
857 | * Note: as far as I know, something like this
|
---|
858 | * is NOT defined with the C++ string class.
|
---|
859 | *
|
---|
860 | *@@added V0.9.7 (2001-01-07) [umoeller]
|
---|
861 | */
|
---|
862 |
|
---|
863 | int BSString::_extract_word(unsigned long ulIndex, // in: word index
|
---|
864 | BSString &strTarget, // out: word
|
---|
865 | size_type ulPos, // defaults to 0
|
---|
866 | const char *pcszBeginChars, // = "\x0d\x0a ()/\\-,.",
|
---|
867 | const char *pcszEndChars) // = "\x0d\x0a ()/\\-,.:;")
|
---|
868 | const
|
---|
869 | {
|
---|
870 | STRINGLOCK;
|
---|
871 | int rc = 0;
|
---|
872 |
|
---|
873 | unsigned long ul = 0;
|
---|
874 | for (ul = 0;
|
---|
875 | ul <= ulIndex;
|
---|
876 | ++ul)
|
---|
877 | {
|
---|
878 | ULONG ulStart = find_first_not_of(pcszBeginChars, ulPos);
|
---|
879 | if (ulStart == npos)
|
---|
880 | break;
|
---|
881 | else
|
---|
882 | {
|
---|
883 | ULONG ulEnd = find_first_of(pcszEndChars, ulStart + 1);
|
---|
884 | if (ulEnd == npos)
|
---|
885 | {
|
---|
886 | if (ul == ulIndex)
|
---|
887 | {
|
---|
888 | // copy till the end
|
---|
889 | strTarget.assign(*this,
|
---|
890 | ulStart);
|
---|
891 | rc = 1;
|
---|
892 | break;
|
---|
893 | }
|
---|
894 | else
|
---|
895 | // not there yet: get outta here
|
---|
896 | break;
|
---|
897 | }
|
---|
898 | else
|
---|
899 | if (ul == ulIndex)
|
---|
900 | {
|
---|
901 | // that's the word we want:
|
---|
902 | strTarget.assign(*this,
|
---|
903 | ulStart,
|
---|
904 | (ulEnd - ulStart));
|
---|
905 | rc = 1;
|
---|
906 | break;
|
---|
907 | }
|
---|
908 | else
|
---|
909 | // we need more words:
|
---|
910 | ulPos = ulEnd;
|
---|
911 | // and search on
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 | return (rc);
|
---|
916 | }
|
---|
917 |
|
---|
918 | /*
|
---|
919 | *@@ _format:
|
---|
920 | * this removes all EOL's and double spaces.
|
---|
921 | * Also removes leading and trailing spaces.
|
---|
922 | *
|
---|
923 | * Note: as far as I know, something like this
|
---|
924 | * is NOT defined with the C++ string class.
|
---|
925 | */
|
---|
926 |
|
---|
927 | VOID BSString::_format()
|
---|
928 | {
|
---|
929 | STRINGLOCK;
|
---|
930 | reserve(0);
|
---|
931 |
|
---|
932 | if (_pBuf && _pBuf->_str.psz)
|
---|
933 | {
|
---|
934 | PSZ p;
|
---|
935 | // replace \n, \r with spaces (length is constant)
|
---|
936 | for (p = _pBuf->_str.psz;
|
---|
937 | p < _pBuf->_str.psz + _pBuf->_str.ulLength;
|
---|
938 | ++p)
|
---|
939 | {
|
---|
940 | if (*p == 0x0a)
|
---|
941 | *p = ' ';
|
---|
942 | else if (*p == 0x0d)
|
---|
943 | *p = ' ';
|
---|
944 | }
|
---|
945 |
|
---|
946 | // remove double spaces
|
---|
947 | for (p = _pBuf->_str.psz;
|
---|
948 | p < _pBuf->_str.psz + _pBuf->_str.ulLength;
|
---|
949 | ++p)
|
---|
950 | {
|
---|
951 | if ((*p == ' ') && (*(p+1) == ' '))
|
---|
952 | {
|
---|
953 | PSZ p2 = p;
|
---|
954 | while (*p2)
|
---|
955 | {
|
---|
956 | *p2 = *(p2+1);
|
---|
957 | ++p2;
|
---|
958 | }
|
---|
959 | --(_pBuf->_str.ulLength);
|
---|
960 | --p;
|
---|
961 | }
|
---|
962 | }
|
---|
963 |
|
---|
964 | // remove leading spaces
|
---|
965 | for (p = _pBuf->_str.psz;
|
---|
966 | p < _pBuf->_str.psz + _pBuf->_str.ulLength;
|
---|
967 | ++p)
|
---|
968 | {
|
---|
969 | if (*p == ' ')
|
---|
970 | {
|
---|
971 | PSZ p2 = p;
|
---|
972 | while (*p2)
|
---|
973 | {
|
---|
974 | *p2 = *(p2+1);
|
---|
975 | ++p2;
|
---|
976 | }
|
---|
977 | _pBuf->_str.ulLength--;
|
---|
978 | }
|
---|
979 | else
|
---|
980 | break;
|
---|
981 | }
|
---|
982 |
|
---|
983 | // remove trailing spaces
|
---|
984 | for (p = _pBuf->_str.psz + _pBuf->_str.ulLength - 1;
|
---|
985 | p > _pBuf->_str.psz;
|
---|
986 | p--)
|
---|
987 | {
|
---|
988 | if (*p == ' ')
|
---|
989 | {
|
---|
990 | *p = 0;
|
---|
991 | _pBuf->_str.ulLength--;
|
---|
992 | }
|
---|
993 | else
|
---|
994 | break;
|
---|
995 | }
|
---|
996 | }
|
---|
997 | }
|
---|
998 |
|
---|
999 | int operator==(const BSString &s1, const BSString &s2)
|
---|
1000 | {
|
---|
1001 | return (s1.compare(s2) == 0);
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | int operator==(const char *psz1, const BSString &s2)
|
---|
1005 | {
|
---|
1006 | return (s2.compare(psz1) == 0);
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | int operator==(const BSString &s1, const char *psz2)
|
---|
1010 | {
|
---|
1011 | return (s1.compare(psz2) == 0);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | int operator!=(const BSString &s1, const BSString &s2)
|
---|
1015 | {
|
---|
1016 | return (s1.compare(s2) != 0);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | int operator!=(const char *psz1, const BSString &s2)
|
---|
1020 | {
|
---|
1021 | return (s2.compare(psz1) != 0);
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | int operator!=(const BSString &s1, const char *psz2)
|
---|
1025 | {
|
---|
1026 | return (s1.compare(psz2) != 0);
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | int operator<(const BSString &s1, const BSString &s2)
|
---|
1030 | {
|
---|
1031 | return (s1.compare(s2) < 0);
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | BSString operator+(const BSString &s1, const BSString &s2)
|
---|
1035 | {
|
---|
1036 | BSString str(s1);
|
---|
1037 | str.append(s2);
|
---|
1038 | return (str);
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | BSString operator+(const char *psz1, const BSString &s2)
|
---|
1042 | {
|
---|
1043 | BSString str(psz1);
|
---|
1044 | str.append(s2);
|
---|
1045 | return (str);
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | BSString operator+(const BSString &s1, const char *psz2)
|
---|
1049 | {
|
---|
1050 | BSString str(s1);
|
---|
1051 | str.append(psz2);
|
---|
1052 | return (str);
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | // test case
|
---|
1057 |
|
---|
1058 | /* int main()
|
---|
1059 | {
|
---|
1060 | BSString str1("This is a test string."),
|
---|
1061 | str2(str1, 0);
|
---|
1062 |
|
---|
1063 | printf("Original was: %s\n", str1.c_str());
|
---|
1064 | printf("Second is: %s\n", str2.c_str());
|
---|
1065 |
|
---|
1066 | str2 = str1 + " Now we appended something.";
|
---|
1067 | printf("Third is: %s\n", str2.c_str());
|
---|
1068 |
|
---|
1069 | BSString strFind = "e";
|
---|
1070 | BSString strRepl = "DJASDKLASDPO(u";
|
---|
1071 | ULONG ulPos = 0;
|
---|
1072 | while (str2._find_replace(strFind, strRepl, &ulPos)
|
---|
1073 | != string::npos)
|
---|
1074 | ;
|
---|
1075 |
|
---|
1076 | printf("After repl: %s\n", str2.c_str());
|
---|
1077 |
|
---|
1078 | return (0);
|
---|
1079 | }
|
---|
1080 | */
|
---|