Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _STRING_HXX
21 : #define _STRING_HXX
22 :
23 : #include <tools/solar.h>
24 : #include <osl/thread.h>
25 : #include <rtl/textenc.h>
26 : #include <rtl/textcvt.h>
27 : #include <rtl/ustrbuf.h>
28 : #include <rtl/string.hxx>
29 : #include <rtl/ustring.hxx>
30 : #include "tools/toolsdllapi.h"
31 : #include "tools/lineend.hxx"
32 :
33 : /*******************************************************************************
34 : *
35 : * THIS CODE IS DEPRECATED. DO NOT USE IT IN ANY NEW CODE.
36 : *
37 : * Use the string classes in rtl/ustring.hxx and rtl/ustrbuf.hxx (and
38 : * rtl/string.hxx and rtl/strbuf.hxx for byte-sized strings) instead. If you
39 : * feel functionality missing from those string classes, please request
40 : * improvements on discuss@openoffice.org.
41 : *
42 : * There will not be any fixes to the code here.
43 : ******************************************************************************/
44 :
45 : class ResId;
46 : class String;
47 : class UniString;
48 :
49 : #define BYTESTRING_TO_UNISTRING_CVTFLAGS (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE |\
50 : RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |\
51 : RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT)
52 :
53 : // CharSet
54 :
55 : #ifndef ENUM_CHARSET_DECLARED
56 : #define ENUM_CHARSET_DECLARED
57 :
58 : typedef rtl_TextEncoding CharSet;
59 :
60 : #endif
61 :
62 : // String-Types
63 :
64 : #ifdef STRING32
65 : #define STRING_NOTFOUND ((xub_StrLen)0x7FFFFFFF)
66 : #define STRING_MATCH ((xub_StrLen)0x7FFFFFFF)
67 : #define STRING_LEN ((xub_StrLen)0x7FFFFFFF)
68 : #define STRING_MAXLEN ((xub_StrLen)0x7FFFFFFF)
69 : #else
70 : #define STRING_NOTFOUND ((xub_StrLen)0xFFFF)
71 : #define STRING_MATCH ((xub_StrLen)0xFFFF)
72 : #define STRING_LEN ((xub_StrLen)0xFFFF)
73 : #define STRING_MAXLEN ((xub_StrLen)0xFFFF)
74 : #endif
75 :
76 : enum StringCompare { COMPARE_LESS = -1, COMPARE_EQUAL = 0, COMPARE_GREATER = 1 };
77 :
78 : //Internal String data
79 :
80 : // Data used for the management of this String
81 : // use only for debugging purposes (never assign to String directly!)
82 :
83 : #ifdef SAL_W32
84 : #pragma pack(push, 4)
85 : #endif
86 :
87 : typedef struct _UniStringData
88 : {
89 : sal_Int32 mnRefCount; // reference counter
90 : sal_Int32 mnLen; // Length of the String
91 : sal_Unicode maStr[1]; // CharArray (String)
92 : } UniStringData;
93 :
94 : #ifdef SAL_W32
95 : #pragma pack(pop)
96 : #endif
97 :
98 : // UniString
99 :
100 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED UniString
101 : {
102 : private:
103 : UniStringData* mpData;
104 :
105 : TOOLS_DLLPRIVATE inline void ImplCopyData();
106 : TOOLS_DLLPRIVATE inline sal_Unicode * ImplCopyStringData(sal_Unicode *);
107 :
108 : UniString( const int* pDummy ); // not implemented: to prevent UniString( NULL )
109 : UniString(int); // not implemented; to detect misuses of
110 : // UniString(sal_Unicode)
111 : void Assign(int); // not implemented; to detect misuses of
112 : // Assign(sal_Unicode)
113 : void operator =(int); // not implemented; to detect misuses
114 : // of operator =(sal_Unicode)
115 : void Append(int); // not implemented; to detect misuses of
116 : // Append(sal_Unicode)
117 : void operator +=(int); // not implemented; to detect misuses
118 : // of operator +=(sal_Unicode)
119 :
120 : //detect and reject use of RTL_CONSTASCII_STRINGPARAM instead of RTL_CONSTASCII_USTRINGPARAM
121 : TOOLS_DLLPRIVATE UniString( const sal_Char*, sal_Int32 );
122 :
123 : //detect and reject wrong way to attempt to create a UniString from a substring of
124 : //a rtl::OString
125 : TOOLS_DLLPRIVATE UniString(const rtl::OString& rByteStr, xub_StrLen nPos, xub_StrLen nLen,
126 : sal_uInt32 nCvtFlags = BYTESTRING_TO_UNISTRING_CVTFLAGS);
127 :
128 : //no longer implemented
129 : TOOLS_DLLPRIVATE UniString( const rtl::OString& rByteStr,
130 : rtl_TextEncoding eTextEncoding,
131 : sal_uInt32 nCvtFlags = BYTESTRING_TO_UNISTRING_CVTFLAGS );
132 : TOOLS_DLLPRIVATE UniString( const sal_Unicode* pCharStr );
133 : TOOLS_DLLPRIVATE UniString( const sal_Unicode* pCharStr, xub_StrLen nLen );
134 : TOOLS_DLLPRIVATE UniString( sal_Unicode c );
135 : TOOLS_DLLPRIVATE UniString& Assign( const sal_Unicode* pCharStr, xub_StrLen nLen );
136 : TOOLS_DLLPRIVATE UniString& Expand( xub_StrLen nCount, sal_Unicode cExpandChar );
137 : public:
138 : UniString();
139 : UniString( const ResId& rResId );
140 : UniString( const UniString& rStr );
141 : UniString( const UniString& rStr, xub_StrLen nPos, xub_StrLen nLen );
142 : UniString( const rtl::OUString& rStr );
143 : UniString(char c); // ...but allow "UniString('a')"
144 : UniString( const sal_Char* pByteStr,
145 : rtl_TextEncoding eTextEncoding,
146 : sal_uInt32 nCvtFlags = BYTESTRING_TO_UNISTRING_CVTFLAGS );
147 : UniString( const sal_Char* pByteStr, xub_StrLen nLen,
148 : rtl_TextEncoding eTextEncoding,
149 : sal_uInt32 nCvtFlags = BYTESTRING_TO_UNISTRING_CVTFLAGS );
150 : ~UniString();
151 :
152 1105475 : operator rtl::OUString () const
153 : {
154 : return rtl::OUString( rtl_uStringBuffer_refReturn(
155 1105475 : reinterpret_cast<rtl_uString*>(mpData)), SAL_NO_ACQUIRE );
156 : }
157 :
158 : #ifdef RTL_FAST_STRING
159 : template< typename T1, typename T2 >
160 1 : UniString( const rtl::OUStringConcat< T1, T2 >& concat )
161 1 : : mpData(NULL) { Assign( rtl::OUString( concat )); }
162 : template< typename T1, typename T2 >
163 4 : UniString& operator =( const rtl::OUStringConcat< T1, T2 >& concat )
164 4 : { return Assign( rtl::OUString( concat )); }
165 : template< typename T1, typename T2 >
166 0 : UniString& operator +=( const rtl::OUStringConcat< T1, T2 >& concat )
167 0 : { return Append( UniString( concat ) ); }
168 : #endif
169 :
170 : static UniString CreateFromInt32( sal_Int32 n, sal_Int16 nRadix = 10 );
171 : static const UniString& EmptyString();
172 : sal_Int32 ToInt32() const;
173 : sal_Int64 ToInt64() const;
174 :
175 : UniString& Assign( const UniString& rStr );
176 : UniString& Assign( const rtl::OUString& rStr );
177 : UniString& Assign( const sal_Unicode* pCharStr );
178 : UniString& Assign( sal_Unicode c );
179 0 : inline UniString & Assign(char c) // ...but allow "Assign('a')"
180 0 : { return Assign(static_cast< sal_Unicode >(c)); }
181 : UniString& AssignAscii( const sal_Char* pAsciiStr );
182 : UniString& AssignAscii( const sal_Char* pAsciiStr, xub_StrLen nLen );
183 1403421 : UniString& operator =( const UniString& rStr )
184 1403421 : { return Assign( rStr ); }
185 368639 : UniString& operator =( const rtl::OUString& rStr )
186 368639 : { return Assign( rStr ); }
187 8211 : UniString& operator =( const sal_Unicode* pCharStr )
188 8211 : { return Assign( pCharStr ); }
189 58 : UniString& operator =( sal_Unicode c )
190 58 : { return Assign( c ); }
191 1 : inline UniString & operator =(char c) // ...but allow "= 'a'"
192 1 : { return operator =(static_cast< sal_Unicode >(c)); }
193 :
194 : UniString& Append( const UniString& rStr );
195 : UniString& Append( const sal_Unicode* pCharStr );
196 : UniString& Append( const sal_Unicode* pCharStr, xub_StrLen nLen );
197 : UniString& Append( sal_Unicode c );
198 195 : inline UniString & Append(char c) // ...but allow "Append('a')"
199 195 : { return Append(static_cast< sal_Unicode >(c)); }
200 : UniString& AppendAscii( const sal_Char* pAsciiStr );
201 : UniString& AppendAscii( const sal_Char* pAsciiStr, xub_StrLen nLen );
202 32119 : UniString& operator +=( const UniString& rStr )
203 32119 : { return Append( rStr ); }
204 26972 : UniString& operator +=( const rtl::OUString& rStr )
205 26972 : { return Append( UniString(rStr) ); }
206 0 : UniString& operator +=( const sal_Unicode* pCharStr )
207 0 : { return Append( pCharStr ); }
208 11588 : UniString& operator +=( sal_Unicode c )
209 11588 : { return Append( c ); }
210 3202 : inline UniString & operator +=(char c) // ...but allow "+= 'a'"
211 3202 : { return operator +=(static_cast< sal_Unicode >(c)); }
212 :
213 : void SetChar( xub_StrLen nIndex, sal_Unicode c );
214 3519274 : sal_Unicode GetChar( xub_StrLen nIndex ) const
215 3519274 : { return mpData->maStr[nIndex]; }
216 :
217 3543266 : xub_StrLen Len() const { return (xub_StrLen)mpData->mnLen; }
218 :
219 : UniString& Insert( const UniString& rStr, xub_StrLen nIndex = STRING_LEN );
220 : UniString& Insert( const UniString& rStr, xub_StrLen nPos, xub_StrLen nLen,
221 : xub_StrLen nIndex = STRING_LEN );
222 : UniString& Insert( sal_Unicode c, xub_StrLen nIndex = STRING_LEN );
223 : UniString& InsertAscii( const sal_Char* pAsciiStr, xub_StrLen nIndex = STRING_LEN );
224 : UniString& Replace( xub_StrLen nIndex, xub_StrLen nLen, const UniString& rStr );
225 : UniString& ReplaceAscii( xub_StrLen nIndex, xub_StrLen nLen,
226 : const sal_Char* pAsciiStr, xub_StrLen nStrLen = STRING_LEN );
227 : UniString& Erase( xub_StrLen nIndex = 0, xub_StrLen nCount = STRING_LEN );
228 : UniString Copy( xub_StrLen nIndex = 0, xub_StrLen nCount = STRING_LEN ) const;
229 :
230 : UniString& ToLowerAscii();
231 : UniString& ToUpperAscii();
232 :
233 : StringCompare CompareTo( const UniString& rStr,
234 : xub_StrLen nLen = STRING_LEN ) const;
235 : StringCompare CompareToAscii( const sal_Char* pAsciiStr,
236 : xub_StrLen nLen = STRING_LEN ) const;
237 : StringCompare CompareIgnoreCaseToAscii( const UniString& rStr,
238 : xub_StrLen nLen = STRING_LEN ) const;
239 : StringCompare CompareIgnoreCaseToAscii( const sal_Char* pAsciiStr,
240 : xub_StrLen nLen = STRING_LEN ) const;
241 : sal_Bool Equals( const UniString& rStr ) const;
242 : sal_Bool EqualsAscii( const sal_Char* pAsciiStr ) const;
243 : sal_Bool EqualsIgnoreCaseAscii( const UniString& rStr ) const;
244 : sal_Bool EqualsIgnoreCaseAscii( const sal_Unicode* pCharStr ) const;
245 : sal_Bool EqualsIgnoreCaseAscii( const sal_Char* pAsciiStr ) const;
246 : sal_Bool Equals( const UniString& rStr,
247 : xub_StrLen nIndex, xub_StrLen nLen ) const;
248 : sal_Bool Equals( const sal_Unicode* pCharStr,
249 : xub_StrLen nIndex, xub_StrLen nLen ) const;
250 : sal_Bool EqualsAscii( const sal_Char* pAsciiStr,
251 : xub_StrLen nIndex, xub_StrLen nLen ) const;
252 : sal_Bool EqualsIgnoreCaseAscii( const UniString& rStr,
253 : xub_StrLen nIndex, xub_StrLen nLen ) const;
254 : sal_Bool EqualsIgnoreCaseAscii( const sal_Char* pAsciiStr,
255 : xub_StrLen nIndex, xub_StrLen nLen ) const;
256 :
257 : xub_StrLen Match( const UniString& rStr ) const;
258 :
259 : xub_StrLen Search( sal_Unicode c, xub_StrLen nIndex = 0 ) const;
260 : xub_StrLen Search( const UniString& rStr, xub_StrLen nIndex = 0 ) const;
261 : xub_StrLen Search( const sal_Unicode* pCharStr, xub_StrLen nIndex = 0 ) const;
262 : xub_StrLen SearchAscii( const sal_Char* pAsciiStr, xub_StrLen nIndex = 0 ) const;
263 : xub_StrLen SearchBackward( sal_Unicode c, xub_StrLen nIndex = STRING_LEN ) const;
264 : xub_StrLen SearchChar( const sal_Unicode* pChars, xub_StrLen nIndex = 0 ) const;
265 :
266 : xub_StrLen SearchAndReplace( sal_Unicode c, sal_Unicode cRep,
267 : xub_StrLen nIndex = 0 );
268 : xub_StrLen SearchAndReplace( const UniString& rStr, const UniString& rRepStr,
269 : xub_StrLen nIndex = 0 );
270 : xub_StrLen SearchAndReplaceAscii( const sal_Char* pAsciiStr, const UniString& rRepStr,
271 : xub_StrLen nIndex = 0 );
272 : void SearchAndReplaceAll( sal_Unicode c, sal_Unicode cRep );
273 : void SearchAndReplaceAll( const UniString& rStr, const UniString& rRepStr );
274 : void SearchAndReplaceAllAscii( const sal_Char* pAsciiStr, const UniString& rRepStr );
275 :
276 : void SetToken( xub_StrLen nToken, sal_Unicode cTok, const UniString& rStr,
277 : xub_StrLen nIndex = 0 );
278 : UniString GetToken( xub_StrLen nToken, sal_Unicode cTok, xub_StrLen& rIndex ) const;
279 : UniString GetToken( xub_StrLen nToken, sal_Unicode cTok = ';' ) const;
280 :
281 1534741 : const sal_Unicode* GetBuffer() const { return mpData->maStr; }
282 : sal_Unicode* GetBufferAccess();
283 : void ReleaseBufferAccess( xub_StrLen nLen = STRING_LEN );
284 : sal_Unicode* AllocBuffer( xub_StrLen nLen );
285 :
286 2854581 : friend sal_Bool operator == ( const UniString& rStr1, const UniString& rStr2 )
287 2854581 : { return rStr1.Equals( rStr2 ); }
288 612249 : friend sal_Bool operator != ( const UniString& rStr1, const UniString& rStr2 )
289 612249 : { return !(operator == ( rStr1, rStr2 )); }
290 17086 : friend sal_Bool operator < ( const UniString& rStr1, const UniString& rStr2 )
291 17086 : { return (rStr1.CompareTo( rStr2 ) == COMPARE_LESS); }
292 0 : friend sal_Bool operator > ( const UniString& rStr1, const UniString& rStr2 )
293 0 : { return (rStr1.CompareTo( rStr2 ) == COMPARE_GREATER); }
294 : friend sal_Bool operator <= ( const UniString& rStr1, const UniString& rStr2 )
295 : { return !(operator > ( rStr1, rStr2 )); }
296 : friend sal_Bool operator >= ( const UniString& rStr1, const UniString& rStr2 )
297 : { return !(operator < ( rStr1, rStr2 )); }
298 : };
299 :
300 35312 : inline UniString UniString::Copy( xub_StrLen nIndex, xub_StrLen nCount ) const
301 : {
302 35312 : return UniString( *this, nIndex, nCount );
303 : }
304 :
305 3329 : inline UniString UniString::GetToken( xub_StrLen nToken, sal_Unicode cTok ) const
306 : {
307 3329 : xub_StrLen nTempPos = 0;
308 3329 : return GetToken( nToken, cTok, nTempPos );
309 : }
310 :
311 : template< typename charT, typename traits > std::basic_ostream<charT, traits> &
312 : operator <<(
313 : std::basic_ostream<charT, traits> & stream, UniString const & string)
314 : {
315 : return stream <<
316 : rtl::OUStringToOString(string, RTL_TEXTENCODING_UTF8).getStr();
317 : // best effort; potentially loses data due to conversion failures
318 : // (stray surrogate halves) and embedded null characters
319 : }
320 :
321 : #ifdef RTL_FAST_STRING
322 : namespace rtl
323 : {
324 : template<>
325 : struct ToStringHelper< UniString >
326 : {
327 4 : static int length( const UniString& s ) { return s.Len(); }
328 4 : static sal_Unicode* addData( sal_Unicode* buffer, const UniString& s ) { return addDataHelper( buffer, s.GetBuffer(), s.Len()); }
329 : static const bool allowOStringConcat = false;
330 : static const bool allowOUStringConcat = true;
331 : };
332 : }
333 :
334 : #endif
335 :
336 : #endif
337 :
338 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|