1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ustrbuf.c,v $
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>
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
,
53 rtl_uString_new_WithLength( newStr
, 16 );
57 rtl_uString_new_WithLength( newStr
, count
+ 16 );
58 (*newStr
)->length
= count
;
59 rtl_copyMemory( (*newStr
)->buffer
, value
, count
* sizeof(sal_Unicode
));
63 /*************************************************************************
64 * rtl_uStringbuffer_newFromStringBuffer
66 sal_Int32 SAL_CALL
rtl_uStringbuffer_newFromStringBuffer( rtl_uString
** newStr
,
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
));
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
;
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
,
114 const sal_Unicode
* str
,
122 if (*capacity
< (*This
)->length
+ len
)
123 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
129 nOldLen
= (*This
)->length
;
130 pBuf
= (*This
)->buffer
;
133 n
= (nOldLen
- offset
);
135 /* optimized for 1 character */
136 pBuf
[offset
+ len
] = pBuf
[offset
];
138 rtl_moveMemory( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
140 /* insert the new characters */
142 /* optimized for 1 character */
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
)
156 OSL_ASSERT(c
<= 0x10FFFF && !(c
>= 0xD800 && c
<= 0xDFFF));
158 buf
[0] = (sal_Unicode
) c
;
162 buf
[0] = (sal_Unicode
) ((c
>> 10) | 0xD800);
163 buf
[1] = (sal_Unicode
) ((c
& 0x3FF) | 0xDC00);
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
,
175 const sal_Char
* str
,
183 if (*capacity
< (*This
)->length
+ len
)
184 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
186 nOldLen
= (*This
)->length
;
187 pBuf
= (*This
)->buffer
;
190 n
= (nOldLen
- offset
);
192 /* optimized for 1 character */
193 pBuf
[offset
+ len
] = pBuf
[offset
];
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;