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>
28 #define RTL_LOG_STRING_BITS 16
31 void SAL_CALL
rtl_uStringbuffer_newFromStr_WithLength( rtl_uString
** newStr
,
32 const sal_Unicode
* value
,
37 rtl_uString_new_WithLength( newStr
, 16 );
41 rtl_uString_new_WithLength( newStr
, count
+ 16 );
42 (*newStr
)->length
= count
;
43 memcpy( (*newStr
)->buffer
, value
, count
* sizeof(sal_Unicode
));
44 RTL_LOG_STRING_NEW( *newStr
);
48 rtl_uString
* SAL_CALL
rtl_uStringBuffer_refReturn( rtl_uString
* pThis
)
50 RTL_LOG_STRING_NEW( pThis
);
51 rtl_uString_acquire( pThis
);
55 rtl_uString
* SAL_CALL
rtl_uStringBuffer_makeStringAndClear( rtl_uString
** ppThis
,
56 sal_Int32
*nCapacity
)
58 // avoid an un-necessary atomic ref/unref pair
59 rtl_uString
*pStr
= *ppThis
;
62 rtl_uString_new (ppThis
);
65 RTL_LOG_STRING_NEW( pStr
);
70 sal_Int32 SAL_CALL
rtl_uStringbuffer_newFromStringBuffer( rtl_uString
** newStr
,
72 rtl_uString
* oldStr
)
74 sal_Int32 newCapacity
= capacity
;
76 if (newCapacity
< oldStr
->length
)
77 newCapacity
= oldStr
->length
;
79 rtl_uString_new_WithLength( newStr
, newCapacity
);
81 if (oldStr
->length
> 0) {
82 (*newStr
)->length
= oldStr
->length
;
83 memcpy( (*newStr
)->buffer
, oldStr
->buffer
, oldStr
->length
* sizeof(sal_Unicode
));
85 RTL_LOG_STRING_NEW( *newStr
);
89 void SAL_CALL rtl_uStringbuffer_ensureCapacity
90 (rtl_uString
** This
, sal_Int32
* capacity
, sal_Int32 minimumCapacity
)
92 if (minimumCapacity
> *capacity
)
94 rtl_uString
* pTmp
= *This
;
95 rtl_uString
* pNew
= NULL
;
96 *capacity
= ((*This
)->length
+ 1) * 2;
97 if (minimumCapacity
> *capacity
)
98 /* still lower, set to the minimum capacity */
99 *capacity
= minimumCapacity
;
101 rtl_uString_new_WithLength(&pNew
, *capacity
);
102 pNew
->length
= (*This
)->length
;
105 memcpy( (*This
)->buffer
, pTmp
->buffer
, pTmp
->length
* sizeof(sal_Unicode
) );
107 RTL_LOG_STRING_NEW( pTmp
); // with accurate contents
108 rtl_uString_release( pTmp
);
112 void SAL_CALL
rtl_uStringbuffer_insert( rtl_uString
** This
,
113 sal_Int32
* capacity
,
115 const sal_Unicode
* str
,
123 if (*capacity
< (*This
)->length
+ len
)
124 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
130 nOldLen
= (*This
)->length
;
131 pBuf
= (*This
)->buffer
;
134 n
= (nOldLen
- offset
);
136 /* optimized for 1 character */
137 pBuf
[offset
+ len
] = pBuf
[offset
];
139 memmove( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
141 /* insert the new characters */
143 /* optimized for 1 character */
146 memcpy( pBuf
+ offset
, str
, len
* sizeof(sal_Unicode
) );
147 (*This
)->length
= nOldLen
+ len
;
148 pBuf
[ nOldLen
+ len
] = 0;
152 void rtl_uStringbuffer_insertUtf32(
153 rtl_uString
** pThis
, sal_Int32
* capacity
, sal_Int32 offset
, sal_uInt32 c
)
158 OSL_ASSERT(c
<= 0x10FFFF && !(c
>= 0xD800 && c
<= 0xDFFF));
160 buf
[0] = (sal_Unicode
) c
;
164 buf
[0] = (sal_Unicode
) ((c
>> 10) | 0xD800);
165 buf
[1] = (sal_Unicode
) ((c
& 0x3FF) | 0xDC00);
168 rtl_uStringbuffer_insert(pThis
, capacity
, offset
, buf
, len
);
171 void SAL_CALL
rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString
** This
,
172 /*inout*/sal_Int32
* capacity
,
174 const sal_Char
* str
,
182 if (*capacity
< (*This
)->length
+ len
)
183 rtl_uStringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
185 nOldLen
= (*This
)->length
;
186 pBuf
= (*This
)->buffer
;
189 n
= (nOldLen
- offset
);
191 /* optimized for 1 character */
192 pBuf
[offset
+ len
] = pBuf
[offset
];
194 memmove( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Unicode
) );
196 /* insert the new characters */
197 for( n
= 0; n
< len
; n
++ )
199 /* Check ASCII range */
200 OSL_ENSURE( (*str
& 0x80) == 0, "Found ASCII char > 127");
202 pBuf
[offset
+ n
] = (sal_Unicode
)*(str
++);
205 (*This
)->length
= nOldLen
+ len
;
206 pBuf
[ nOldLen
+ len
] = 0;
210 /*************************************************************************
211 * rtl_uStringbuffer_remove
213 void SAL_CALL
rtl_uStringbuffer_remove( rtl_uString
** This
,
220 if (len
> (*This
)->length
- start
)
221 len
= (*This
)->length
- start
;
227 pBuf
= (*This
)->buffer
;
228 nTailLen
= (*This
)->length
- ( start
+ len
);
233 memmove(pBuf
+ start
, pBuf
+ start
+ len
, nTailLen
* sizeof(sal_Unicode
));
236 (*This
)->length
-=len
;
237 pBuf
[ (*This
)->length
] = 0;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */