Bump version to 4.1-6
[LibreOffice.git] / sal / rtl / ustrbuf.cxx
blob9010a0ea0cddfa63461a82757d66143cf6832e1c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
22 #include <osl/interlck.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <strimp.hxx>
27 void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
28 const sal_Unicode * value,
29 sal_Int32 count)
31 if (!value)
33 rtl_uString_new_WithLength( newStr, 16 );
34 return;
37 rtl_uString_new_WithLength( newStr, count + 16 );
38 (*newStr)->length = count;
39 memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
40 RTL_LOG_STRING_NEW( *newStr );
41 return;
44 rtl_uString * SAL_CALL rtl_uStringBuffer_refReturn( rtl_uString * pThis )
46 RTL_LOG_STRING_NEW( pThis );
47 rtl_uString_acquire( pThis );
48 return pThis;
51 rtl_uString * SAL_CALL rtl_uStringBuffer_makeStringAndClear( rtl_uString ** ppThis,
52 sal_Int32 *nCapacity )
54 // avoid an un-necessary atomic ref/unref pair
55 rtl_uString *pStr = *ppThis;
56 *ppThis = NULL;
58 rtl_uString_new (ppThis);
59 *nCapacity = 0;
61 RTL_LOG_STRING_NEW( pStr );
63 return pStr;
66 sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
67 sal_Int32 capacity,
68 rtl_uString * oldStr )
70 sal_Int32 newCapacity = capacity;
72 if (newCapacity < oldStr->length)
73 newCapacity = oldStr->length;
75 rtl_uString_new_WithLength( newStr, newCapacity );
77 if (oldStr->length > 0) {
78 (*newStr)->length = oldStr->length;
79 memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
81 RTL_LOG_STRING_NEW( *newStr );
82 return newCapacity;
85 void SAL_CALL rtl_uStringbuffer_ensureCapacity
86 (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
88 if (minimumCapacity > *capacity)
90 rtl_uString * pTmp = *This;
91 rtl_uString * pNew = NULL;
92 *capacity = ((*This)->length + 1) * 2;
93 if (minimumCapacity > *capacity)
94 /* still lower, set to the minimum capacity */
95 *capacity = minimumCapacity;
97 rtl_uString_new_WithLength(&pNew, *capacity);
98 pNew->length = (*This)->length;
99 *This = pNew;
101 memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
103 RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
104 rtl_uString_release( pTmp );
108 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)
114 sal_Int32 nOldLen;
115 sal_Unicode * pBuf;
116 sal_Int32 n;
117 if( len != 0 )
119 if (*capacity < (*This)->length + len)
120 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
123 if( len == 1 )
124 This->buffer
126 nOldLen = (*This)->length;
127 pBuf = (*This)->buffer;
129 /* copy the tail */
130 n = (nOldLen - offset);
131 if( n == 1 )
132 /* optimized for 1 character */
133 pBuf[offset + len] = pBuf[offset];
134 else if( n > 1 )
135 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
137 /* insert the new characters */
138 if( len == 1 )
139 /* optimized for 1 character */
140 pBuf[offset] = *str;
141 else if( len > 1 )
142 memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
143 (*This)->length = nOldLen + len;
144 pBuf[ nOldLen + len ] = 0;
148 void rtl_uStringbuffer_insertUtf32(
149 rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
150 SAL_THROW_EXTERN_C()
152 sal_Unicode buf[2];
153 sal_Int32 len;
154 OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
155 if (c <= 0xFFFF) {
156 buf[0] = (sal_Unicode) c;
157 len = 1;
158 } else {
159 c -= 0x10000;
160 buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
161 buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
162 len = 2;
164 rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
167 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)
173 sal_Int32 nOldLen;
174 sal_Unicode * pBuf;
175 sal_Int32 n;
176 if( len != 0 )
178 if (*capacity < (*This)->length + len)
179 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
181 nOldLen = (*This)->length;
182 pBuf = (*This)->buffer;
184 /* copy the tail */
185 n = (nOldLen - offset);
186 if( n == 1 )
187 /* optimized for 1 character */
188 pBuf[offset + len] = pBuf[offset];
189 else if( n > 1 )
190 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
192 /* insert the new characters */
193 for( n = 0; n < len; n++ )
195 /* Check ASCII range */
196 OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
198 pBuf[offset + n] = (sal_Unicode)*(str++);
201 (*This)->length = nOldLen + len;
202 pBuf[ nOldLen + len ] = 0;
206 /*************************************************************************
207 * rtl_uStringbuffer_remove
209 void SAL_CALL rtl_uStringbuffer_remove( rtl_uString ** This,
210 sal_Int32 start,
211 sal_Int32 len )
213 sal_Int32 nTailLen;
214 sal_Unicode * pBuf;
216 if (len > (*This)->length - start)
217 len = (*This)->length - start;
219 //remove nothing
220 if (!len)
221 return;
223 pBuf = (*This)->buffer;
224 nTailLen = (*This)->length - ( start + len );
226 if (nTailLen)
228 /* move the tail */
229 memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Unicode));
232 (*This)->length-=len;
233 pBuf[ (*This)->length ] = 0;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */