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