xref: /aoo41x/main/tools/source/string/tustring.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 #ifndef _OSL_INTERLCK_H
32cdf0e10cSrcweir #include <osl/interlck.h>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #ifndef _RTL_ALLOC_H
35cdf0e10cSrcweir #include <rtl/alloc.h>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #ifndef _RTL_MEMORY_H
38cdf0e10cSrcweir #include <rtl/memory.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #include <rtl/tencinfo.h>
41cdf0e10cSrcweir #include <rtl/instance.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <tools/string.hxx>
44cdf0e10cSrcweir #include <impstrg.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <tools/debug.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // =======================================================================
49cdf0e10cSrcweir 
50cdf0e10cSrcweir DBG_NAME( UniString )
DBG_NAMEEX(ByteString)51cdf0e10cSrcweir DBG_NAMEEX( ByteString )
52cdf0e10cSrcweir 
53cdf0e10cSrcweir // -----------------------------------------------------------------------
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define STRCODE 		sal_Unicode
56cdf0e10cSrcweir #define STRCODEU        sal_Unicode
57cdf0e10cSrcweir #define STRING			UniString
58cdf0e10cSrcweir #define STRINGDATA		UniStringData
59cdf0e10cSrcweir #define DBGCHECKSTRING	DbgCheckUniString
60cdf0e10cSrcweir #define STRING_TYPE     rtl_uString
61cdf0e10cSrcweir #define STRING_ACQUIRE  rtl_uString_acquire
62cdf0e10cSrcweir #define STRING_RELEASE  rtl_uString_release
63cdf0e10cSrcweir #define STRING_NEW      rtl_uString_new
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // -----------------------------------------------------------------------
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #include <strimp.cxx>
68cdf0e10cSrcweir #include <strucvt.cxx>
69cdf0e10cSrcweir #include <strascii.cxx>
70cdf0e10cSrcweir 
71cdf0e10cSrcweir UniString::UniString(char c): mpData(ImplAllocData(1)) { mpData->maStr[0] = c; }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // -----------------------------------------------------------------------
74cdf0e10cSrcweir 
CreateFromInt32(sal_Int32 n,sal_Int16 nRadix)75cdf0e10cSrcweir UniString UniString::CreateFromInt32( sal_Int32 n, sal_Int16 nRadix )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT32];
78cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_USTR_MAX_VALUEOFINT32 <= STRING_MAXLEN);
79cdf0e10cSrcweir 	return UniString(
80cdf0e10cSrcweir         aBuf,
81cdf0e10cSrcweir         static_cast< xub_StrLen >(rtl_ustr_valueOfInt32( aBuf, n, nRadix )) );
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // -----------------------------------------------------------------------
85cdf0e10cSrcweir 
CreateFromInt64(sal_Int64 n,sal_Int16 nRadix)86cdf0e10cSrcweir UniString UniString::CreateFromInt64( sal_Int64 n, sal_Int16 nRadix )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT64];
89cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_USTR_MAX_VALUEOFINT64 <= STRING_MAXLEN);
90cdf0e10cSrcweir 	return UniString(
91cdf0e10cSrcweir         aBuf,
92cdf0e10cSrcweir         static_cast< xub_StrLen >(rtl_ustr_valueOfInt64( aBuf, n, nRadix )) );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir // -----------------------------------------------------------------------
96cdf0e10cSrcweir 
CreateFromFloat(float f)97cdf0e10cSrcweir UniString UniString::CreateFromFloat( float f )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFFLOAT];
100cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_USTR_MAX_VALUEOFFLOAT <= STRING_MAXLEN);
101cdf0e10cSrcweir 	return UniString(
102cdf0e10cSrcweir         aBuf, static_cast< xub_StrLen >(rtl_ustr_valueOfFloat( aBuf, f )) );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir // -----------------------------------------------------------------------
106cdf0e10cSrcweir 
CreateFromDouble(double d)107cdf0e10cSrcweir UniString UniString::CreateFromDouble( double d )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFDOUBLE];
110cdf0e10cSrcweir     BOOST_STATIC_ASSERT(RTL_USTR_MAX_VALUEOFDOUBLE <= STRING_MAXLEN);
111cdf0e10cSrcweir 	return UniString(
112cdf0e10cSrcweir         aBuf, static_cast< xub_StrLen >(rtl_ustr_valueOfDouble( aBuf, d )) );
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir // -----------------------------------------------------------------------
116cdf0e10cSrcweir 
117cdf0e10cSrcweir namespace { struct Empty : public rtl::Static< const UniString, Empty> {}; }
EmptyString()118cdf0e10cSrcweir const UniString& UniString::EmptyString()
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	return Empty::get();
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // -----------------------------------------------------------------------
124cdf0e10cSrcweir 
ToInt32() const125cdf0e10cSrcweir sal_Int32 UniString::ToInt32() const
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	return rtl_ustr_toInt32( mpData->maStr, 10 );
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // -----------------------------------------------------------------------
133cdf0e10cSrcweir 
ToInt64() const134cdf0e10cSrcweir sal_Int64 UniString::ToInt64() const
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	return rtl_ustr_toInt64( mpData->maStr, 10 );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir // -----------------------------------------------------------------------
142cdf0e10cSrcweir 
ToFloat() const143cdf0e10cSrcweir float UniString::ToFloat() const
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	return rtl_ustr_toFloat( mpData->maStr );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir // -----------------------------------------------------------------------
151cdf0e10cSrcweir 
ToDouble() const152cdf0e10cSrcweir double UniString::ToDouble() const
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	DBG_CHKTHIS( UniString, DbgCheckUniString );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	return rtl_ustr_toDouble( mpData->maStr );
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159