Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include <osl/interlck.h>
23 :
24 : #include <rtl/ustrbuf.hxx>
25 : #include <strimp.hxx>
26 :
27 727819 : void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
28 : const sal_Unicode * value,
29 : sal_Int32 count)
30 : {
31 727819 : if (!value)
32 : {
33 0 : rtl_uString_new_WithLength( newStr, 16 );
34 0 : return;
35 : }
36 :
37 727819 : rtl_uString_new_WithLength( newStr, count + 16 );
38 727819 : (*newStr)->length = count;
39 727819 : memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
40 : RTL_LOG_STRING_NEW( *newStr );
41 727819 : return;
42 : }
43 :
44 1645600 : rtl_uString * SAL_CALL rtl_uStringBuffer_refReturn( rtl_uString * pThis )
45 : {
46 : RTL_LOG_STRING_NEW( pThis );
47 1645600 : rtl_uString_acquire( pThis );
48 1645600 : return pThis;
49 : }
50 :
51 1724101 : rtl_uString * SAL_CALL rtl_uStringBuffer_makeStringAndClear( rtl_uString ** ppThis,
52 : sal_Int32 *nCapacity )
53 : {
54 : // avoid an un-necessary atomic ref/unref pair
55 1724101 : rtl_uString *pStr = *ppThis;
56 1724101 : *ppThis = NULL;
57 :
58 1724101 : rtl_uString_new (ppThis);
59 1724101 : *nCapacity = 0;
60 :
61 : RTL_LOG_STRING_NEW( pStr );
62 :
63 1724101 : return pStr;
64 : }
65 :
66 84001 : sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
67 : sal_Int32 capacity,
68 : rtl_uString * oldStr )
69 : {
70 84001 : sal_Int32 newCapacity = capacity;
71 :
72 84001 : if (newCapacity < oldStr->length)
73 0 : newCapacity = oldStr->length;
74 :
75 84001 : rtl_uString_new_WithLength( newStr, newCapacity );
76 :
77 84001 : if (oldStr->length > 0) {
78 60085 : (*newStr)->length = oldStr->length;
79 60085 : memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
80 : }
81 : RTL_LOG_STRING_NEW( *newStr );
82 84001 : return newCapacity;
83 : }
84 :
85 509121 : void SAL_CALL rtl_uStringbuffer_ensureCapacity
86 : (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
87 : {
88 509121 : if (minimumCapacity > *capacity)
89 : {
90 508901 : rtl_uString * pTmp = *This;
91 508901 : rtl_uString * pNew = NULL;
92 508901 : *capacity = ((*This)->length + 1) * 2;
93 508901 : if (minimumCapacity > *capacity)
94 : /* still lower, set to the minimum capacity */
95 240664 : *capacity = minimumCapacity;
96 :
97 508901 : rtl_uString_new_WithLength(&pNew, *capacity);
98 508901 : pNew->length = (*This)->length;
99 508901 : *This = pNew;
100 :
101 508901 : memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
102 :
103 : RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
104 508901 : rtl_uString_release( pTmp );
105 : }
106 509121 : }
107 :
108 64603678 : void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
109 : sal_Int32 * capacity,
110 : sal_Int32 offset,
111 : const sal_Unicode * str,
112 : sal_Int32 len)
113 : {
114 : sal_Int32 nOldLen;
115 : sal_Unicode * pBuf;
116 : sal_Int32 n;
117 64603678 : if( len != 0 )
118 : {
119 64573079 : if (*capacity < (*This)->length + len)
120 451372 : rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
121 :
122 : /*
123 : if( len == 1 )
124 : This->buffer
125 : */
126 64573079 : nOldLen = (*This)->length;
127 64573079 : pBuf = (*This)->buffer;
128 :
129 : /* copy the tail */
130 64573079 : n = (nOldLen - offset);
131 64573079 : if( n == 1 )
132 : /* optimized for 1 character */
133 150 : pBuf[offset + len] = pBuf[offset];
134 64572929 : else if( n > 1 )
135 7506 : memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
136 :
137 : /* insert the new characters */
138 64573079 : if( len == 1 )
139 : /* optimized for 1 character */
140 63378331 : pBuf[offset] = *str;
141 1194748 : else if( len > 1 )
142 1194748 : memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
143 64573079 : (*This)->length = nOldLen + len;
144 64573079 : pBuf[ nOldLen + len ] = 0;
145 : }
146 64603678 : }
147 :
148 201971 : void rtl_uStringbuffer_insertUtf32(
149 : rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
150 : SAL_THROW_EXTERN_C()
151 : {
152 : sal_Unicode buf[2];
153 : sal_Int32 len;
154 : OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
155 201971 : if (c <= 0xFFFF) {
156 201969 : buf[0] = (sal_Unicode) c;
157 201969 : len = 1;
158 : } else {
159 2 : c -= 0x10000;
160 2 : buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
161 2 : buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
162 2 : len = 2;
163 : }
164 201971 : rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
165 201971 : }
166 :
167 294472 : void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This,
168 : /*inout*/sal_Int32 * capacity,
169 : sal_Int32 offset,
170 : const sal_Char * str,
171 : sal_Int32 len)
172 : {
173 : sal_Int32 nOldLen;
174 : sal_Unicode * pBuf;
175 : sal_Int32 n;
176 294472 : if( len != 0 )
177 : {
178 294471 : if (*capacity < (*This)->length + len)
179 8709 : rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
180 :
181 294471 : nOldLen = (*This)->length;
182 294471 : pBuf = (*This)->buffer;
183 :
184 : /* copy the tail */
185 294471 : n = (nOldLen - offset);
186 294471 : if( n == 1 )
187 : /* optimized for 1 character */
188 13 : pBuf[offset + len] = pBuf[offset];
189 294458 : else if( n > 1 )
190 6520 : memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
191 :
192 : /* insert the new characters */
193 1643206 : for( n = 0; n < len; n++ )
194 : {
195 : /* Check ASCII range */
196 : OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
197 :
198 1348735 : pBuf[offset + n] = (sal_Unicode)*(str++);
199 : }
200 :
201 294471 : (*This)->length = nOldLen + len;
202 294471 : pBuf[ nOldLen + len ] = 0;
203 : }
204 294472 : }
205 :
206 : /*************************************************************************
207 : * rtl_uStringbuffer_remove
208 : */
209 40207 : void SAL_CALL rtl_uStringbuffer_remove( rtl_uString ** This,
210 : sal_Int32 start,
211 : sal_Int32 len )
212 : {
213 : sal_Int32 nTailLen;
214 : sal_Unicode * pBuf;
215 :
216 40207 : if (len > (*This)->length - start)
217 0 : len = (*This)->length - start;
218 :
219 : //remove nothing
220 40207 : if (!len)
221 40207 : return;
222 :
223 40207 : pBuf = (*This)->buffer;
224 40207 : nTailLen = (*This)->length - ( start + len );
225 :
226 40207 : if (nTailLen)
227 : {
228 : /* move the tail */
229 6699 : memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Unicode));
230 : }
231 :
232 40207 : (*This)->length-=len;
233 40207 : pBuf[ (*This)->length ] = 0;
234 : }
235 :
236 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|