| 1 | /* |
| 2 | * Class MString: a thread-safe version of QString |
| 3 | * |
| 4 | * Copyright (C) 2008 Jerome Brock <jpbrock@users.sourceforge.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | // Avoid deadlock: do not read/write from another MString from within |
| 23 | // this class while holding your own lock! |
| 24 | // |
| 25 | // Don't bypass the thread safety of this class, i.e. don't bypass the lock by |
| 26 | // reading another MString's private QString directly // Use the 'toString()' |
| 27 | // method to retrieve the string in a thread-safe manner |
| 28 | // |
| 29 | // According to the documentation |
| 30 | // (see 4.3/threads.html#threads-and-implicit-sharing) sharing QStrings |
| 31 | // between threads is safe in QT4 as related to QString's implicit sharing. |
| 32 | // In MString we are covering the thread safety for the QString objects, |
| 33 | // themselves. |
| 34 | |
| 35 | #include "mstring.h" |
| 36 | #include <QtCore/QStringList> |
| 37 | #include <QtCore/QVector> |
| 38 | |
| 39 | |
| 40 | MString & MString::append(const MString & str) |
| 41 | { |
| 42 | const QString other_qstring = str.toQString(); |
| 43 | |
| 44 | QWriteLocker locker(&m_lock); |
| 45 | |
| 46 | m_string.append(other_qstring); |
| 47 | return *this; |
| 48 | }; |
| 49 | |
| 50 | MString & MString::append(const QLatin1String & str) |
| 51 | { |
| 52 | QWriteLocker locker(&m_lock); |
| 53 | |
| 54 | m_string.append(str); |
| 55 | return *this; |
| 56 | }; |
| 57 | |
| 58 | MString & MString::append(const QByteArray & ba) |
| 59 | { |
| 60 | QWriteLocker locker(&m_lock); |
| 61 | |
| 62 | m_string.append(ba); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | MString & MString::append(const char * str) |
| 67 | { |
| 68 | QWriteLocker locker(&m_lock); |
| 69 | |
| 70 | m_string.append(str); |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | MString & MString::append(QChar ch) |
| 75 | { |
| 76 | QWriteLocker locker(&m_lock); |
| 77 | |
| 78 | m_string.append(ch); |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | MString MString::arg(const MString & a, int fieldWidth, const QChar & fillChar) const |
| 83 | { |
| 84 | QString a_string = a.toQString(); |
| 85 | |
| 86 | QReadLocker locker(&m_lock); |
| 87 | |
| 88 | return MString(m_string.arg(a_string, fieldWidth, fillChar)); |
| 89 | } |
| 90 | |
| 91 | MString MString::arg(const MString & a1, const MString & a2) const |
| 92 | { |
| 93 | QString a1_string = a1.toQString(); |
| 94 | QString a2_string = a2.toQString(); |
| 95 | |
| 96 | QReadLocker locker(&m_lock); |
| 97 | |
| 98 | return MString(m_string.arg(a1_string, a2_string)); |
| 99 | } |
| 100 | |
| 101 | MString MString::arg(const MString & a1, const MString & a2, |
| 102 | const MString & a3) const |
| 103 | { |
| 104 | QString a1_string = a1.toQString(); |
| 105 | QString a2_string = a2.toQString(); |
| 106 | QString a3_string = a3.toQString(); |
| 107 | |
| 108 | QReadLocker locker(&m_lock); |
| 109 | |
| 110 | return MString(m_string.arg(a1_string, a2_string, a3_string)); |
| 111 | } |
| 112 | |
| 113 | MString MString::arg(const MString & a1, const MString & a2, |
| 114 | const MString & a3, const MString & a4) const |
| 115 | { |
| 116 | QString a1_string = a1.toQString(); |
| 117 | QString a2_string = a2.toQString(); |
| 118 | QString a3_string = a3.toQString(); |
| 119 | QString a4_string = a4.toQString(); |
| 120 | |
| 121 | QReadLocker locker(&m_lock); |
| 122 | |
| 123 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string)); |
| 124 | } |
| 125 | |
| 126 | MString MString::arg(const MString & a1, const MString & a2, |
| 127 | const MString & a3, const MString & a4, |
| 128 | const MString & a5) const |
| 129 | { |
| 130 | QString a1_string = a1.toQString(); |
| 131 | QString a2_string = a2.toQString(); |
| 132 | QString a3_string = a3.toQString(); |
| 133 | QString a4_string = a4.toQString(); |
| 134 | QString a5_string = a5.toQString(); |
| 135 | |
| 136 | QReadLocker locker(&m_lock); |
| 137 | |
| 138 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string, |
| 139 | a5_string)); |
| 140 | } |
| 141 | |
| 142 | MString MString::arg(const MString & a1, const MString & a2, |
| 143 | const MString & a3, const MString & a4, |
| 144 | const MString & a5, const MString & a6) const |
| 145 | { |
| 146 | QString a1_string = a1.toQString(); |
| 147 | QString a2_string = a2.toQString(); |
| 148 | QString a3_string = a3.toQString(); |
| 149 | QString a4_string = a4.toQString(); |
| 150 | QString a5_string = a5.toQString(); |
| 151 | QString a6_string = a6.toQString(); |
| 152 | |
| 153 | QReadLocker locker(&m_lock); |
| 154 | |
| 155 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string, |
| 156 | a5_string, a6_string)); |
| 157 | } |
| 158 | |
| 159 | MString MString::arg(const MString & a1, const MString & a2, |
| 160 | const MString & a3, const MString & a4, |
| 161 | const MString & a5, const MString & a6, |
| 162 | const MString & a7) const |
| 163 | { |
| 164 | QString a1_string = a1.toQString(); |
| 165 | QString a2_string = a2.toQString(); |
| 166 | QString a3_string = a3.toQString(); |
| 167 | QString a4_string = a4.toQString(); |
| 168 | QString a5_string = a5.toQString(); |
| 169 | QString a6_string = a6.toQString(); |
| 170 | QString a7_string = a7.toQString(); |
| 171 | |
| 172 | QReadLocker locker(&m_lock); |
| 173 | |
| 174 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string, |
| 175 | a5_string, a6_string, a7_string)); |
| 176 | } |
| 177 | |
| 178 | MString MString::arg(const MString & a1, const MString & a2, |
| 179 | const MString & a3, const MString & a4, |
| 180 | const MString & a5, const MString & a6, |
| 181 | const MString & a7, const MString & a8) const |
| 182 | { |
| 183 | QString a1_string = a1.toQString(); |
| 184 | QString a2_string = a2.toQString(); |
| 185 | QString a3_string = a3.toQString(); |
| 186 | QString a4_string = a4.toQString(); |
| 187 | QString a5_string = a5.toQString(); |
| 188 | QString a6_string = a6.toQString(); |
| 189 | QString a7_string = a7.toQString(); |
| 190 | QString a8_string = a8.toQString(); |
| 191 | |
| 192 | QReadLocker locker(&m_lock); |
| 193 | |
| 194 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string, |
| 195 | a5_string, a6_string, a7_string, a8_string)); |
| 196 | } |
| 197 | |
| 198 | MString MString::arg(const MString & a1, const MString & a2, |
| 199 | const MString & a3, const MString & a4, |
| 200 | const MString & a5, const MString & a6, |
| 201 | const MString & a7, const MString & a8, |
| 202 | const MString & a9) const |
| 203 | { |
| 204 | QString a1_string = a1.toQString(); |
| 205 | QString a2_string = a2.toQString(); |
| 206 | QString a3_string = a3.toQString(); |
| 207 | QString a4_string = a4.toQString(); |
| 208 | QString a5_string = a5.toQString(); |
| 209 | QString a6_string = a6.toQString(); |
| 210 | QString a7_string = a7.toQString(); |
| 211 | QString a8_string = a8.toQString(); |
| 212 | QString a9_string = a9.toQString(); |
| 213 | |
| 214 | QReadLocker locker(&m_lock); |
| 215 | |
| 216 | return MString(m_string.arg(a1_string, a2_string, a3_string, a4_string, |
| 217 | a5_string, a6_string, a7_string, a8_string, a9_string)); |
| 218 | } |
| 219 | |
| 220 | const QChar MString::at(int position) const |
| 221 | { |
| 222 | QReadLocker locker(&m_lock); |
| 223 | |
| 224 | return m_string.at(position); |
| 225 | } |
| 226 | |
| 227 | int MString::capacity() const |
| 228 | { |
| 229 | QReadLocker locker(&m_lock); |
| 230 | |
| 231 | return m_string.capacity(); |
| 232 | } |
| 233 | |
| 234 | void MString::chop(int n) |
| 235 | { |
| 236 | QWriteLocker locker(&m_lock); |
| 237 | |
| 238 | m_string.chop(n); |
| 239 | } |
| 240 | |
| 241 | void MString::clear() |
| 242 | { |
| 243 | QWriteLocker locker(&m_lock); |
| 244 | |
| 245 | m_string.clear(); |
| 246 | } |
| 247 | |
| 248 | int MString::compare(const MString & other) const |
| 249 | { |
| 250 | const QString other_qstring = other.toQString(); |
| 251 | |
| 252 | QReadLocker locker(&m_lock); |
| 253 | |
| 254 | return m_string.compare(other_qstring); |
| 255 | } |
| 256 | |
| 257 | int MString::compare(const MString & other, Qt::CaseSensitivity cs) const |
| 258 | { |
| 259 | const QString other_qstring = other.toQString(); |
| 260 | |
| 261 | QReadLocker locker(&m_lock); |
| 262 | |
| 263 | return m_string.compare(other_qstring, cs); |
| 264 | } |
| 265 | |
| 266 | int MString::compare(const QLatin1String & other, Qt::CaseSensitivity cs) const |
| 267 | { |
| 268 | QReadLocker locker(&m_lock); |
| 269 | |
| 270 | return m_string.compare(other, cs); |
| 271 | } |
| 272 | |
| 273 | const QChar * MString::constData() const |
| 274 | { |
| 275 | QReadLocker locker(&m_lock); |
| 276 | |
| 277 | return m_string.constData(); |
| 278 | } |
| 279 | |
| 280 | bool MString::contains(const MString & str, Qt::CaseSensitivity cs) const |
| 281 | { |
| 282 | const QString other_qstring = str.toQString(); |
| 283 | |
| 284 | QReadLocker locker(&m_lock); |
| 285 | |
| 286 | return m_string.contains(other_qstring, cs); |
| 287 | } |
| 288 | |
| 289 | bool MString::contains(QChar ch, Qt::CaseSensitivity cs) const |
| 290 | { |
| 291 | QReadLocker locker(&m_lock); |
| 292 | |
| 293 | return m_string.contains(ch, cs); |
| 294 | } |
| 295 | |
| 296 | bool MString::contains(const QRegExp & rx) const |
| 297 | { |
| 298 | QReadLocker locker(&m_lock); |
| 299 | |
| 300 | return m_string.contains(rx); |
| 301 | } |
| 302 | |
| 303 | int MString::count(const MString & str, Qt::CaseSensitivity cs) const |
| 304 | { |
| 305 | const QString other_qstring = str.toQString(); |
| 306 | |
| 307 | QReadLocker locker(&m_lock); |
| 308 | |
| 309 | return m_string.count(other_qstring, cs); |
| 310 | } |
| 311 | |
| 312 | int MString::count(QChar ch, Qt::CaseSensitivity cs) const |
| 313 | { |
| 314 | QReadLocker locker(&m_lock); |
| 315 | |
| 316 | return m_string.count(ch, cs); |
| 317 | } |
| 318 | |
| 319 | int MString::count(const QRegExp & rx) const |
| 320 | { |
| 321 | QReadLocker locker(&m_lock); |
| 322 | |
| 323 | return m_string.count(rx); |
| 324 | } |
| 325 | |
| 326 | int MString::count() const |
| 327 | { |
| 328 | QReadLocker locker(&m_lock); |
| 329 | |
| 330 | return m_string.count(); |
| 331 | } |
| 332 | |
| 333 | bool MString::endsWith(const MString & s, Qt::CaseSensitivity cs) const |
| 334 | { |
| 335 | const QString other_qstring = s.toQString(); |
| 336 | |
| 337 | QReadLocker locker(&m_lock); |
| 338 | |
| 339 | return m_string.endsWith(other_qstring, cs); |
| 340 | } |
| 341 | |
| 342 | bool MString::endsWith(const QLatin1String & s, Qt::CaseSensitivity cs) const |
| 343 | { |
| 344 | QReadLocker locker(&m_lock); |
| 345 | |
| 346 | return m_string.endsWith(s, cs); |
| 347 | } |
| 348 | |
| 349 | bool MString::endsWith(const QChar & c, Qt::CaseSensitivity cs) const |
| 350 | { |
| 351 | QReadLocker locker(&m_lock); |
| 352 | |
| 353 | return m_string.endsWith(c, cs); |
| 354 | } |
| 355 | |
| 356 | MString & MString::fill(QChar ch, int size) |
| 357 | { |
| 358 | QWriteLocker locker(&m_lock); |
| 359 | |
| 360 | m_string.fill(ch, size); |
| 361 | return *this; |
| 362 | } |
| 363 | |
| 364 | int MString::indexOf(const MString & str, int from, Qt::CaseSensitivity cs) const |
| 365 | { |
| 366 | const QString other_qstring = str.toQString(); |
| 367 | |
| 368 | QReadLocker locker(&m_lock); |
| 369 | |
| 370 | return m_string.indexOf(other_qstring, from, cs); |
| 371 | } |
| 372 | |
| 373 | int MString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 374 | { |
| 375 | QReadLocker locker(&m_lock); |
| 376 | |
| 377 | return m_string.indexOf(ch, from, cs); |
| 378 | } |
| 379 | |
| 380 | int MString::indexOf(const QRegExp & rx, int from) const |
| 381 | { |
| 382 | QReadLocker locker(&m_lock); |
| 383 | |
| 384 | return m_string.indexOf(rx, from); |
| 385 | } |
| 386 | |
| 387 | MString & MString::insert(int position, const MString & str) |
| 388 | { |
| 389 | const QString other_qstring = str.toQString(); |
| 390 | |
| 391 | QWriteLocker locker(&m_lock); |
| 392 | |
| 393 | m_string.insert(position, other_qstring); |
| 394 | return *this; |
| 395 | } |
| 396 | |
| 397 | MString & MString::insert(int position, const QLatin1String & str) |
| 398 | { |
| 399 | QWriteLocker locker(&m_lock); |
| 400 | |
| 401 | m_string.insert(position, str); |
| 402 | return *this; |
| 403 | } |
| 404 | |
| 405 | MString & MString::insert(int position, const QChar * unicode, int size) |
| 406 | { |
| 407 | QWriteLocker locker(&m_lock); |
| 408 | |
| 409 | m_string.insert(position, unicode, size); |
| 410 | return *this; |
| 411 | } |
| 412 | |
| 413 | MString & MString::insert(int position, QChar ch) |
| 414 | { |
| 415 | QWriteLocker locker(&m_lock); |
| 416 | |
| 417 | m_string.insert(position, ch); |
| 418 | return *this; |
| 419 | } |
| 420 | |
| 421 | bool MString::isEmpty() const |
| 422 | { |
| 423 | QReadLocker locker(&m_lock); |
| 424 | |
| 425 | return m_string.isEmpty(); |
| 426 | } |
| 427 | |
| 428 | bool MString::isNull() const |
| 429 | { |
| 430 | QReadLocker locker(&m_lock); |
| 431 | |
| 432 | return m_string.isNull(); |
| 433 | } |
| 434 | |
| 435 | int MString::lastIndexOf(const MString & str, int from, Qt::CaseSensitivity cs) const |
| 436 | { |
| 437 | const QString other_qstring = str.toQString(); |
| 438 | |
| 439 | QReadLocker locker(&m_lock); |
| 440 | |
| 441 | return m_string.lastIndexOf(other_qstring, from, cs); |
| 442 | } |
| 443 | |
| 444 | int MString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
| 445 | { |
| 446 | QReadLocker locker(&m_lock); |
| 447 | |
| 448 | return m_string.lastIndexOf(ch, from, cs); |
| 449 | } |
| 450 | |
| 451 | int MString::lastIndexOf(const QRegExp & rx, int from) const |
| 452 | { |
| 453 | QReadLocker locker(&m_lock); |
| 454 | |
| 455 | return m_string.lastIndexOf(rx, from); |
| 456 | } |
| 457 | |
| 458 | MString MString::left(int n) const |
| 459 | { |
| 460 | QReadLocker locker(&m_lock); |
| 461 | |
| 462 | return MString(m_string.left(n)); |
| 463 | } |
| 464 | |
| 465 | MString MString::leftJustified(int width, QChar fill, bool truncate) const |
| 466 | { |
| 467 | QReadLocker locker(&m_lock); |
| 468 | |
| 469 | return MString(m_string.leftJustified(width, fill, truncate)); |
| 470 | } |
| 471 | |
| 472 | int MString::length() const |
| 473 | { |
| 474 | QReadLocker locker(&m_lock); |
| 475 | |
| 476 | return m_string.length(); |
| 477 | } |
| 478 | |
| 479 | int MString::localeAwareCompare(const MString & other) const |
| 480 | { |
| 481 | const QString other_qstring = other.toQString(); |
| 482 | |
| 483 | QReadLocker locker(&m_lock); |
| 484 | |
| 485 | return m_string.localeAwareCompare(other_qstring); |
| 486 | } |
| 487 | |
| 488 | MString MString::mid(int position, int n) const |
| 489 | { |
| 490 | QReadLocker locker(&m_lock); |
| 491 | |
| 492 | return MString(m_string.mid(position, n)); |
| 493 | } |
| 494 | |
| 495 | MString MString::normalized(QString::NormalizationForm mode) const |
| 496 | { |
| 497 | QReadLocker locker(&m_lock); |
| 498 | |
| 499 | return MString(m_string.normalized(mode)); |
| 500 | } |
| 501 | |
| 502 | MString MString::normalized(QString::NormalizationForm mode, |
| 503 | QChar::UnicodeVersion version) const |
| 504 | { |
| 505 | QReadLocker locker(&m_lock); |
| 506 | |
| 507 | return MString(m_string.normalized(mode, version)); |
| 508 | } |
| 509 | |
| 510 | MString & MString::prepend(const MString & str) |
| 511 | { |
| 512 | const QString other_qstring = str.toQString(); |
| 513 | |
| 514 | QWriteLocker locker(&m_lock); |
| 515 | |
| 516 | m_string.prepend(other_qstring); |
| 517 | return *this; |
| 518 | } |
| 519 | |
| 520 | MString & MString::prepend(const QLatin1String & str) |
| 521 | { |
| 522 | QWriteLocker locker(&m_lock); |
| 523 | |
| 524 | m_string.prepend(str); |
| 525 | return *this; |
| 526 | } |
| 527 | |
| 528 | MString & MString::prepend(const QByteArray & ba) |
| 529 | { |
| 530 | QWriteLocker locker(&m_lock); |
| 531 | |
| 532 | m_string.prepend(ba); |
| 533 | return *this; |
| 534 | } |
| 535 | |
| 536 | MString & MString::prepend(const char * str) |
| 537 | { |
| 538 | QWriteLocker locker(&m_lock); |
| 539 | |
| 540 | m_string.prepend(str); |
| 541 | return *this; |
| 542 | } |
| 543 | |
| 544 | MString & MString::prepend(QChar ch) |
| 545 | { |
| 546 | QWriteLocker locker(&m_lock); |
| 547 | |
| 548 | m_string.prepend(ch); |
| 549 | return *this; |
| 550 | } |
| 551 | |
| 552 | MString & MString::remove(int position, int n) |
| 553 | { |
| 554 | QWriteLocker locker(&m_lock); |
| 555 | |
| 556 | m_string.remove(position, n); |
| 557 | return *this; |
| 558 | } |
| 559 | |
| 560 | MString & MString::remove(const MString & str, Qt::CaseSensitivity cs) |
| 561 | { |
| 562 | const QString other_qstring = str.toQString(); |
| 563 | |
| 564 | QWriteLocker locker(&m_lock); |
| 565 | m_string.remove(other_qstring, cs); |
| 566 | return *this; |
| 567 | } |
| 568 | |
| 569 | MString & MString::remove(QChar ch, Qt::CaseSensitivity cs) |
| 570 | { |
| 571 | QWriteLocker locker(&m_lock); |
| 572 | |
| 573 | m_string.remove(ch, cs); |
| 574 | return *this; |
| 575 | } |
| 576 | |
| 577 | MString & MString::remove(const QRegExp & rx) |
| 578 | { |
| 579 | QWriteLocker locker(&m_lock); |
| 580 | |
| 581 | m_string.remove(rx); |
| 582 | return *this; |
| 583 | } |
| 584 | |
| 585 | MString & MString::replace(int position, int n, const MString & after) |
| 586 | { |
| 587 | const QString other_qstring = after.toQString(); |
| 588 | |
| 589 | QWriteLocker locker(&m_lock); |
| 590 | m_string.replace(position, n, other_qstring); |
| 591 | return *this; |
| 592 | } |
| 593 | |
| 594 | MString & MString::replace(int position, int n, const QChar * unicode, int size) |
| 595 | { |
| 596 | QWriteLocker locker(&m_lock); |
| 597 | |
| 598 | m_string.replace(position, n, unicode, size); |
| 599 | return *this; |
| 600 | } |
| 601 | |
| 602 | MString & MString::replace(int position, int n, QChar after) |
| 603 | { |
| 604 | QWriteLocker locker(&m_lock); |
| 605 | |
| 606 | m_string.replace(position, n, after); |
| 607 | return *this; |
| 608 | } |
| 609 | |
| 610 | MString & MString::replace(const MString & before, const MString & after, |
| 611 | Qt::CaseSensitivity cs) |
| 612 | { |
| 613 | const QString before_qstring = before.toQString(); |
| 614 | const QString after_qstring = after.toQString(); |
| 615 | |
| 616 | QWriteLocker locker(&m_lock); |
| 617 | |
| 618 | m_string.replace(before_qstring, after_qstring, cs); |
| 619 | return *this; |
| 620 | } |
| 621 | |
| 622 | MString & MString::replace(QChar ch, const MString & after, Qt::CaseSensitivity cs) |
| 623 | { |
| 624 | const QString after_qstring = after.toQString(); |
| 625 | |
| 626 | QWriteLocker locker(&m_lock); |
| 627 | |
| 628 | m_string.replace(ch, after_qstring, cs); |
| 629 | return *this; |
| 630 | } |
| 631 | |
| 632 | MString & MString::replace(QChar before, QChar after, Qt::CaseSensitivity cs) |
| 633 | { |
| 634 | QWriteLocker locker(&m_lock); |
| 635 | |
| 636 | m_string.replace(before, after, cs); |
| 637 | return *this; |
| 638 | } |
| 639 | |
| 640 | MString & MString::replace(const QRegExp & rx, const MString & after) |
| 641 | { |
| 642 | const QString after_qstring = after.toQString(); |
| 643 | |
| 644 | QWriteLocker locker(&m_lock); |
| 645 | |
| 646 | m_string.replace(rx, after_qstring); |
| 647 | return *this; |
| 648 | } |
| 649 | |
| 650 | void MString::reserve(int size) |
| 651 | { |
| 652 | QWriteLocker locker(&m_lock); |
| 653 | |
| 654 | m_string.reserve(size); |
| 655 | } |
| 656 | |
| 657 | void MString::resize(int size) |
| 658 | { |
| 659 | QWriteLocker locker(&m_lock); |
| 660 | |
| 661 | m_string.resize(size); |
| 662 | } |
| 663 | |
| 664 | MString MString::right(int n) const |
| 665 | { |
| 666 | QReadLocker locker(&m_lock); |
| 667 | |
| 668 | return MString(m_string.right(n)); |
| 669 | } |
| 670 | |
| 671 | MString MString::rightJustified(int width, QChar fill, bool truncate) const |
| 672 | { |
| 673 | QReadLocker locker(&m_lock); |
| 674 | |
| 675 | return MString(m_string.rightJustified(width, fill, truncate)); |
| 676 | } |
| 677 | |
| 678 | MString MString::section(QChar sep, int start, int end, SectionFlags flags) const |
| 679 | { |
| 680 | int qflag_int = flags; |
| 681 | QString::SectionFlags qflags(qflag_int); |
| 682 | QReadLocker locker(&m_lock); |
| 683 | |
| 684 | return MString(m_string.section(sep, start, end, qflags)); |
| 685 | } |
| 686 | |
| 687 | MString MString::section(const MString & sep, int start, int end, |
| 688 | SectionFlags flags) const |
| 689 | { |
| 690 | const QString sep_qstring = sep.toQString(); |
| 691 | int qflag_int = flags; |
| 692 | QString::SectionFlags qflags(qflag_int); |
| 693 | |
| 694 | QReadLocker locker(&m_lock); |
| 695 | return MString(m_string.section(sep_qstring, start, end, qflags)); |
| 696 | } |
| 697 | |
| 698 | MString MString::section(const QRegExp & reg, int start, int end, |
| 699 | SectionFlags flags) const |
| 700 | { |
| 701 | int qflag_int = flags; |
| 702 | QString::SectionFlags qflags(qflag_int); |
| 703 | QReadLocker locker(&m_lock); |
| 704 | |
| 705 | return MString(m_string.section(reg, start, end, qflags)); |
| 706 | } |
| 707 | |
| 708 | MString & MString::setNum(uint n, int base) |
| 709 | { |
| 710 | QWriteLocker locker(&m_lock); |
| 711 | |
| 712 | m_string.setNum(n, base); |
| 713 | return *this; |
| 714 | } |
| 715 | |
| 716 | MString & MString::setNum(long n, int base) |
| 717 | { |
| 718 | QWriteLocker locker(&m_lock); |
| 719 | |
| 720 | m_string.setNum(n, base); |
| 721 | return *this; |
| 722 | } |
| 723 | |
| 724 | MString & MString::setNum(ulong n, int base) |
| 725 | { |
| 726 | QWriteLocker locker(&m_lock); |
| 727 | |
| 728 | m_string.setNum(n, base); |
| 729 | return *this; |
| 730 | } |
| 731 | |
| 732 | MString & MString::setNum(qlonglong n, int base) |
| 733 | { |
| 734 | QWriteLocker locker(&m_lock); |
| 735 | |
| 736 | m_string.setNum(n, base); |
| 737 | return *this; |
| 738 | } |
| 739 | |
| 740 | MString & MString::setNum(qulonglong n, int base) |
| 741 | { |
| 742 | QWriteLocker locker(&m_lock); |
| 743 | |
| 744 | m_string.setNum(n, base); |
| 745 | return *this; |
| 746 | } |
| 747 | |
| 748 | MString & MString::setNum(short n, int base) |
| 749 | { |
| 750 | QWriteLocker locker(&m_lock); |
| 751 | |
| 752 | m_string.setNum(n, base); |
| 753 | return *this; |
| 754 | } |
| 755 | |
| 756 | MString & MString::setNum(ushort n, int base) |
| 757 | { |
| 758 | QWriteLocker locker(&m_lock); |
| 759 | |
| 760 | m_string.setNum(n, base); |
| 761 | return *this; |
| 762 | } |
| 763 | |
| 764 | MString & MString::setNum(double n, char format, int precision) |
| 765 | { |
| 766 | QWriteLocker locker(&m_lock); |
| 767 | |
| 768 | m_string.setNum(n, format, precision); |
| 769 | return *this; |
| 770 | } |
| 771 | |
| 772 | MString & MString::setNum(float n, char format, int precision) |
| 773 | { |
| 774 | QWriteLocker locker(&m_lock); |
| 775 | |
| 776 | m_string.setNum(n, format, precision); |
| 777 | return *this; |
| 778 | } |
| 779 | |
| 780 | MString & MString::setUnicode(const QChar * unicode, int size) |
| 781 | { |
| 782 | QWriteLocker locker(&m_lock); |
| 783 | |
| 784 | m_string.setUnicode(unicode, size); |
| 785 | return *this; |
| 786 | } |
| 787 | |
| 788 | MString & MString::setUtf16(const ushort * unicode, int size) |
| 789 | { |
| 790 | QWriteLocker locker(&m_lock); |
| 791 | |
| 792 | m_string.setUtf16(unicode, size); |
| 793 | return *this; |
| 794 | } |
| 795 | |
| 796 | MString MString::simplified() const |
| 797 | { |
| 798 | QReadLocker locker(&m_lock); |
| 799 | |
| 800 | return MString(m_string.simplified()); |
| 801 | } |
| 802 | |
| 803 | int MString::size() const |
| 804 | { |
| 805 | QReadLocker locker(&m_lock); |
| 806 | |
| 807 | return m_string.size(); |
| 808 | } |
| 809 | |
| 810 | QStringList MString::split(const MString & sep, SplitBehavior behavior, |
| 811 | Qt::CaseSensitivity cs) const |
| 812 | { |
| 813 | const QString sep_qstring = sep.toQString(); |
| 814 | |
| 815 | QReadLocker locker(&m_lock); |
| 816 | return m_string.split(sep_qstring, (QString::SplitBehavior)behavior, cs); |
| 817 | } |
| 818 | |
| 819 | QStringList MString::split(const QChar & sep, SplitBehavior behavior, |
| 820 | Qt::CaseSensitivity cs) const |
| 821 | { |
| 822 | QReadLocker locker(&m_lock); |
| 823 | |
| 824 | return m_string.split(sep, (QString::SplitBehavior)behavior, cs); |
| 825 | } |
| 826 | |
| 827 | QStringList MString::split(const QRegExp & rx, SplitBehavior behavior) const |
| 828 | { |
| 829 | QReadLocker locker(&m_lock); |
| 830 | |
| 831 | return m_string.split(rx, (QString::SplitBehavior)behavior); |
| 832 | } |
| 833 | |
| 834 | void MString::squeeze() |
| 835 | { |
| 836 | QWriteLocker locker(&m_lock); |
| 837 | |
| 838 | m_string.squeeze(); |
| 839 | } |
| 840 | |
| 841 | bool MString::startsWith(const MString & s, Qt::CaseSensitivity cs) const |
| 842 | { |
| 843 | const QString other_qstring = s.toQString(); |
| 844 | |
| 845 | QReadLocker locker(&m_lock); |
| 846 | |
| 847 | return m_string.startsWith(other_qstring, cs); |
| 848 | } |
| 849 | |
| 850 | bool MString::startsWith(const QLatin1String & s, Qt::CaseSensitivity cs) const |
| 851 | { |
| 852 | QReadLocker locker(&m_lock); |
| 853 | |
| 854 | return m_string.startsWith(s, cs); |
| 855 | } |
| 856 | |
| 857 | bool MString::startsWith(const QChar & c, Qt::CaseSensitivity cs) const |
| 858 | { |
| 859 | QReadLocker locker(&m_lock); |
| 860 | |
| 861 | return m_string.startsWith(c, cs); |
| 862 | } |
| 863 | |
| 864 | QByteArray MString::toAscii() const |
| 865 | { |
| 866 | QReadLocker locker(&m_lock); |
| 867 | |
| 868 | return m_string.toAscii(); |
| 869 | } |
| 870 | |
| 871 | MString MString::toCaseFolded() const |
| 872 | { |
| 873 | QReadLocker locker(&m_lock); |
| 874 | |
| 875 | return m_string.toCaseFolded(); |
| 876 | } |
| 877 | |
| 878 | double MString::toDouble(bool * ok) const |
| 879 | { |
| 880 | QReadLocker locker(&m_lock); |
| 881 | |
| 882 | return m_string.toDouble(ok); |
| 883 | } |
| 884 | |
| 885 | float MString::toFloat(bool * ok) const |
| 886 | { |
| 887 | QReadLocker locker(&m_lock); |
| 888 | |
| 889 | return m_string.toFloat(ok); |
| 890 | } |
| 891 | |
| 892 | int MString::toInt(bool * ok, int base) const |
| 893 | { |
| 894 | QReadLocker locker(&m_lock); |
| 895 | |
| 896 | return m_string.toInt(ok, base); |
| 897 | } |
| 898 | |
| 899 | QByteArray MString::toLatin1() const |
| 900 | { |
| 901 | QReadLocker locker(&m_lock); |
| 902 | |
| 903 | return m_string.toLatin1(); |
| 904 | } |
| 905 | |
| 906 | QByteArray MString::toLocal8Bit() const |
| 907 | { |
| 908 | QReadLocker locker(&m_lock); |
| 909 | |
| 910 | return m_string.toLocal8Bit(); |
| 911 | } |
| 912 | |
| 913 | long MString::toLong(bool * ok, int base) const |
| 914 | { |
| 915 | QReadLocker locker(&m_lock); |
| 916 | |
| 917 | return m_string.toLong(ok, base); |
| 918 | } |
| 919 | |
| 920 | qlonglong MString::toLongLong(bool * ok, int base) const |
| 921 | { |
| 922 | QReadLocker locker(&m_lock); |
| 923 | |
| 924 | return m_string.toLongLong(ok, base); |
| 925 | } |
| 926 | |
| 927 | MString MString::toLower() const |
| 928 | { |
| 929 | QReadLocker locker(&m_lock); |
| 930 | |
| 931 | return m_string.toLower(); |
| 932 | } |
| 933 | |
| 934 | QString MString::toQString() const |
| 935 | { |
| 936 | QReadLocker locker(&m_lock); |
| 937 | |
| 938 | return QString(m_string); |
| 939 | } |
| 940 | |
| 941 | short MString::toShort(bool * ok, int base) const |
| 942 | { |
| 943 | QReadLocker locker(&m_lock); |
| 944 | |
| 945 | return m_string.toShort(ok, base); |
| 946 | } |
| 947 | |
| 948 | std::string MString::toStdString() const |
| 949 | { |
| 950 | QReadLocker locker(&m_lock); |
| 951 | |
| 952 | return m_string.toStdString(); |
| 953 | } |
| 954 | |
| 955 | std::wstring MString::toStdWString() const |
| 956 | { |
| 957 | QReadLocker locker(&m_lock); |
| 958 | |
| 959 | return m_string.toStdWString(); |
| 960 | } |
| 961 | |
| 962 | uint MString::toUInt(bool * ok, int base) const |
| 963 | { |
| 964 | QReadLocker locker(&m_lock); |
| 965 | |
| 966 | return m_string.toUInt(ok, base); |
| 967 | } |
| 968 | |
| 969 | ulong MString::toULong(bool * ok, int base) const |
| 970 | { |
| 971 | QReadLocker locker(&m_lock); |
| 972 | |
| 973 | return m_string.toULong(ok, base); |
| 974 | } |
| 975 | |
| 976 | qulonglong MString::toULongLong(bool * ok, int base) const |
| 977 | { |
| 978 | QReadLocker locker(&m_lock); |
| 979 | |
| 980 | return m_string.toULongLong(ok, base); |
| 981 | } |
| 982 | |
| 983 | ushort MString::toUShort(bool * ok, int base) const |
| 984 | { |
| 985 | QReadLocker locker(&m_lock); |
| 986 | |
| 987 | return m_string.toUShort(ok, base); |
| 988 | } |
| 989 | |
| 990 | QVector<uint> MString::toUcs4() const |
| 991 | { |
| 992 | QReadLocker locker(&m_lock); |
| 993 | |
| 994 | return m_string.toUcs4(); |
| 995 | } |
| 996 | |
| 997 | MString MString::toUpper() const |
| 998 | { |
| 999 | QReadLocker locker(&m_lock); |
| 1000 | |
| 1001 | return m_string.toUpper(); |
| 1002 | } |
| 1003 | |
| 1004 | QByteArray MString::toUtf8() const |
| 1005 | { |
| 1006 | QReadLocker locker(&m_lock); |
| 1007 | |
| 1008 | return m_string.toUtf8(); |
| 1009 | } |
| 1010 | |
| 1011 | int MString::toWCharArray(wchar_t * array) const |
| 1012 | { |
| 1013 | QReadLocker locker(&m_lock); |
| 1014 | |
| 1015 | return m_string.toWCharArray(array); |
| 1016 | } |
| 1017 | |
| 1018 | MString MString::trimmed() const |
| 1019 | { |
| 1020 | QReadLocker locker(&m_lock); |
| 1021 | |
| 1022 | return MString(m_string.trimmed()); |
| 1023 | } |
| 1024 | |
| 1025 | void MString::truncate(int position) |
| 1026 | { |
| 1027 | QWriteLocker locker(&m_lock); |
| 1028 | |
| 1029 | m_string.truncate(position); |
| 1030 | } |
| 1031 | |
| 1032 | const QChar * MString::unicode() const |
| 1033 | { |
| 1034 | QReadLocker locker(&m_lock); |
| 1035 | |
| 1036 | return m_string.unicode(); |
| 1037 | } |
| 1038 | |
| 1039 | MString & MString::vsprintf(const char * cformat, va_list ap) |
| 1040 | { |
| 1041 | QWriteLocker locker(&m_lock); |
| 1042 | |
| 1043 | m_string.vsprintf(cformat, ap); |
| 1044 | return *this; |
| 1045 | } |
| 1046 | |
| 1047 | bool MString::operator< (const MString & other) const |
| 1048 | { |
| 1049 | const QString other_qstring = other.toQString(); |
| 1050 | |
| 1051 | QReadLocker locker(&m_lock); |
| 1052 | |
| 1053 | return (m_string < other_qstring); |
| 1054 | } |
| 1055 | |
| 1056 | bool MString::operator< (const QLatin1String & other) const |
| 1057 | { |
| 1058 | QReadLocker locker(&m_lock); |
| 1059 | |
| 1060 | return (m_string < other); |
| 1061 | } |
| 1062 | |
| 1063 | bool MString::operator< (const QByteArray & other) const |
| 1064 | { |
| 1065 | QReadLocker locker(&m_lock); |
| 1066 | |
| 1067 | return (m_string < other); |
| 1068 | } |
| 1069 | |
| 1070 | bool MString::operator< (const char * other) const |
| 1071 | { |
| 1072 | QReadLocker locker(&m_lock); |
| 1073 | |
| 1074 | return (m_string < other); |
| 1075 | } |
| 1076 | |
| 1077 | bool MString::operator<= (const MString & other) const |
| 1078 | { |
| 1079 | const QString other_qstring = other.toQString(); |
| 1080 | |
| 1081 | QReadLocker locker(&m_lock); |
| 1082 | |
| 1083 | return (m_string <= other_qstring); |
| 1084 | } |
| 1085 | |
| 1086 | bool MString::operator<= (const QLatin1String & other) const |
| 1087 | { |
| 1088 | QReadLocker locker(&m_lock); |
| 1089 | |
| 1090 | return (m_string <= other); |
| 1091 | } |
| 1092 | |
| 1093 | bool MString::operator<= (const QByteArray & other) const |
| 1094 | { |
| 1095 | QReadLocker locker(&m_lock); |
| 1096 | |
| 1097 | return (m_string <= other); |
| 1098 | } |
| 1099 | |
| 1100 | bool MString::operator<= (const char * other) const |
| 1101 | { |
| 1102 | QReadLocker locker(&m_lock); |
| 1103 | |
| 1104 | return (m_string <= other); |
| 1105 | } |
| 1106 | |
| 1107 | MString & MString::operator= (const MString & other) |
| 1108 | { |
| 1109 | if (this == &other) |
| 1110 | { |
| 1111 | return *this; //skip self-assignment |
| 1112 | } |
| 1113 | const QString other_qstring = other.toQString(); |
| 1114 | |
| 1115 | QWriteLocker locker(&m_lock); |
| 1116 | |
| 1117 | m_string = other_qstring; |
| 1118 | return *this; |
| 1119 | } |
| 1120 | |
| 1121 | MString & MString::operator= (const QLatin1String & str) |
| 1122 | { |
| 1123 | QWriteLocker locker(&m_lock); |
| 1124 | |
| 1125 | m_string = str; |
| 1126 | return *this; |
| 1127 | } |
| 1128 | |
| 1129 | MString & MString::operator= (const QByteArray & ba) |
| 1130 | { |
| 1131 | QWriteLocker locker(&m_lock); |
| 1132 | |
| 1133 | m_string = ba; |
| 1134 | return *this; |
| 1135 | } |
| 1136 | |
| 1137 | MString & MString::operator= (const char * str) |
| 1138 | { |
| 1139 | QWriteLocker locker(&m_lock); |
| 1140 | |
| 1141 | m_string = str; |
| 1142 | return *this; |
| 1143 | } |
| 1144 | |
| 1145 | MString & MString::operator= (char ch) |
| 1146 | { |
| 1147 | QWriteLocker locker(&m_lock); |
| 1148 | |
| 1149 | m_string = ch; |
| 1150 | return *this; |
| 1151 | } |
| 1152 | |
| 1153 | MString & MString::operator= (QChar ch) |
| 1154 | { |
| 1155 | QWriteLocker locker(&m_lock); |
| 1156 | |
| 1157 | m_string = ch; |
| 1158 | return *this; |
| 1159 | } |
| 1160 | |
| 1161 | bool MString::operator== (const MString & other) const |
| 1162 | { |
| 1163 | const QString other_qstring = other.toQString(); |
| 1164 | |
| 1165 | QReadLocker locker(&m_lock); |
| 1166 | |
| 1167 | return (m_string == other_qstring); |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | bool MString::operator== (const QLatin1String & other) const |
| 1172 | { |
| 1173 | QReadLocker locker(&m_lock); |
| 1174 | |
| 1175 | return (m_string == other); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | bool MString::operator== (const QByteArray & other) const |
| 1180 | { |
| 1181 | QReadLocker locker(&m_lock); |
| 1182 | |
| 1183 | return (m_string == other); |
| 1184 | } |
| 1185 | |
| 1186 | bool MString::operator== (const char * other) const |
| 1187 | { |
| 1188 | QReadLocker locker(&m_lock); |
| 1189 | |
| 1190 | return (m_string == other); |
| 1191 | } |
| 1192 | |
| 1193 | const QChar MString::operator[](int position) const |
| 1194 | { |
| 1195 | QReadLocker locker(&m_lock); |
| 1196 | |
| 1197 | return m_string[position]; |
| 1198 | } |
| 1199 | |
| 1200 | const QChar MString::operator[](uint position) const |
| 1201 | { |
| 1202 | QReadLocker locker(&m_lock); |
| 1203 | |
| 1204 | return m_string[position]; |
| 1205 | } |
| 1206 | |
| 1207 | QDataStream & operator<< (QDataStream & stream, const MString & string) |
| 1208 | { |
| 1209 | QReadLocker locker(&(string.m_lock)); |
| 1210 | |
| 1211 | stream << string.m_string; |
| 1212 | return stream; |
| 1213 | } |
| 1214 | |
| 1215 | QDataStream & operator>> (QDataStream & stream, MString & string) |
| 1216 | { |
| 1217 | QWriteLocker locker(&(string.m_lock)); |
| 1218 | |
| 1219 | stream >> string.m_string; |
| 1220 | return stream; |
| 1221 | } |
| 1222 | |
| 1223 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |