bump product version to 4.1.6.2
[LibreOffice.git] / include / cosv / string.hxx
blob756aa9a1492c1493ab950c60235625874dcc5203
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 #ifndef COSV_STRING_HXX
21 #define COSV_STRING_HXX
23 // USED SERVICES
24 #include <string.h>
25 #include <cosv/stringdata.hxx>
26 #include <cosv/str_types.hxx>
27 #include <cosv/csv_ostream.hxx>
28 #include <vector>
33 namespace csv
37 /** The Simple String:
38 It is used to just hold short to middle long texts as
39 data, which are constant at most times. They are reference
40 counted, so they are space efficient and have constant time
41 copy semantics.
43 For all compare() functions the return value is like in strcmp().
45 @attention
46 The present version of this class is NOT thread safe.
50 class String
52 public:
53 typedef String self;
55 typedef str::size size_type;
56 typedef str::position position_type;
58 typedef const char * const_iterator;
60 // LIFECYCLE
61 String();
63 /// Intentionally not explicit, smooth casting is intended.
64 String(
65 const char * i_str );
66 /// @precond i_nLength <= strlen(i_str) or i_nLength == str::maxsize.
67 String(
68 const char * i_str,
69 size_type i_nLength );
71 /** @precond i_itBegin and i_itEnd are in the same valid
72 memory-area, such that zero to finite times repetition of
73 ++i_itBegin leads to i_itBegin == i_itEnd.
75 String(
76 const_iterator i_itBegin,
77 const_iterator i_itEnd );
79 String(
80 const self & i_rStr );
82 ~String();
83 // OPERATORS
84 self & operator=(
85 const self & i_rStr );
86 self & operator=(
87 const char * i_str );
89 operator const char * () const;
91 bool operator==(
92 const self & i_rStr ) const;
93 bool operator!=(
94 const self & i_rStr ) const;
95 bool operator<(
96 const self & i_rStr ) const;
97 bool operator>(
98 const self & i_rStr ) const;
99 bool operator<=(
100 const self & i_rStr ) const;
101 bool operator>=(
102 const self & i_rStr ) const;
104 // OPERATIONS
105 void clear();
107 /// @precond i_nLength == str::maxsize OR i_nLength < strlen(i_str) .
108 void assign(
109 const char * i_str,
110 size_type i_nLength );
111 /// Create a string consisting of a sequence of i_nCount times the same char.
112 void assign(
113 size_type i_nCount,
114 char i_c );
116 // INQUIRY
117 const char * c_str() const;
118 const char * data() const;
120 bool empty() const;
121 size_type size() const;
122 size_type length() const;
124 const char & char_at(
125 position_type i_nPosition ) const;
127 const_iterator begin() const;
129 /// This is inefficient, so shouldn't be used within loops.
130 const_iterator end() const;
132 int compare(
133 const self & i_rStr ) const;
134 int compare(
135 const CharOrder_Table &
136 i_rOrder,
137 const self & i_rStr ) const;
139 //*********** Not yet implemented *********************//
140 position_type rfind(
141 const char * i_strToSearch,
142 position_type i_nSearchStartPosition = str::npos ) const;
143 position_type rfind(
144 char i_charToSearch,
145 position_type i_nSearchStartPosition = str::npos ) const;
147 position_type find_first_not_of(
148 const char * i_strToSearch,
149 position_type i_nSearchStartPosition = 0 ) const;
150 position_type find_first_not_of(
151 char i_charToSearch,
152 position_type i_nSearchStartPosition = 0 ) const;
154 position_type find_last_not_of(
155 const char * i_strToSearch,
156 position_type i_nSearchStartPosition = str::npos ) const;
157 position_type find_last_not_of(
158 char i_charToSearch,
159 position_type i_nSearchStartPosition = str::npos ) const;
160 //*********** end - not yet implemented *****************//
162 static const self & Null_();
163 static const char & Nulch_();
165 private:
166 struct S_Data
168 S_Data();
169 /// @precond i_nValidLength <= strlen(i_sData) or i_nValidLength == str::maxsize.
170 explicit S_Data(
171 const char * i_sData,
172 size_type i_nValidLength = str::maxsize );
173 ~S_Data();
175 const S_Data * Acquire() const;
177 /// Deletes this, if nCount becomes 0.
178 void Release() const;
180 StringData<char> aStr;
181 mutable UINT32 nCount;
183 private:
184 // Forbidden functions, because this is a refcounted structure.
185 S_Data(const S_Data&);
186 S_Data & operator=(const S_Data&);
189 // Locals
190 const StringData<char> &
191 Str() const;
193 // DATA
194 const S_Data * pd;
198 //********** Global compare functions ***************//
200 //*** Natural order, no substrings
202 inline int compare(
203 const String & i_s1,
204 const String & i_s2 );
205 inline int compare(
206 const String & i_s1,
207 const char * i_s2 );
208 inline int compare(
209 const char * i_s1,
210 const String & i_s2 );
211 inline int compare(
212 const char * i_s1,
213 const char * i_s2 );
215 //*** Natural order, substrings
217 int compare(
218 const String & i_s1,
219 csv::str::position i_nStartPosition1,
220 const char * i_s2,
221 csv::str::size i_nLength );
222 inline int compare(
223 const char * i_s1,
224 const char * i_s2,
225 csv::str::size i_nLength );
227 //*** Defined order, no substrings
229 inline int compare(
230 const CharOrder_Table & i_rOrder,
231 const String & i_s1,
232 const char * i_s2 );
233 inline int compare(
234 const CharOrder_Table & i_rOrder,
235 const char * i_s1,
236 const String & i_s2 );
237 int compare(
238 const CharOrder_Table & i_rOrder,
239 const char * i_s1,
240 const char * i_s2 );
241 } // namespace csv
246 //****************** global comparation operators *********************//
248 inline bool operator==(
249 const csv::String & i_s1,
250 const char * i_s2 );
251 inline bool operator!=(
252 const csv::String & i_s1,
253 const char * i_s2 );
254 inline bool operator<(
255 const csv::String & i_s1,
256 const char * i_s2 );
257 inline bool operator>(
258 const csv::String & i_s1,
259 const char * i_s2 );
260 inline bool operator<=(
261 const csv::String & i_s1,
262 const char * i_s2 );
263 inline bool operator>=(
264 const csv::String & i_s1,
265 const char * i_s2 );
267 inline bool operator==(
268 const char * i_s1,
269 const csv::String & i_s2 );
270 inline bool operator!=(
271 const char * i_s1,
272 const csv::String & i_s2 );
273 inline bool operator<(
274 const char * i_s1,
275 const csv::String & i_s2 );
276 inline bool operator>(
277 const char * i_s1,
278 const csv::String & i_s2 );
279 inline bool operator<=(
280 const char * i_s1,
281 const csv::String & i_s2 );
282 inline bool operator>=(
283 const char * i_s1,
284 const csv::String & i_s2 );
287 //****************** global stream operators *********************//
290 inline csv::ostream &
291 operator<<( csv::ostream & o_rOut,
292 const csv::String & i_rSrc );
297 // IMPLEMENTATION
298 namespace csv
302 inline const StringData<char> &
303 String::Str() const
304 { return pd->aStr; }
307 inline const char &
308 String::char_at( position_type i_nPosition ) const
309 { if ( i_nPosition < Str().Size() )
310 return Str().Data()[i_nPosition];
311 return Nulch_();
314 inline bool
315 String::operator==( const self & i_rStr ) const
316 { return compare(i_rStr) == 0; }
318 inline bool
319 String::operator!=( const self & i_rStr ) const
320 { return compare(i_rStr) != 0; }
322 inline bool
323 String::operator<( const self & i_rStr ) const
324 { return compare(i_rStr) < 0; }
326 inline bool
327 String::operator>( const self & i_rStr ) const
328 { return compare(i_rStr) > 0; }
330 inline bool
331 String::operator<=( const self & i_rStr ) const
332 { return compare(i_rStr) <= 0; }
334 inline bool
335 String::operator>=( const self & i_rStr ) const
336 { return compare(i_rStr) >= 0; }
338 inline void
339 String::clear()
340 { operator=( String::Null_() ); }
342 inline const char *
343 String::c_str() const
344 { return Str().Data(); }
346 inline
347 String::operator const char * () const
348 { return c_str(); }
350 inline const char *
351 String::data() const
352 { return c_str(); }
354 inline String::size_type
355 String::size() const
356 { return Str().Size(); }
358 inline bool
359 String::empty() const
360 { return size() == 0; }
362 inline String::size_type
363 String::length() const
364 { return size(); }
366 inline String::const_iterator
367 String::begin() const
368 { return data(); }
370 inline String::const_iterator
371 String::end() const
372 { return data() + size(); }
376 //****************** global compare-functions ********************//
377 inline int
378 compare( const String & i_s1,
379 const String & i_s2 )
380 { return i_s1.compare(i_s2); }
382 inline int
383 compare( const String & i_s1,
384 const char * i_s2 )
385 { return strcmp(i_s1.c_str(), i_s2); }
387 inline int
388 compare( const char * i_s1,
389 const String & i_s2 )
390 { return strcmp(i_s1, i_s2.c_str()); }
392 inline int
393 compare( const char * i_s1,
394 const char * i_s2 )
395 { return strcmp(i_s1, i_s2); }
397 inline int
398 compare( const char * i_s1,
399 const char * i_s2,
400 str::size i_nLength )
401 { return strncmp( i_s1, i_s2, i_nLength ); }
403 inline int
404 compare( const CharOrder_Table & i_rOrder,
405 const String & i_s1,
406 const char * i_s2 )
407 { return compare( i_rOrder, i_s1.c_str(), i_s2 ); }
409 inline int
410 compare( const CharOrder_Table & i_rOrder,
411 const char * i_s1,
412 const String & i_s2 )
413 { return compare( i_rOrder, i_s1, i_s2.c_str() ); }
416 } // namespace csv
419 inline bool
420 operator==( const csv::String & i_s1,
421 const char * i_s2 )
422 { return csv::compare( i_s1, i_s2 ) == 0; }
424 inline bool
425 operator!=( const csv::String & i_s1,
426 const char * i_s2 )
427 { return csv::compare( i_s1, i_s2 ) != 0; }
429 inline bool
430 operator<( const csv::String & i_s1,
431 const char * i_s2 )
432 { return csv::compare( i_s1, i_s2 ) < 0; }
434 inline bool
435 operator>( const csv::String & i_s1,
436 const char * i_s2 )
437 { return csv::compare( i_s1, i_s2 ) > 0; }
439 inline bool
440 operator<=( const csv::String & i_s1,
441 const char * i_s2 )
442 { return csv::compare( i_s1, i_s2 ) <= 0; }
444 inline bool
445 operator>=( const csv::String & i_s1,
446 const char * i_s2 )
447 { return csv::compare( i_s1, i_s2 ) >= 0; }
450 inline bool
451 operator==( const char * i_s1,
452 const csv::String & i_s2 )
453 { return csv::compare( i_s1, i_s2 ) == 0; }
455 inline bool
456 operator!=( const char * i_s1,
457 const csv::String & i_s2 )
458 { return csv::compare( i_s1, i_s2 ) != 0; }
460 inline bool
461 operator<( const char * i_s1,
462 const csv::String & i_s2 )
463 { return csv::compare( i_s1, i_s2 ) < 0; }
465 inline bool
466 operator>( const char * i_s1,
467 const csv::String & i_s2 )
468 { return csv::compare( i_s1, i_s2 ) > 0; }
470 inline bool
471 operator<=( const char * i_s1,
472 const csv::String & i_s2 )
473 { return csv::compare( i_s1, i_s2 ) <= 0; }
475 inline bool
476 operator>=( const char * i_s1,
477 const csv::String & i_s2 )
478 { return csv::compare( i_s1, i_s2 ) >= 0; }
481 //************ global stream operators **************//
484 inline csv::ostream &
485 operator<<( csv::ostream & o_rOut,
486 const csv::String & i_rSrc )
487 { o_rOut << i_rSrc.c_str(); return o_rOut; }
490 //****************** typedefs *********************//
492 namespace csv
495 typedef std::vector<String> StringVector;
499 #endif
501 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */