Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include <osl/interlck.h>
23 : #include <rtl/strbuf.hxx>
24 :
25 : /*************************************************************************
26 : * rtl_stringbuffer_newFromStr_WithLength
27 : */
28 55230 : void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
29 : const sal_Char * value,
30 : sal_Int32 count )
31 : {
32 : assert(newStr);
33 : assert(count >= 0);
34 55230 : if (!value)
35 : {
36 0 : rtl_string_new_WithLength( newStr, 16 );
37 0 : return;
38 : }
39 :
40 55230 : rtl_string_new_WithLength( newStr, count + 16 );
41 55230 : (*newStr)->length = count;
42 55230 : memcpy( (*newStr)->buffer, value, count );
43 55230 : return;
44 : }
45 :
46 : /*************************************************************************
47 : * rtl_stringbuffer_newFromStringBuffer
48 : */
49 14280 : sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
50 : sal_Int32 capacity,
51 : rtl_String * oldStr )
52 : {
53 : assert(newStr);
54 : assert(oldStr);
55 : assert(capacity >= 0);
56 14280 : sal_Int32 newCapacity = capacity;
57 :
58 14280 : if (newCapacity < oldStr->length)
59 0 : newCapacity = oldStr->length;
60 :
61 14280 : rtl_string_new_WithLength( newStr, newCapacity );
62 14280 : if (oldStr->length > 0) {
63 12602 : (*newStr)->length = oldStr->length;
64 12602 : memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length );
65 : }
66 14280 : return newCapacity;
67 : }
68 :
69 : /*************************************************************************
70 : * rtl_stringbuffer_ensureCapacity
71 : */
72 804074 : void SAL_CALL rtl_stringbuffer_ensureCapacity
73 : (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
74 : {
75 : assert(This);
76 : // assert(capacity && *capacity >= 0);
77 : // assert(minimumCapacity >= 0);
78 804074 : if (minimumCapacity > *capacity)
79 : {
80 445341 : rtl_String * pTmp = *This;
81 445341 : rtl_String * pNew = NULL;
82 445341 : *capacity = ((*This)->length + 1) * 2;
83 445341 : if (minimumCapacity > *capacity)
84 : /* still lower, set to the minimum capacity */
85 227533 : *capacity = minimumCapacity;
86 :
87 445341 : rtl_string_new_WithLength(&pNew, *capacity);
88 445341 : pNew->length = (*This)->length;
89 445341 : *This = pNew;
90 :
91 445341 : memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
92 445341 : rtl_string_release( pTmp );
93 : }
94 804074 : }
95 :
96 : /*************************************************************************
97 : * rtl_stringbuffer_insert
98 : */
99 26793060 : void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
100 : sal_Int32 * capacity,
101 : sal_Int32 offset,
102 : const sal_Char * str,
103 : sal_Int32 len )
104 : {
105 : assert(This);
106 : assert(capacity && *capacity >= 0);
107 : assert(offset >= 0 && offset <= (**This).length);
108 : assert(len >= 0);
109 : sal_Int32 nOldLen;
110 : sal_Char * pBuf;
111 : sal_Int32 n;
112 26793060 : if( len != 0 )
113 : {
114 26759421 : if (*capacity < (*This)->length + len)
115 395616 : rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
116 :
117 26759421 : nOldLen = (*This)->length;
118 26759421 : pBuf = (*This)->buffer;
119 :
120 : /* copy the tail */
121 26759421 : n = (nOldLen - offset);
122 26759421 : if( n == 1 )
123 : /* optimized for 1 character */
124 13976 : pBuf[offset + len] = pBuf[offset];
125 26745445 : else if( n > 1 )
126 59788 : memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
127 :
128 : /* insert the new characters */
129 26759421 : if( str != nullptr )
130 : {
131 26759419 : if( len == 1 )
132 : /* optimized for 1 character */
133 24393580 : pBuf[offset] = *str;
134 : else
135 2365839 : memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
136 : }
137 26759421 : (*This)->length = nOldLen + len;
138 26759421 : pBuf[ nOldLen + len ] = 0;
139 : }
140 26793060 : }
141 :
142 : /*************************************************************************
143 : * rtl_stringbuffer_remove
144 : */
145 2379 : void SAL_CALL rtl_stringbuffer_remove( rtl_String ** This,
146 : sal_Int32 start,
147 : sal_Int32 len )
148 : {
149 : assert(This);
150 : assert(start >= 0 && start <= (**This).length);
151 : assert(len >= 0);
152 : sal_Int32 nTailLen;
153 : sal_Char * pBuf;
154 :
155 2379 : if (len > (*This)->length - start)
156 2 : len = (*This)->length - start;
157 :
158 : //remove nothing
159 2379 : if (!len)
160 2379 : return;
161 :
162 2379 : pBuf = (*This)->buffer;
163 2379 : nTailLen = (*This)->length - ( start + len );
164 :
165 2379 : if (nTailLen)
166 : {
167 : /* move the tail */
168 1188 : memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Char));
169 : }
170 :
171 2379 : (*This)->length-=len;
172 2379 : pBuf[ (*This)->length ] = 0;
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|