1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <osl/interlck.h>
24 #include <rtl/ustrbuf.hxx>
27 void SAL_CALL
rtl_uStringbuffer_newFromStr_WithLength( rtl_uString
** newStr
,
28 const sal_Unicode
* value
,
33 rtl_uString_new_WithLength( newStr
, 16 );
37 rtl_uString_new_WithLength( newStr
, count
+ 16 );
38 (*newStr
)->length
= count
;
39 memcpy( (*newStr
)->buffer
, value
, count
* sizeof(sal_Unicode
));
40 RTL_LOG_STRING_NEW( *newStr
);
44 rtl_uString
* SAL_CALL
rtl_uStringBuffer_refReturn( rtl_uString
* pThis
)
46 RTL_LOG_STRING_NEW( pThis
);
47 rtl_uString_acquire( pThis
);
51 rtl_uString
* SAL_CALL
rtl_uStringBuffer_makeStringAndClear( rtl_uString
** ppThis
,
52 sal_Int32
*nCapacity
)
54 // avoid an un-necessary atomic ref/unref pair
55 rtl_uString
*pStr
= *ppThis
;
58 rtl_uString_new (ppThis
);
61 RTL_LOG_STRING_NEW( pStr
);
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 memcpy( (*newStr
)->buffer
, oldStr
->buffer
, oldStr
->length
* sizeof(sal_Unicode
));
81 RTL_LOG_STRING_NEW( *newStr
);
85 void SAL_CALL rtl_uStringbuffer_ensureCapacity
86 (rtl_uString
** This
, sal_Int32
* capacity
, sal_Int32 minimumCapacity
)
88 if (minimumCapacity
> *capacity
)
90 rtl_uString
* pTmp
= *This
;
91 rtl_uString
* pNew
= NULL
;
92 *capacity
= ((*This
)->length
+ 1) * 2;
93 if (minimumCapacity
> *capacity
)
94 /* still lower, set to the minimum capacity */
95 *capacity
= minimumCapacity
;
97 rtl_uString_new_WithLength(&pNew
, *capacity
);
98 pNew
->length
= (*This
)->length
;
101 memcpy( (*This
)->buffer
, pTmp
->buffer
, pTmp
->length
* sizeof(sal_Unicode
) );
103 RTL_LOG_STRING_NEW( pTmp
); // with accurate contents
104 rtl_uString_release( pTmp
);
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 memmove( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
137 /* insert the new characters */
139 /* optimized for 1 character */
142 memcpy( 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
)
154 OSL_ASSERT(c
<= 0x10FFFF && !(c
>= 0xD800 && c
<= 0xDFFF));
156 buf
[0] = (sal_Unicode
) c
;
160 buf
[0] = (sal_Unicode
) ((c
>> 10) | 0xD800);
161 buf
[1] = (sal_Unicode
) ((c
& 0x3FF) | 0xDC00);
164 rtl_uStringbuffer_insert(pThis
, capacity
, offset
, buf
, len
);
167 void SAL_CALL
rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString
** This
,
168 /*inout*/sal_Int32
* capacity
,
170 const sal_Char
* str
,
178 if (*capacity
< (*This
)->length
+ len
)
179 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
181 nOldLen
= (*This
)->length
;
182 pBuf
= (*This
)->buffer
;
185 n
= (nOldLen
- offset
);
187 /* optimized for 1 character */
188 pBuf
[offset
+ len
] = pBuf
[offset
];
190 memmove( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
192 /* insert the new characters */
193 for( n
= 0; n
< len
; n
++ )
195 /* Check ASCII range */
196 OSL_ENSURE( (*str
& 0x80) == 0, "Found ASCII char > 127");
198 pBuf
[offset
+ n
] = (sal_Unicode
)*(str
++);
201 (*This
)->length
= nOldLen
+ len
;
202 pBuf
[ nOldLen
+ len
] = 0;
206 /*************************************************************************
207 * rtl_uStringbuffer_remove
209 void SAL_CALL
rtl_uStringbuffer_remove( rtl_uString
** This
,
216 if (len
> (*This
)->length
- start
)
217 len
= (*This
)->length
- start
;
223 pBuf
= (*This
)->buffer
;
224 nTailLen
= (*This
)->length
- ( start
+ len
);
229 memmove(pBuf
+ start
, pBuf
+ start
+ len
, nTailLen
* sizeof(sal_Unicode
));
232 (*This
)->length
-=len
;
233 pBuf
[ (*This
)->length
] = 0;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */