xref: /AOO41X/main/sal/rtl/source/strbuf.c (revision 647f063d49501903f1667b75f5634541fc603283)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <osl/interlck.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifndef _RTL_STRING_HXX_
27cdf0e10cSrcweir #include <rtl/strbuf.hxx>
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <rtl/memory.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /*
32cdf0e10cSrcweir #include <rtl/alloc.h>
33cdf0e10cSrcweir */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /*************************************************************************
38cdf0e10cSrcweir  *	rtl_stringbuffer_newFromStr_WithLength
39cdf0e10cSrcweir  */
rtl_stringbuffer_newFromStr_WithLength(rtl_String ** newStr,const sal_Char * value,sal_Int32 count)40cdf0e10cSrcweir void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
41cdf0e10cSrcweir 													  const sal_Char * value,
42cdf0e10cSrcweir 													  sal_Int32 count )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	if (!value)
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 		rtl_string_new_WithLength( newStr, 16 );
47cdf0e10cSrcweir 		return;
48cdf0e10cSrcweir 	}
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	rtl_string_new_WithLength( newStr, count + 16 );
51cdf0e10cSrcweir 	(*newStr)->length = count;
52cdf0e10cSrcweir 	rtl_copyMemory( (*newStr)->buffer, value, count );
53cdf0e10cSrcweir 	return;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /*************************************************************************
57cdf0e10cSrcweir  *	rtl_stringbuffer_newFromStringBuffer
58cdf0e10cSrcweir  */
rtl_stringbuffer_newFromStringBuffer(rtl_String ** newStr,sal_Int32 capacity,rtl_String * oldStr)59cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
60cdf0e10cSrcweir 														 sal_Int32 capacity,
61cdf0e10cSrcweir 													     rtl_String * oldStr )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	sal_Int32 newCapacity = capacity;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	if (newCapacity < oldStr->length)
66cdf0e10cSrcweir 		newCapacity = oldStr->length;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	rtl_string_new_WithLength( newStr, newCapacity );
69cdf0e10cSrcweir     if (oldStr->length > 0) {
70cdf0e10cSrcweir         (*newStr)->length = oldStr->length;
71cdf0e10cSrcweir         rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length );
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 	return newCapacity;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir /*************************************************************************
77cdf0e10cSrcweir  *	rtl_stringbuffer_ensureCapacity
78cdf0e10cSrcweir  */
rtl_stringbuffer_ensureCapacity(rtl_String ** This,sal_Int32 * capacity,sal_Int32 minimumCapacity)79cdf0e10cSrcweir void SAL_CALL rtl_stringbuffer_ensureCapacity
80cdf0e10cSrcweir 	(rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	if (minimumCapacity > *capacity)
83cdf0e10cSrcweir 	{
84cdf0e10cSrcweir 		rtl_String * pTmp = *This;
85cdf0e10cSrcweir 		rtl_String * pNew = NULL;
86cdf0e10cSrcweir 		*capacity = ((*This)->length + 1) * 2;
87cdf0e10cSrcweir 		if (minimumCapacity > *capacity)
88cdf0e10cSrcweir 			/* still lower, set to the minimum capacity */
89cdf0e10cSrcweir 			*capacity = minimumCapacity;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 		rtl_string_new_WithLength(&pNew, *capacity);
92cdf0e10cSrcweir 		pNew->length = (*This)->length;
93cdf0e10cSrcweir 		*This = pNew;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length );
96cdf0e10cSrcweir 		rtl_string_release( pTmp );
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir /*************************************************************************
101cdf0e10cSrcweir  *	rtl_stringbuffer_insert
102cdf0e10cSrcweir  */
rtl_stringbuffer_insert(rtl_String ** This,sal_Int32 * capacity,sal_Int32 offset,const sal_Char * str,sal_Int32 len)103cdf0e10cSrcweir void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
104cdf0e10cSrcweir 									   sal_Int32 * capacity,
105cdf0e10cSrcweir 									   sal_Int32 offset,
106cdf0e10cSrcweir 									   const sal_Char * str,
107cdf0e10cSrcweir 									   sal_Int32 len )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	sal_Int32 nOldLen;
110cdf0e10cSrcweir 	sal_Char * pBuf;
111cdf0e10cSrcweir 	sal_Int32 n;
112cdf0e10cSrcweir 	if( len != 0 )
113cdf0e10cSrcweir 	{
114cdf0e10cSrcweir 		if (*capacity < (*This)->length + len)
115cdf0e10cSrcweir 			rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		/*
118cdf0e10cSrcweir 		if( len == 1 )
119cdf0e10cSrcweir 			This->buffer
120cdf0e10cSrcweir 		*/
121cdf0e10cSrcweir 		nOldLen = (*This)->length;
122cdf0e10cSrcweir 		pBuf = (*This)->buffer;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 		/* copy the tail */
125cdf0e10cSrcweir 		n = (nOldLen - offset);
126cdf0e10cSrcweir 		if( n == 1 )
127cdf0e10cSrcweir             /* optimized for 1 character */
128cdf0e10cSrcweir 			pBuf[offset + len] = pBuf[offset];
129cdf0e10cSrcweir 		else if( n > 1 )
130cdf0e10cSrcweir 			rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 		/* insert the new characters */
133cdf0e10cSrcweir 		n = len;
134cdf0e10cSrcweir 		if( len == 1 )
135cdf0e10cSrcweir                             /* optimized for 1 character */
136cdf0e10cSrcweir 			pBuf[offset] = *str;
137cdf0e10cSrcweir 		else if( n > 1 )
138cdf0e10cSrcweir 			rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Char) );
139cdf0e10cSrcweir 		(*This)->length = nOldLen + len;
140cdf0e10cSrcweir 		pBuf[ nOldLen + len ] = 0;
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144