xref: /aoo42x/main/tools/source/string/tstring.cxx (revision 89b56da7)
1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*89b56da7SAndrew Rist  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*89b56da7SAndrew Rist  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19*89b56da7SAndrew Rist  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "boost/static_assert.hpp"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "osl/diagnose.h"
32cdf0e10cSrcweir #ifndef _OSL_INTERLCK_H
33cdf0e10cSrcweir #include <osl/interlck.h>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #ifndef _RTL_ALLOC_H
36cdf0e10cSrcweir #include <rtl/alloc.h>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _RTL_MEMORY_H
39cdf0e10cSrcweir #include <rtl/memory.h>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #include <rtl/tencinfo.h>
42cdf0e10cSrcweir #include <rtl/instance.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <tools/string.hxx>
45cdf0e10cSrcweir #include <impstrg.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // For shared byte convert tables
48cdf0e10cSrcweir #ifndef _TOOLS_TOOLSIN_HXX
49cdf0e10cSrcweir #include <toolsin.hxx>
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <tools/debug.hxx>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // =======================================================================
55cdf0e10cSrcweir 
56cdf0e10cSrcweir DBG_NAME( ByteString )
DBG_NAMEEX(UniString)57cdf0e10cSrcweir DBG_NAMEEX( UniString )
58cdf0e10cSrcweir 
59cdf0e10cSrcweir // -----------------------------------------------------------------------
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #define STRCODE 		sal_Char
62cdf0e10cSrcweir #define STRCODEU        unsigned char
63cdf0e10cSrcweir #define STRING			ByteString
64cdf0e10cSrcweir #define STRINGDATA		ByteStringData
65cdf0e10cSrcweir #define DBGCHECKSTRING	DbgCheckByteString
66cdf0e10cSrcweir #define STRING_TYPE     rtl_String
67cdf0e10cSrcweir #define STRING_ACQUIRE  rtl_string_acquire
68cdf0e10cSrcweir #define STRING_RELEASE  rtl_string_release
69cdf0e10cSrcweir #define STRING_NEW      rtl_string_new
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir // -----------------------------------------------------------------------
73cdf0e10cSrcweir 
74cdf0e10cSrcweir xub_StrLen ImplStringLen( const sal_Char* pStr )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	const sal_Char* pTempStr = pStr;
77cdf0e10cSrcweir 	while( *pTempStr )
78cdf0e10cSrcweir 		++pTempStr;
79cdf0e10cSrcweir 	return (xub_StrLen)(pTempStr-pStr);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir // -----------------------------------------------------------------------
83cdf0e10cSrcweir 
ImplStringLen(const sal_Unicode * pStr)84cdf0e10cSrcweir xub_StrLen ImplStringLen( const sal_Unicode* pStr )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	const sal_Unicode* pTempStr = pStr;
87cdf0e10cSrcweir 	while( *pTempStr )
88cdf0e10cSrcweir 		++pTempStr;
89cdf0e10cSrcweir 	return (xub_StrLen)(pTempStr-pStr);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir // -----------------------------------------------------------------------
93cdf0e10cSrcweir 
94cdf0e10cSrcweir #include <strimp.cxx>
95cdf0e10cSrcweir #include <strcvt.cxx>
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // -----------------------------------------------------------------------
98cdf0e10cSrcweir 
CreateFromInt32(sal_Int32 n,sal_Int16 nRadix)99cdf0e10cSrcweir ByteString ByteString::CreateFromInt32( sal_Int32 n, sal_Int16 nRadix )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
102cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT32 <= STRING_MAXLEN);
103cdf0e10cSrcweir 	return ByteString(
104cdf0e10cSrcweir         aBuf,
105cdf0e10cSrcweir         static_cast< xub_StrLen >(rtl_str_valueOfInt32( aBuf, n, nRadix )) );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir // -----------------------------------------------------------------------
109cdf0e10cSrcweir 
CreateFromInt64(sal_Int64 n,sal_Int16 nRadix)110cdf0e10cSrcweir ByteString ByteString::CreateFromInt64( sal_Int64 n, sal_Int16 nRadix )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
113cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT64 <= STRING_MAXLEN);
114cdf0e10cSrcweir 	return ByteString(
115cdf0e10cSrcweir         aBuf,
116cdf0e10cSrcweir         static_cast< xub_StrLen >(rtl_str_valueOfInt64( aBuf, n, nRadix )) );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // -----------------------------------------------------------------------
120cdf0e10cSrcweir 
CreateFromFloat(float f)121cdf0e10cSrcweir ByteString ByteString::CreateFromFloat( float f )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
124cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFFLOAT <= STRING_MAXLEN);
125cdf0e10cSrcweir 	return ByteString(
126cdf0e10cSrcweir         aBuf, static_cast< xub_StrLen >(rtl_str_valueOfFloat( aBuf, f )) );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir // -----------------------------------------------------------------------
130cdf0e10cSrcweir 
CreateFromDouble(double d)131cdf0e10cSrcweir ByteString ByteString::CreateFromDouble( double d )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
134cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFDOUBLE <= STRING_MAXLEN);
135cdf0e10cSrcweir 	return ByteString(
136cdf0e10cSrcweir         aBuf, static_cast< xub_StrLen >(rtl_str_valueOfDouble( aBuf, d )) );
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir // -----------------------------------------------------------------------
140cdf0e10cSrcweir 
141cdf0e10cSrcweir namespace { struct Empty : public rtl::Static< const ByteString, Empty> {}; }
EmptyString()142cdf0e10cSrcweir const ByteString& ByteString::EmptyString()
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	return Empty::get();
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // -----------------------------------------------------------------------
148cdf0e10cSrcweir 
ToInt32() const149cdf0e10cSrcweir sal_Int32 ByteString::ToInt32() const
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	return atoi( mpData->maStr );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // -----------------------------------------------------------------------
157cdf0e10cSrcweir 
ToInt64() const158cdf0e10cSrcweir sal_Int64 ByteString::ToInt64() const
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	return atoi( mpData->maStr );
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir // -----------------------------------------------------------------------
166cdf0e10cSrcweir 
ToFloat() const167cdf0e10cSrcweir float ByteString::ToFloat() const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     OSL_ENSURE(false, "ByteString::ToFloat unusable");
172cdf0e10cSrcweir 	return 0;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir // -----------------------------------------------------------------------
176cdf0e10cSrcweir 
ToDouble() const177cdf0e10cSrcweir double ByteString::ToDouble() const
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     OSL_ENSURE(false, "ByteString::ToDouble unusable");
182cdf0e10cSrcweir 	return 0;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // -----------------------------------------------------------------------
186cdf0e10cSrcweir 
IsLowerAscii() const187cdf0e10cSrcweir sal_Bool ByteString::IsLowerAscii() const
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
192cdf0e10cSrcweir 	sal_Int32 nLen = mpData->mnLen;
193cdf0e10cSrcweir 	const sal_Char* pStr = mpData->maStr;
194cdf0e10cSrcweir 	while ( nIndex < nLen )
195cdf0e10cSrcweir 	{
196cdf0e10cSrcweir 		if ( (*pStr >= 65) && (*pStr <= 90) )
197cdf0e10cSrcweir 			return sal_False;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		++pStr,
200cdf0e10cSrcweir 		++nIndex;
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	return sal_True;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir // -----------------------------------------------------------------------
207cdf0e10cSrcweir 
IsUpperAscii() const208cdf0e10cSrcweir sal_Bool ByteString::IsUpperAscii() const
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
213cdf0e10cSrcweir 	sal_Int32 nLen = mpData->mnLen;
214cdf0e10cSrcweir 	const sal_Char* pStr = mpData->maStr;
215cdf0e10cSrcweir 	while ( nIndex < nLen )
216cdf0e10cSrcweir 	{
217cdf0e10cSrcweir 		if ( (*pStr >= 97) && (*pStr <= 122) )
218cdf0e10cSrcweir 			return sal_False;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 		++pStr,
221cdf0e10cSrcweir 		++nIndex;
222cdf0e10cSrcweir 	}
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	return sal_True;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir // -----------------------------------------------------------------------
228cdf0e10cSrcweir 
IsAlphaAscii() const229cdf0e10cSrcweir sal_Bool ByteString::IsAlphaAscii() const
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
234cdf0e10cSrcweir 	sal_Int32 nLen = mpData->mnLen;
235cdf0e10cSrcweir 	const sal_Char* pStr = mpData->maStr;
236cdf0e10cSrcweir 	while ( nIndex < nLen )
237cdf0e10cSrcweir 	{
238cdf0e10cSrcweir 		if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
239cdf0e10cSrcweir 			   ((*pStr >= 65) && (*pStr <=	90))) )
240cdf0e10cSrcweir 			return sal_False;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 		++pStr,
243cdf0e10cSrcweir 		++nIndex;
244cdf0e10cSrcweir 	}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	return sal_True;
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir // -----------------------------------------------------------------------
250cdf0e10cSrcweir 
IsNumericAscii() const251cdf0e10cSrcweir sal_Bool ByteString::IsNumericAscii() const
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
256cdf0e10cSrcweir 	sal_Int32 nLen = mpData->mnLen;
257cdf0e10cSrcweir 	const sal_Char* pStr = mpData->maStr;
258cdf0e10cSrcweir 	while ( nIndex < nLen )
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 		if ( !((*pStr >= 48) && (*pStr <= 57)) )
261cdf0e10cSrcweir 			return sal_False;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		++pStr,
264cdf0e10cSrcweir 		++nIndex;
265cdf0e10cSrcweir 	}
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	return sal_True;
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir // -----------------------------------------------------------------------
271cdf0e10cSrcweir 
IsAlphaNumericAscii() const272cdf0e10cSrcweir sal_Bool ByteString::IsAlphaNumericAscii() const
273cdf0e10cSrcweir {
274cdf0e10cSrcweir 	DBG_CHKTHIS( ByteString, DbgCheckByteString );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
277cdf0e10cSrcweir 	sal_Int32 nLen = mpData->mnLen;
278cdf0e10cSrcweir 	const sal_Char* pStr = mpData->maStr;
279cdf0e10cSrcweir 	while ( nIndex < nLen )
280cdf0e10cSrcweir 	{
281cdf0e10cSrcweir 		if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
282cdf0e10cSrcweir 			   ((*pStr >= 65) && (*pStr <=	90)) ||
283cdf0e10cSrcweir 			   ((*pStr >= 48) && (*pStr <=	57))) )
284cdf0e10cSrcweir 			return sal_False;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 		++pStr,
287cdf0e10cSrcweir 		++nIndex;
288cdf0e10cSrcweir 	}
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 	return sal_True;
291cdf0e10cSrcweir }
292