1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <string.h>
#include "boost/static_assert.hpp"
#include <osl/interlck.h>
#include <rtl/alloc.h>
#include <rtl/tencinfo.h>
#include <rtl/instance.hxx>
#include <tools/string.hxx>
#include <impstrg.hxx>
#include <tools/debug.hxx>
DBG_NAME( UniString )
#define STRCODE sal_Unicode
#define STRCODEU sal_Unicode
#define STRING UniString
#define STRINGDATA UniStringData
#define STRING_TYPE rtl_uString
#define STRING_ACQUIRE rtl_uString_acquire
#define STRING_RELEASE rtl_uString_release
#define STRING_NEW rtl_uString_new
#if defined DBG_UTIL
#define DBGCHECKSTRING DbgCheckUniString
#endif
#include <strimp.cxx>
#include <strucvt.cxx>
UniString::UniString(char c): mpData(ImplAllocData(1)) { mpData->maStr[0] = c; }
sal_Int32 UniString::ToInt32() const
{
DBG_CHKTHIS( UniString, DbgCheckUniString );
return rtl_ustr_toInt32( mpData->maStr, 10 );
}
STRING& STRING::Insert( const STRING& rStr, xub_StrLen nPos, xub_StrLen nLen,
xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// Determine string length
if ( nPos > rStr.mpData->mnLen )
nLen = 0;
else
{
// Correct length if necessary
sal_Int32 nMaxLen = rStr.mpData->mnLen-nPos;
if ( nLen > nMaxLen )
nLen = static_cast< xub_StrLen >(nMaxLen);
}
// Detect overflow
sal_Int32 nCopyLen = ImplGetCopyLen( mpData->mnLen, nLen );
if ( !nCopyLen )
return *this;
// Correct index if necessary
if ( nIndex > mpData->mnLen )
nIndex = static_cast< xub_StrLen >(mpData->mnLen);
// Determine new length and allocate string
STRINGDATA* pNewData = ImplAllocData( mpData->mnLen+nCopyLen );
// copy string to newdata
memcpy( pNewData->maStr, mpData->maStr, nIndex*sizeof( STRCODE ) );
memcpy( pNewData->maStr+nIndex, rStr.mpData->maStr+nPos, nCopyLen*sizeof( STRCODE ) );
memcpy( pNewData->maStr+nIndex+nCopyLen, mpData->maStr+nIndex,
(mpData->mnLen-nIndex)*sizeof( STRCODE ) );
// release old data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
return *this;
}
STRING& STRING::Insert( STRCODE c, xub_StrLen nIndex )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// Don't insert 0 char or string size is maximum
if ( !c || (mpData->mnLen == STRING_MAXLEN) )
return *this;
// Adjust string index
if ( nIndex > mpData->mnLen )
nIndex = static_cast< xub_StrLen >(mpData->mnLen);
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( mpData->mnLen+1 );
// copy string
memcpy( pNewData->maStr, mpData->maStr, nIndex*sizeof( STRCODE ) );
pNewData->maStr[nIndex] = c;
memcpy( pNewData->maStr+nIndex+1, mpData->maStr+nIndex,
(mpData->mnLen-nIndex)*sizeof( STRCODE ) );
// free old data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
return *this;
}
StringCompare STRING::CompareTo( const STRING& rStr, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return COMPARE_EQUAL;
// determine maximal length
if ( mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(mpData->mnLen+1);
if ( rStr.mpData->mnLen < nLen )
nLen = static_cast< xub_StrLen >(rStr.mpData->mnLen+1);
sal_Int32 nCompare = ImplStringCompareWithoutZero( mpData->maStr, rStr.mpData->maStr, nLen );
if ( nCompare == 0 )
return COMPARE_EQUAL;
else if ( nCompare < 0 )
return COMPARE_LESS;
else
return COMPARE_GREATER;
}
sal_Bool STRING::Equals( const STRING& rStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
if ( mpData == rStr.mpData )
return sal_True;
if ( mpData->mnLen != rStr.mpData->mnLen )
return sal_False;
return (ImplStringCompareWithoutZero( mpData->maStr, rStr.mpData->maStr, mpData->mnLen ) == 0);
}
sal_Bool STRING::Equals( const STRING& rStr, xub_StrLen nIndex, xub_StrLen nLen ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// Are there enough codes for comparing?
if ( nIndex > mpData->mnLen )
return (rStr.mpData->mnLen == 0);
sal_Int32 nMaxLen = mpData->mnLen-nIndex;
if ( nMaxLen < nLen )
{
if ( rStr.mpData->mnLen != nMaxLen )
return sal_False;
nLen = static_cast< xub_StrLen >(nMaxLen);
}
return (ImplStringCompareWithoutZero( mpData->maStr+nIndex, rStr.mpData->maStr, nLen ) == 0);
}
xub_StrLen STRING::Match( const STRING& rStr ) const
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_CHKOBJ( &rStr, STRING, DBGCHECKSTRING );
// return if string is empty
if ( !mpData->mnLen )
return STRING_MATCH;
// Search the string for unmatching chars
const STRCODE* pStr1 = mpData->maStr;
const STRCODE* pStr2 = rStr.mpData->maStr;
xub_StrLen i = 0;
while ( i < mpData->mnLen )
{
// Abort on the first unmatching char
if ( *pStr1 != *pStr2 )
return i;
++pStr1,
++pStr2,
++i;
}
return STRING_MATCH;
}
STRING& STRING::Append( STRCODE c )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
// don't append null characters and keep string length < maxlen
sal_Int32 nLen = mpData->mnLen;
if ( c && (nLen < STRING_MAXLEN) )
{
// allocate string of new size
STRINGDATA* pNewData = ImplAllocData( nLen+1 );
// copy string
memcpy( pNewData->maStr, mpData->maStr, nLen*sizeof( STRCODE ) );
pNewData->maStr[nLen] = c;
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = pNewData;
}
return *this;
}
STRING& STRING::Assign( STRCODE c )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( c, "String::Assign() - c is 0" );
// initialize maintenance data
STRING_RELEASE((STRING_TYPE *)mpData);
mpData = ImplAllocData( 1 );
mpData->maStr[0] = c;
return *this;
}
STRING& STRING::Assign( const STRCODE* pCharStr )
{
DBG_CHKTHIS( STRING, DBGCHECKSTRING );
DBG_ASSERT( pCharStr, "String::Assign() - pCharStr is NULL" );
xub_StrLen nLen = ImplStringLen( pCharStr );
if ( !nLen )
{
STRING_NEW((STRING_TYPE **)&mpData);
}
else
{
// copy without allocation if string length is identical
if ( (nLen == mpData->mnLen) && (mpData->mnRefCount == 1) )
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
else
{
// free old string
STRING_RELEASE((STRING_TYPE *)mpData);
// allocate string of new size and copy
mpData = ImplAllocData( nLen );
memcpy( mpData->maStr, pCharStr, nLen*sizeof( STRCODE ) );
}
}
return *this;
}
xub_StrLen ImplStringLen( const sal_Char* pStr )
{
const sal_Char* pTempStr = pStr;
while( *pTempStr )
++pTempStr;
return (xub_StrLen)(pTempStr-pStr);
}
xub_StrLen ImplStringLen( const sal_Unicode* pStr )
{
const sal_Unicode* pTempStr = pStr;
while( *pTempStr )
++pTempStr;
return (xub_StrLen)(pTempStr-pStr);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|