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