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>
23 #include <rtl/strbuf.hxx>
25 /*************************************************************************
26 * rtl_stringbuffer_newFromStr_WithLength
28 void SAL_CALL
rtl_stringbuffer_newFromStr_WithLength( rtl_String
** newStr
,
29 const sal_Char
* value
,
36 rtl_string_new_WithLength( newStr
, 16 );
40 rtl_string_new_WithLength( newStr
, count
+ 16 );
41 (*newStr
)->length
= count
;
42 memcpy( (*newStr
)->buffer
, value
, count
);
46 /*************************************************************************
47 * rtl_stringbuffer_newFromStringBuffer
49 sal_Int32 SAL_CALL
rtl_stringbuffer_newFromStringBuffer( rtl_String
** newStr
,
55 assert(capacity
>= 0);
56 sal_Int32 newCapacity
= capacity
;
58 if (newCapacity
< oldStr
->length
)
59 newCapacity
= oldStr
->length
;
61 rtl_string_new_WithLength( newStr
, newCapacity
);
62 if (oldStr
->length
> 0) {
63 (*newStr
)->length
= oldStr
->length
;
64 memcpy( (*newStr
)->buffer
, oldStr
->buffer
, oldStr
->length
);
69 /*************************************************************************
70 * rtl_stringbuffer_ensureCapacity
72 void SAL_CALL rtl_stringbuffer_ensureCapacity
73 (rtl_String
** This
, sal_Int32
* capacity
, sal_Int32 minimumCapacity
)
76 // assert(capacity && *capacity >= 0);
77 // assert(minimumCapacity >= 0);
78 if (minimumCapacity
> *capacity
)
80 rtl_String
* pTmp
= *This
;
81 rtl_String
* pNew
= NULL
;
82 *capacity
= ((*This
)->length
+ 1) * 2;
83 if (minimumCapacity
> *capacity
)
84 /* still lower, set to the minimum capacity */
85 *capacity
= minimumCapacity
;
87 rtl_string_new_WithLength(&pNew
, *capacity
);
88 pNew
->length
= (*This
)->length
;
91 memcpy( (*This
)->buffer
, pTmp
->buffer
, pTmp
->length
);
92 rtl_string_release( pTmp
);
96 /*************************************************************************
97 * rtl_stringbuffer_insert
99 void SAL_CALL
rtl_stringbuffer_insert( rtl_String
** This
,
100 sal_Int32
* capacity
,
102 const sal_Char
* str
,
106 assert(capacity
&& *capacity
>= 0);
107 assert(offset
>= 0 && offset
<= (**This
).length
);
114 if (*capacity
< (*This
)->length
+ len
)
115 rtl_stringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
117 nOldLen
= (*This
)->length
;
118 pBuf
= (*This
)->buffer
;
121 n
= (nOldLen
- offset
);
123 /* optimized for 1 character */
124 pBuf
[offset
+ len
] = pBuf
[offset
];
126 memmove( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Char
) );
128 /* insert the new characters */
132 /* optimized for 1 character */
135 memcpy( pBuf
+ offset
, str
, len
* sizeof(sal_Char
) );
137 (*This
)->length
= nOldLen
+ len
;
138 pBuf
[ nOldLen
+ len
] = 0;
142 /*************************************************************************
143 * rtl_stringbuffer_remove
145 void SAL_CALL
rtl_stringbuffer_remove( rtl_String
** This
,
150 assert(start
>= 0 && start
<= (**This
).length
);
155 if (len
> (*This
)->length
- start
)
156 len
= (*This
)->length
- start
;
162 pBuf
= (*This
)->buffer
;
163 nTailLen
= (*This
)->length
- ( start
+ len
);
168 memmove(pBuf
+ start
, pBuf
+ start
+ len
, nTailLen
* sizeof(sal_Char
));
171 (*This
)->length
-=len
;
172 pBuf
[ (*This
)->length
] = 0;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */