1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
25 #include <cosv/stringdata.hxx>
26 #include <cosv/str_types.hxx>
27 #include <cosv/csv_ostream.hxx>
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
43 For all compare() functions the return value is like in strcmp().
46 The present version of this class is NOT thread safe.
55 typedef str::size size_type
;
56 typedef str::position position_type
;
58 typedef const char * const_iterator
;
63 /// Intentionally not explicit, smooth casting is intended.
66 /// @precond i_nLength <= strlen(i_str) or i_nLength == str::maxsize.
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.
76 const_iterator i_itBegin
,
77 const_iterator i_itEnd
);
80 const self
& i_rStr
);
85 const self
& i_rStr
);
89 operator const char * () const;
92 const self
& i_rStr
) const;
94 const self
& i_rStr
) const;
96 const self
& i_rStr
) const;
98 const self
& i_rStr
) const;
100 const self
& i_rStr
) const;
102 const self
& i_rStr
) const;
107 /// @precond i_nLength == str::maxsize OR i_nLength < strlen(i_str) .
110 size_type i_nLength
);
111 /// Create a string consisting of a sequence of i_nCount times the same char.
117 const char * c_str() const;
118 const char * data() 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;
133 const self
& i_rStr
) const;
135 const CharOrder_Table
&
137 const self
& i_rStr
) const;
139 //*********** Not yet implemented *********************//
141 const char * i_strToSearch
,
142 position_type i_nSearchStartPosition
= str::npos
) const;
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(
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(
159 position_type i_nSearchStartPosition
= str::npos
) const;
160 //*********** end - not yet implemented *****************//
162 static const self
& Null_();
163 static const char & Nulch_();
169 /// @precond i_nValidLength <= strlen(i_sData) or i_nValidLength == str::maxsize.
171 const char * i_sData
,
172 size_type i_nValidLength
= str::maxsize
);
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
;
184 // Forbidden functions, because this is a refcounted structure.
185 S_Data(const S_Data
&);
186 S_Data
& operator=(const S_Data
&);
190 const StringData
<char> &
198 //********** Global compare functions ***************//
200 //*** Natural order, no substrings
204 const String
& i_s2
);
210 const String
& i_s2
);
215 //*** Natural order, substrings
219 csv::str::position i_nStartPosition1
,
221 csv::str::size i_nLength
);
225 csv::str::size i_nLength
);
227 //*** Defined order, no substrings
230 const CharOrder_Table
& i_rOrder
,
234 const CharOrder_Table
& i_rOrder
,
236 const String
& i_s2
);
238 const CharOrder_Table
& i_rOrder
,
246 //****************** global comparation operators *********************//
248 inline bool operator==(
249 const csv::String
& i_s1
,
251 inline bool operator!=(
252 const csv::String
& i_s1
,
254 inline bool operator<(
255 const csv::String
& i_s1
,
257 inline bool operator>(
258 const csv::String
& i_s1
,
260 inline bool operator<=(
261 const csv::String
& i_s1
,
263 inline bool operator>=(
264 const csv::String
& i_s1
,
267 inline bool operator==(
269 const csv::String
& i_s2
);
270 inline bool operator!=(
272 const csv::String
& i_s2
);
273 inline bool operator<(
275 const csv::String
& i_s2
);
276 inline bool operator>(
278 const csv::String
& i_s2
);
279 inline bool operator<=(
281 const csv::String
& i_s2
);
282 inline bool operator>=(
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
);
302 inline const StringData
<char> &
308 String::char_at( position_type i_nPosition
) const
309 { if ( i_nPosition
< Str().Size() )
310 return Str().Data()[i_nPosition
];
315 String::operator==( const self
& i_rStr
) const
316 { return compare(i_rStr
) == 0; }
319 String::operator!=( const self
& i_rStr
) const
320 { return compare(i_rStr
) != 0; }
323 String::operator<( const self
& i_rStr
) const
324 { return compare(i_rStr
) < 0; }
327 String::operator>( const self
& i_rStr
) const
328 { return compare(i_rStr
) > 0; }
331 String::operator<=( const self
& i_rStr
) const
332 { return compare(i_rStr
) <= 0; }
335 String::operator>=( const self
& i_rStr
) const
336 { return compare(i_rStr
) >= 0; }
340 { operator=( String::Null_() ); }
343 String::c_str() const
344 { return Str().Data(); }
347 String::operator const char * () const
354 inline String::size_type
356 { return Str().Size(); }
359 String::empty() const
360 { return size() == 0; }
362 inline String::size_type
363 String::length() const
366 inline String::const_iterator
367 String::begin() const
370 inline String::const_iterator
372 { return data() + size(); }
376 //****************** global compare-functions ********************//
378 compare( const String
& i_s1
,
379 const String
& i_s2
)
380 { return i_s1
.compare(i_s2
); }
383 compare( const String
& i_s1
,
385 { return strcmp(i_s1
.c_str(), i_s2
); }
388 compare( const char * i_s1
,
389 const String
& i_s2
)
390 { return strcmp(i_s1
, i_s2
.c_str()); }
393 compare( const char * i_s1
,
395 { return strcmp(i_s1
, i_s2
); }
398 compare( const char * i_s1
,
400 str::size i_nLength
)
401 { return strncmp( i_s1
, i_s2
, i_nLength
); }
404 compare( const CharOrder_Table
& i_rOrder
,
407 { return compare( i_rOrder
, i_s1
.c_str(), i_s2
); }
410 compare( const CharOrder_Table
& i_rOrder
,
412 const String
& i_s2
)
413 { return compare( i_rOrder
, i_s1
, i_s2
.c_str() ); }
420 operator==( const csv::String
& i_s1
,
422 { return csv::compare( i_s1
, i_s2
) == 0; }
425 operator!=( const csv::String
& i_s1
,
427 { return csv::compare( i_s1
, i_s2
) != 0; }
430 operator<( const csv::String
& i_s1
,
432 { return csv::compare( i_s1
, i_s2
) < 0; }
435 operator>( const csv::String
& i_s1
,
437 { return csv::compare( i_s1
, i_s2
) > 0; }
440 operator<=( const csv::String
& i_s1
,
442 { return csv::compare( i_s1
, i_s2
) <= 0; }
445 operator>=( const csv::String
& i_s1
,
447 { return csv::compare( i_s1
, i_s2
) >= 0; }
451 operator==( const char * i_s1
,
452 const csv::String
& i_s2
)
453 { return csv::compare( i_s1
, i_s2
) == 0; }
456 operator!=( const char * i_s1
,
457 const csv::String
& i_s2
)
458 { return csv::compare( i_s1
, i_s2
) != 0; }
461 operator<( const char * i_s1
,
462 const csv::String
& i_s2
)
463 { return csv::compare( i_s1
, i_s2
) < 0; }
466 operator>( const char * i_s1
,
467 const csv::String
& i_s2
)
468 { return csv::compare( i_s1
, i_s2
) > 0; }
471 operator<=( const char * i_s1
,
472 const csv::String
& i_s2
)
473 { return csv::compare( i_s1
, i_s2
) <= 0; }
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 *********************//
495 typedef std::vector
<String
> StringVector
;
501 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */