Update ooo320-m1
[ooovba.git] / tools / source / string / tstring.cxx
bloba71b60658351e14c5c69d5fb5959dab9a16bf1f5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tstring.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 #include <string.h>
36 #include "boost/static_assert.hpp"
38 #include "osl/diagnose.h"
39 #ifndef _OSL_INTERLCK_H
40 #include <osl/interlck.h>
41 #endif
42 #ifndef _RTL_ALLOC_H
43 #include <rtl/alloc.h>
44 #endif
45 #ifndef _RTL_MEMORY_H
46 #include <rtl/memory.h>
47 #endif
48 #include <rtl/tencinfo.h>
49 #include <rtl/instance.hxx>
51 #include <tools/string.hxx>
52 #include <impstrg.hxx>
54 // For shared byte convert tables
55 #ifndef _TOOLS_TOOLSIN_HXX
56 #include <toolsin.hxx>
57 #endif
59 #include <tools/debug.hxx>
61 // =======================================================================
63 DBG_NAME( ByteString )
64 DBG_NAMEEX( UniString )
66 // -----------------------------------------------------------------------
68 #define STRCODE sal_Char
69 #define STRCODEU unsigned char
70 #define STRING ByteString
71 #define STRINGDATA ByteStringData
72 #define DBGCHECKSTRING DbgCheckByteString
73 #define STRING_TYPE rtl_String
74 #define STRING_ACQUIRE rtl_string_acquire
75 #define STRING_RELEASE rtl_string_release
76 #define STRING_NEW rtl_string_new
79 // -----------------------------------------------------------------------
81 xub_StrLen ImplStringLen( const sal_Char* pStr )
83 const sal_Char* pTempStr = pStr;
84 while( *pTempStr )
85 ++pTempStr;
86 return (xub_StrLen)(pTempStr-pStr);
89 // -----------------------------------------------------------------------
91 xub_StrLen ImplStringLen( const sal_Unicode* pStr )
93 const sal_Unicode* pTempStr = pStr;
94 while( *pTempStr )
95 ++pTempStr;
96 return (xub_StrLen)(pTempStr-pStr);
99 // -----------------------------------------------------------------------
101 #include <strimp.cxx>
102 #include <strcvt.cxx>
104 // -----------------------------------------------------------------------
106 ByteString ByteString::CreateFromInt32( sal_Int32 n, sal_Int16 nRadix )
108 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
109 BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT32 <= STRING_MAXLEN);
110 return ByteString(
111 aBuf,
112 static_cast< xub_StrLen >(rtl_str_valueOfInt32( aBuf, n, nRadix )) );
115 // -----------------------------------------------------------------------
117 ByteString ByteString::CreateFromInt64( sal_Int64 n, sal_Int16 nRadix )
119 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
120 BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFINT64 <= STRING_MAXLEN);
121 return ByteString(
122 aBuf,
123 static_cast< xub_StrLen >(rtl_str_valueOfInt64( aBuf, n, nRadix )) );
126 // -----------------------------------------------------------------------
128 ByteString ByteString::CreateFromFloat( float f )
130 sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
131 BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFFLOAT <= STRING_MAXLEN);
132 return ByteString(
133 aBuf, static_cast< xub_StrLen >(rtl_str_valueOfFloat( aBuf, f )) );
136 // -----------------------------------------------------------------------
138 ByteString ByteString::CreateFromDouble( double d )
140 sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
141 BOOST_STATIC_ASSERT(RTL_STR_MAX_VALUEOFDOUBLE <= STRING_MAXLEN);
142 return ByteString(
143 aBuf, static_cast< xub_StrLen >(rtl_str_valueOfDouble( aBuf, d )) );
146 // -----------------------------------------------------------------------
148 namespace { struct Empty : public rtl::Static< const ByteString, Empty> {}; }
149 const ByteString& ByteString::EmptyString()
151 return Empty::get();
154 // -----------------------------------------------------------------------
156 sal_Int32 ByteString::ToInt32() const
158 DBG_CHKTHIS( ByteString, DbgCheckByteString );
160 return atoi( mpData->maStr );
163 // -----------------------------------------------------------------------
165 sal_Int64 ByteString::ToInt64() const
167 DBG_CHKTHIS( ByteString, DbgCheckByteString );
169 return atoi( mpData->maStr );
172 // -----------------------------------------------------------------------
174 float ByteString::ToFloat() const
176 DBG_CHKTHIS( ByteString, DbgCheckByteString );
178 OSL_ENSURE(false, "ByteString::ToFloat unusable");
179 return 0;
182 // -----------------------------------------------------------------------
184 double ByteString::ToDouble() const
186 DBG_CHKTHIS( ByteString, DbgCheckByteString );
188 OSL_ENSURE(false, "ByteString::ToDouble unusable");
189 return 0;
192 // -----------------------------------------------------------------------
194 BOOL ByteString::IsLowerAscii() const
196 DBG_CHKTHIS( ByteString, DbgCheckByteString );
198 sal_Int32 nIndex = 0;
199 sal_Int32 nLen = mpData->mnLen;
200 const sal_Char* pStr = mpData->maStr;
201 while ( nIndex < nLen )
203 if ( (*pStr >= 65) && (*pStr <= 90) )
204 return FALSE;
206 ++pStr,
207 ++nIndex;
210 return TRUE;
213 // -----------------------------------------------------------------------
215 BOOL ByteString::IsUpperAscii() const
217 DBG_CHKTHIS( ByteString, DbgCheckByteString );
219 sal_Int32 nIndex = 0;
220 sal_Int32 nLen = mpData->mnLen;
221 const sal_Char* pStr = mpData->maStr;
222 while ( nIndex < nLen )
224 if ( (*pStr >= 97) && (*pStr <= 122) )
225 return FALSE;
227 ++pStr,
228 ++nIndex;
231 return TRUE;
234 // -----------------------------------------------------------------------
236 BOOL ByteString::IsAlphaAscii() const
238 DBG_CHKTHIS( ByteString, DbgCheckByteString );
240 sal_Int32 nIndex = 0;
241 sal_Int32 nLen = mpData->mnLen;
242 const sal_Char* pStr = mpData->maStr;
243 while ( nIndex < nLen )
245 if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
246 ((*pStr >= 65) && (*pStr <= 90))) )
247 return FALSE;
249 ++pStr,
250 ++nIndex;
253 return TRUE;
256 // -----------------------------------------------------------------------
258 BOOL ByteString::IsNumericAscii() const
260 DBG_CHKTHIS( ByteString, DbgCheckByteString );
262 sal_Int32 nIndex = 0;
263 sal_Int32 nLen = mpData->mnLen;
264 const sal_Char* pStr = mpData->maStr;
265 while ( nIndex < nLen )
267 if ( !((*pStr >= 48) && (*pStr <= 57)) )
268 return FALSE;
270 ++pStr,
271 ++nIndex;
274 return TRUE;
277 // -----------------------------------------------------------------------
279 BOOL ByteString::IsAlphaNumericAscii() const
281 DBG_CHKTHIS( ByteString, DbgCheckByteString );
283 sal_Int32 nIndex = 0;
284 sal_Int32 nLen = mpData->mnLen;
285 const sal_Char* pStr = mpData->maStr;
286 while ( nIndex < nLen )
288 if ( !(((*pStr >= 97) && (*pStr <= 122)) ||
289 ((*pStr >= 65) && (*pStr <= 90)) ||
290 ((*pStr >= 48) && (*pStr <= 57))) )
291 return FALSE;
293 ++pStr,
294 ++nIndex;
297 return TRUE;