update credits
[LibreOffice.git] / cosv / source / strings / string.cxx
blob57f98a50e077185acf36325e29644b883b073ed8
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 #define CSV_USE_CSV_ASSERTIONS
21 #include <cosv/csv_env.hxx>
23 #include <cosv/comfunc.hxx>
24 #include <cosv/string.hxx>
25 #include <cosv/streamstr.hxx>
26 #include <cosv/std_outp.hxx>
27 #include <cosv/tpl/dyn.hxx>
29 // NOT FULLY DECLARED SERVICES
30 #include <string.h>
35 namespace csv
39 inline const char *
40 str_from_StringOffset( const String & i_rStr,
41 str::size i_nOffset )
43 return i_nOffset < i_rStr.size()
44 ? i_rStr.c_str() + i_nOffset
45 : "";
48 inline const char *
49 str_from_ptr( const char * i_str )
52 return valid_str(i_str);
56 //********************* String::S_Data **********************//
58 inline String::
59 S_Data::S_Data()
60 : nCount(1)
64 String::
65 S_Data::S_Data( const char * i_sData,
66 size_type i_nValidLength )
67 : aStr( str_from_ptr(i_sData),
68 (i_nValidLength != str::maxsize
69 ? i_nValidLength
70 : strlen(i_sData)) ),
71 nCount(1)
75 String::
76 S_Data::~S_Data()
78 csv_assert( nCount == 0 );
81 const String::S_Data *
82 String::
83 S_Data::Acquire() const
85 #ifdef CSV_NO_MUTABLE
86 ++ (const_cast< uintt& >(nCount));
87 #else
88 ++nCount;
89 #endif
90 return this;
93 void
94 String::
95 S_Data::Release() const
97 #ifdef CSV_NO_MUTABLE
98 -- (const_cast< uintt& >(nCount));
99 #else
100 --nCount;
101 #endif
102 if (nCount == 0)
103 delete (const_cast< S_Data* >(this));
107 //************************** String **************************//
110 String::String()
111 : pd( String::Null_().pd->Acquire() )
115 String::String( const char * i_str )
116 : pd( new S_Data(i_str) )
120 String::String( const char * i_str,
121 size_type i_nLength )
122 : pd( new S_Data(i_str, i_nLength) )
126 String::String( const_iterator i_itBegin,
127 const_iterator i_itEnd )
128 : pd( new S_Data(i_itBegin, size_type(i_itEnd - i_itBegin)) )
132 String::String( const self & i_rStr )
133 : pd( i_rStr.pd->Acquire() )
137 String::~String()
139 pd->Release();
143 String &
144 String::operator=( const self & i_rStr )
146 i_rStr.pd->Acquire();
147 pd->Release();
148 pd = i_rStr.pd;
150 return *this;
153 String &
154 String::operator=( const char * i_str )
156 const S_Data *
157 pTemp = new S_Data(i_str);
158 pd->Release();
159 pd = pTemp;
161 return *this;
164 void
165 String::assign( const char * i_str,
166 size_type i_nLength )
168 const S_Data *
169 pTemp = new S_Data( i_str, i_nLength );
170 pd->Release();
171 pd = pTemp;
175 String::compare( const self & i_rStr ) const
177 return strcmp( c_str(), i_rStr.c_str() );
181 String::compare( const CharOrder_Table & i_rOrder,
182 const self & i_rStr ) const
184 return csv::compare( i_rOrder, c_str(), i_rStr.c_str() );
187 const String &
188 String::Null_()
190 // Must not use the default constructor! Because that one calls
191 // this function, which would create a circular dependency.
192 static const String aNull_("");
193 return aNull_;
196 const char &
197 String::Nulch_()
199 static const char cNull_ = '\0';
200 return cNull_;
205 compare( const String & i_s1,
206 csv::str::position i_nStartPosition1,
207 const char * i_s2,
208 csv::str::size i_nLength )
210 const char * pS1 = str_from_StringOffset( i_s1, i_nStartPosition1 );
212 if ( i_nLength != csv::str::maxsize )
213 return strncmp( pS1,
214 i_s2,
215 i_nLength );
216 else
217 return strcmp( pS1,
218 i_s2 );
222 compare( const CharOrder_Table & i_rOrder,
223 const char * i_s1,
224 const char * i_s2 )
226 const char * it1 = i_s1;
227 const char * it2 = i_s2;
228 for ( ; i_rOrder(*it1) == i_rOrder(*it2) AND *it1 != '\0'; ++it1, ++it2 )
230 return int( i_rOrder(*it1) - i_rOrder(*it2) );
233 } // namespace csv
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */