~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/qt3support/tools/q3cstring.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "q3cstring.h"
 
30
#include "qregexp.h"
 
31
#include "qdatastream.h"
 
32
 
 
33
#include <stdio.h>
 
34
#include <stdarg.h>
 
35
#include <stdlib.h>
 
36
#include <ctype.h>
 
37
#include <limits.h>
 
38
 
 
39
 
 
40
/*****************************************************************************
 
41
  Q3CString member functions
 
42
 *****************************************************************************/
 
43
 
 
44
/*!
 
45
    \class Q3CString q3cstring.h
 
46
    \reentrant
 
47
    \brief The Q3CString class provides an abstraction of the classic C
 
48
    zero-terminated char array (char *).
 
49
 
 
50
    \compat
 
51
 
 
52
    Q3CString tries to behave like a more convenient \c{const char *}.
 
53
    The price of doing this is that some algorithms will perform
 
54
    badly. For example, append() is O(length()) since it scans for a
 
55
    null terminator. Although you might use Q3CString for text that is
 
56
    never exposed to the user, for most purposes, and especially for
 
57
    user-visible text, you should use QString. QString provides
 
58
    implicit sharing, Unicode and other internationalization support,
 
59
    and is well optimized.
 
60
 
 
61
    Note that for the Q3CString methods that take a \c{const char *}
 
62
    parameter the \c{const char *} must either be 0 (null) or not-null
 
63
    and '\0' (NUL byte) terminated; otherwise the results are
 
64
    undefined.
 
65
 
 
66
    A Q3CString that has not been assigned to anything is \e null, i.e.
 
67
    both the length and the data pointer is 0. A Q3CString that
 
68
    references the empty string ("", a single '\0' char) is \e empty.
 
69
    Both null and empty Q3CStrings are legal parameters to the methods.
 
70
    Assigning \c{const char *} 0 to Q3CString produces a null Q3CString.
 
71
 
 
72
    The length() function returns the length of the string; resize()
 
73
    resizes the string and truncate() truncates the string. A string
 
74
    can be filled with a character using fill(). Strings can be left
 
75
    or right padded with characters using leftJustify() and
 
76
    rightJustify(). Characters, strings and regular expressions can be
 
77
    searched for using find() and findRev(), and counted using
 
78
    contains().
 
79
 
 
80
    Strings and characters can be inserted with insert() and appended
 
81
    with append(). A string can be prepended with prepend().
 
82
    Characters can be removed from the string with remove() and
 
83
    replaced with replace().
 
84
 
 
85
    Portions of a string can be extracted using left(), right() and
 
86
    mid(). Whitespace can be removed using stripWhiteSpace() and
 
87
    simplifyWhiteSpace(). Strings can be converted to uppercase or
 
88
    lowercase with upper() and lower() respectively.
 
89
 
 
90
    Strings that contain numbers can be converted to numbers with
 
91
    toShort(), toInt(), toLong(), toULong(), toFloat() and toDouble().
 
92
    Numbers can be converted to strings with setNum().
 
93
 
 
94
    Many operators are overloaded to work with Q3CStrings. Q3CString
 
95
    also supports some more obscure functions, e.g. sprintf(),
 
96
    setStr() and setExpand().
 
97
 
 
98
    \sidebar Note on Character Comparisons
 
99
 
 
100
    In Q3CString the notion of uppercase and lowercase and of which
 
101
    character is greater than or less than another character is locale
 
102
    dependent. This affects functions which support a case insensitive
 
103
    option or which compare or lowercase or uppercase their arguments.
 
104
    Case insensitive operations and comparisons will be accurate if
 
105
    both strings contain only ASCII characters. (If \c $LC_CTYPE is
 
106
    set, most Unix systems do "the right thing".) Functions that this
 
107
    affects include contains(), find(), findRev(), \l operator<(), \l
 
108
    operator<=(), \l operator>(), \l operator>=(), lower() and
 
109
    upper().
 
110
 
 
111
    This issue does not apply to \l{QString}s since they represent
 
112
    characters using Unicode.
 
113
    \endsidebar
 
114
 
 
115
    Performance note: The Q3CString methods for QRegExp searching are
 
116
    implemented by converting the Q3CString to a QString and performing
 
117
    the search on that. This implies a deep copy of the Q3CString data.
 
118
    If you are going to perform many QRegExp searches on a large
 
119
    Q3CString, you will get better performance by converting the
 
120
    Q3CString to a QString yourself, and then searching in the QString.
 
121
*/
 
122
 
 
123
/*!
 
124
    \fn Q3CString Q3CString::left(uint len)  const
 
125
 
 
126
    \internal
 
127
*/
 
128
 
 
129
/*!
 
130
    \fn Q3CString Q3CString::right(uint len) const
 
131
 
 
132
    \internal
 
133
*/
 
134
 
 
135
/*!
 
136
    \fn Q3CString Q3CString::mid(uint index, uint len) const
 
137
 
 
138
    \internal
 
139
*/
 
140
 
 
141
/*!
 
142
    \fn Q3CString  Q3CString::lower() const
 
143
 
 
144
    Use QByteArray::toLower() instead.
 
145
*/
 
146
 
 
147
/*!
 
148
    \fn Q3CString  Q3CString::upper() const
 
149
 
 
150
    Use QByteArray::toUpper() instead.
 
151
*/
 
152
 
 
153
/*!
 
154
    \fn Q3CString  Q3CString::stripWhiteSpace() const
 
155
 
 
156
    Use QByteArray::trimmed() instead.
 
157
*/
 
158
 
 
159
/*!
 
160
    \fn Q3CString  Q3CString::simplifyWhiteSpace() const
 
161
 
 
162
    Use QByteArray::simplified() instead.
 
163
*/
 
164
 
 
165
/*!
 
166
    \fn Q3CString& Q3CString::insert(uint index, const char *c)
 
167
 
 
168
    \internal
 
169
*/
 
170
 
 
171
/*!
 
172
    \fn Q3CString& Q3CString::insert(uint index, char c)
 
173
 
 
174
    \internal
 
175
*/
 
176
 
 
177
/*!
 
178
    \fn Q3CString& Q3CString::prepend(const char *c)
 
179
 
 
180
    \internal
 
181
*/
 
182
 
 
183
/*!
 
184
    \fn Q3CString& Q3CString::remove(uint index, uint len)
 
185
 
 
186
    \internal
 
187
*/
 
188
 
 
189
/*!
 
190
    \fn Q3CString& Q3CString::replace(uint index, uint len, const char *c)
 
191
 
 
192
    \internal
 
193
*/
 
194
 
 
195
/*!
 
196
    \fn Q3CString& Q3CString::replace(char c, const Q3CString &after)
 
197
 
 
198
    \internal
 
199
*/
 
200
 
 
201
/*!
 
202
    \fn Q3CString& Q3CString::replace(char c, const char *after)
 
203
 
 
204
    \internal
 
205
*/
 
206
 
 
207
/*!
 
208
    \fn Q3CString& Q3CString::replace(const Q3CString &b, const Q3CString &a)
 
209
 
 
210
    \internal
 
211
*/
 
212
 
 
213
/*!
 
214
    \fn Q3CString& Q3CString::replace(const char *b, const char *a)
 
215
 
 
216
    \internal
 
217
*/
 
218
 
 
219
/*!
 
220
    \fn Q3CString& Q3CString::replace(char b, char a)
 
221
 
 
222
    \internal
 
223
*/
 
224
 
 
225
/*!
 
226
    \fn Q3CString::Q3CString()
 
227
 
 
228
    Constructs a null string.
 
229
 
 
230
    \sa isNull()
 
231
*/
 
232
 
 
233
/*!
 
234
    \fn Q3CString::Q3CString(const QByteArray &ba)
 
235
 
 
236
    Constructs a copy of \a ba.
 
237
*/
 
238
 
 
239
/*!
 
240
    \fn Q3CString::Q3CString(const Q3CString &s)
 
241
 
 
242
    Constructs a shallow copy \a s.
 
243
*/
 
244
 
 
245
/*! \fn Q3CString::Q3CString(int size)
 
246
    Constructs a string with room for \a size characters, including
 
247
    the '\0'-terminator. Makes a null string if \a size == 0.
 
248
 
 
249
    If \a size \> 0, then the first and last characters in the string
 
250
    are initialized to '\0'. All other characters are uninitialized.
 
251
 
 
252
    \sa resize(), isNull()
 
253
*/
 
254
 
 
255
/*! \fn Q3CString::Q3CString(const char *str)
 
256
    Constructs a string that is a deep copy of \a str.
 
257
 
 
258
    If \a str is 0 a null string is created.
 
259
 
 
260
    \sa isNull()
 
261
*/
 
262
 
 
263
 
 
264
/*! \fn Q3CString::Q3CString(const char *str, uint maxsize)
 
265
 
 
266
    Constructs a string that is a deep copy of \a str. The copy will
 
267
    be at most \a maxsize bytes long including the '\0'-terminator.
 
268
 
 
269
    Example:
 
270
    \code
 
271
    Q3CString str("helloworld", 6); // assigns "hello" to str
 
272
    \endcode
 
273
 
 
274
    If \a str contains a 0 byte within the first \a maxsize bytes, the
 
275
    resulting Q3CString will be terminated by this 0. If \a str is 0 a
 
276
    null string is created.
 
277
 
 
278
    \sa isNull()
 
279
*/
 
280
 
 
281
/*!
 
282
    \fn Q3CString &Q3CString::operator=(const QByteArray &ba)
 
283
 
 
284
    Assigns byte array \a ba to this Q3CString.
 
285
*/
 
286
 
 
287
/*!
 
288
    \fn Q3CString &Q3CString::operator=(const Q3CString &s)
 
289
 
 
290
    Assigns a shallow copy of \a s to this string and returns a
 
291
    reference to this string.
 
292
*/
 
293
 
 
294
/*!
 
295
    \fn Q3CString &Q3CString::operator=(const char *str)
 
296
    \overload
 
297
 
 
298
    Assigns a deep copy of \a str to this string and returns a
 
299
    reference to this string.
 
300
 
 
301
    If \a str is 0 a null string is created.
 
302
 
 
303
    \sa isNull()
 
304
*/
 
305
 
 
306
/*
 
307
    \fn bool Q3CString::isNull() const
 
308
 
 
309
    Returns true if the string is null, i.e. if data() == 0; otherwise
 
310
    returns false. A null string is also an empty string.
 
311
 
 
312
    Example:
 
313
    \code
 
314
    Q3CString a;                // a.data() == 0,  a.size() == 0, a.length() == 0
 
315
    Q3CString b == "";        // b.data() == "", b.size() == 1, b.length() == 0
 
316
    a.isNull();                // true  because a.data() == 0
 
317
    a.isEmpty();        // true  because a.length() == 0
 
318
    b.isNull();                // false because b.data() == ""
 
319
    b.isEmpty();        // true  because b.length() == 0
 
320
    \endcode
 
321
 
 
322
    \sa isEmpty(), length(), size()
 
323
*/
 
324
 
 
325
/*
 
326
    \fn bool Q3CString::isEmpty() const
 
327
 
 
328
    Returns true if the string is empty, i.e. if length() == 0;
 
329
    otherwise returns false. An empty string is not always a null
 
330
    string.
 
331
 
 
332
    See example in isNull().
 
333
 
 
334
    \sa isNull(), length(), size()
 
335
*/
 
336
 
 
337
/*
 
338
    \fn uint Q3CString::length() const
 
339
 
 
340
    Returns the length of the string, excluding the '\0'-terminator.
 
341
    Equivalent to calling \c strlen(data()).
 
342
 
 
343
    Null strings and empty strings have zero length.
 
344
 
 
345
    \sa size(), isNull(), isEmpty()
 
346
*/
 
347
 
 
348
/*
 
349
    \fn bool Q3CString::truncate(uint pos)
 
350
 
 
351
    Truncates the string at position \a pos.
 
352
 
 
353
    Equivalent to calling \c resize(pos+1).
 
354
 
 
355
    Example:
 
356
    \code
 
357
    Q3CString s = "truncate this string";
 
358
    s.truncate(5);                      // s == "trunc"
 
359
    \endcode
 
360
 
 
361
    \sa resize()
 
362
*/
 
363
 
 
364
 
 
365
 
 
366
/*!
 
367
    Implemented as a call to the native vsprintf() (see the manual for
 
368
    your C library).
 
369
 
 
370
    If the string is shorter than 256 characters, this sprintf() calls
 
371
    resize(256) to decrease the chance of memory corruption. The
 
372
    string is resized back to its actual length before sprintf()
 
373
    returns.
 
374
 
 
375
    Example:
 
376
    \code
 
377
    Q3CString s;
 
378
    s.sprintf("%d - %s", 1, "first");                // result < 256 chars
 
379
 
 
380
    Q3CString big(25000);                        // very long string
 
381
    big.sprintf("%d - %s", 2, longString);        // result < 25000 chars
 
382
    \endcode
 
383
 
 
384
    \warning All vsprintf() implementations will write past the end of
 
385
    the target string (*this) if the \a format specification and
 
386
    arguments happen to be longer than the target string, and some
 
387
    will also fail if the target string is longer than some arbitrary
 
388
    implementation limit.
 
389
 
 
390
    Giving user-supplied arguments to sprintf() is risky: Sooner or
 
391
    later someone will paste a huge line into your application.
 
392
*/
 
