update dev300-m57
[ooovba.git] / sal / rtl / source / ustrbuf.c
blob819deb4b40d6f4b5bcc8554b30f59850d48b5724
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ustrbuf.c,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/interlck.h>
33 #ifndef _RTL_STRING_HXX_
34 #include <rtl/ustrbuf.hxx>
35 #endif
36 #include <rtl/memory.h>
39 #include <rtl/alloc.h>
44 /*************************************************************************
45 * rtl_uStringbuffer_newFromStr_WithLength
47 void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr,
48 const sal_Unicode * value,
49 sal_Int32 count)
51 if (!value)
53 rtl_uString_new_WithLength( newStr, 16 );
54 return;
57 rtl_uString_new_WithLength( newStr, count + 16 );
58 (*newStr)->length = count;
59 rtl_copyMemory( (*newStr)->buffer, value, count * sizeof(sal_Unicode));
60 return;
63 /*************************************************************************
64 * rtl_uStringbuffer_newFromStringBuffer
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 rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode));
81 return newCapacity;
84 /*************************************************************************
85 * rtl_uStringbuffer_ensureCapacity
87 void SAL_CALL rtl_uStringbuffer_ensureCapacity
88 (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
90 if (minimumCapacity > *capacity)
92 rtl_uString * pTmp = *This;
93 rtl_uString * pNew = NULL;
94 *capacity = ((*This)->length + 1) * 2;
95 if (minimumCapacity > *capacity)
96 /* still lower, set to the minimum capacity */
97 *capacity = minimumCapacity;
99 rtl_uString_new_WithLength(&pNew, *capacity);
100 pNew->length = (*This)->length;
101 *This = pNew;
103 rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) );
104 rtl_uString_release( pTmp );
108 /*************************************************************************
109 * rtl_uStringbuffer_insert
111 void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
112 sal_Int32 * capacity,
113 sal_Int32 offset,
114 const sal_Unicode * str,
115 sal_Int32 len)
117 sal_Int32 nOldLen;
118 sal_Unicode * pBuf;
119 sal_Int32 n;
120 if( len != 0 )
122 if (*capacity < (*This)->length + len)
123 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
126 if( len == 1 )
127 This->buffer
129 nOldLen = (*This)->length;
130 pBuf = (*This)->buffer;
132 /* copy the tail */
133 n = (nOldLen - offset);
134 if( n == 1 )
135 /* optimized for 1 character */
136 pBuf[offset + len] = pBuf[offset];
137 else if( n > 1 )
138 rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
140 /* insert the new characters */
141 if( len == 1 )
142 /* optimized for 1 character */
143 pBuf[offset] = *str;
144 else if( len > 1 )
145 rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Unicode) );
146 (*This)->length = nOldLen + len;
147 pBuf[ nOldLen + len ] = 0;
151 void rtl_uStringbuffer_insertUtf32(
152 rtl_uString ** pThis, sal_Int32 * capacity, sal_Int32 offset, sal_uInt32 c)
154 sal_Unicode buf[2];
155 sal_Int32 len;
156 OSL_ASSERT(c <= 0x10FFFF && !(c >= 0xD800 && c <= 0xDFFF));
157 if (c <= 0xFFFF) {
158 buf[0] = (sal_Unicode) c;
159 len = 1;
160 } else {
161 c -= 0x10000;
162 buf[0] = (sal_Unicode) ((c >> 10) | 0xD800);
163 buf[1] = (sal_Unicode) ((c & 0x3FF) | 0xDC00);
164 len = 2;
166 rtl_uStringbuffer_insert(pThis, capacity, offset, buf, len);
169 /*************************************************************************
170 * rtl_uStringbuffer_insert_ascii
172 void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This,
173 /*inout*/sal_Int32 * capacity,
174 sal_Int32 offset,
175 const sal_Char * str,
176 sal_Int32 len)
178 sal_Int32 nOldLen;
179 sal_Unicode * pBuf;
180 sal_Int32 n;
181 if( len != 0 )
183 if (*capacity < (*This)->length + len)
184 rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
186 nOldLen = (*This)->length;
187 pBuf = (*This)->buffer;
189 /* copy the tail */
190 n = (nOldLen - offset);
191 if( n == 1 )
192 /* optimized for 1 character */
193 pBuf[offset + len] = pBuf[offset];
194 else if( n > 1 )
195 rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
197 /* insert the new characters */
198 for( n = 0; n < len; n++ )
200 /* Check ASCII range */
201 OSL_ENSURE( (*str & 0x80) == 0, "Found ASCII char > 127");
203 pBuf[offset + n] = (sal_Unicode)*(str++);
206 (*This)->length = nOldLen + len;
207 pBuf[ nOldLen + len ] = 0;