merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / rtl / source / ustrbuf.c
blobd9fdb9983fb156cf2f8fa44b78c70cb639522e8b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <osl/interlck.h>
30 #ifndef _RTL_STRING_HXX_
31 #include <rtl/ustrbuf.hxx>
32 #endif
33 #include <rtl/memory.h>
36 #include <rtl/alloc.h>
41 /*************************************************************************
42 * rtl_uStringbuffer_newFromStr_WithLength
44 void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
45 const sal_Unicode * value,
46 sal_Int32 count)
48 if (!value)
50 rtl_uString_new_WithLength( newStr, 16 );
51 return;
54 rtl_uString_new_WithLength( newStr, count + 16 );
55 (*newStr)->length = count;
56 rtl_copyMemory( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
57 return;
60 /*************************************************************************
61 * rtl_uStringbuffer_newFromStringBuffer
63 sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr,
64 sal_Int32 capacity,
65 rtl_uString * oldStr )
67 sal_Int32 newCapacity = capacity;
69 if (newCapacity < oldStr->length)
70 newCapacity = oldStr->length;
72 rtl_uString_new_WithLength( newStr, newCapacity );
74 if (oldStr->length > 0) {
75 (*newStr)->length = oldStr->length;
76 rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
78 return newCapacity;
81 /*************************************************************************
82 * rtl_uStringbuffer_ensureCapacity
84 void SAL_CALL rtl_uStringbuffer_ensureCapacity
85 (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
87 if (minimumCapacity > *capacity)
89 rtl_uString * pTmp = *This;
90 rtl_uString * pNew = NULL;
91 *capacity = ((*This)->length + 1) * 2;
92 if (minimumCapacity > *capacity)
93 /* still lower, set to the minimum capacity */
94 *capacity = minimumCapacity;
96 rtl_uString_new_WithLength(&pNew, *capacity);
97 pNew->length = (*This)->length;
98 *This = pNew;
100 rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
101 rtl_uString_release( pTmp );
105 /*************************************************************************
106 * rtl_uStringbuffer_insert
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 rtl_moveMemory( 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 rtl_copyMemory( 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)
151 sal_Unicode buf[2];
152 sal_Int32 len;
153 OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
154 if (c <= 0xFFFF) {
155 buf[0] = (sal_Unicode) c;
156 len = 1;
157 } else {
158 c -= 0x10000;
159 buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
160 buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
161 len = 2;
163 rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
166 /*************************************************************************
167 * rtl_uStringbuffer_insert_ascii
169 void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This,
170 /*inout*/sal_Int32 * capacity,
171 sal_Int32 offset,
172 const sal_Char * str,
173 sal_Int32 len)
175 sal_Int32 nOldLen;
176 sal_Unicode * pBuf;
177 sal_Int32 n;
178 if( len != 0 )
180 if (*capacity < (*This)->length + len)
181 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
183 nOldLen = (*This)->length;
184 pBuf = (*This)->buffer;
186 /* copy the tail */
187 n = (nOldLen - offset);
188 if( n == 1 )
189 /* optimized for 1 character */
190 pBuf[offset + len] = pBuf[offset];
191 else if( n > 1 )
192 rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
194 /* insert the new characters */
195 for( n = 0; n < len; n++ )
197 /* Check ASCII range */
198 OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
200 pBuf[offset + n] = (sal_Unicode)*(str++);
203 (*This)->length = nOldLen + len;
204 pBuf[ nOldLen + len ] = 0;