393
 
 
394
Q3CString &Q3CString::sprintf(const char *format, ...)
 
395
{
 
396
    detach();
 
397
    va_list ap;
 
398
    va_start(ap, format);
 
399
    if (size() < 256)
 
400
        resize(256);                // make string big enough
 
401
    vsprintf(data(), format, ap);
 
402
    resize(qstrlen(constData()));
 
403
    va_end(ap);
 
404
    return *this;
 
405
}
 
406
 
 
407
 
 
408
 
 
409
/*!
 
410
    \fn Q3CString Q3CString::copy() const
 
411
 
 
412
    Returns a deep copy of this string.
 
413
 
 
414
    \sa detach()
 
415
*/
 
416
 
 
417
 
 
418
/*!
 
419
    Returns a string of length \a width (plus one for the terminating
 
420
    '\0') that contains this string padded with the \a fill character.
 
421
 
 
422
    If the length of the string exceeds \a width and \a truncate is
 
423
    false (the default), then the returned string is a copy of the
 
424
    string. If the length of the string exceeds \a width and \a
 
425
    truncate is true, then the returned string is a left(\a width).
 
426
 
 
427
    Example:
 
428
    \code
 
429
    Q3CString s("apple");
 
430
    Q3CString t = s.leftJustify(8, '.');  // t == "apple..."
 
431
    \endcode
 
432
 
 
433
    \sa rightJustify()
 
434
*/
 
435
 
 
436
Q3CString Q3CString::leftJustify(uint width, char fill, bool truncate) const
 
437
{
 
438
    Q3CString result;
 
439
    int len = qstrlen(constData());
 
440
    int padlen = width - len;
 
441
    if (padlen > 0) {
 
442
        result.resize(len+padlen);
 
443
        memcpy(result.data(), constData(), len);
 
444
        memset(result.data()+len, fill, padlen);
 
445
    } else {
 
446
        if (truncate)
 
447
            result = left(width);
 
448
        else
 
449
            result = *this;
 
450
    }
 
451
    return result;
 
452
}
 
453
 
 
454
/*!
 
455
    Returns a string of length \a width (plus one for the terminating
 
456
    '\0') that contains zero or more of the \a fill character followed
 
457
    by this string.
 
458
 
 
459
    If the length of the string exceeds \a width and \a truncate is
 
460
    false (the default), then the returned string is a copy of the
 
461
    string. If the length of the string exceeds \a width and \a
 
462
    truncate is true, then the returned string is a left(\a width).
 
463
 
 
464
    Example:
 
465
    \code
 
466
    Q3CString s("pie");
 
467
    Q3CString t = s.rightJustify(8, '.');  // t == ".....pie"
 
468
    \endcode
 
469
 
 
470
    \sa leftJustify()
 
471
*/
 
472
 
 
473
Q3CString Q3CString::rightJustify(uint width, char fill, bool truncate) const
 
474
{
 
475
    Q3CString result;
 
476
    int len = qstrlen(constData());
 
477
    int padlen = width - len;
 
478
    if (padlen > 0) {
 
479
        result.resize(len+padlen);
 
480
        memset(result.data(), fill, padlen);
 
481
        memcpy(result.data()+padlen, constData(), len);
 
482
    } else {
 
483
        if (truncate)
 
484
            result = left(width);
 
485
        else
 
486
            result = *this;
 
487
    }
 
488
    return result;
 
489
}
 
490
 
 
491
/*!
 
492
    Returns the string converted to a \c long value.
 
493
 
 
494
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
495
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
496
    true.
 
497
*/
 
498
 
 
499
long Q3CString::toLong(bool *ok) const
 
500
{
 
501
    const char *p = constData();
 
502
    long val=0;
 
503
    const long max_mult = 214748364;
 
504
    bool is_ok = false;
 
505
    int neg = 0;
 
506
    if (!p)
 
507
        goto bye;
 
508
    while (isspace((uchar) *p))                // skip leading space
 
509
        p++;
 
510
    if (*p == '-') {
 
511
        p++;
 
512
        neg = 1;
 
513
    } else if (*p == '+') {
 
514
        p++;
 
515
    }
 
516
    if (!isdigit((uchar) *p))
 
517
        goto bye;
 
518
    while (isdigit((uchar) *p)) {
 
519
        if (val > max_mult || (val == max_mult && (*p-'0') > 7+neg))
 
520
            goto bye;
 
521
        val = 10*val + (*p++ - '0');
 
522
    }
 
523
    if (neg)
 
524
        val = -val;
 
525
    while (isspace((uchar) *p))                // skip trailing space
 
526
        p++;
 
527
    if (*p == '\0')
 
528
        is_ok = true;
 
529
bye:
 
530
    if (ok)
 
531
        *ok = is_ok;
 
532
    return is_ok ? val : 0;
 
533
}
 
534
 
 
535
/*!
 
536
    Returns the string converted to an \c{unsigned long} value.
 
537
 
 
538
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
539
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
540
    true.
 
541
*/
 
542
 
 
543
ulong Q3CString::toULong(bool *ok) const
 
544
{
 
545
    const char *p = constData();
 
546
    ulong val=0;
 
547
    const ulong max_mult = 429496729;
 
548
    bool is_ok = false;
 
549
    if (!p)
 
550
        goto bye;
 
551
    while (isspace((uchar) *p))                // skip leading space
 
552
        p++;
 
553
    if (*p == '+')
 
554
        p++;
 
555
    if (!isdigit((uchar) *p))
 
556
        goto bye;
 
557
    while (isdigit((uchar) *p)) {
 
558
        if (val > max_mult || (val == max_mult && (*p-'0') > 5))
 
559
            goto bye;
 
560
        val = 10*val + (*p++ - '0');
 
561
    }
 
562
    while (isspace((uchar) *p))                // skip trailing space
 
563
        p++;
 
564
    if (*p == '\0')
 
565
        is_ok = true;
 
566
bye:
 
567
    if (ok)
 
568
        *ok = is_ok;
 
569
    return is_ok ? val : 0;
 
570
}
 
571
 
 
572
/*!
 
573
    Returns the string converted to a \c{short} value.
 
574
 
 
575
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
576
    number, is out of range, or if it has trailing garbage; otherwise
 
577
    *\a ok is set to true.
 
578
*/
 
579
 
 
580
short Q3CString::toShort(bool *ok) const
 
581
{
 
582
    long v = toLong(ok);
 
583
    if (ok && *ok && (v < -32768 || v > 32767))
 
584
        *ok = false;
 
585
    return (short)v;
 
586
}
 
587
 
 
588
/*!
 
589
    Returns the string converted to an \c{unsigned short} value.
 
590
 
 
591
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
592
    number, is out of range, or if it has trailing garbage; otherwise
 
593
    *\a ok is set to true.
 
594
*/
 
595
 
 
596
ushort Q3CString::toUShort(bool *ok) const
 
597
{
 
598
    ulong v = toULong(ok);
 
599
    if (ok && *ok && (v > 65535))
 
600
        *ok = false;
 
601
    return (ushort)v;
 
602
}
 
603
 
 
604
 
 
605
/*!
 
606
    Returns the string converted to a \c{int} value.
 
607
 
 
608
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
609
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
610
    true.
 
611
*/
 
612
 
 
613
int Q3CString::toInt(bool *ok) const
 
614
{
 
615
    return (int)toLong(ok);
 
616
}
 
617
 
 
618
/*!
 
619
    Returns the string converted to an \c{unsigned int} value.
 
620
 
 
621
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
622
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
623
    true.
 
624
*/
 
625
 
 
626
uint Q3CString::toUInt(bool *ok) const
 
627
{
 
628
    return (uint)toULong(ok);
 
629
}
 
630
 
 
631
/*!
 
632
    Returns the string converted to a \c{double} value.
 
633
 
 
634
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
635
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
636
    true.
 
637
*/
 
638
 
 
639
double Q3CString::toDouble(bool *ok) const
 
640
{
 
641
    char *end;
 
642
    double val = strtod(constData() ? constData() : "", &end);
 
643
    if (ok)
 
644
        *ok = (constData() && *constData() && (end == 0 || *end == '\0'));
 
645
    return val;
 
646
}
 
647
 
 
648
/*!
 
649
    Returns the string converted to a \c{float} value.
 
650
 
 
651
    If \a ok is not 0: *\a ok is set to false if the string is not a
 
652
    number, or if it has trailing garbage; otherwise *\a ok is set to
 
653
    true.
 
654
*/
 
655
 
 
656
float Q3CString::toFloat(bool *ok) const
 
657
{
 
658
    return (float)toDouble(ok);
 
659
}
 
660
 
 
661
 
 
662
/*! \fn Q3CString &Q3CString::setStr(const char *str)
 
663
    Makes a deep copy of \a str. Returns a reference to the string.
 
664
*/
 
665
 
 
666
/*!
 
667
    \overload
 
668
 
 
669
    Sets the string to the string representation of the number \a n
 
670
    and returns a reference to the string.
 
671
*/
 
672
 
 
673
Q3CString &Q3CString::setNum(long n)
 
674
{
 
675
    data();
 
676
    char buf[20];
 
677
    register char *p = &buf[19];
 
678
    bool neg;
 
679
    if (n < 0) {
 
680
        neg = true;
 
681
        n = -n;
 
682
    } else {
 
683
        neg = false;
 
684
    }
 
685
    *p = '\0';
 
686
    do {
 
687
        *--p = ((int)(n%10)) + '0';
 
688
        n /= 10;
 
689
    } while (n);
 
690
    if (neg)
 
691
        *--p = '-';
 
692
    *this = p;
 
693
    return *this;
 
694
}
 
695
 
 
696
/*!
 
697
    \overload
 
698
 
 
699
    Sets the string to the string representation of the number \a n
 
700
    and returns a reference to the string.
 
701
*/
 
702
 
 
703
Q3CString &Q3CString::setNum(ulong n)
 
704
{
 
705
    data();
 
706
    char buf[20];
 
707
    register char *p = &buf[19];
 
708
    *p = '\0';
 
709
    do {
 
710
        *--p = ((int)(n%10)) + '0';
 
711
        n /= 10;
 
712
    } while (n);
 
713
    *this = p;
 
714
    return *this;
 
715
}
 
716
 
 
717
/*!
 
718
    \fn Q3CString &Q3CString::setNum(int n)
 
719
    \overload
 
720
 
 
721
    Sets the string to the string representation of the number \a n
 
722
    and returns a reference to the string.
 
723
*/
 
724
 
 
725
/*!
 
726
    \fn Q3CString &Q3CString::setNum(uint n)
 
727
    \overload
 
728
 
 
729
    Sets the string to the string representation of the number \a n
 
730
    and returns a reference to the string.
 
731
*/
 
732
 
 
733
/*!
 
734
    \fn Q3CString &Q3CString::setNum(short n)
 
735
    \overload
 
736
 
 
737
    Sets the string to the string representation of the number \a n
 
738
    and returns a reference to the string.
 
739
*/
 
740
 
 
741
/*!
 
742
    \fn Q3CString &Q3CString::setNum(ushort n)
 
743
    \overload
 
744
 
 
745
    Sets the string to the string representation of the number \a n
 
746
    and returns a reference to the string.
 
747
*/
 
748
 
 
749
/*!
 
750
    Sets the string to the string representation of the number \a n
 
751
    and returns a reference to the string.
 
752
 
 
753
    The format of the string representation is specified by the format
 
754
    character \a f, and the precision (number of digits after the
 
755
    decimal point) is specified with \a prec.
 
756
 
 
757
    The valid formats for \a f are 'e', 'E', 'f', 'g' and 'G'. The
 
758
    formats are the same as for sprintf(); they are explained in \l
 
759
    QString::arg().
 
760
*/
 
761
 
 
762
Q3CString &Q3CString::setNum(double n, char f, int prec)
 
763
{
 
764
#ifndef QT_NO_DEBUG
 
765
    if (!(f=='f' || f=='F' || f=='e' || f=='E' || f=='g' || f=='G'))
 
766
        qWarning("Q3CString::setNum: Invalid format char '%c'", f);
 
767
#endif
 
768
    char format[20];
 
769
    register char *fs = format;                        // generate format string
 
770
    *fs++ = '%';                                //   "%.<prec>l<f>"
 
771
    if (prec > 99)
 
772
        prec = 99;
 
773
    *fs++ = '.';
 
774
    if (prec >= 10) {
 
775
        *fs++ = prec / 10 + '0';
 
776
        *fs++ = prec % 10 + '0';
 
777
    } else {
 
778
        *fs++ = prec + '0';
 
779
    }
 
780
    *fs++ = 'l';
 
781
    *fs++ = f;
 
782
    *fs = '\0';
 
783
    return sprintf(format, n);
 
784
}
 
785
 
 
786
/*! \fn Q3CString &Q3CString::setNum(float n, char f, int prec)
 
787
    \overload
 
788
*/
 
789
 
 
790
/*!
 
791
    Sets the character at position \a index to \a c and expands the
 
792
    string if necessary, padding with spaces.
 
793
 
 
794
    Returns false if \a index was out of range and the string could
 
795
    not be expanded; otherwise returns true.
 
796
*/
 
797
 
 
798
bool Q3CString::setExpand(uint index, char c)
 
799
{
 
800
    uint oldlen = length();
 
801
    if (index >= oldlen) {
 
802
        resize(index+1);
 
803
        if (index > oldlen)
 
804
            memset(data() + oldlen, ' ', index - oldlen);
 
805
    }
 
806
    *(data() + index) = c;
 
807
    return true;
 
808
}
 
809
 
 
810
 
 
811
/*
 
812
    \fn Q3CString::operator const char *() const
 
813
 
 
814
    Returns the string data.
 
815
*/
 
816
 
 
817
 
 
818
/*!
 
819
    \fn Q3CString& Q3CString::append(const char *str)
 
820
 
 
821
    Appends string \a str to the string and returns a reference to the
 
822
    string. Equivalent to operator+=().
 
823
*/
 
824
 
 
825
 
 
826
 
 
827
/*! \fn QDataStream &operator<<(QDataStream &s, const Q3CString &str)
 
828
    \relates Q3CString
 
829
 
 
830
    Writes string \a str to the stream \a s.
 
831
 
 
832
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
833
*/
 
834
 
 
835
/*!
 
836
  \fn QDataStream &operator>>(QDataStream &s, Q3CString &str)
 
837
    \relates Q3CString
 
838
 
 
839
    Reads a string into \a str from the stream \a s.
 
840
 
 
841
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
842
*/
 
843
 
 
844
/*****************************************************************************
 
845
  Documentation for related functions
 
846
 *****************************************************************************/
 
847
 
 
848
/*!
 
849
    \fn bool operator==(const Q3CString &s1, const Q3CString &s2)
 
850
 
 
851
    \relates Q3CString
 
852
 
 
853
    Returns true if \a s1 and \a s2 are equal; otherwise returns false.
 
854
 
 
855
    Equivalent to qstrcmp(\a s1, \a s2) == 0.
 
856
*/
 
857
 
 
858
/*!
 
859
    \fn bool operator==(const Q3CString &s1, const char *s2)
 
860
    \overload
 
861
 
 
862
    \relates Q3CString
 
863
 
 
864
    Returns true if \a s1 and \a s2 are equal; otherwise returns false.
 
865
 
 
866
    Equivalent to qstrcmp(\a s1, \a s2) == 0.
 
867
*/
 
868
 
 
869
/*!
 
870
    \fn bool operator==(const char *s1, const Q3CString &s2)
 
871
    \overload
 
872
 
 
873
    \relates Q3CString
 
874
 
 
875
    Returns true if \a s1 and \a s2 are equal; otherwise returns false.
 
876
 
 
877
    Equivalent to qstrcmp(\a s1, \a s2) == 0.
 
878
*/
 
879
 
 
880
/*!
 
881
    \fn bool operator!=(const Q3CString &s1, const Q3CString &s2)
 
882
 
 
883
    \relates Q3CString
 
884
 
 
885
    Returns true if \a s1 and \a s2 are different; otherwise returns false.
 
886
 
 
887
    Equivalent to qstrcmp(\a s1, \a s2) != 0.
 
888
*/
 
889
 
 
890
/*!
 
891
    \fn bool operator!=(const Q3CString &s1, const char *s2)
 
892
    \overload
 
893
 
 
894
    \relates Q3CString
 
895
 
 
896
    Returns true if \a s1 and \a s2 are different; otherwise returns false.
 
897
 
 
898
    Equivalent to qstrcmp(\a s1, \a s2) != 0.
 
899
*/
 
900
 
 
901
/*!
 
902
    \fn bool operator!=(const char *s1, const Q3CString &s2)
 
903
    \overload
 
904
 
 
905
    \relates Q3CString
 
906
 
 
907
    Returns true if \a s1 and \a s2 are different; otherwise returns false.
 
908
 
 
909
    Equivalent to qstrcmp(\a s1, \a s2) != 0.
 
910
*/
 
911
 
 
912
/*!
 
913
    \fn bool operator<(const Q3CString &s1, const char *s2)
 
914
 
 
915
    \relates Q3CString
 
916
 
 
917
    Returns true if \a s1 is less than \a s2; otherwise returns false.
 
918
 
 
919
    Equivalent to qstrcmp(\a s1, \a s2) \< 0.
 
920
*/
 
921
 
 
922
/*!
 
923
    \fn bool operator<(const char *s1, const Q3CString &s2)
 
924
    \overload
 
925
 
 
926
    \relates Q3CString
 
927
 
 
928
    Returns true if \a s1 is less than \a s2; otherwise returns false.
 
929
 
 
930
    Equivalent to qstrcmp(\a s1, \a s2) \< 0.
 
931
*/
 
932
 
 
933
/*!
 
934
    \fn bool operator<=(const Q3CString &s1, const char *s2)
 
935
 
 
936
    \relates Q3CString
 
937
 
 
938
    Returns true if \a s1 is less than or equal to \a s2; otherwise
 
939
    returns false.
 
940
 
 
941
    Equivalent to qstrcmp(\a s1, \a s2) \<= 0.
 
942
*/
 
943
 
 
944
/*!
 
945
    \fn bool operator<=(const char *s1, const Q3CString &s2)
 
946
    \overload
 
947
 
 
948
    \relates Q3CString
 
949
 
 
950
    Returns true if \a s1 is less than or equal to \a s2; otherwise
 
951
    returns false.
 
952
 
 
953
    Equivalent to qstrcmp(\a s1, \a s2) \<= 0.
 
954
*/
 
955
 
 
956
/*!
 
957
    \fn bool operator>(const Q3CString &s1, const char *s2)
 
958
 
 
959
    \relates Q3CString
 
960
 
 
961
    Returns true if \a s1 is greater than \a s2; otherwise returns false.
 
962
 
 
963
    Equivalent to qstrcmp(\a s1, \a s2) \> 0.
 
964
*/
 
965
 
 
966
/*!
 
967
    \fn bool operator>(const char *s1, const Q3CString &s2)
 
968
    \overload
 
969
 
 
970
    \relates Q3CString
 
971
 
 
972
    Returns true if \a s1 is greater than \a s2; otherwise returns false.
 
973
 
 
974
    Equivalent to qstrcmp(\a s1, \a s2) \> 0.
 
975
*/
 
976
 
 
977
/*!
 
978
    \fn bool operator>=(const Q3CString &s1, const char *s2)
 
979
 
 
980
    \relates Q3CString
 
981
 
 
982
    Returns true if \a s1 is greater than or equal to \a s2; otherwise
 
983
    returns false.
 
984
 
 
985
    Equivalent to qstrcmp(\a s1, \a s2) \>= 0.
 
986
*/
 
987
 
 
988
/*!
 
989
    \fn bool operator>=(const char *s1, const Q3CString &s2)
 
990
    \overload
 
991
 
 
992
    \relates Q3CString
 
993
 
 
994
    Returns true if \a s1 is greater than or equal to \a s2; otherwise
 
995
    returns false.
 
996
 
 
997
    Equivalent to qstrcmp(\a s1, \a s2) \>= 0.
 
998
*/
 
999
 
 
1000
/*!
 
1001
    \fn const Q3CString operator+(const Q3CString &s1, const Q3CString &s2)
 
1002
 
 
1003
    \relates Q3CString
 
1004
 
 
1005
    Returns a string which consists of the concatenation of \a s1 and
 
1006
    \a s2.
 
1007
*/
 
1008
 
 
1009
/*!
 
1010
    \fn const Q3CString operator+(const Q3CString &s1, const char *s2)
 
1011
    \overload
 
1012
 
 
1013
    \relates Q3CString
 
1014
 
 
1015
    Returns a string which consists of the concatenation of \a s1 and \a s2.
 
1016
*/
 
1017
 
 
1018
/*!
 
1019
    \fn const Q3CString operator+(const char *s1, const Q3CString &s2)
 
1020
    \overload
 
1021
 
 
1022
    \relates Q3CString
 
1023
 
 
1024
    Returns a string which consists of the concatenation of \a s1 and \a s2.
 
1025
*/
 
1026
 
 
1027
/*!
 
1028
    \fn const Q3CString operator+(const Q3CString &s, char c)
 
1029
    \overload
 
1030
 
 
1031
    \relates Q3CString
 
1032
 
 
1033
    Returns a string which consists of the concatenation of \a s and \a c.
 
1034
*/
 
1035
 
 
1036
/*!
 
1037
    \fn const Q3CString operator+(char c, const Q3CString &s)
 
1038
    \overload
 
1039
 
 
1040
    \relates Q3CString
 
1041
 
 
1042
    Returns a string which consists of the concatenation of \a c and \a s.
 
1043
*/