Bump version to 4.1-6
[LibreOffice.git] / sal / rtl / strbuf.cxx
blob5472ceb7209122decd0bb16aa107f6121cec822c
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 if (!value)
34 rtl_string_new_WithLength( newStr, 16 );
35 return;
38 rtl_string_new_WithLength( newStr, count + 16 );
39 (*newStr)->length = count;
40 memcpy( (*newStr)->buffer, value, count );
41 return;
44 /*************************************************************************
45 * rtl_stringbuffer_newFromStringBuffer
47 sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
48 sal_Int32 capacity,
49 rtl_String * oldStr )
51 sal_Int32 newCapacity = capacity;
53 if (newCapacity < oldStr->length)
54 newCapacity = oldStr->length;
56 rtl_string_new_WithLength( newStr, newCapacity );
57 if (oldStr->length > 0) {
58 (*newStr)->length = oldStr->length;
59 memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length );
61 return newCapacity;
64 /*************************************************************************
65 * rtl_stringbuffer_ensureCapacity
67 void SAL_CALL rtl_stringbuffer_ensureCapacity
68 (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
70 if (minimumCapacity > *capacity)
72 rtl_String * pTmp = *This;
73 rtl_String * pNew = NULL;
74 *capacity = ((*This)->length + 1) * 2;
75 if (minimumCapacity > *capacity)
76 /* still lower, set to the minimum capacity */
77 *capacity = minimumCapacity;
79 rtl_string_new_WithLength(&pNew, *capacity);
80 pNew->length = (*This)->length;
81 *This = pNew;
83 memcpy( (*This)->buffer, pTmp->buffer, pTmp->length );
84 rtl_string_release( pTmp );
88 /*************************************************************************
89 * rtl_stringbuffer_insert
91 void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
92 sal_Int32 * capacity,
93 sal_Int32 offset,
94 const sal_Char * str,
95 sal_Int32 len )
97 sal_Int32 nOldLen;
98 sal_Char * pBuf;
99 sal_Int32 n;
100 if( len != 0 )
102 if (*capacity < (*This)->length + len)
103 rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
106 if( len == 1 )
107 This->buffer
109 nOldLen = (*This)->length;
110 pBuf = (*This)->buffer;
112 /* copy the tail */
113 n = (nOldLen - offset);
114 if( n == 1 )
115 /* optimized for 1 character */
116 pBuf[offset + len] = pBuf[offset];
117 else if( n > 1 )
118 memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
120 /* insert the new characters */
121 n = len;
122 if( len == 1 )
123 /* optimized for 1 character */
124 pBuf[offset] = *str;
125 else if( n > 1 )
126 memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
127 (*This)->length = nOldLen + len;
128 pBuf[ nOldLen + len ] = 0;
132 /*************************************************************************
133 * rtl_stringbuffer_remove
135 void SAL_CALL rtl_stringbuffer_remove( rtl_String ** This,
136 sal_Int32 start,
137 sal_Int32 len )
139 sal_Int32 nTailLen;
140 sal_Char * pBuf;
142 if (len > (*This)->length - start)
143 len = (*This)->length - start;
145 //remove nothing
146 if (!len)
147 return;
149 pBuf = (*This)->buffer;
150 nTailLen = (*This)->length - ( start + len );
152 if (nTailLen)
154 /* move the tail */
155 memmove(pBuf + start, pBuf + start + len, nTailLen * sizeof(sal_Char));
158 (*This)->length-=len;
159 pBuf[ (*This)->length ] = 0;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */