update credits
[LibreOffice.git] / sal / rtl / strbuf.cxx
blobb88b0d9585c15dc5b1e5183dc53b25354ddfcdfc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <string.h>
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,
30 sal_Int32 count )
32 assert(newStr);
33 assert(count >= 0);
34 if (!value)
36 rtl_string_new_WithLength( newStr, 16 );
37 return;
40 rtl_string_new_WithLength( newStr, count + 16 );
41 (*newStr)->length = count;
42 memcpy( (*newStr)->buffer, value, count );
43 return;
46 /*************************************************************************
47 * rtl_stringbuffer_newFromStringBuffer
49 sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
50 sal_Int32 capacity,
51 rtl_String * oldStr )
53 assert(newStr);
54 assert(oldStr);
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 );
66 return newCapacity;
69 /*************************************************************************
70 * rtl_stringbuffer_ensureCapacity
72 void SAL_CALL rtl_stringbuffer_ensureCapacity
73 (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
75 assert(This);
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;
89 *This = pNew;
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,
101 sal_Int32 offset,
102 const sal_Char * str,
103 sal_Int32 len )
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 if( len != 0 )
114 if (*capacity < (*This)->length + len)
115 rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
117 nOldLen = (*This)->length;
118 pBuf = (*This)->buffer;
120 /* copy the tail */
121 n = (nOldLen - offset);
122 if( n == 1 )
123 /* optimized for 1 character */
124 pBuf[offset + len] = pBuf[offset];
125 else if( n > 1 )
126 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
128 /* insert the new characters */
129 if( str != nullptr )
131 if( len == 1 )
132 /* optimized for 1 character */
133 pBuf[offset] = *str;
134 else
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,
146 sal_Int32 start,
147 sal_Int32 len )
149 assert(This);
150 assert(start >= 0 && start <= (**This).length);
151 assert(len >= 0);
152 sal_Int32 nTailLen;
153 sal_Char * pBuf;
155 if (len > (*This)->length - start)
156 len = (*This)->length - start;
158 //remove nothing
159 if (!len)
160 return;
162 pBuf = (*This)->buffer;
163 nTailLen = (*This)->length - ( start + len );
165 if (nTailLen)
167 /* move the tail */
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: */