[250] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | * bs_string.h:
|
---|
| 4 | * header file for the various string classes.
|
---|
| 5 | *
|
---|
| 6 | * This declares BSString, BSUString, and
|
---|
| 7 | * BSUniCodec, among other things. These give
|
---|
| 8 | * you not only a very efficient basic string
|
---|
| 9 | * class (BSString), but also full Unicode
|
---|
| 10 | * and codepage support and conversion between
|
---|
| 11 | * them.
|
---|
| 12 | *
|
---|
| 13 | * Warning: if you include this header, you'll
|
---|
| 14 | * get a typedef for "string" to BSString and
|
---|
| 15 | * for "ustring" to BSUString, which might
|
---|
| 16 | * conflict with other string classes that you
|
---|
| 17 | * might be using.
|
---|
| 18 | *
|
---|
| 19 | *@@include #include "helpers\stringh.h"
|
---|
| 20 | *@@include #include "helpers\xstring.h"
|
---|
| 21 | *@@include #include "base\bs_string.h"
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
[449] | 25 | * This file Copyright (C) 1999-2020 Ulrich Mller.
|
---|
[250] | 26 | * This program is free software; you can redistribute it and/or modify
|
---|
| 27 | * it under the terms of the GNU General Public License as published by
|
---|
| 28 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
---|
| 29 | * file of this distribution.
|
---|
| 30 | * This program is distributed in the hope that it will be useful,
|
---|
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 33 | * GNU General Public License for more details.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef WARPIN_STRING_HEADER_INCLUDED
|
---|
| 37 | #define WARPIN_STRING_HEADER_INCLUDED
|
---|
| 38 |
|
---|
| 39 | typedef unsigned long size_type;
|
---|
| 40 |
|
---|
| 41 | class BSString;
|
---|
| 42 | class BSUString;
|
---|
| 43 |
|
---|
| 44 | // extern BSMutex G_mtxStrings;
|
---|
| 45 |
|
---|
| 46 | // #define STRINGLOCK BSLock lock(G_mtxStrings)
|
---|
| 47 |
|
---|
| 48 | #define STRINGLOCK
|
---|
| 49 |
|
---|
| 50 | /*
|
---|
| 51 | *@@ BSUniCodec:
|
---|
| 52 | * Unicode conversion object (COder and
|
---|
| 53 | * DECoder).
|
---|
| 54 | *
|
---|
| 55 | * Create one such object to allow conversion
|
---|
| 56 | * from UTF-8 to codepage-specific and back.
|
---|
| 57 | * An instance of this is required as input
|
---|
| 58 | * to the conversion methods from BSString
|
---|
| 59 | * to BSUString and vice versa, such as:
|
---|
| 60 | *
|
---|
| 61 | * -- BSString::assignUtf8 (convert UTF-8
|
---|
| 62 | * to codepage-specific)
|
---|
| 63 | *
|
---|
| 64 | * -- BSString::appendUtf8 (append UTF-8
|
---|
| 65 | * to codepage-specific)
|
---|
| 66 | *
|
---|
| 67 | * -- BSUString::assignCP (convert
|
---|
| 68 | * codepage-specific to UTF-8)
|
---|
| 69 | *
|
---|
| 70 | * This thing throws BSExcptBase if the
|
---|
| 71 | * input codepage is not supported or if
|
---|
| 72 | * we run into invalid code sequences.
|
---|
| 73 | *
|
---|
| 74 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 | class BSUniCodec : public BSRoot
|
---|
| 78 | {
|
---|
| 79 | private:
|
---|
| 80 | void *_pCodec;
|
---|
| 81 | unsigned short _usCodepage;
|
---|
| 82 | char _achDBCS[12];
|
---|
| 83 | BOOL _fDouble; // TRUE if double-byte codepage
|
---|
| 84 |
|
---|
| 85 | public:
|
---|
| 86 | DECLARE_CLASS(BSUniCodec);
|
---|
| 87 |
|
---|
| 88 | BSUniCodec(unsigned short usCodepage);
|
---|
| 89 | ~BSUniCodec();
|
---|
| 90 |
|
---|
| 91 | void Codepage2Uni(BSUString &ustr,
|
---|
| 92 | const char *pcszCP,
|
---|
| 93 | unsigned long ulLength);
|
---|
| 94 |
|
---|
| 95 | void Uni2Codepage(BSString &str,
|
---|
| 96 | const char *pcszUni,
|
---|
| 97 | unsigned long ulLength);
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | *@@ QueryCodepage:
|
---|
| 101 | * returns the codepage that this
|
---|
| 102 | * BSUniCodec was created for.
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | inline unsigned short QueryCodepage()
|
---|
| 106 | {
|
---|
| 107 | return _usCodepage;
|
---|
| 108 | }
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | *@@ BSStringBuf:
|
---|
| 113 | * string buffer encapsulation for BSStringBase.
|
---|
| 114 | *
|
---|
| 115 | * Several BSStringBase instances can share
|
---|
| 116 | * the same string buffer when copied from
|
---|
| 117 | * one another. This is new with V0.9.18,
|
---|
| 118 | * while previously each BSString had its own
|
---|
| 119 | * buffer, which lead to a lot of redundant
|
---|
| 120 | * copying.
|
---|
| 121 | *
|
---|
| 122 | * Note that all methods are private in order
|
---|
| 123 | * not to let anyone mess with these things.
|
---|
| 124 | *
|
---|
| 125 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
| 126 | */
|
---|
| 127 |
|
---|
| 128 | class BSStringBuf : public BSRoot
|
---|
| 129 | {
|
---|
| 130 | friend class BSStringBase;
|
---|
| 131 | friend class BSString;
|
---|
| 132 | friend class BSUString;
|
---|
| 133 |
|
---|
| 134 | private:
|
---|
| 135 | XSTRING _str;
|
---|
| 136 |
|
---|
| 137 | size_t *_pShiftTable;
|
---|
| 138 | unsigned long _fRepeat;
|
---|
| 139 |
|
---|
| 140 | unsigned long _cShared; // if 0, buf is used only by one
|
---|
| 141 | // instance; otherwise, no. of
|
---|
| 142 | // BSString's that share this buf
|
---|
| 143 |
|
---|
| 144 | DECLARE_CLASS(BSStringBuf);
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | *@@ BSStringBuf:
|
---|
| 148 | * constructor to create a
|
---|
| 149 | * new member XSTRING from the
|
---|
| 150 | * given C string.
|
---|
| 151 | */
|
---|
| 152 |
|
---|
| 153 | BSStringBuf(const char *pcsz,
|
---|
| 154 | unsigned long ulLength,
|
---|
| 155 | unsigned long cbAllocate)
|
---|
| 156 | : BSRoot(tBSStringBuf)
|
---|
| 157 | {
|
---|
| 158 | xstrInit(&_str, cbAllocate);
|
---|
| 159 | xstrcpy(&_str, pcsz, ulLength);
|
---|
| 160 | _pShiftTable = NULL;
|
---|
| 161 | _fRepeat = 0;
|
---|
| 162 | _cShared = 0;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | *@@ BSStringBuf:
|
---|
| 167 | * evil second constructor
|
---|
| 168 | * to take over an existing
|
---|
| 169 | * XSTRING. Used by BSString::_take_from
|
---|
| 170 | * only.
|
---|
| 171 | */
|
---|
| 172 |
|
---|
| 173 | BSStringBuf(XSTRING &str)
|
---|
| 174 | : BSRoot(tBSStringBuf)
|
---|
| 175 | {
|
---|
| 176 | memcpy(&_str, &str, sizeof(XSTRING));
|
---|
| 177 | _pShiftTable = NULL;
|
---|
| 178 | _fRepeat = 0;
|
---|
| 179 | _cShared = 0;
|
---|
| 180 |
|
---|
| 181 | // and nuke the source XSTRING so
|
---|
| 182 | // that the caller won't free it
|
---|
| 183 | memset(&str, 0, sizeof(XSTRING));
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | *@@ ~BSStringBuf:
|
---|
| 188 | * destructor. Frees all memory associated
|
---|
| 189 | * with the buffer.
|
---|
| 190 | */
|
---|
| 191 |
|
---|
| 192 | ~BSStringBuf()
|
---|
| 193 | {
|
---|
| 194 | ClearShiftTable();
|
---|
| 195 | xstrClear(&_str);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /*
|
---|
| 199 | *@@ ClearShiftTable:
|
---|
| 200 | * clears the internal shift
|
---|
| 201 | * table when the string buffer
|
---|
| 202 | * has changed.
|
---|
| 203 | */
|
---|
| 204 |
|
---|
| 205 | inline void ClearShiftTable()
|
---|
| 206 | {
|
---|
| 207 | if (_pShiftTable)
|
---|
| 208 | {
|
---|
| 209 | free(_pShiftTable);
|
---|
| 210 | _pShiftTable = 0;
|
---|
| 211 | }
|
---|
| 212 | _fRepeat = FALSE;
|
---|
| 213 | }
|
---|
| 214 | };
|
---|
| 215 |
|
---|
| 216 | /*
|
---|
| 217 | *@@ BSStringBase:
|
---|
| 218 | * common superclass for BSString and
|
---|
| 219 | * BSUString. This implements most of
|
---|
| 220 | * the functionality for the two, but
|
---|
| 221 | * since the constructor is protected,
|
---|
| 222 | * this cannot be created as such.
|
---|
| 223 | *
|
---|
| 224 | * BSString and BSUString are implemented
|
---|
| 225 | * as subclasses of this to prevent illegal
|
---|
| 226 | * assignments between codepage-specific
|
---|
| 227 | * and UTF-8 strings already by the compiler.
|
---|
| 228 | *
|
---|
| 229 | * This has a BSStringBuf pointer as
|
---|
| 230 | * a member so that several strings can
|
---|
| 231 | * share the same memory.
|
---|
| 232 | *
|
---|
| 233 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
| 234 | */
|
---|
| 235 |
|
---|
| 236 | class BSStringBase : public BSRoot
|
---|
| 237 | {
|
---|
| 238 | public:
|
---|
| 239 | DECLARE_CLASS(BSStringBase);
|
---|
| 240 |
|
---|
| 241 | #ifdef __IBMCPP__
|
---|
| 242 | friend class BSStringBase;
|
---|
| 243 | #endif
|
---|
| 244 | // 2000-01-15: This line is extraordinary useless - class instances
|
---|
| 245 | // of the same type are always friends (czw)
|
---|
| 246 | // V0.9.1 (2000-02-01) [umoeller]: Hi Jens. Not with VAC++, apparently,
|
---|
| 247 | // because without this line, this wouldn't compile.
|
---|
| 248 |
|
---|
| 249 | protected:
|
---|
| 250 | BSStringBuf *_pBuf;
|
---|
| 251 |
|
---|
| 252 | /********************************************
|
---|
| 253 | *
|
---|
| 254 | * protected methods
|
---|
| 255 | *
|
---|
| 256 | ********************************************/
|
---|
| 257 |
|
---|
| 258 | /*
|
---|
| 259 | *@@ Init:
|
---|
| 260 | * private helper method to initialize
|
---|
| 261 | * the instance. Called by all constructors.
|
---|
| 262 | */
|
---|
| 263 |
|
---|
| 264 | void Init();
|
---|
| 265 |
|
---|
| 266 | void FreeBuf();
|
---|
| 267 |
|
---|
| 268 | void CopyFrom(const BSStringBase &s);
|
---|
| 269 | void CopyFrom(const BSStringBase &s,
|
---|
| 270 | size_type ulPos,
|
---|
| 271 | size_type n = npos);
|
---|
| 272 | void CopyFrom(const char *psz);
|
---|
| 273 | void CopyFrom(const char *p1, const char *p2);
|
---|
| 274 | void CopyFrom(char c);
|
---|
| 275 |
|
---|
| 276 | BSStringBase(BSClassID &Class);
|
---|
| 277 |
|
---|
| 278 | private:
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | *@@ BSStringBase:
|
---|
| 282 | * private default copy constructor
|
---|
| 283 | * to make sure this can never be
|
---|
| 284 | * called by the subclasses.
|
---|
| 285 | */
|
---|
| 286 |
|
---|
| 287 | BSStringBase(const BSStringBase &s)
|
---|
| 288 | : BSRoot(s._Class)
|
---|
| 289 | {
|
---|
| 290 | };
|
---|
| 291 |
|
---|
| 292 | protected:
|
---|
| 293 |
|
---|
| 294 | BSStringBase(const BSStringBase &s,
|
---|
| 295 | BSClassID &Class);
|
---|
| 296 |
|
---|
| 297 | virtual ~BSStringBase();
|
---|
| 298 |
|
---|
| 299 | public:
|
---|
| 300 |
|
---|
| 301 | static const size_type npos;
|
---|
| 302 |
|
---|
| 303 | /********************************************
|
---|
| 304 | *
|
---|
| 305 | * property queries
|
---|
| 306 | *
|
---|
| 307 | ********************************************/
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | *@@ size:
|
---|
| 311 | * returns the no. of characters in
|
---|
| 312 | * the string (excluding our internal
|
---|
| 313 | * null terminator) or 0 if the string
|
---|
| 314 | * is empty.
|
---|
| 315 | *
|
---|
| 316 | * Be warned, if the string is UTF-8
|
---|
| 317 | * encoded, this will not take multi-byte
|
---|
| 318 | * encodings into account. This returns
|
---|
| 319 | * the number of bytes, not the number
|
---|
| 320 | * of UTF characters.
|
---|
| 321 | */
|
---|
| 322 |
|
---|
| 323 | inline size_type size()
|
---|
| 324 | const
|
---|
| 325 | {
|
---|
| 326 | return ((_pBuf) ? (_pBuf->_str.ulLength) : 0);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /*
|
---|
| 330 | *@@ length:
|
---|
| 331 | * STL string compatibility function,
|
---|
| 332 | * same as BSStringBase::size().
|
---|
| 333 | */
|
---|
| 334 |
|
---|
| 335 | inline size_type length()
|
---|
| 336 | const
|
---|
| 337 | {
|
---|
| 338 | return (size());
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /*
|
---|
| 342 | *@@ capacity:
|
---|
| 343 | * returns the amount of memory presently
|
---|
| 344 | * allocated for the string. This is at
|
---|
| 345 | * least what size() or length() return,
|
---|
| 346 | * but can be more.
|
---|
| 347 | *
|
---|
| 348 | *@@added V0.9.6 (2000-10-31) [umoeller]
|
---|
| 349 | */
|
---|
| 350 |
|
---|
| 351 | inline size_type capacity()
|
---|
| 352 | const
|
---|
| 353 | {
|
---|
| 354 | return ((_pBuf) ? (_pBuf->_str.cbAllocated) : 0);
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | /*
|
---|
| 358 | *@@ operator():
|
---|
| 359 | * returns 1 (TRUE) if the string
|
---|
| 360 | * contains something, 0 (FALSE)
|
---|
| 361 | * otherwise.
|
---|
| 362 | *
|
---|
| 363 | * Note: as far as I know, this operator
|
---|
| 364 | * is NOT defined with the C++ string class.
|
---|
| 365 | */
|
---|
| 366 |
|
---|
| 367 | inline int operator()()
|
---|
| 368 | const
|
---|
| 369 | {
|
---|
| 370 | return (size() != 0);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /*
|
---|
| 374 | *@@ empty:
|
---|
| 375 | * returns 1 (TRUE) if the string
|
---|
| 376 | * contains nothing, 0 (FALSE) otherwise.
|
---|
| 377 | */
|
---|
| 378 |
|
---|
| 379 | inline int empty()
|
---|
| 380 | const
|
---|
| 381 | {
|
---|
| 382 | return (size() == 0);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /********************************************
|
---|
| 386 | *
|
---|
| 387 | * assignment, modification
|
---|
| 388 | *
|
---|
| 389 | ********************************************/
|
---|
| 390 |
|
---|
| 391 | void reserve(size_type stExtra = 0);
|
---|
| 392 |
|
---|
| 393 | void _take_from(XSTRING &str);
|
---|
| 394 |
|
---|
| 395 | size_type _printf(const char *pszFormatString, ...);
|
---|
| 396 |
|
---|
| 397 | void _itoa10(int value, char cThousands);
|
---|
| 398 |
|
---|
| 399 | BSStringBase& erase(size_type ulPos = 0, size_type n = npos);
|
---|
| 400 |
|
---|
| 401 | /********************************************
|
---|
| 402 | *
|
---|
| 403 | * comparison
|
---|
| 404 | *
|
---|
| 405 | ********************************************/
|
---|
| 406 |
|
---|
| 407 | protected:
|
---|
| 408 |
|
---|
| 409 | int compare(const BSStringBase &s) const;
|
---|
| 410 |
|
---|
| 411 | int compare(const char *psz) const;
|
---|
| 412 |
|
---|
| 413 | /********************************************
|
---|
| 414 | *
|
---|
| 415 | * find, replace
|
---|
| 416 | *
|
---|
| 417 | ********************************************/
|
---|
| 418 |
|
---|
| 419 | size_type _find_replace(BSStringBase &strFind,
|
---|
| 420 | const BSStringBase &strReplaceWith,
|
---|
| 421 | size_type *pulPos);
|
---|
| 422 |
|
---|
| 423 | size_type _find_replace(const char *pszFind,
|
---|
| 424 | const BSStringBase &strReplaceWith,
|
---|
| 425 | size_type *pulPos);
|
---|
| 426 |
|
---|
| 427 | public:
|
---|
| 428 |
|
---|
| 429 | size_type _find_replace(char cFind,
|
---|
| 430 | char cReplace,
|
---|
| 431 | size_type *pulPos);
|
---|
| 432 |
|
---|
| 433 | /********************************************
|
---|
| 434 | *
|
---|
| 435 | * misc
|
---|
| 436 | *
|
---|
| 437 | ********************************************/
|
---|
| 438 |
|
---|
| 439 | /*
|
---|
| 440 | *@@ swap:
|
---|
| 441 | * swaps the contents of this instance
|
---|
| 442 | * with that of another BSStringBase.
|
---|
| 443 | *
|
---|
| 444 | * This is very fast because only the
|
---|
| 445 | * pointers are exchanged.
|
---|
| 446 | *
|
---|
| 447 | *@@added V0.9.6 (2000-10-31) [umoeller]
|
---|
| 448 | */
|
---|
| 449 |
|
---|
| 450 | void swap(BSStringBase &str)
|
---|
| 451 | {
|
---|
| 452 | BSStringBuf *pBuf = str._pBuf;
|
---|
| 453 | str._pBuf = _pBuf;
|
---|
| 454 | _pBuf = pBuf;
|
---|
| 455 | }
|
---|
| 456 | };
|
---|
| 457 |
|
---|
| 458 | /*
|
---|
| 459 | *@@ BSString:
|
---|
| 460 | * string class vaguely modelled after the C++ ANSI
|
---|
| 461 | * string class which IBM VAC 3.08 has no support for.
|
---|
| 462 | *
|
---|
| 463 | * This implements the "BSString" class and typedefs "string"
|
---|
| 464 | * to "BSString". As a result, you can use either "BSString"
|
---|
| 465 | * or "string".
|
---|
| 466 | *
|
---|
| 467 | * <B>Introduction</B>
|
---|
| 468 | *
|
---|
| 469 | * If you're not familiar with the C++ "string" class, here's
|
---|
| 470 | * a short introduction.
|
---|
| 471 | *
|
---|
| 472 | * Basically, the "string" class allows you to use strings
|
---|
| 473 | * as a basic type, like int, char, long, and so on, without
|
---|
| 474 | * having to worry about memory management.
|
---|
| 475 | *
|
---|
| 476 | * Here is an example:
|
---|
| 477 | +
|
---|
| 478 | + string str = "Hello."
|
---|
| 479 | +
|
---|
| 480 | + if (str == "Hello")
|
---|
| 481 | + {
|
---|
| 482 | + string str = "We should say ";
|
---|
| 483 | + string str2 = str + "Hello now"
|
---|
| 484 | * str2 += '.';
|
---|
| 485 | +
|
---|
| 486 | + printf("%s", str2.c_str());
|
---|
| 487 | + }
|
---|
| 488 | *
|
---|
| 489 | * This shows some of the most convenient features of the
|
---|
| 490 | * string class:
|
---|
| 491 | *
|
---|
| 492 | * -- You can just create an instance of it on the stack.
|
---|
| 493 | * The destructor will automatically free the memory
|
---|
| 494 | * that has been allocated for the string -- no more
|
---|
| 495 | * memory leaks as with the C functions.
|
---|
| 496 | *
|
---|
| 497 | * -- You can compare strings using ==, !=, <, >. (This
|
---|
| 498 | * calls strcmp internally.)
|
---|
| 499 | *
|
---|
| 500 | * -- You can concatenate strings using the "+" operator.
|
---|
| 501 | *
|
---|
| 502 | * -- If you need a C-type string (e.g. for OS/2 API
|
---|
| 503 | * functions), use the BSString::c_str() method,
|
---|
| 504 | * which returns a "const char*" pointer to the string
|
---|
| 505 | * in the class.
|
---|
| 506 | *
|
---|
| 507 | * In addition, the string class has methods for more
|
---|
| 508 | * complicated things, such BSString::find and
|
---|
| 509 | * BSString::replace.
|
---|
| 510 | *
|
---|
| 511 | * Finally, this class is NOT thread-safe. If methods are
|
---|
| 512 | * operating on the same instance on several threads at
|
---|
| 513 | * the same time, this will DEFINITELY crash.
|
---|
| 514 | *
|
---|
| 515 | * <B>C++ ANSI "string" class compatibility</B>
|
---|
| 516 | *
|
---|
| 517 | * Unless marked otherwise, all methods and operators are
|
---|
| 518 | * compatible with the standard "string" class.
|
---|
| 519 | *
|
---|
| 520 | * However, we only support single-byte (8-bit) character
|
---|
| 521 | * sets at this point, and only a subset of the "string"
|
---|
| 522 | * methods and operators. But since WarpIN uses UTF-8
|
---|
| 523 | * for encoding things, this is not really a problem.
|
---|
| 524 | *
|
---|
| 525 | * Non-standard functions are marked with a leading
|
---|
| 526 | * underscore (e.g. BSString::_printf).
|
---|
| 527 | *
|
---|
| 528 | * <B>Changes</B>
|
---|
| 529 | *
|
---|
| 530 | * With V0.9.6, I have made improvements on memory management.
|
---|
| 531 | * Memory no longer gets reallocated with every change, but
|
---|
| 532 | * only if necessary.
|
---|
| 533 | *
|
---|
| 534 | * With V0.9.18, I finally implemented buffer sharing as
|
---|
| 535 | * with the C++ string class. This allows several BSString
|
---|
| 536 | * instances to share the same memory buffer. See BSStringBuf.
|
---|
| 537 | *
|
---|
| 538 | * In addition, V0.9.18 added Unicode support. See BSUString
|
---|
| 539 | * and BSUniCodec for more information.
|
---|
| 540 | *
|
---|
| 541 | * All the V0.9.18 changes are backward-compatible. So even
|
---|
| 542 | * though the implementation has changed significantly,
|
---|
| 543 | * all old code should still compile.
|
---|
| 544 | */
|
---|
| 545 |
|
---|
| 546 | class BSString : public BSStringBase
|
---|
| 547 | {
|
---|
| 548 | public:
|
---|
| 549 | DECLARE_CLASS(BSString);
|
---|
| 550 |
|
---|
| 551 | /*
|
---|
| 552 | *@@ BSString:
|
---|
| 553 | * default constructor to create an empty string.
|
---|
| 554 | */
|
---|
| 555 |
|
---|
| 556 | BSString()
|
---|
| 557 | : BSStringBase(tBSString)
|
---|
| 558 | {
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | /*
|
---|
| 562 | *@@ BSString:
|
---|
| 563 | * default copy constructor;
|
---|
| 564 | * required for exception handling!
|
---|
| 565 | */
|
---|
| 566 |
|
---|
| 567 | BSString(const BSString &s)
|
---|
| 568 | : BSStringBase(s, tBSString)
|
---|
| 569 | {
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | BSString(const BSString &s,
|
---|
| 573 | size_type ulPos,
|
---|
| 574 | size_type n = npos);
|
---|
| 575 | BSString(const char *psz);
|
---|
| 576 | BSString(const char *p1, const char *p2);
|
---|
| 577 | BSString(char c);
|
---|
| 578 |
|
---|
| 579 | BSString(BSUniCodec *pCodec,
|
---|
| 580 | const BSUString &ustr);
|
---|
| 581 |
|
---|
| 582 | /********************************************
|
---|
| 583 | *
|
---|
| 584 | * property queries
|
---|
| 585 | *
|
---|
| 586 | ********************************************/
|
---|
| 587 |
|
---|
| 588 | char operator[](const size_type ul) const;
|
---|
| 589 |
|
---|
| 590 | /*
|
---|
| 591 | *@@ c_str:
|
---|
| 592 | * returns the string member as a
|
---|
| 593 | * classic const C string. Note that
|
---|
| 594 | * this _never_ returns NULL. If the
|
---|
| 595 | * member string is empty, this
|
---|
| 596 | * returns a pointer to a null byte.
|
---|
| 597 | */
|
---|
| 598 |
|
---|
| 599 | inline const char* c_str()
|
---|
| 600 | const
|
---|
| 601 | {
|
---|
| 602 | return ((_pBuf && _pBuf->_str.psz) ? _pBuf->_str.psz : "");
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | /********************************************
|
---|
| 606 | *
|
---|
| 607 | * assignment, modification
|
---|
| 608 | *
|
---|
| 609 | ********************************************/
|
---|
| 610 |
|
---|
| 611 | BSString& assign(const BSString &s);
|
---|
| 612 | BSString& assign(const BSString &s,
|
---|
| 613 | size_type ulPos,
|
---|
| 614 | size_type n = npos);
|
---|
| 615 | BSString& assign(const char *psz);
|
---|
| 616 | BSString& assign(const char *p1, const char *p2);
|
---|
| 617 | BSString& assign(char c);
|
---|
| 618 |
|
---|
| 619 | BSString& assignUtf8(BSUniCodec *pCodec,
|
---|
| 620 | const BSUString &ustr);
|
---|
| 621 |
|
---|
| 622 | /*
|
---|
| 623 | *@@ operator=:
|
---|
| 624 | *
|
---|
| 625 | */
|
---|
| 626 |
|
---|
| 627 | inline BSString& operator=(const BSString &s)
|
---|
| 628 | {
|
---|
| 629 | return (assign(s));
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | /*
|
---|
| 633 | *@@ operator=:
|
---|
| 634 | *
|
---|
| 635 | */
|
---|
| 636 |
|
---|
| 637 | inline BSString& operator=(const char *psz)
|
---|
| 638 | {
|
---|
| 639 | return (assign(psz));
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | /*
|
---|
| 643 | *@@ operator=:
|
---|
| 644 | *
|
---|
| 645 | */
|
---|
| 646 |
|
---|
| 647 | inline BSString& operator=(char c)
|
---|
| 648 | {
|
---|
| 649 | return (assign(c));
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | BSString& append(const BSString &s);
|
---|
| 653 | BSString& append(const char *psz);
|
---|
| 654 | BSString& append(char c);
|
---|
| 655 |
|
---|
| 656 | BSString& appendUtf8(BSUniCodec *pCodec, const BSUString &s);
|
---|
| 657 |
|
---|
| 658 | /*
|
---|
| 659 | *@@ operator+=:
|
---|
| 660 | * shortcut to append(BSStringBase &s).
|
---|
| 661 | *
|
---|
| 662 | *@@changed V0.9.2 (2000-03-29) [umoeller]: added return *this
|
---|
| 663 | */
|
---|
| 664 |
|
---|
| 665 | inline BSString& operator+=(const BSString &s) // in: string to append
|
---|
| 666 | {
|
---|
| 667 | return (append(s));
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | /*
|
---|
| 671 | *@@ operator+=:
|
---|
| 672 | * shortcut to append(const char *psz).
|
---|
| 673 | *
|
---|
| 674 | *@@changed V0.9.2 (2000-03-29) [umoeller]: added return *this
|
---|
| 675 | */
|
---|
| 676 |
|
---|
| 677 | inline BSString& operator+=(const char *psz) // in: string to append
|
---|
| 678 | {
|
---|
| 679 | return (append(psz));
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | /*
|
---|
| 683 | *@@ operator+=:
|
---|
| 684 | * shortcut to append(char c).
|
---|
| 685 | *
|
---|
| 686 | *@@changed V0.9.2 (2000-03-29) [umoeller]: added return *this
|
---|
| 687 | */
|
---|
| 688 |
|
---|
| 689 | inline BSString& operator+=(char c) // in: character to append
|
---|
| 690 | {
|
---|
| 691 | return (append(c));
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | /********************************************
|
---|
| 695 | *
|
---|
| 696 | * comparison
|
---|
| 697 | *
|
---|
| 698 | ********************************************/
|
---|
| 699 |
|
---|
| 700 | /*
|
---|
| 701 | *@@ compare:
|
---|
| 702 | *
|
---|
| 703 | */
|
---|
| 704 |
|
---|
| 705 | inline int compare(const BSString &s) const
|
---|
| 706 | {
|
---|
| 707 | return BSStringBase::compare(s);
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | /*
|
---|
| 711 | *@@ compare:
|
---|
| 712 | *
|
---|
| 713 | */
|
---|
| 714 |
|
---|
| 715 | inline int compare(const char *psz) const
|
---|
| 716 | {
|
---|
| 717 | return BSStringBase::compare(psz);
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | int compare(size_type ulPos, size_type n, const BSString &s) const;
|
---|
| 721 |
|
---|
| 722 | /********************************************
|
---|
| 723 | *
|
---|
| 724 | * find, replace
|
---|
| 725 | *
|
---|
| 726 | ********************************************/
|
---|
| 727 |
|
---|
| 728 | size_type find(const BSString &strFind, size_type ulPos = 0) const;
|
---|
| 729 | size_type find(const char *pszFind, size_type ulPos = 0) const;
|
---|
| 730 | size_type find(char c, size_type ulPos = 0) const;
|
---|
| 731 |
|
---|
| 732 | size_type rfind(char c, size_type ulPos = npos) const;
|
---|
| 733 |
|
---|
[421] | 734 | size_type find_first_of(char c, size_type ulPos = 0) const;
|
---|
[250] | 735 | size_type find_first_of(const char *achChars, size_type ulPos = 0) const;
|
---|
| 736 |
|
---|
| 737 | size_type find_first_not_of(char c, size_type ulPos = 0) const;
|
---|
| 738 | size_type find_first_not_of(const char *achChars, size_type ulPos = 0) const;
|
---|
| 739 |
|
---|
| 740 | BSString& replace(size_type ulPosThis,
|
---|
| 741 | size_type nThis,
|
---|
| 742 | const BSString &strReplace);
|
---|
| 743 |
|
---|
| 744 | /* BSString& replace(size_type ulPosThis,
|
---|
| 745 | size_type nThis,
|
---|
| 746 | const BSString &strReplace,
|
---|
| 747 | size_type ulPosReplace = 0,
|
---|
| 748 | size_type nReplace = npos);
|
---|
| 749 |
|
---|
| 750 | BSString& replace(size_type ulPosThis,
|
---|
| 751 | size_type nThis,
|
---|
| 752 | const char *pszReplace,
|
---|
| 753 | size_type nReplace);
|
---|
| 754 |
|
---|
| 755 | BSString& replace(size_type ulPosThis,
|
---|
| 756 | size_type nThis,
|
---|
| 757 | const char *pszReplace); */
|
---|
| 758 |
|
---|
| 759 | size_type _find_word(BSString &strFind,
|
---|
| 760 | size_type ulPos = 0,
|
---|
| 761 | const char *pcszBeginChars = "\x0d\x0a ()/\\-,.;*",
|
---|
| 762 | const char *pcszEndChars = "\x0d\x0a ()/\\-,.:;*&'")
|
---|
| 763 | const;
|
---|
| 764 |
|
---|
| 765 | size_type _find_word(const char *pszFind,
|
---|
| 766 | size_type ulPos = 0,
|
---|
| 767 | const char *pcszBeginChars = "\x0d\x0a ()/\\-,.;*",
|
---|
| 768 | const char *pcszEndChars = "\x0d\x0a ()/\\-,.:;*&'")
|
---|
| 769 | const;
|
---|
| 770 |
|
---|
| 771 | /*
|
---|
| 772 | *@@ _find_replace:
|
---|
| 773 | *
|
---|
| 774 | */
|
---|
| 775 |
|
---|
| 776 | inline size_type _find_replace(BSString &strFind,
|
---|
| 777 | const BSString &strReplaceWith,
|
---|
| 778 | size_type *pulPos)
|
---|
| 779 | {
|
---|
| 780 | return BSStringBase::_find_replace(strFind, strReplaceWith, pulPos);
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | /*
|
---|
| 784 | *@@ _find_replace:
|
---|
| 785 | *
|
---|
| 786 | */
|
---|
| 787 |
|
---|
| 788 | inline size_type _find_replace(const char *pszFind,
|
---|
| 789 | const BSString &strReplaceWith,
|
---|
| 790 | size_type *pulPos)
|
---|
| 791 | {
|
---|
| 792 | return BSStringBase::_find_replace(pszFind, strReplaceWith, pulPos);
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | /*
|
---|
| 796 | *@@ _find_replace:
|
---|
| 797 | *
|
---|
| 798 | */
|
---|
| 799 |
|
---|
| 800 | inline size_type _find_replace(char cFind,
|
---|
| 801 | char cReplace,
|
---|
| 802 | size_type *pulPos)
|
---|
| 803 | {
|
---|
| 804 | return BSStringBase::_find_replace(cFind, cReplace, pulPos);
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | BSString substr(size_type ulPos = 0,
|
---|
| 808 | size_type n = npos)
|
---|
| 809 | const;
|
---|
| 810 |
|
---|
| 811 | int _extract_word(unsigned long ulIndex,
|
---|
| 812 | BSString &strTarget,
|
---|
| 813 | size_type ulPos = 0,
|
---|
| 814 | const char *pcszBeginChars = "\x0d\x0a ()/\\-,.;*",
|
---|
| 815 | const char *pcszEndChars = "\x0d\x0a ()/\\-,.:;*&'")
|
---|
| 816 | const;
|
---|
| 817 |
|
---|
| 818 | void _format();
|
---|
| 819 | };
|
---|
| 820 |
|
---|
| 821 | int operator==(const BSString &s1, const BSString &s2);
|
---|
| 822 |
|
---|
| 823 | int operator==(const char *psz1, const BSString &s2);
|
---|
| 824 |
|
---|
| 825 | int operator==(const BSString &s1, const char *psz2);
|
---|
| 826 |
|
---|
| 827 | int operator!=(const BSString &s1, const BSString &s2);
|
---|
| 828 |
|
---|
| 829 | int operator!=(const char *psz1, const BSString &s2);
|
---|
| 830 |
|
---|
| 831 | int operator!=(const BSString &s1, const char *psz2);
|
---|
| 832 |
|
---|
| 833 | int operator<(const BSString &s1, const BSString &s2);
|
---|
| 834 |
|
---|
| 835 | BSString operator+(const BSString &s1, const BSString &s2);
|
---|
| 836 |
|
---|
| 837 | BSString operator+(const char *psz1, const BSString &s2);
|
---|
| 838 |
|
---|
| 839 | BSString operator+(const BSString &s1, const char *psz2);
|
---|
| 840 |
|
---|
| 841 | /*
|
---|
| 842 | *@@ string:
|
---|
| 843 | * typedef for using BSStrings like the
|
---|
| 844 | * standard string class.
|
---|
| 845 | */
|
---|
| 846 |
|
---|
| 847 | typedef BSString string;
|
---|
| 848 |
|
---|
| 849 | /*
|
---|
| 850 | *@@ BSUString:
|
---|
| 851 | * unmodified BSString subclass for marking
|
---|
| 852 | * strings that are not plain ASCII, but UTF-8
|
---|
| 853 | * encoded. This is used in some declarations
|
---|
| 854 | * to make sure that interfaces can react
|
---|
| 855 | * properly.
|
---|
| 856 | *
|
---|
| 857 | * Most notably, this has restrictions on the
|
---|
| 858 | * assigment and conversion methods. In order
|
---|
| 859 | * to not accidentally assign codepage strings
|
---|
| 860 | * to a BSUString, regular assignments from
|
---|
| 861 | * const char * to BSUStrings are not allowed.
|
---|
| 862 | * Use BSUstring::assignUtf8 for this.
|
---|
| 863 | *
|
---|
| 864 | * In detail, the following things do not work:
|
---|
| 865 | *
|
---|
| 866 | * -- extracting substrings, because with UTF-8
|
---|
| 867 | * you can never be sure if an offset points
|
---|
| 868 | * into the middle of a multi-byte character;
|
---|
| 869 | *
|
---|
| 870 | * -- c_str() does not exist to avoid accidentally
|
---|
| 871 | * passing the member buffer to legacy APIs.
|
---|
| 872 | * Use BSUString::GetBuffer() instead.
|
---|
| 873 | *
|
---|
| 874 | * -- "=" assignments, "==" comparisons and "+"
|
---|
| 875 | * concatenation between BSString and BSUString.
|
---|
| 876 | * See BSUniCodec on how to convert BSString's
|
---|
| 877 | * to BSUString's and back.
|
---|
| 878 | *
|
---|
| 879 | * However, "==" between two BSUString's works.
|
---|
| 880 | *
|
---|
| 881 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
| 882 | */
|
---|
| 883 |
|
---|
| 884 | class BSUString : public BSStringBase
|
---|
| 885 | {
|
---|
| 886 | public:
|
---|
| 887 | DECLARE_CLASS(BSUString);
|
---|
| 888 |
|
---|
| 889 | /*
|
---|
| 890 | *@@ BSUString:
|
---|
| 891 | * default constructor to create an empty string.
|
---|
| 892 | */
|
---|
| 893 |
|
---|
| 894 | BSUString()
|
---|
| 895 | : BSStringBase(tBSUString)
|
---|
| 896 | {
|
---|
| 897 | }
|
---|
| 898 |
|
---|
| 899 | /*
|
---|
| 900 | *@@ BSUString:
|
---|
| 901 | * default copy constructor;
|
---|
| 902 | * required for exception handling!
|
---|
| 903 | */
|
---|
| 904 |
|
---|
| 905 | BSUString(const BSUString &s)
|
---|
| 906 | : BSStringBase(s, tBSUString)
|
---|
| 907 | {
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | BSUString(BSUniCodec *pCodec,
|
---|
| 911 | const BSString &str);
|
---|
[449] | 912 | BSUString(BSUniCodec *pCodec,
|
---|
| 913 | const char *psz);
|
---|
[250] | 914 |
|
---|
| 915 | /********************************************
|
---|
| 916 | *
|
---|
| 917 | * property queries
|
---|
| 918 | *
|
---|
| 919 | ********************************************/
|
---|
| 920 |
|
---|
| 921 | char operator[](const size_type ul) const;
|
---|
| 922 |
|
---|
| 923 | /*
|
---|
| 924 | *@@ GetBuffer:
|
---|
| 925 | * like BSString::c_str(), but with
|
---|
| 926 | * a different name to make sure
|
---|
| 927 | * ustrings don't get passed to
|
---|
| 928 | * code which cannot handle that.
|
---|
| 929 | */
|
---|
| 930 |
|
---|
| 931 | inline const char* GetBuffer()
|
---|
| 932 | const
|
---|
| 933 | {
|
---|
| 934 | return ((_pBuf && _pBuf->_str.psz) ? _pBuf->_str.psz : "");
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | /********************************************
|
---|
| 938 | *
|
---|
| 939 | * assignment, modification
|
---|
| 940 | *
|
---|
| 941 | ********************************************/
|
---|
| 942 |
|
---|
| 943 | BSUString& assign(const BSUString &s);
|
---|
| 944 | BSUString& assignUtf8(const char *psz);
|
---|
| 945 | BSUString& assignUtf8(const char *p1, const char *p2);
|
---|
| 946 | BSUString& assignCP(BSUniCodec *pCodec,
|
---|
| 947 | const BSString &str);
|
---|
| 948 | BSUString& assignCP(BSUniCodec *pCodec,
|
---|
| 949 | const char *psz);
|
---|
| 950 |
|
---|
| 951 | /*
|
---|
| 952 | *@@ operator=:
|
---|
| 953 | *
|
---|
| 954 | */
|
---|
| 955 |
|
---|
| 956 | inline BSUString& operator=(const BSUString &s)
|
---|
| 957 | {
|
---|
| 958 | return (assign(s));
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 | BSUString& append(const BSUString &s);
|
---|
| 962 | BSUString& appendUtf8(const char *psz);
|
---|
| 963 | BSUString& appendCP(BSUniCodec *pCodec,
|
---|
| 964 | const BSString &str);
|
---|
| 965 |
|
---|
| 966 | /*
|
---|
| 967 | *@@ operator+=:
|
---|
| 968 | * shortcut to append(BSUString &s).
|
---|
| 969 | *
|
---|
| 970 | */
|
---|
| 971 |
|
---|
| 972 | inline BSUString& operator+=(const BSUString &s) // in: string to append
|
---|
| 973 | {
|
---|
| 974 | return (append(s));
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | /********************************************
|
---|
| 978 | *
|
---|
| 979 | * comparison
|
---|
| 980 | *
|
---|
| 981 | ********************************************/
|
---|
| 982 |
|
---|
| 983 | inline int compare(const BSUString &s) const
|
---|
| 984 | {
|
---|
| 985 | return BSStringBase::compare(s);
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 | inline int compareUtf8(const char *psz) const
|
---|
| 989 | {
|
---|
| 990 | return BSStringBase::compare(psz);
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | int compareI(const BSUString &s) const;
|
---|
| 994 |
|
---|
| 995 | int compareUtf8I(const char *psz) const;
|
---|
| 996 |
|
---|
| 997 | /********************************************
|
---|
| 998 | *
|
---|
| 999 | * find, replace
|
---|
| 1000 | *
|
---|
| 1001 | ********************************************/
|
---|
| 1002 |
|
---|
| 1003 | /*
|
---|
| 1004 | *@@ _find_replace:
|
---|
| 1005 | *
|
---|
| 1006 | */
|
---|
| 1007 |
|
---|
| 1008 | inline size_type _find_replace(BSUString &strFind,
|
---|
| 1009 | const BSUString &strReplaceWith,
|
---|
| 1010 | size_type *pulPos)
|
---|
| 1011 | {
|
---|
| 1012 | return BSStringBase::_find_replace(strFind, strReplaceWith, pulPos);
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | /*
|
---|
| 1016 | *@@ _find_replace:
|
---|
| 1017 | *
|
---|
| 1018 | */
|
---|
| 1019 |
|
---|
| 1020 | inline size_type _find_replace(const char *pszFind,
|
---|
| 1021 | const BSUString &strReplaceWith,
|
---|
| 1022 | size_type *pulPos)
|
---|
| 1023 | {
|
---|
| 1024 | return BSStringBase::_find_replace(pszFind, strReplaceWith, pulPos);
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | /*
|
---|
| 1028 | *@@ _find_replace:
|
---|
| 1029 | *
|
---|
| 1030 | */
|
---|
| 1031 |
|
---|
| 1032 | inline size_type _find_replace(char cFind,
|
---|
| 1033 | char cReplace,
|
---|
| 1034 | size_type *pulPos)
|
---|
| 1035 | {
|
---|
| 1036 | return BSStringBase::_find_replace(cFind, cReplace, pulPos);
|
---|
| 1037 | }
|
---|
| 1038 | };
|
---|
| 1039 |
|
---|
| 1040 | int operator==(const BSUString &s1, const BSUString &s2);
|
---|
| 1041 |
|
---|
| 1042 | int operator!=(const BSUString &s1, const BSUString &s2);
|
---|
| 1043 |
|
---|
| 1044 | BSUString operator+(const BSUString &s1, const BSUString &s2);
|
---|
| 1045 |
|
---|
| 1046 | /*
|
---|
| 1047 | *@@ ustring:
|
---|
| 1048 | *
|
---|
| 1049 | *@@added V0.9.18 (2002-03-08) [umoeller]
|
---|
| 1050 | */
|
---|
| 1051 |
|
---|
| 1052 | typedef BSUString ustring;
|
---|
| 1053 |
|
---|
| 1054 | #endif
|
---|
| 1055 |
|
---|