xref: /trunk/main/tools/source/string/tstring.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 
31 #include <string.h>
32 
33 #include "boost/static_assert.hpp"
34 
35 #include "osl/diagnose.h"
36 #ifndef _OSL_INTERLCK_H
37 #include <osl/interlck.h>
38 #endif
39 #ifndef _RTL_ALLOC_H
40 #include <rtl/alloc.h>
41 #endif
42 #ifndef _RTL_MEMORY_H
43 #include <rtl/memory.h>
44 #endif
45 #include <rtl/tencinfo.h>
46 #include <rtl/instance.hxx>
47 
48 #include <tools/string.hxx>
49 #include <impstrg.hxx>
50 
51 // For shared byte convert tables
52 #ifndef _TOOLS_TOOLSIN_HXX
53 #include <toolsin.hxx>
54 #endif
55 
56 #include <tools/debug.hxx>
57 
58 // =======================================================================
59 
60 DBG_NAME( ByteString )
61 DBG_NAMEEX( UniString )
62 
63 // -----------------------------------------------------------------------
64 
65 #define STRCODE         sal_Char
66 #define STRCODEU        unsigned char
67 #define STRING          ByteString
68 #define STRINGDATA      ByteStringData
69 #define DBGCHECKSTRING  DbgCheckByteString
70 #define STRING_TYPE     rtl_String
71 #define STRING_ACQUIRE  rtl_string_acquire
72 #define STRING_RELEASE  rtl_string_release
73 #define STRING_NEW      rtl_string_new
74 
75 
76 // -----------------------------------------------------------------------
77 
78 xub_StrLen ImplStringLen( const sal_Char* pStr )
79 {
80     const sal_Char* pTempStr = pStr;
81     while( *pTempStr )
82         ++pTempStr;
83     return (xub_StrLen)(pTempStr-pStr);
84 }
85 
86 // -----------------------------------------------------------------------
87 
88 xub_StrLen ImplStringLen( const sal_Unicode* pStr )
89 {
90     const sal_Unicode* pTempStr = pStr;
91     while( *pTempStr )
92         ++pTempStr;
93     return (xub_StrLen)(pTempStr-pStr);
94 }
95 
96 // -----------------------------------------------------------------------
97 
98 #include <strimp.cxx>
99 #include <strcvt.cxx>
100 
101 // -----------------------------------------------------------------------
102 
103 ByteString ByteString::CreateFromInt32( sal_Int32 n, sal_Int16 nRadix )
104 {
105     sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
106     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT32 <= STRING_MAXLEN);
107     return ByteString(
108         aBuf,
109         static_cast< xub_StrLen >(rtl_str_valueOfInt32( aBuf, n, nRadix )) );
110 }
111 
112 // -----------------------------------------------------------------------
113 
114 ByteString ByteString::CreateFromInt64( sal_Int64 n, sal_Int16 nRadix )
115 {
116     sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
117     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT64 <= STRING_MAXLEN);
118     return ByteString(
119         aBuf,
120         static_cast< xub_StrLen >(rtl_str_valueOfInt64( aBuf, n, nRadix )) );
121 }
122 
123 // -----------------------------------------------------------------------
124 
125 ByteString ByteString::CreateFromFloat( float f )
126 {
127     sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
128     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFFLOAT <= STRING_MAXLEN);
129     return ByteString(
130         aBuf, static_cast< xub_StrLen >(rtl_str_valueOfFloat( aBuf, f )) );
131 }
132 
133 // -----------------------------------------------------------------------
134 
135 ByteString ByteString::CreateFromDouble( double d )
136 {
137     sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
138     BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFDOUBLE <= STRING_MAXLEN);
139     return ByteString(
140         aBuf, static_cast< xub_StrLen >(rtl_str_valueOfDouble( aBuf, d )) );
141 }
142 
143 // -----------------------------------------------------------------------
144 
145 namespace { struct Empty : public rtl::Static< const ByteString, Empty> {}; }
146 const ByteString& ByteString::EmptyString()
147 {
148     return Empty::get();
149 }
150 
151 // -----------------------------------------------------------------------
152 
153 sal_Int32 ByteString::ToInt32() const
154 {
155     DBG_CHKTHIS( ByteString, DbgCheckByteString );
156 
157     return atoi( mpData->maStr );
158 }
159 
160 // -----------------------------------------------------------------------
161 
162 sal_Int64 ByteString::ToInt64() const
163 {
164     DBG_CHKTHIS( ByteString, DbgCheckByteString );
165 
166     return atoi( mpData->maStr );
167 }
168 
169 // -----------------------------------------------------------------------
170 
171 float ByteString::ToFloat() const
172 {
173     DBG_CHKTHIS( ByteString, DbgCheckByteString );
174 
175     OSL_ENSURE(false, "ByteString::ToFloat unusable");
176     return 0;
177 }
178 
179 // -----------------------------------------------------------------------
180 
181 double ByteString::ToDouble() const
182 {
183     DBG_CHKTHIS( ByteString, DbgCheckByteString );
184 
185     OSL_ENSURE(false, "ByteString::ToDouble unusable");
186     return 0;
187 }
188 
189 // -----------------------------------------------------------------------
190 
191 sal_Bool ByteString::IsLowerAscii() const
192 {
193     DBG_CHKTHIS( ByteString, DbgCheckByteString );
194 
195     sal_Int32 nIndex = 0;
196     sal_Int32 nLen = mpData->mnLen;
197     const sal_Char* pStr = mpData->maStr;
198     while ( nIndex < nLen )
199     {
200         if ( (*pStr >= 65) && (*pStr <= 90) )
201             return sal_False;
202 
203         ++pStr,
204         ++nIndex;
205     }
206 
207     return sal_True;
208 }
209 
210 // -----------------------------------------------------------------------
211 
212 sal_Bool ByteString::IsUpperAscii() const
213 {
214     DBG_CHKTHIS( ByteString, DbgCheckByteString );
215 
216     sal_Int32 nIndex = 0;
217     sal_Int32 nLen = mpData->mnLen;
218     const sal_Char* pStr = mpData->maStr;
219     while ( nIndex < nLen )
220     {
221         if ( (*pStr >= 97) && (*pStr <= 122) )
222             return sal_False;
223 
224         ++pStr,
225         ++nIndex;
226     }
227 
228     return sal_True;
229 }
230 
231 // -----------------------------------------------------------------------
232 
233 sal_Bool ByteString::IsAlphaAscii() const
234 {
235     DBG_CHKTHIS( ByteString, DbgCheckByteString );
236 
237     sal_Int32 nIndex = 0;
238     sal_Int32 nLen = mpData->mnLen;
239     const sal_Char* pStr = mpData->maStr;
240     while ( nIndex < nLen )
241     {
242         if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
243                ((*pStr >= 65) && (*pStr <=  90))) )
244             return sal_False;
245 
246         ++pStr,
247         ++nIndex;
248     }
249 
250     return sal_True;
251 }
252 
253 // -----------------------------------------------------------------------
254 
255 sal_Bool ByteString::IsNumericAscii() const
256 {
257     DBG_CHKTHIS( ByteString, DbgCheckByteString );
258 
259     sal_Int32 nIndex = 0;
260     sal_Int32 nLen = mpData->mnLen;
261     const sal_Char* pStr = mpData->maStr;
262     while ( nIndex < nLen )
263     {
264         if ( !((*pStr >= 48) && (*pStr <= 57)) )
265             return sal_False;
266 
267         ++pStr,
268         ++nIndex;
269     }
270 
271     return sal_True;
272 }
273 
274 // -----------------------------------------------------------------------
275 
276 sal_Bool ByteString::IsAlphaNumericAscii() const
277 {
278     DBG_CHKTHIS( ByteString, DbgCheckByteString );
279 
280     sal_Int32 nIndex = 0;
281     sal_Int32 nLen = mpData->mnLen;
282     const sal_Char* pStr = mpData->maStr;
283     while ( nIndex < nLen )
284     {
285         if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
286                ((*pStr >= 65) && (*pStr <=  90)) ||
287                ((*pStr >= 48) && (*pStr <=  57))) )
288             return sal_False;
289 
290         ++pStr,
291         ++nIndex;
292     }
293 
294     return sal_True;
295 }
296