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>
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
,
50 rtl_uString_new_WithLength( newStr
, 16 );
54 rtl_uString_new_WithLength( newStr
, count
+ 16 );
55 (*newStr
)->length
= count
;
56 rtl_copyMemory( (*newStr
)->buffer
, value
, count
* sizeof(sal_Unicode
));
60 /*************************************************************************
61 * rtl_uStringbuffer_newFromStringBuffer
63 sal_Int32 SAL_CALL
rtl_uStringbuffer_newFromStringBuffer( rtl_uString
** newStr
,
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
));
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
;
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
,
111 const sal_Unicode
* str
,
119 if (*capacity
< (*This
)->length
+ len
)
120 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
126 nOldLen
= (*This
)->length
;
127 pBuf
= (*This
)->buffer
;
130 n
= (nOldLen
- offset
);
132 /* optimized for 1 character */
133 pBuf
[offset
+ len
] = pBuf
[offset
];
135 rtl_moveMemory( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
137 /* insert the new characters */
139 /* optimized for 1 character */
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
)
153 OSL_ASSERT(c
<= 0x10FFFF && !(c
>= 0xD800 && c
<= 0xDFFF));
155 buf
[0] = (sal_Unicode
) c
;
159 buf
[0] = (sal_Unicode
) ((c
>> 10) | 0xD800);
160 buf
[1] = (sal_Unicode
) ((c
& 0x3FF) | 0xDC00);
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
,
172 const sal_Char
* str
,
180 if (*capacity
< (*This
)->length
+ len
)
181 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
183 nOldLen
= (*This
)->length
;
184 pBuf
= (*This
)->buffer
;
187 n
= (nOldLen
- offset
);
189 /* optimized for 1 character */
190 pBuf
[offset
+ len
] = pBuf
[offset
];
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;