Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sal / rtl / ustrbuf.cxx
blob30f4efe39c7fd96b06df1aaa728b888609e595a4
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 #if USE_SDT_PROBES
28 #define RTL_LOG_STRING_BITS 16
29 #endif
31 void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
32 const sal_Unicode * value,
33 sal_Int32 count)
35 if (!value)
37 rtl_uString_new_WithLength( newStr, 16 );
38 return;
41 rtl_uString_new_WithLength( newStr, count + 16 );
42 (*newStr)->length = count;
43 memcpy( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
44 RTL_LOG_STRING_NEW( *newStr );
45 return;
48 rtl_uString * SAL_CALL rtl_uStringBuffer_refReturn( rtl_uString * pThis )
50 RTL_LOG_STRING_NEW( pThis );
51 rtl_uString_acquire( pThis );
52 return pThis;
55 rtl_uString * SAL_CALL rtl_uStringBuffer_makeStringAndClear( rtl_uString ** ppThis,
56 sal_Int32 *nCapacity )
58 // avoid an un-necessary atomic ref/unref pair
59 rtl_uString *pStr = *ppThis;
60 *ppThis = NULL;
62 rtl_uString_new (ppThis);
63 *nCapacity = 0;
65 RTL_LOG_STRING_NEW( pStr );
67 return pStr;
70 sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
71 sal_Int32 capacity,
72 rtl_uString * oldStr )
74 sal_Int32 newCapacity = capacity;
76 if (newCapacity < oldStr->length)
77 newCapacity = oldStr->length;
79 rtl_uString_new_WithLength( newStr, newCapacity );
81 if (oldStr->length > 0) {
82 (*newStr)->length = oldStr->length;
83 memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
85 RTL_LOG_STRING_NEW( *newStr );
86 return newCapacity;
89 void SAL_CALL rtl_uStringbuffer_ensureCapacity
90 (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
92 if (minimumCapacity > *capacity)
94 rtl_uString * pTmp = *This;
95 rtl_uString * pNew = NULL;
96 *capacity = ((*This)->length + 1) * 2;
97 if (minimumCapacity > *capacity)
98 /* still lower, set to the minimum capacity */
99 *capacity = minimumCapacity;
101 rtl_uString_new_WithLength(&pNew, *capacity);
102 pNew->length = (*This)->length;
103 *This = pNew;
105 memcpy( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
107 RTL_LOG_STRING_NEW( pTmp ); // with accurate contents
108 rtl_uString_release( pTmp );
112 void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
113 sal_Int32 * capacity,
114 sal_Int32 offset,
115 const sal_Unicode * str,
116 sal_Int32 len)
118 sal_Int32 nOldLen;
119 sal_Unicode * pBuf;
120 sal_Int32 n;
121 if( len != 0 )
123 if (*capacity < (*This)->length + len)
124 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
127 if( len == 1 )
128 This->buffer
130 nOldLen = (*This)->length;
131 pBuf = (*This)->buffer;
133 /* copy the tail */
134 n = (nOldLen - offset);
135 if( n == 1 )
136 /* optimized for 1 character */
137 pBuf[offset + len] = pBuf[offset];
138 else if( n > 1 )
139 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
141 /* insert the new characters */
142 if( len == 1 )
143 /* optimized for 1 character */
144 pBuf[offset] = *str;
145 else if( len > 1 )
146 memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
147 (*This)->length = nOldLen + len;
148 pBuf[ nOldLen + len ] = 0;
152 void rtl_uStringbuffer_insertUtf32(
153 rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
154 SAL_THROW_EXTERN_C()
156 sal_Unicode buf[2];
157 sal_Int32 len;
158 OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
159 if (c <= 0xFFFF) {
160 buf[0] = (sal_Unicode) c;
161 len = 1;
162 } else {
163 c -= 0x10000;
164 buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
165 buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
166 len = 2;
168 rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
171 void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This,
172 /*inout*/sal_Int32 * capacity,
173 sal_Int32 offset,
174 const sal_Char * str,
175 sal_Int32 len)
177 sal_Int32 nOldLen;
178 sal_Unicode * pBuf;
179 sal_Int32 n;
180 if( len != 0 )
182 if (*capacity < (*This)->length + len)
183 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
185 nOldLen = (*This)->length;
186 pBuf = (*This)->buffer;
188 /* copy the tail */
189 n = (nOldLen - offset);
190 if( n == 1 )
191 /* optimized for 1 character */
192 pBuf[offset + len] = pBuf[offset];
193 else if( n > 1 )
194 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
196 /* insert the new characters */
197 for( n = 0; n < len; n++ )
199 /* Check ASCII range */
200 OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
202 pBuf[offset + n] = (sal_Unicode)*(str++);
205 (*This)->length = nOldLen + len;
206 pBuf[ nOldLen + len ] = 0;
210 /*************************************************************************
211 * rtl_uStringbuffer_remove
213 void SAL_CALL rtl_uStringbuffer_remove( rtl_uString ** This,
214 sal_Int32 start,
215 sal_Int32 len )
217 sal_Int32 nTailLen;
218 sal_Unicode * pBuf;
220 if (len > (*This)->length - start)
221 len = (*This)->length - start;
223 //remove nothing
224 if (!len)
225 return;
227 pBuf = (*This)->buffer;
228 nTailLen = (*This)->length - ( start + len );
230 if (nTailLen)
232 /* move the tail */
233 memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Unicode));
236 (*This)->length-=len;
237 pBuf[ (*This)->length ] = 0;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */