xref: /AOO41X/main/cosv/source/strings/string.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <precomp.h>
29*cdf0e10cSrcweir #include <cosv/string.hxx>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir #include <cosv/comfunc.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace csv
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir inline const char *
43*cdf0e10cSrcweir str_from_StringOffset( const String &     i_rStr,
44*cdf0e10cSrcweir                        str::size       i_nOffset )
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir  	return i_nOffset < i_rStr.size()
47*cdf0e10cSrcweir                 ?   i_rStr.c_str() + i_nOffset
48*cdf0e10cSrcweir                 :   "";
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir inline const char *
52*cdf0e10cSrcweir str_from_ptr( const char * i_str )
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     return valid_str(i_str);
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir //*********************     String::S_Data    **********************//
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir inline String::
62*cdf0e10cSrcweir S_Data::S_Data()
63*cdf0e10cSrcweir     :   nCount(1)
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir String::
68*cdf0e10cSrcweir S_Data::S_Data( const char *        i_sData,
69*cdf0e10cSrcweir                 size_type           i_nValidLength )
70*cdf0e10cSrcweir     :   aStr( str_from_ptr(i_sData),
71*cdf0e10cSrcweir               (i_nValidLength != str::maxsize
72*cdf0e10cSrcweir                     ?   i_nValidLength
73*cdf0e10cSrcweir                     :   strlen(i_sData)) ),
74*cdf0e10cSrcweir         nCount(1)
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir String::
79*cdf0e10cSrcweir S_Data::~S_Data()
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     csv_assert( nCount == 0 );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir const String::S_Data *
85*cdf0e10cSrcweir String::
86*cdf0e10cSrcweir S_Data::Acquire() const
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir #ifdef CSV_NO_MUTABLE
89*cdf0e10cSrcweir     ++ (const_cast< uintt& >(nCount));
90*cdf0e10cSrcweir #else
91*cdf0e10cSrcweir     ++nCount;
92*cdf0e10cSrcweir #endif
93*cdf0e10cSrcweir     return this;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void
97*cdf0e10cSrcweir String::
98*cdf0e10cSrcweir S_Data::Release() const
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir #ifdef CSV_NO_MUTABLE
101*cdf0e10cSrcweir     -- (const_cast< uintt& >(nCount));
102*cdf0e10cSrcweir #else
103*cdf0e10cSrcweir     --nCount;
104*cdf0e10cSrcweir #endif
105*cdf0e10cSrcweir     if (nCount == 0)
106*cdf0e10cSrcweir         delete (const_cast< S_Data* >(this));
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir //**************************     String    **************************//
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir String::String()
114*cdf0e10cSrcweir     :   pd( String::Null_().pd->Acquire() )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir String::String( const char * i_str )
119*cdf0e10cSrcweir     :   pd( new S_Data(i_str) )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir String::String( const char *        i_str,
124*cdf0e10cSrcweir                 size_type           i_nLength )
125*cdf0e10cSrcweir     :   pd( new S_Data(i_str, i_nLength) )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir /*  For efficiency see the previous c'tor.
130*cdf0e10cSrcweir */
131*cdf0e10cSrcweir String::String( const self &        i_rStr,
132*cdf0e10cSrcweir                 position_type       i_nStartPosition,
133*cdf0e10cSrcweir                 size_type           i_nLength )
134*cdf0e10cSrcweir     :   pd( new S_Data(str_from_StringOffset(i_rStr, i_nStartPosition), i_nLength) )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir String::String( const_iterator i_itBegin,
139*cdf0e10cSrcweir                 const_iterator i_itEnd )
140*cdf0e10cSrcweir     :   pd( new S_Data(i_itBegin, size_type(i_itEnd - i_itBegin)) )
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir String::String( const self & i_rStr )
145*cdf0e10cSrcweir     :   pd( i_rStr.pd->Acquire() )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir String::~String()
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     pd->Release();
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir String &
156*cdf0e10cSrcweir String::operator=( const self & i_rStr )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     i_rStr.pd->Acquire();
159*cdf0e10cSrcweir     pd->Release();
160*cdf0e10cSrcweir     pd = i_rStr.pd;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     return *this;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir String &
166*cdf0e10cSrcweir String::operator=( const char * i_str )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     const S_Data *
169*cdf0e10cSrcweir         pTemp = new S_Data(i_str);
170*cdf0e10cSrcweir     pd->Release();
171*cdf0e10cSrcweir     pd = pTemp;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     return *this;
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir void
177*cdf0e10cSrcweir String::swap( self & i_rStr )
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     const S_Data * pTemp = pd;
180*cdf0e10cSrcweir     pd = i_rStr.pd;
181*cdf0e10cSrcweir     i_rStr.pd = pTemp;
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir void
185*cdf0e10cSrcweir String::assign( const self &        i_rStr,
186*cdf0e10cSrcweir                 position_type       i_nStartPosition,
187*cdf0e10cSrcweir                 size_type           i_nLength )
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir     const S_Data *
190*cdf0e10cSrcweir         pTemp = new S_Data( str_from_StringOffset(i_rStr, i_nStartPosition),
191*cdf0e10cSrcweir                             i_nLength );
192*cdf0e10cSrcweir     pd->Release();
193*cdf0e10cSrcweir     pd = pTemp;
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir void
197*cdf0e10cSrcweir String::assign( const char *        i_str )
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     const S_Data *
200*cdf0e10cSrcweir         pTemp = new S_Data( i_str );
201*cdf0e10cSrcweir     pd->Release();
202*cdf0e10cSrcweir     pd = pTemp;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir void
206*cdf0e10cSrcweir String::assign( const char *        i_str,
207*cdf0e10cSrcweir                 size_type           i_nLength )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     const S_Data *
210*cdf0e10cSrcweir         pTemp = new S_Data( i_str, i_nLength );
211*cdf0e10cSrcweir     pd->Release();
212*cdf0e10cSrcweir     pd = pTemp;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir void
216*cdf0e10cSrcweir String::assign( const_iterator      i_itBegin,
217*cdf0e10cSrcweir                 const_iterator      i_itEnd )
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir     const S_Data *
220*cdf0e10cSrcweir         pTemp = new S_Data( i_itBegin,
221*cdf0e10cSrcweir                             size_type(i_itEnd - i_itBegin) );
222*cdf0e10cSrcweir     pd->Release();
223*cdf0e10cSrcweir     pd = pTemp;
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir int
228*cdf0e10cSrcweir String::compare( const self & i_rStr ) const
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     return strcmp( c_str(), i_rStr.c_str() );
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir int
234*cdf0e10cSrcweir String::compare( const CharOrder_Table & i_rOrder,
235*cdf0e10cSrcweir                  const self &            i_rStr ) const
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     return csv::compare( i_rOrder, c_str(), i_rStr.c_str() );
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir String
241*cdf0e10cSrcweir String::substr( position_type       i_nStartPosition,
242*cdf0e10cSrcweir                 size_type           i_nLength ) const
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir     size_type nSize = size();
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir     if ( i_nStartPosition < nSize )
247*cdf0e10cSrcweir     {
248*cdf0e10cSrcweir         if ( i_nLength == str::maxsize
249*cdf0e10cSrcweir              OR i_nLength >= nSize - i_nStartPosition )
250*cdf0e10cSrcweir      	    return String( c_str() + i_nStartPosition );
251*cdf0e10cSrcweir         else
252*cdf0e10cSrcweir             return String( c_str() + i_nStartPosition,
253*cdf0e10cSrcweir                                  i_nLength );
254*cdf0e10cSrcweir     }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     return Null_();
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir String::position_type
260*cdf0e10cSrcweir String::find( const char *        i_strToSearch,
261*cdf0e10cSrcweir               position_type       i_nSearchStartPosition ) const
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     csv_assert(i_strToSearch != 0);
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     if ( i_nSearchStartPosition < length()
266*cdf0e10cSrcweir          AND
267*cdf0e10cSrcweir          *i_strToSearch != '\0' )
268*cdf0e10cSrcweir     {
269*cdf0e10cSrcweir         const char * p = strstr(c_str() + i_nSearchStartPosition, i_strToSearch);
270*cdf0e10cSrcweir         if (p != 0)
271*cdf0e10cSrcweir             return static_cast<position_type>(p - c_str());
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir     return str::position(str::npos);
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir String::position_type
277*cdf0e10cSrcweir String::find( char                i_charToSearch,
278*cdf0e10cSrcweir               position_type       i_nSearchStartPosition ) const
279*cdf0e10cSrcweir {
280*cdf0e10cSrcweir     if (i_nSearchStartPosition <= length())
281*cdf0e10cSrcweir     {
282*cdf0e10cSrcweir         const char * p = strchr(c_str() + i_nSearchStartPosition, i_charToSearch);
283*cdf0e10cSrcweir         if (p != 0)
284*cdf0e10cSrcweir             return static_cast<position_type>(p - c_str());
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir     return str::position(str::npos);
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir const String &
290*cdf0e10cSrcweir String::Null_()
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     // Must not use the default constructor! Because that one calls
293*cdf0e10cSrcweir     //   this function, which would create a circular dependency.
294*cdf0e10cSrcweir     static const String aNull_("");
295*cdf0e10cSrcweir     return aNull_;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir const char &
299*cdf0e10cSrcweir String::Nulch_()
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     static const char cNull_ = '\0';
302*cdf0e10cSrcweir     return cNull_;
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir int
307*cdf0e10cSrcweir compare( const String &      i_s1,
308*cdf0e10cSrcweir          csv::str::position        i_nStartPosition1,
309*cdf0e10cSrcweir          const char *              i_s2,
310*cdf0e10cSrcweir          csv::str::size            i_nLength )
311*cdf0e10cSrcweir {
312*cdf0e10cSrcweir     const char * pS1 = str_from_StringOffset( i_s1, i_nStartPosition1 );
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
315*cdf0e10cSrcweir         return strncmp( pS1,
316*cdf0e10cSrcweir                         i_s2,
317*cdf0e10cSrcweir                         i_nLength );
318*cdf0e10cSrcweir     else
319*cdf0e10cSrcweir         return strcmp( pS1,
320*cdf0e10cSrcweir                        i_s2 );
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir int
324*cdf0e10cSrcweir compare( const char *              i_s1,
325*cdf0e10cSrcweir          const String &      i_s2,
326*cdf0e10cSrcweir          csv::str::position        i_nStartPosition2,
327*cdf0e10cSrcweir          csv::str::size            i_nLength )
328*cdf0e10cSrcweir {
329*cdf0e10cSrcweir     const char * pS2 = str_from_StringOffset( i_s2, i_nStartPosition2 );
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
332*cdf0e10cSrcweir         return strncmp( i_s1,
333*cdf0e10cSrcweir                         pS2,
334*cdf0e10cSrcweir                         i_nLength );
335*cdf0e10cSrcweir     else
336*cdf0e10cSrcweir         return strcmp( i_s1,
337*cdf0e10cSrcweir                        pS2 );
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir int
341*cdf0e10cSrcweir compare( const CharOrder_Table &            i_rOrder,
342*cdf0e10cSrcweir          const char *                       i_s1,
343*cdf0e10cSrcweir          const char *                       i_s2 )
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir     const char * it1 = i_s1;
346*cdf0e10cSrcweir     const char * it2 = i_s2;
347*cdf0e10cSrcweir     for ( ; i_rOrder(*it1) == i_rOrder(*it2) AND *it1 != '\0'; ++it1, ++it2 )
348*cdf0e10cSrcweir     {}
349*cdf0e10cSrcweir     return int( i_rOrder(*it1) - i_rOrder(*it2) );
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir int
353*cdf0e10cSrcweir compare( const CharOrder_Table &   i_rOrder,
354*cdf0e10cSrcweir          const String &      i_s1,
355*cdf0e10cSrcweir          csv::str::position        i_nStartPosition1,
356*cdf0e10cSrcweir          const char *              i_s2,
357*cdf0e10cSrcweir          csv::str::size            i_nLength )
358*cdf0e10cSrcweir {
359*cdf0e10cSrcweir     const char * pS1 = str_from_StringOffset( i_s1, i_nStartPosition1 );
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
362*cdf0e10cSrcweir         return compare( i_rOrder,
363*cdf0e10cSrcweir                         pS1,
364*cdf0e10cSrcweir                         i_s2,
365*cdf0e10cSrcweir                         i_nLength );
366*cdf0e10cSrcweir     else
367*cdf0e10cSrcweir         return compare( i_rOrder,
368*cdf0e10cSrcweir                         pS1,
369*cdf0e10cSrcweir                         i_s2 );
370*cdf0e10cSrcweir }
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir int
373*cdf0e10cSrcweir compare( const CharOrder_Table &   i_rOrder,
374*cdf0e10cSrcweir          const char *              i_s1,
375*cdf0e10cSrcweir          const String &            i_s2,
376*cdf0e10cSrcweir          csv::str::position        i_nStartPosition2,
377*cdf0e10cSrcweir          csv::str::size                 i_nLength )
378*cdf0e10cSrcweir {
379*cdf0e10cSrcweir     const char * pS2 = str_from_StringOffset( i_s2, i_nStartPosition2 );
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
382*cdf0e10cSrcweir         return compare( i_rOrder,
383*cdf0e10cSrcweir                         i_s1,
384*cdf0e10cSrcweir                         pS2,
385*cdf0e10cSrcweir                         i_nLength );
386*cdf0e10cSrcweir     else
387*cdf0e10cSrcweir         return compare( i_rOrder,
388*cdf0e10cSrcweir                         i_s1,
389*cdf0e10cSrcweir                         pS2 );
390*cdf0e10cSrcweir }
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir int
393*cdf0e10cSrcweir compare( const CharOrder_Table &            i_rOrder,
394*cdf0e10cSrcweir          const char *                       i_s1,
395*cdf0e10cSrcweir          const char *                       i_s2,
396*cdf0e10cSrcweir          csv::str::size                     i_nLength )
397*cdf0e10cSrcweir {
398*cdf0e10cSrcweir     const char * sEnd = i_s1 + i_nLength;
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir     const char * it1 = i_s1;
401*cdf0e10cSrcweir     const char * it2 = i_s2;
402*cdf0e10cSrcweir     for ( ; i_rOrder(*it1) == i_rOrder(*it2) AND *it1 != '\0' AND it1 != sEnd; ++it1, ++it2 )
403*cdf0e10cSrcweir     {}
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir     if ( it1 != sEnd )
406*cdf0e10cSrcweir         return int( i_rOrder(*it1) - i_rOrder(*it2) );
407*cdf0e10cSrcweir     else
408*cdf0e10cSrcweir         return 0;
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir }   // namespace csv
415