summaryrefslogtreecommitdiffstats
path: root/debian/pinentry-tqt/pinentry-tqt-1.1.0/tqt/secqstring.cpp
blob: 82dd91800470df4a3451537f129d5b79340bff68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/* secqstring.cpp - Secure version of TQString.
 * Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
 * Copyright (C) 2003 g10 Code GmbH
 *
 * The license of the original qstring.cpp file from which this file
 * is derived can be found below.  Modified by Marcus Brinkmann
 * <marcus@g10code.de>.  All modifications are licensed as follows, so
 * that the intersection of the two licenses is then the GNU General
 * Public License version 2.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-2.0
 */

/****************************************************************************
** $Id$
**
** Implementation of the SecTQString class and related Unicode functions
**
** Created : 920722
**
** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the tools module of the TQt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.TQPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
** licenses may use this file in accordance with the TQt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about TQt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for TQPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

// Don't define it while compiling this module, or USERS of TQt will
// not be able to link.

#include "secqstring.h"

static uint computeNewMax( uint len )
{
    uint newMax = 4;
    while ( newMax < len )
	newMax *= 2;
    // try to save some memory
    if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) )
	newMax -= newMax >> 2;
    return newMax;
}

// These macros are used for efficient allocation of TQChar strings.
// IMPORTANT! If you change these, make sure you also change the
// "delete unicode" statement in ~SecTQStringData() in SecTQString.h correspondingly!

#define QT_ALLOC_SECTQCHAR_VEC(N) (TQChar*) ::secmem_malloc (sizeof(TQChar) * (N))
#define QT_DELETE_SECTQCHAR_VEC(P) ::secmem_free (P)


/*****************************************************************************
  SecTQString member functions
 *****************************************************************************/

/*!
    \class SecTQString SecTQString.h
    \reentrant

    \brief The SecTQString class provides an abstraction of Unicode text
    and the classic C '\0'-terminated char array.

    \ingroup tools
    \ingroup shared
    \ingroup text
    \mainclass

    SecTQString uses \link shclass.html implicit sharing\endlink, which
    makes it very efficient and easy to use.

    In all of the SecTQString methods that take \c {const char *}
    parameters, the \c {const char *} is interpreted as a classic
    C-style '\0'-terminated ASCII string. It is legal for the \c
    {const char *} parameter to be 0. If the \c {const char *} is not
    '\0'-terminated, the results are undefined. Functions that copy
    classic C strings into a SecTQString will not copy the terminating
    '\0' character. The TQChar array of the SecTQString (as returned by
    unicode()) is generally not terminated by a '\0'. If you need to
    pass a SecTQString to a function that requires a C '\0'-terminated
    string use latin1().

    \keyword SecTQString::null
    A SecTQString that has not been assigned to anything is \e null, i.e.
    both the length and data pointer is 0. A SecTQString that references
    the empty string ("", a single '\0' char) is \e empty. Both null
    and empty SecTQStrings are legal parameters to the methods. Assigning
    \c{(const char *) 0} to SecTQString gives a null SecTQString. For
    convenience, \c SecTQString::null is a null SecTQString. When sorting,
    empty strings come first, followed by non-empty strings, followed
    by null strings. We recommend using \c{if ( !str.isNull() )} to
    check for a non-null string rather than \c{if ( !str )}; see \l
    operator!() for an explanation.

    Note that if you find that you are mixing usage of \l TQCString,
    SecTQString, and \l TQByteArray, this causes lots of unnecessary
    copying and might indicate that the true nature of the data you
    are dealing with is uncertain. If the data is '\0'-terminated 8-bit
    data, use \l TQCString; if it is unterminated (i.e. contains '\0's)
    8-bit data, use \l TQByteArray; if it is text, use SecTQString.

    Lists of strings are handled by the SecTQStringList class. You can
    split a string into a list of strings using SecTQStringList::split(),
    and join a list of strings into a single string with an optional
    separator using SecTQStringList::join(). You can obtain a list of
    strings from a string list that contain a particular substring or
    that match a particular \link ntqregexp.html regex\endlink using
    SecTQStringList::grep().

    <b>Note for C programmers</b>

    Due to C++'s type system and the fact that SecTQString is implicitly
    shared, SecTQStrings may be treated like ints or other simple base
    types. For example:

    \code
    SecTQString boolToString( bool b )
    {
	SecTQString result;
	if ( b )
	    result = "True";
	else
	    result = "False";
	return result;
    }
    \endcode

    The variable, result, is an auto variable allocated on the stack.
    When return is called, because we're returning by value, The copy
    constructor is called and a copy of the string is returned. (No
    actual copying takes place thanks to the implicit sharing, see
    below.)

    Throughout TQt's source code you will encounter SecTQString usages like
    this:
    \code
    SecTQString func( const SecTQString& input )
    {
	SecTQString output = input;
	// process output
	return output;
    }
    \endcode

    The 'copying' of input to output is almost as fast as copying a
    pointer because behind the scenes copying is achieved by
    incrementing a reference count. SecTQString (like all TQt's implicitly
    shared classes) operates on a copy-on-write basis, only copying if
    an instance is actually changed.

    If you wish to create a deep copy of a SecTQString without losing any
    Unicode information then you should use TQDeepCopy.

    \sa TQChar TQCString TQByteArray SecTQConstString
*/

Q_EXPORT SecTQStringData *SecTQString::shared_null = 0;
QT_STATIC_CONST_IMPL SecTQString SecTQString::null;
QT_STATIC_CONST_IMPL TQChar TQChar::null;
QT_STATIC_CONST_IMPL TQChar TQChar::replacement((ushort)0xfffd);
QT_STATIC_CONST_IMPL TQChar TQChar::byteOrderMark((ushort)0xfeff);
QT_STATIC_CONST_IMPL TQChar TQChar::byteOrderSwapped((ushort)0xfffe);
QT_STATIC_CONST_IMPL TQChar TQChar::nbsp((ushort)0x00a0);

SecTQStringData* SecTQString::makeSharedNull()
{
    SecTQString::shared_null = new SecTQStringData;
#if defined( Q_OS_MAC )
    SecTQString *that = const_cast<SecTQString *>(&SecTQString::null);
    that->d = SecTQString::shared_null;
#endif
    return SecTQString::shared_null;
}

/*!
    \fn SecTQString::SecTQString()

    Constructs a null string, i.e. both the length and data pointer
    are 0.

    \sa isNull()
*/

/*!
    Constructs a string of length one, containing the character \a ch.
*/
SecTQString::SecTQString( TQChar ch )
{
    d = new SecTQStringData( QT_ALLOC_SECTQCHAR_VEC( 1 ), 1, 1 );
    d->unicode[0] = ch;
}

/*!
    Constructs an implicitly shared copy of \a s. This is very fast
    since it only involves incrementing a reference count.
*/
SecTQString::SecTQString( const SecTQString &s ) :
    d(s.d)
{
    d->ref();
}


SecTQString::SecTQString( int size, bool /*dummy*/ )
{
    if ( size ) {
	int l = size;
	TQChar* uc = QT_ALLOC_SECTQCHAR_VEC( l );
	d = new SecTQStringData( uc, 0, l );
    } else {
	d = shared_null ? shared_null : (shared_null=new SecTQStringData);
	d->ref();
    }
}


/* Deep copy of STR.  */
SecTQString::SecTQString( const TQString &str )
{
    const TQChar *unicode = str.unicode ();
    uint length = str.length ();

    if ( !unicode && !length ) {
	d = shared_null ? shared_null : makeSharedNull();
	d->ref();
    } else {
	TQChar* uc = QT_ALLOC_SECTQCHAR_VEC( length );
	if ( unicode )
	    memcpy(uc, unicode, length*sizeof(TQChar));
	d = new SecTQStringData(uc,unicode ? length : 0,length);
    }
}


/*!
    Constructs a string that is a deep copy of the first \a length
    characters in the TQChar array.

    If \a unicode and \a length are 0, then a null string is created.

    If only \a unicode is 0, the string is empty but has \a length
    characters of space preallocated: SecTQString expands automatically
    anyway, but this may speed up some cases a little. We recommend
    using the plain constructor and setLength() for this purpose since
    it will result in more readable code.

    \sa isNull() setLength()
*/

SecTQString::SecTQString( const TQChar* unicode, uint length )
{
    if ( !unicode && !length ) {
	d = shared_null ? shared_null : makeSharedNull();
	d->ref();
    } else {
	TQChar* uc = QT_ALLOC_SECTQCHAR_VEC( length );
	if ( unicode )
	    memcpy(uc, unicode, length*sizeof(TQChar));
	d = new SecTQStringData(uc,unicode ? length : 0,length);
    }
}

/*!
    \fn SecTQString::~SecTQString()

    Destroys the string and frees the string's data if this is the
    last reference to the string.
*/


/*!
    Deallocates any space reserved solely by this SecTQString.

    If the string does not share its data with another SecTQString
    instance, nothing happens; otherwise the function creates a new,
    unique copy of this string. This function is called whenever the
    string is modified.
*/

void SecTQString::real_detach()
{
    setLength( length() );
}

void SecTQString::deref()
{
    if ( d && d->deref() ) {
	if ( d != shared_null )
	    delete d;
	d = 0;
    }
}

void SecTQStringData::deleteSelf()
{
    delete this;
}

/*!
    \fn SecTQString& SecTQString::operator=( TQChar c )

    Sets the string to contain just the single character \a c.
*/


/*!
    \overload

    Assigns a shallow copy of \a s to this string and returns a
    reference to this string. This is very fast because the string
    isn't actually copied.
*/
SecTQString &SecTQString::operator=( const SecTQString &s )
{
    s.d->ref();
    deref();
    d = s.d;
    return *this;
}


/*!
    \fn bool SecTQString::isNull() const

    Returns TRUE if the string is null; otherwise returns FALSE. A
    null string is always empty.

    \code
	SecTQString a;          // a.unicode() == 0, a.length() == 0
	a.isNull();         // TRUE, because a.unicode() == 0
	a.isEmpty();        // TRUE, because a.length() == 0
    \endcode

    \sa isEmpty(), length()
*/

/*!
    \fn bool SecTQString::isEmpty() const

    Returns TRUE if the string is empty, i.e. if length() == 0;
    otherwise returns FALSE. Null strings are also empty.

    \code
	SecTQString a( "" );
	a.isEmpty();        // TRUE
	a.isNull();         // FALSE

	SecTQString b;
	b.isEmpty();        // TRUE
	b.isNull();         // TRUE
    \endcode

    \sa isNull(), length()
*/

/*!
    \fn uint SecTQString::length() const

    Returns the length of the string.

    Null strings and empty strings have zero length.

    \sa isNull(), isEmpty()
*/

/*!
    If \a newLen is less than the length of the string, then the
    string is truncated at position \a newLen. Otherwise nothing
    happens.

    \code
	SecTQString s = "truncate me";
	s.truncate( 5 );            // s == "trunc"
    \endcode

    \sa setLength()
*/

void SecTQString::truncate( uint newLen )
{
    if ( newLen < d->len )
	setLength( newLen );
}

/*!
    Ensures that at least \a newLen characters are allocated to the
    string, and sets the length of the string to \a newLen. Any new
    space allocated contains arbitrary data.

    \sa reserve(), truncate()
*/
void SecTQString::setLength( uint newLen )
{
    if ( d->count != 1 || newLen > d->maxl ||
	 ( newLen * 4 < d->maxl && d->maxl > 4 ) ) {
	// detach, grow or shrink
	uint newMax = computeNewMax( newLen );
	TQChar* nd = QT_ALLOC_SECTQCHAR_VEC( newMax );
	if ( nd ) {
	    uint len = TQMIN( d->len, newLen );
	    memcpy( nd, d->unicode, sizeof(TQChar) * len );
	    deref();
	    d = new SecTQStringData( nd, newLen, newMax );
	}
    } else {
	d->len = newLen;
    }
}


/*!
    \internal

    Like setLength, but doesn't shrink the allocated memory.
*/
void SecTQString::grow( uint newLen )
{
    if ( d->count != 1 || newLen > d->maxl ) {
	setLength( newLen );
    } else {
	d->len = newLen;
    }
}


/*!
    Returns a substring that contains the \a len leftmost characters
    of the string.

    The whole string is returned if \a len exceeds the length of the
    string.

    \code
	SecTQString s = "Pineapple";
	SecTQString t = s.left( 4 );    // t == "Pine"
    \endcode

    \sa right(), mid(), isEmpty()
*/

SecTQString SecTQString::left( uint len ) const
{
    if ( isEmpty() ) {
	return SecTQString();
    } else if ( len == 0 ) {                    // ## just for 1.x compat:
	return SecTQString ("");
    } else if ( len >= length() ) {
	return *this;
    } else {
	SecTQString s( len, TRUE );
	memcpy( s.d->unicode, d->unicode, len * sizeof(TQChar) );
	s.d->len = len;
	return s;
    }
}

/*!
    Returns a string that contains the \a len rightmost characters of
    the string.

    If \a len is greater than the length of the string then the whole
    string is returned.

    \code
	SecTQString string( "Pineapple" );
	SecTQString t = string.right( 5 );   // t == "apple"
    \endcode

    \sa left(), mid(), isEmpty()
*/

SecTQString SecTQString::right( uint len ) const
{
    if ( isEmpty() ) {
	return SecTQString();
    } else if ( len == 0 ) {                    // ## just for 1.x compat:
	return SecTQString ("");
    } else {
	uint l = length();
	if ( len >= l )
	    return *this;
	SecTQString s( len, TRUE );
	memcpy( s.d->unicode, d->unicode+(l-len), len*sizeof(TQChar) );
	s.d->len = len;
	return s;
    }
}

/*!
    Returns a string that contains the \a len characters of this
    string, starting at position \a index.

    Returns a null string if the string is empty or \a index is out of
    range. Returns the whole string from \a index if \a index + \a len
    exceeds the length of the string.

    \code
	SecTQString s( "Five pineapples" );
	SecTQString t = s.mid( 5, 4 );                  // t == "pine"
    \endcode

    \sa left(), right()
*/

SecTQString SecTQString::mid( uint index, uint len ) const
{
    uint slen = length();
    if ( isEmpty() || index >= slen ) {
	return SecTQString();
    } else if ( len == 0 ) {                    // ## just for 1.x compat:
	return SecTQString ("");
    } else {
	if ( len > slen-index )
	    len = slen - index;
	if ( index == 0 && len == slen )
	    return *this;
	register const TQChar *p = unicode()+index;
	SecTQString s( len, TRUE );
	memcpy( s.d->unicode, p, len * sizeof(TQChar) );
	s.d->len = len;
	return s;
    }
}

/*!
    Inserts \a s into the string at position \a index.

    If \a index is beyond the end of the string, the string is
    extended with spaces to length \a index and \a s is then appended
    and returns a reference to the string.

    \code
	SecTQString string( "I like fish" );
	str = string.insert( 2, "don't " );
	// str == "I don't like fish"
    \endcode

    \sa remove(), replace()
*/

SecTQString &SecTQString::insert( uint index, const SecTQString &s )
{
    // the sub function takes care of &s == this case.
    return insert( index, s.unicode(), s.length() );
}

/*!
    \overload

    Inserts the first \a len characters in \a s into the string at
    position \a index and returns a reference to the string.
*/

SecTQString &SecTQString::insert( uint index, const TQChar* s, uint len )
{
    if ( len == 0 )
	return *this;
    uint olen = length();
    int nlen = olen + len;

    if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
	// Part of me - take a copy.
	TQChar *tmp = QT_ALLOC_SECTQCHAR_VEC( len );
	memcpy(tmp,s,len*sizeof(TQChar));
	insert(index,tmp,len);
	QT_DELETE_SECTQCHAR_VEC( tmp );
	return *this;
    }

    if ( index >= olen ) {                      // insert after end of string
	grow( len + index );
	int n = index - olen;
	TQChar* uc = d->unicode+olen;
	while (n--)
	    *uc++ = ' ';
	memcpy( d->unicode+index, s, sizeof(TQChar)*len );
    } else {                                    // normal insert
	grow( nlen );
	memmove( d->unicode + index + len, unicode() + index,
		 sizeof(TQChar) * (olen - index) );
	memcpy( d->unicode + index, s, sizeof(TQChar) * len );
    }
    return *this;
}

/*!
    Removes \a len characters from the string starting at position \a
    index, and returns a reference to the string.

    If \a index is beyond the length of the string, nothing happens.
    If \a index is within the string, but \a index + \a len is beyond
    the end of the string, the string is truncated at position \a
    index.

    \code
	SecTQString string( "Montreal" );
	string.remove( 1, 4 );      // string == "Meal"
    \endcode

    \sa insert(), replace()
*/

SecTQString &SecTQString::remove( uint index, uint len )
{
    uint olen = length();
    if ( index >= olen  ) {
	// range problems
    } else if ( index + len >= olen ) {  // index ok
	setLength( index );
    } else if ( len != 0 ) {
	real_detach();
	memmove( d->unicode+index, d->unicode+index+len,
		 sizeof(TQChar)*(olen-index-len) );
	setLength( olen-len );
    }
    return *this;
}


/*!
    \overload

    Replaces \a len characters with \a slen characters of TQChar data
    from \a s, starting at position \a index, and returns a reference
    to the string.

    \sa insert(), remove()
*/

SecTQString &SecTQString::replace( uint index, uint len, const TQChar* s, uint slen )
{
    real_detach();
    if ( len == slen && index + len <= length() ) {
	// Optimized common case: replace without size change
	memcpy( d->unicode+index, s, len * sizeof(TQChar) );
    } else if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
	// Part of me - take a copy.
	TQChar *tmp = QT_ALLOC_SECTQCHAR_VEC( slen );
	memcpy( tmp, s, slen * sizeof(TQChar) );
	replace( index, len, tmp, slen );
	QT_DELETE_SECTQCHAR_VEC( tmp );
    } else {
	remove( index, len );
	insert( index, s, slen );
    }
    return *this;
}


/*!
    Replaces \a len characters from the string with \a s, starting at
    position \a index, and returns a reference to the string.

    If \a index is beyond the length of the string, nothing is deleted
    and \a s is appended at the end of the string. If \a index is
    valid, but \a index + \a len is beyond the end of the string,
    the string is truncated at position \a index, then \a s is
    appended at the end.

    \code
	TQString string( "Say yes!" );
	string = string.replace( 4, 3, "NO" );
	// string == "Say NO!"
    \endcode

    \sa insert(), remove()
*/

SecTQString &SecTQString::replace( uint index, uint len, const SecTQString &s )
{
    return replace( index, len, s.unicode(), s.length() );
}


/*!
    Appends \a str to the string and returns a reference to the string.
*/
SecTQString& SecTQString::operator+=( const SecTQString &str )
{
    uint len1 = length();
    uint len2 = str.length();
    if ( len2 ) {
	if ( isEmpty() ) {
	    operator=( str );
	} else {
	    grow( len1+len2 );
	    memcpy( d->unicode+len1, str.unicode(), sizeof(TQChar)*len2 );
	}
    } else if ( isNull() && !str.isNull() ) {   // ## just for 1.x compat:
	*this = SecTQString ("");
    }
    return *this;
}


/*!
    Returns the string encoded in UTF-8 format.

    See TQTextCodec for more diverse coding/decoding of Unicode strings.

    \sa fromUtf8(), ascii(), latin1(), local8Bit()
*/
uchar *SecTQString::utf8() const
{
    int l = length();
    int rlen = l*3+1;
    uchar* rstr = (uchar*) ::secmem_malloc (rlen);
    uchar* cursor = rstr;
    const TQChar *ch = d->unicode;
    for (int i=0; i < l; i++) {
	uint u = ch->unicode();
	if ( u < 0x80 ) {
	    *cursor++ = (uchar)u;
	} else {
	    if ( u < 0x0800 ) {
		*cursor++ = 0xc0 | ((uchar) (u >> 6));
	    } else {
		if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
		    unsigned short low = ch[1].unicode();
		    if (low >= 0xdc00 && low < 0xe000) {
			++ch;
			++i;
			u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
		    }
		}
		if (u > 0xffff) {
		    // if people are working in utf8, but strings are encoded in eg. latin1, the resulting
		    // name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
		    // we can handle this without loosing information. This can happen with latin filenames
		    // and a utf8 locale under Unix.
		    if (u > 0x10fe00 && u < 0x10ff00) {
			*cursor++ = (u - 0x10fe00);
			++ch;
			continue;
		    } else {
			*cursor++ = 0xf0 | ((uchar) (u >> 18));
			*cursor++ = 0x80 | ( ((uchar) (u >> 12)) & 0x3f);
		    }
		} else {
		    *cursor++ = 0xe0 | ((uchar) (u >> 12));
		}
		*cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f);
	    }
	    *cursor++ = 0x80 | ((uchar) (u&0x3f));
	}
	++ch;
    }
    /* FIXME: secmem_realloc doesn't release extra memory.  */
    *cursor = '\0';
    return rstr;
}


/*!
  \fn TQChar SecTQString::at( uint ) const

    Returns the character at index \a i, or 0 if \a i is beyond the
    length of the string.

    \code
	const SecTQString string( "abcdefgh" );
	TQChar ch = string.at( 4 );
	// ch == 'e'
    \endcode

    If the SecTQString is not const (i.e. const SecTQString) or const& (i.e.
    const SecTQString &), then the non-const overload of at() will be used
    instead.
*/

/*!
    \fn TQChar SecTQString::constref(uint i) const

    Returns the TQChar at index \a i by value.

    Equivalent to at(\a i).

    \sa ref()
*/

/*!
    \fn TQChar& SecTQString::ref(uint i)

    Returns the TQChar at index \a i by reference, expanding the string
    with TQChar::null if necessary. The resulting reference can be
    assigned to, or otherwise used immediately, but becomes invalid
    once furher modifications are made to the string.

    \code
	SecTQString string("ABCDEF");
	TQChar ch = string.ref( 3 );         // ch == 'D'
    \endcode

    \sa constref()
*/

/*!
    \fn TQChar SecTQString::operator[]( int ) const

    Returns the character at index \a i, or TQChar::null if \a i is
    beyond the length of the string.

    If the SecTQString is not const (i.e., const SecTQString) or const\&
    (i.e., const SecTQString\&), then the non-const overload of operator[]
    will be used instead.
*/

/*!
    \fn TQCharRef SecTQString::operator[]( int )

    \overload

    The function returns a reference to the character at index \a i.
    The resulting reference can then be assigned to, or used
    immediately, but it will become invalid once further modifications
    are made to the original string.

    If \a i is beyond the length of the string then the string is
    expanded with TQChar::nulls, so that the TQCharRef references a
    valid (null) character in the string.

    The TQCharRef internal class can be used much like a constant
    TQChar, but if you assign to it, you change the original string
    (which will detach itself because of SecTQString's copy-on-write
    semantics). You will get compilation errors if you try to use the
    result as anything but a TQChar.
*/

/*!
    \fn TQCharRef SecTQString::at( uint i )

    \overload

    The function returns a reference to the character at index \a i.
    The resulting reference can then be assigned to, or used
    immediately, but it will become invalid once further modifications
    are made to the original string.

    If \a i is beyond the length of the string then the string is
    expanded with TQChar::null.
*/

/*
  Internal chunk of code to handle the
  uncommon cases of at() above.
*/
void SecTQString::subat( uint i )
{
    uint olen = d->len;
    if ( i >= olen ) {
	setLength( i+1 );               // i is index; i+1 is needed length
	for ( uint j=olen; j<=i; j++ )
	    d->unicode[j] = TQChar::null;
    } else {
	// Just be sure to detach
	real_detach();
    }
}


/*! \internal
 */
bool SecTQString::isRightToLeft() const
{
    int len = length();
    TQChar *p = d->unicode;
    while ( len-- ) {
	switch( (*p).direction () )
	{
	case TQChar::DirL:
	case TQChar::DirLRO:
	case TQChar::DirLRE:
	    return FALSE;
	case TQChar::DirR:
	case TQChar::DirAL:
	case TQChar::DirRLO:
	case TQChar::DirRLE:
	    return TRUE;
	default:
	    break;
	}
	++p;
    }
    return FALSE;
}


/*!
    \fn const SecTQString operator+( const SecTQString &s1, const SecTQString &s2 )

    \relates SecTQString

    Returns a string which is the result of concatenating the string
    \a s1 and the string \a s2.

    Equivalent to \a {s1}.append(\a s2).
*/


/*! \fn void SecTQString::detach()
  If the string does not share its data with another SecTQString instance,
  nothing happens; otherwise the function creates a new, unique copy of
  this string. This function is called whenever the string is modified. The
  implicit sharing mechanism is implemented this way.
*/