159617ebcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
359617ebcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
459617ebcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
559617ebcSAndrew Rist  * distributed with this work for additional information
659617ebcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
759617ebcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
859617ebcSAndrew Rist  * "License"); you may not use this file except in compliance
959617ebcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1059617ebcSAndrew Rist  *
1159617ebcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1259617ebcSAndrew Rist  *
1359617ebcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1459617ebcSAndrew Rist  * software distributed under the License is distributed on an
1559617ebcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1659617ebcSAndrew Rist  * KIND, either express or implied.  See the License for the
1759617ebcSAndrew Rist  * specific language governing permissions and limitations
1859617ebcSAndrew Rist  * under the License.
1959617ebcSAndrew Rist  *
2059617ebcSAndrew Rist  *************************************************************/
2159617ebcSAndrew Rist 
2259617ebcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <precomp.h>
25cdf0e10cSrcweir #include <cosv/string.hxx>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #include <cosv/comfunc.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace csv
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir inline const char *
str_from_StringOffset(const String & i_rStr,str::size i_nOffset)39cdf0e10cSrcweir str_from_StringOffset( const String &     i_rStr,
40cdf0e10cSrcweir                        str::size       i_nOffset )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir  	return i_nOffset < i_rStr.size()
43cdf0e10cSrcweir                 ?   i_rStr.c_str() + i_nOffset
44cdf0e10cSrcweir                 :   "";
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir inline const char *
str_from_ptr(const char * i_str)48cdf0e10cSrcweir str_from_ptr( const char * i_str )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     return valid_str(i_str);
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //*********************     String::S_Data    **********************//
56cdf0e10cSrcweir 
57cdf0e10cSrcweir inline String::
S_Data()58cdf0e10cSrcweir S_Data::S_Data()
59cdf0e10cSrcweir     :   nCount(1)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir String::
S_Data(const char * i_sData,size_type i_nValidLength)64cdf0e10cSrcweir S_Data::S_Data( const char *        i_sData,
65cdf0e10cSrcweir                 size_type           i_nValidLength )
66cdf0e10cSrcweir     :   aStr( str_from_ptr(i_sData),
67cdf0e10cSrcweir               (i_nValidLength != str::maxsize
68cdf0e10cSrcweir                     ?   i_nValidLength
69cdf0e10cSrcweir                     :   strlen(i_sData)) ),
70cdf0e10cSrcweir         nCount(1)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir String::
~S_Data()75cdf0e10cSrcweir S_Data::~S_Data()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     csv_assert( nCount == 0 );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir const String::S_Data *
81cdf0e10cSrcweir String::
Acquire() const82cdf0e10cSrcweir S_Data::Acquire() const
83cdf0e10cSrcweir {
84cdf0e10cSrcweir #ifdef CSV_NO_MUTABLE
85cdf0e10cSrcweir     ++ (const_cast< uintt& >(nCount));
86cdf0e10cSrcweir #else
87cdf0e10cSrcweir     ++nCount;
88cdf0e10cSrcweir #endif
89cdf0e10cSrcweir     return this;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir void
93cdf0e10cSrcweir String::
Release() const94cdf0e10cSrcweir S_Data::Release() const
95cdf0e10cSrcweir {
96cdf0e10cSrcweir #ifdef CSV_NO_MUTABLE
97cdf0e10cSrcweir     -- (const_cast< uintt& >(nCount));
98cdf0e10cSrcweir #else
99cdf0e10cSrcweir     --nCount;
100cdf0e10cSrcweir #endif
101cdf0e10cSrcweir     if (nCount == 0)
102cdf0e10cSrcweir         delete (const_cast< S_Data* >(this));
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
106cdf0e10cSrcweir //**************************     String    **************************//
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
String()109cdf0e10cSrcweir String::String()
110cdf0e10cSrcweir     :   pd( String::Null_().pd->Acquire() )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
String(const char * i_str)114cdf0e10cSrcweir String::String( const char * i_str )
115cdf0e10cSrcweir     :   pd( new S_Data(i_str) )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
String(const char * i_str,size_type i_nLength)119cdf0e10cSrcweir String::String( const char *        i_str,
120cdf0e10cSrcweir                 size_type           i_nLength )
121cdf0e10cSrcweir     :   pd( new S_Data(i_str, i_nLength) )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir /*  For efficiency see the previous c'tor.
126cdf0e10cSrcweir */
String(const self & i_rStr,position_type i_nStartPosition,size_type i_nLength)127cdf0e10cSrcweir String::String( const self &        i_rStr,
128cdf0e10cSrcweir                 position_type       i_nStartPosition,
129cdf0e10cSrcweir                 size_type           i_nLength )
130cdf0e10cSrcweir     :   pd( new S_Data(str_from_StringOffset(i_rStr, i_nStartPosition), i_nLength) )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
String(const_iterator i_itBegin,const_iterator i_itEnd)134cdf0e10cSrcweir String::String( const_iterator i_itBegin,
135cdf0e10cSrcweir                 const_iterator i_itEnd )
136cdf0e10cSrcweir     :   pd( new S_Data(i_itBegin, size_type(i_itEnd - i_itBegin)) )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
String(const self & i_rStr)140cdf0e10cSrcweir String::String( const self & i_rStr )
141cdf0e10cSrcweir     :   pd( i_rStr.pd->Acquire() )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
~String()145cdf0e10cSrcweir String::~String()
146cdf0e10cSrcweir {
147cdf0e10cSrcweir     pd->Release();
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir String &
operator =(const self & i_rStr)152cdf0e10cSrcweir String::operator=( const self & i_rStr )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     i_rStr.pd->Acquire();
155cdf0e10cSrcweir     pd->Release();
156cdf0e10cSrcweir     pd = i_rStr.pd;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     return *this;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir String &
operator =(const char * i_str)162cdf0e10cSrcweir String::operator=( const char * i_str )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     const S_Data *
165cdf0e10cSrcweir         pTemp = new S_Data(i_str);
166cdf0e10cSrcweir     pd->Release();
167cdf0e10cSrcweir     pd = pTemp;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     return *this;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir void
swap(self & i_rStr)173cdf0e10cSrcweir String::swap( self & i_rStr )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     const S_Data * pTemp = pd;
176cdf0e10cSrcweir     pd = i_rStr.pd;
177cdf0e10cSrcweir     i_rStr.pd = pTemp;
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir void
assign(const self & i_rStr,position_type i_nStartPosition,size_type i_nLength)181cdf0e10cSrcweir String::assign( const self &        i_rStr,
182cdf0e10cSrcweir                 position_type       i_nStartPosition,
183cdf0e10cSrcweir                 size_type           i_nLength )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     const S_Data *
186cdf0e10cSrcweir         pTemp = new S_Data( str_from_StringOffset(i_rStr, i_nStartPosition),
187cdf0e10cSrcweir                             i_nLength );
188cdf0e10cSrcweir     pd->Release();
189cdf0e10cSrcweir     pd = pTemp;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir void
assign(const char * i_str)193cdf0e10cSrcweir String::assign( const char *        i_str )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     const S_Data *
196cdf0e10cSrcweir         pTemp = new S_Data( i_str );
197cdf0e10cSrcweir     pd->Release();
198cdf0e10cSrcweir     pd = pTemp;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir void
assign(const char * i_str,size_type i_nLength)202cdf0e10cSrcweir String::assign( const char *        i_str,
203cdf0e10cSrcweir                 size_type           i_nLength )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     const S_Data *
206cdf0e10cSrcweir         pTemp = new S_Data( i_str, i_nLength );
207cdf0e10cSrcweir     pd->Release();
208cdf0e10cSrcweir     pd = pTemp;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir void
assign(const_iterator i_itBegin,const_iterator i_itEnd)212cdf0e10cSrcweir String::assign( const_iterator      i_itBegin,
213cdf0e10cSrcweir                 const_iterator      i_itEnd )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     const S_Data *
216cdf0e10cSrcweir         pTemp = new S_Data( i_itBegin,
217cdf0e10cSrcweir                             size_type(i_itEnd - i_itBegin) );
218cdf0e10cSrcweir     pd->Release();
219cdf0e10cSrcweir     pd = pTemp;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 
223cdf0e10cSrcweir int
compare(const self & i_rStr) const224cdf0e10cSrcweir String::compare( const self & i_rStr ) const
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     return strcmp( c_str(), i_rStr.c_str() );
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir int
compare(const CharOrder_Table & i_rOrder,const self & i_rStr) const230cdf0e10cSrcweir String::compare( const CharOrder_Table & i_rOrder,
231cdf0e10cSrcweir                  const self &            i_rStr ) const
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     return csv::compare( i_rOrder, c_str(), i_rStr.c_str() );
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir String
substr(position_type i_nStartPosition,size_type i_nLength) const237cdf0e10cSrcweir String::substr( position_type       i_nStartPosition,
238cdf0e10cSrcweir                 size_type           i_nLength ) const
239cdf0e10cSrcweir {
240cdf0e10cSrcweir     size_type nSize = size();
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     if ( i_nStartPosition < nSize )
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         if ( i_nLength == str::maxsize
245cdf0e10cSrcweir              OR i_nLength >= nSize - i_nStartPosition )
246cdf0e10cSrcweir      	    return String( c_str() + i_nStartPosition );
247cdf0e10cSrcweir         else
248cdf0e10cSrcweir             return String( c_str() + i_nStartPosition,
249cdf0e10cSrcweir                                  i_nLength );
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     return Null_();
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir String::position_type
find(const char * i_strToSearch,position_type i_nSearchStartPosition) const256cdf0e10cSrcweir String::find( const char *        i_strToSearch,
257cdf0e10cSrcweir               position_type       i_nSearchStartPosition ) const
258cdf0e10cSrcweir {
259cdf0e10cSrcweir     csv_assert(i_strToSearch != 0);
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     if ( i_nSearchStartPosition < length()
262cdf0e10cSrcweir          AND
263cdf0e10cSrcweir          *i_strToSearch != '\0' )
264cdf0e10cSrcweir     {
265cdf0e10cSrcweir         const char * p = strstr(c_str() + i_nSearchStartPosition, i_strToSearch);
266cdf0e10cSrcweir         if (p != 0)
267cdf0e10cSrcweir             return static_cast<position_type>(p - c_str());
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir     return str::position(str::npos);
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir String::position_type
find(char i_charToSearch,position_type i_nSearchStartPosition) const273cdf0e10cSrcweir String::find( char                i_charToSearch,
274cdf0e10cSrcweir               position_type       i_nSearchStartPosition ) const
275cdf0e10cSrcweir {
276cdf0e10cSrcweir     if (i_nSearchStartPosition <= length())
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         const char * p = strchr(c_str() + i_nSearchStartPosition, i_charToSearch);
279cdf0e10cSrcweir         if (p != 0)
280cdf0e10cSrcweir             return static_cast<position_type>(p - c_str());
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir     return str::position(str::npos);
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir const String &
Null_()286cdf0e10cSrcweir String::Null_()
287cdf0e10cSrcweir {
288cdf0e10cSrcweir     // Must not use the default constructor! Because that one calls
289cdf0e10cSrcweir     //   this function, which would create a circular dependency.
290cdf0e10cSrcweir     static const String aNull_("");
291cdf0e10cSrcweir     return aNull_;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir const char &
Nulch_()295cdf0e10cSrcweir String::Nulch_()
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     static const char cNull_ = '\0';
298cdf0e10cSrcweir     return cNull_;
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 
302cdf0e10cSrcweir int
compare(const String & i_s1,csv::str::position i_nStartPosition1,const char * i_s2,csv::str::size i_nLength)303cdf0e10cSrcweir compare( const String &      i_s1,
304cdf0e10cSrcweir          csv::str::position        i_nStartPosition1,
305cdf0e10cSrcweir          const char *              i_s2,
306cdf0e10cSrcweir          csv::str::size            i_nLength )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir     const char * pS1 = str_from_StringOffset( i_s1, i_nStartPosition1 );
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
311cdf0e10cSrcweir         return strncmp( pS1,
312cdf0e10cSrcweir                         i_s2,
313cdf0e10cSrcweir                         i_nLength );
314cdf0e10cSrcweir     else
315cdf0e10cSrcweir         return strcmp( pS1,
316cdf0e10cSrcweir                        i_s2 );
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir int
compare(const char * i_s1,const String & i_s2,csv::str::position i_nStartPosition2,csv::str::size i_nLength)320cdf0e10cSrcweir compare( const char *              i_s1,
321cdf0e10cSrcweir          const String &      i_s2,
322cdf0e10cSrcweir          csv::str::position        i_nStartPosition2,
323cdf0e10cSrcweir          csv::str::size            i_nLength )
324cdf0e10cSrcweir {
325cdf0e10cSrcweir     const char * pS2 = str_from_StringOffset( i_s2, i_nStartPosition2 );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
328cdf0e10cSrcweir         return strncmp( i_s1,
329cdf0e10cSrcweir                         pS2,
330cdf0e10cSrcweir                         i_nLength );
331cdf0e10cSrcweir     else
332cdf0e10cSrcweir         return strcmp( i_s1,
333cdf0e10cSrcweir                        pS2 );
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir int
compare(const CharOrder_Table & i_rOrder,const char * i_s1,const char * i_s2)337cdf0e10cSrcweir compare( const CharOrder_Table &            i_rOrder,
338cdf0e10cSrcweir          const char *                       i_s1,
339cdf0e10cSrcweir          const char *                       i_s2 )
340cdf0e10cSrcweir {
341cdf0e10cSrcweir     const char * it1 = i_s1;
342cdf0e10cSrcweir     const char * it2 = i_s2;
343cdf0e10cSrcweir     for ( ; i_rOrder(*it1) == i_rOrder(*it2) AND *it1 != '\0'; ++it1, ++it2 )
344cdf0e10cSrcweir     {}
345cdf0e10cSrcweir     return int( i_rOrder(*it1) - i_rOrder(*it2) );
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir int
compare(const CharOrder_Table & i_rOrder,const String & i_s1,csv::str::position i_nStartPosition1,const char * i_s2,csv::str::size i_nLength)349cdf0e10cSrcweir compare( const CharOrder_Table &   i_rOrder,
350cdf0e10cSrcweir          const String &      i_s1,
351cdf0e10cSrcweir          csv::str::position        i_nStartPosition1,
352cdf0e10cSrcweir          const char *              i_s2,
353cdf0e10cSrcweir          csv::str::size            i_nLength )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir     const char * pS1 = str_from_StringOffset( i_s1, i_nStartPosition1 );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
358cdf0e10cSrcweir         return compare( i_rOrder,
359cdf0e10cSrcweir                         pS1,
360cdf0e10cSrcweir                         i_s2,
361cdf0e10cSrcweir                         i_nLength );
362cdf0e10cSrcweir     else
363cdf0e10cSrcweir         return compare( i_rOrder,
364cdf0e10cSrcweir                         pS1,
365cdf0e10cSrcweir                         i_s2 );
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir int
compare(const CharOrder_Table & i_rOrder,const char * i_s1,const String & i_s2,csv::str::position i_nStartPosition2,csv::str::size i_nLength)369cdf0e10cSrcweir compare( const CharOrder_Table &   i_rOrder,
370cdf0e10cSrcweir          const char *              i_s1,
371cdf0e10cSrcweir          const String &            i_s2,
372cdf0e10cSrcweir          csv::str::position        i_nStartPosition2,
373cdf0e10cSrcweir          csv::str::size                 i_nLength )
374cdf0e10cSrcweir {
375cdf0e10cSrcweir     const char * pS2 = str_from_StringOffset( i_s2, i_nStartPosition2 );
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     if ( i_nLength != csv::str::maxsize )
378cdf0e10cSrcweir         return compare( i_rOrder,
379cdf0e10cSrcweir                         i_s1,
380cdf0e10cSrcweir                         pS2,
381cdf0e10cSrcweir                         i_nLength );
382cdf0e10cSrcweir     else
383cdf0e10cSrcweir         return compare( i_rOrder,
384cdf0e10cSrcweir                         i_s1,
385cdf0e10cSrcweir                         pS2 );
386cdf0e10cSrcweir }
387cdf0e10cSrcweir 
388cdf0e10cSrcweir int
compare(const CharOrder_Table & i_rOrder,const char * i_s1,const char * i_s2,csv::str::size i_nLength)389cdf0e10cSrcweir compare( const CharOrder_Table &            i_rOrder,
390cdf0e10cSrcweir          const char *                       i_s1,
391cdf0e10cSrcweir          const char *                       i_s2,
392cdf0e10cSrcweir          csv::str::size                     i_nLength )
393cdf0e10cSrcweir {
394cdf0e10cSrcweir     const char * sEnd = i_s1 + i_nLength;
395cdf0e10cSrcweir 
396cdf0e10cSrcweir     const char * it1 = i_s1;
397cdf0e10cSrcweir     const char * it2 = i_s2;
398cdf0e10cSrcweir     for ( ; i_rOrder(*it1) == i_rOrder(*it2) AND *it1 != '\0' AND it1 != sEnd; ++it1, ++it2 )
399cdf0e10cSrcweir     {}
400cdf0e10cSrcweir 
401cdf0e10cSrcweir     if ( it1 != sEnd )
402cdf0e10cSrcweir         return int( i_rOrder(*it1) - i_rOrder(*it2) );
403cdf0e10cSrcweir     else
404cdf0e10cSrcweir         return 0;
405cdf0e10cSrcweir }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 
410cdf0e10cSrcweir }   // namespace csv
411