xref: /AOO41X/main/sal/rtl/source/ustrbuf.c (revision 647f063d49501903f1667b75f5634541fc603283)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <osl/interlck.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifndef _RTL_STRING_HXX_
27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <rtl/memory.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /*
32cdf0e10cSrcweir #include <rtl/alloc.h>
33cdf0e10cSrcweir */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /*************************************************************************
38cdf0e10cSrcweir  *  rtl_uStringbuffer_newFromStr_WithLength
39cdf0e10cSrcweir  */
rtl_uStringbuffer_newFromStr_WithLength(rtl_uString ** newStr,const sal_Unicode * value,sal_Int32 count)40cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
41cdf0e10cSrcweir                                                        const sal_Unicode * value,
42cdf0e10cSrcweir                                                        sal_Int32 count)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     if (!value)
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         rtl_uString_new_WithLength( newStr, 16 );
47cdf0e10cSrcweir         return;
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     rtl_uString_new_WithLength( newStr, count + 16 );
51cdf0e10cSrcweir     (*newStr)->length = count;
52cdf0e10cSrcweir     rtl_copyMemory( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
53cdf0e10cSrcweir     return;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /*************************************************************************
57cdf0e10cSrcweir  *  rtl_uStringbuffer_newFromStringBuffer
58cdf0e10cSrcweir  */
rtl_uStringbuffer_newFromStringBuffer(rtl_uString ** newStr,sal_Int32 capacity,rtl_uString * oldStr)59cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
60cdf0e10cSrcweir                                                           sal_Int32 capacity,
61cdf0e10cSrcweir                                                           rtl_uString * oldStr )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     sal_Int32 newCapacity = capacity;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     if (newCapacity < oldStr->length)
66cdf0e10cSrcweir         newCapacity = oldStr->length;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     rtl_uString_new_WithLength( newStr, newCapacity );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     if (oldStr->length > 0) {
71cdf0e10cSrcweir         (*newStr)->length = oldStr->length;
72cdf0e10cSrcweir         rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir     return newCapacity;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir /*************************************************************************
78cdf0e10cSrcweir  *  rtl_uStringbuffer_ensureCapacity
79cdf0e10cSrcweir  */
rtl_uStringbuffer_ensureCapacity(rtl_uString ** This,sal_Int32 * capacity,sal_Int32 minimumCapacity)80cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_ensureCapacity
81cdf0e10cSrcweir     (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     if (minimumCapacity > *capacity)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         rtl_uString * pTmp = *This;
86cdf0e10cSrcweir         rtl_uString * pNew = NULL;
87cdf0e10cSrcweir         *capacity = ((*This)->length + 1) * 2;
88cdf0e10cSrcweir         if (minimumCapacity > *capacity)
89cdf0e10cSrcweir             /* still lower, set to the minimum capacity */
90cdf0e10cSrcweir             *capacity = minimumCapacity;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         rtl_uString_new_WithLength(&pNew, *capacity);
93cdf0e10cSrcweir         pNew->length = (*This)->length;
94cdf0e10cSrcweir         *This = pNew;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
97cdf0e10cSrcweir         rtl_uString_release( pTmp );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir /*************************************************************************
102cdf0e10cSrcweir  *  rtl_uStringbuffer_insert
103cdf0e10cSrcweir  */
rtl_uStringbuffer_insert(rtl_uString ** This,sal_Int32 * capacity,sal_Int32 offset,const sal_Unicode * str,sal_Int32 len)104cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
105cdf0e10cSrcweir                                         sal_Int32 * capacity,
106cdf0e10cSrcweir                                         sal_Int32 offset,
107cdf0e10cSrcweir                                         const sal_Unicode * str,
108cdf0e10cSrcweir                                         sal_Int32 len)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     sal_Int32 nOldLen;
111cdf0e10cSrcweir     sal_Unicode * pBuf;
112cdf0e10cSrcweir     sal_Int32 n;
113cdf0e10cSrcweir     if( len != 0 )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         if (*capacity < (*This)->length + len)
116cdf0e10cSrcweir             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         /*
119cdf0e10cSrcweir         if( len == 1 )
120cdf0e10cSrcweir             This->buffer
121cdf0e10cSrcweir         */
122cdf0e10cSrcweir         nOldLen = (*This)->length;
123cdf0e10cSrcweir         pBuf = (*This)->buffer;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         /* copy the tail */
126cdf0e10cSrcweir         n = (nOldLen - offset);
127cdf0e10cSrcweir         if( n == 1 )
128cdf0e10cSrcweir                             /* optimized for 1 character */
129cdf0e10cSrcweir             pBuf[offset + len] = pBuf[offset];
130cdf0e10cSrcweir         else if( n > 1 )
131cdf0e10cSrcweir             rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         /* insert the new characters */
134cdf0e10cSrcweir         if( len == 1 )
135cdf0e10cSrcweir             /* optimized for 1 character */
136cdf0e10cSrcweir             pBuf[offset] = *str;
137cdf0e10cSrcweir         else if( len > 1 )
138cdf0e10cSrcweir             rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Unicode) );
139cdf0e10cSrcweir         (*This)->length = nOldLen + len;
140cdf0e10cSrcweir         pBuf[ nOldLen + len ] = 0;
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
rtl_uStringbuffer_insertUtf32(rtl_uString ** pThis,sal_Int32 * capacity,sal_Int32 offset,sal_uInt32 c)144cdf0e10cSrcweir void rtl_uStringbuffer_insertUtf32(
145cdf0e10cSrcweir     rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir     sal_Unicode buf[2];
148cdf0e10cSrcweir     sal_Int32 len;
149cdf0e10cSrcweir     OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
150cdf0e10cSrcweir     if (c <= 0xFFFF) {
151cdf0e10cSrcweir         buf[0] = (sal_Unicode) c;
152cdf0e10cSrcweir         len = 1;
153cdf0e10cSrcweir     } else {
154cdf0e10cSrcweir         c -= 0x10000;
155cdf0e10cSrcweir         buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
156cdf0e10cSrcweir         buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
157cdf0e10cSrcweir         len = 2;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir     rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir /*************************************************************************
163cdf0e10cSrcweir  *  rtl_uStringbuffer_insert_ascii
164cdf0e10cSrcweir  */
rtl_uStringbuffer_insert_ascii(rtl_uString ** This,sal_Int32 * capacity,sal_Int32 offset,const sal_Char * str,sal_Int32 len)165cdf0e10cSrcweir void SAL_CALL rtl_uStringbuffer_insert_ascii(   /*inout*/rtl_uString ** This,
166cdf0e10cSrcweir                                                 /*inout*/sal_Int32 * capacity,
167cdf0e10cSrcweir                                                 sal_Int32 offset,
168cdf0e10cSrcweir                                                 const sal_Char * str,
169cdf0e10cSrcweir                                                 sal_Int32 len)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     sal_Int32 nOldLen;
172cdf0e10cSrcweir     sal_Unicode * pBuf;
173cdf0e10cSrcweir     sal_Int32 n;
174cdf0e10cSrcweir     if( len != 0 )
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         if (*capacity < (*This)->length + len)
177cdf0e10cSrcweir             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         nOldLen = (*This)->length;
180cdf0e10cSrcweir         pBuf = (*This)->buffer;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         /* copy the tail */
183cdf0e10cSrcweir         n = (nOldLen - offset);
184cdf0e10cSrcweir         if( n == 1 )
185cdf0e10cSrcweir             /* optimized for 1 character */
186cdf0e10cSrcweir             pBuf[offset + len] = pBuf[offset];
187cdf0e10cSrcweir         else if( n > 1 )
188cdf0e10cSrcweir             rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         /* insert the new characters */
191cdf0e10cSrcweir         for( n = 0; n < len; n++ )
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             /* Check ASCII range */
194cdf0e10cSrcweir             OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
195cdf0e10cSrcweir 
196cdf0e10cSrcweir             pBuf[offset + n] = (sal_Unicode)*(str++);
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir         (*This)->length = nOldLen + len;
200cdf0e10cSrcweir         pBuf[ nOldLen + len ] = 0;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 
205