xref: /AOO41X/main/sal/rtl/source/ustrbuf.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <osl/interlck.h>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #ifndef _RTL_STRING_HXX_
31*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32*cdf0e10cSrcweir #endif
33*cdf0e10cSrcweir #include <rtl/memory.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /*
36*cdf0e10cSrcweir #include <rtl/alloc.h>
37*cdf0e10cSrcweir */
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir /*************************************************************************
42*cdf0e10cSrcweir  *  rtl_uStringbuffer_newFromStr_WithLength
43*cdf0e10cSrcweir  */
44*cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
45*cdf0e10cSrcweir                                                        const sal_Unicode * value,
46*cdf0e10cSrcweir                                                        sal_Int32 count)
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir     if (!value)
49*cdf0e10cSrcweir     {
50*cdf0e10cSrcweir         rtl_uString_new_WithLength( newStr, 16 );
51*cdf0e10cSrcweir         return;
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     rtl_uString_new_WithLength( newStr, count + 16 );
55*cdf0e10cSrcweir     (*newStr)->length = count;
56*cdf0e10cSrcweir     rtl_copyMemory( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
57*cdf0e10cSrcweir     return;
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir /*************************************************************************
61*cdf0e10cSrcweir  *  rtl_uStringbuffer_newFromStringBuffer
62*cdf0e10cSrcweir  */
63*cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
64*cdf0e10cSrcweir                                                           sal_Int32 capacity,
65*cdf0e10cSrcweir                                                           rtl_uString * oldStr )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     sal_Int32 newCapacity = capacity;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     if (newCapacity < oldStr->length)
70*cdf0e10cSrcweir         newCapacity = oldStr->length;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     rtl_uString_new_WithLength( newStr, newCapacity );
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     if (oldStr->length > 0) {
75*cdf0e10cSrcweir         (*newStr)->length = oldStr->length;
76*cdf0e10cSrcweir         rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir     return newCapacity;
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir /*************************************************************************
82*cdf0e10cSrcweir  *  rtl_uStringbuffer_ensureCapacity
83*cdf0e10cSrcweir  */
84*cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_ensureCapacity
85*cdf0e10cSrcweir     (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     if (minimumCapacity > *capacity)
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         rtl_uString * pTmp = *This;
90*cdf0e10cSrcweir         rtl_uString * pNew = NULL;
91*cdf0e10cSrcweir         *capacity = ((*This)->length + 1) * 2;
92*cdf0e10cSrcweir         if (minimumCapacity > *capacity)
93*cdf0e10cSrcweir             /* still lower, set to the minimum capacity */
94*cdf0e10cSrcweir             *capacity = minimumCapacity;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         rtl_uString_new_WithLength(&pNew, *capacity);
97*cdf0e10cSrcweir         pNew->length = (*This)->length;
98*cdf0e10cSrcweir         *This = pNew;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
101*cdf0e10cSrcweir         rtl_uString_release( pTmp );
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir /*************************************************************************
106*cdf0e10cSrcweir  *  rtl_uStringbuffer_insert
107*cdf0e10cSrcweir  */
108*cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
109*cdf0e10cSrcweir                                         sal_Int32 * capacity,
110*cdf0e10cSrcweir                                         sal_Int32 offset,
111*cdf0e10cSrcweir                                         const sal_Unicode * str,
112*cdf0e10cSrcweir                                         sal_Int32 len)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     sal_Int32 nOldLen;
115*cdf0e10cSrcweir     sal_Unicode * pBuf;
116*cdf0e10cSrcweir     sal_Int32 n;
117*cdf0e10cSrcweir     if( len != 0 )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         if (*capacity < (*This)->length + len)
120*cdf0e10cSrcweir             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         /*
123*cdf0e10cSrcweir         if( len == 1 )
124*cdf0e10cSrcweir             This->buffer
125*cdf0e10cSrcweir         */
126*cdf0e10cSrcweir         nOldLen = (*This)->length;
127*cdf0e10cSrcweir         pBuf = (*This)->buffer;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         /* copy the tail */
130*cdf0e10cSrcweir         n = (nOldLen - offset);
131*cdf0e10cSrcweir         if( n == 1 )
132*cdf0e10cSrcweir                             /* optimized for 1 character */
133*cdf0e10cSrcweir             pBuf[offset + len] = pBuf[offset];
134*cdf0e10cSrcweir         else if( n > 1 )
135*cdf0e10cSrcweir             rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         /* insert the new characters */
138*cdf0e10cSrcweir         if( len == 1 )
139*cdf0e10cSrcweir             /* optimized for 1 character */
140*cdf0e10cSrcweir             pBuf[offset] = *str;
141*cdf0e10cSrcweir         else if( len > 1 )
142*cdf0e10cSrcweir             rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Unicode) );
143*cdf0e10cSrcweir         (*This)->length = nOldLen + len;
144*cdf0e10cSrcweir         pBuf[ nOldLen + len ] = 0;
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir void rtl_uStringbuffer_insertUtf32(
149*cdf0e10cSrcweir     rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     sal_Unicode buf[2];
152*cdf0e10cSrcweir     sal_Int32 len;
153*cdf0e10cSrcweir     OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
154*cdf0e10cSrcweir     if (c <= 0xFFFF) {
155*cdf0e10cSrcweir         buf[0] = (sal_Unicode) c;
156*cdf0e10cSrcweir         len = 1;
157*cdf0e10cSrcweir     } else {
158*cdf0e10cSrcweir         c -= 0x10000;
159*cdf0e10cSrcweir         buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
160*cdf0e10cSrcweir         buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
161*cdf0e10cSrcweir         len = 2;
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir     rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir /*************************************************************************
167*cdf0e10cSrcweir  *  rtl_uStringbuffer_insert_ascii
168*cdf0e10cSrcweir  */
169*cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_insert_ascii(   /*inout*/rtl_uString ** This,
170*cdf0e10cSrcweir                                                 /*inout*/sal_Int32 * capacity,
171*cdf0e10cSrcweir                                                 sal_Int32 offset,
172*cdf0e10cSrcweir                                                 const sal_Char * str,
173*cdf0e10cSrcweir                                                 sal_Int32 len)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir     sal_Int32 nOldLen;
176*cdf0e10cSrcweir     sal_Unicode * pBuf;
177*cdf0e10cSrcweir     sal_Int32 n;
178*cdf0e10cSrcweir     if( len != 0 )
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         if (*capacity < (*This)->length + len)
181*cdf0e10cSrcweir             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         nOldLen = (*This)->length;
184*cdf0e10cSrcweir         pBuf = (*This)->buffer;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         /* copy the tail */
187*cdf0e10cSrcweir         n = (nOldLen - offset);
188*cdf0e10cSrcweir         if( n == 1 )
189*cdf0e10cSrcweir             /* optimized for 1 character */
190*cdf0e10cSrcweir             pBuf[offset + len] = pBuf[offset];
191*cdf0e10cSrcweir         else if( n > 1 )
192*cdf0e10cSrcweir             rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         /* insert the new characters */
195*cdf0e10cSrcweir         for( n = 0; n < len; n++ )
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir             /* Check ASCII range */
198*cdf0e10cSrcweir             OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir             pBuf[offset + n] = (sal_Unicode)*(str++);
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         (*This)->length = nOldLen + len;
204*cdf0e10cSrcweir         pBuf[ nOldLen + len ] = 0;
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 
209