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