diff options
author | David Ostrovsky <david@ostrovsky.org> | 2013-02-04 11:14:53 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2013-02-04 11:54:11 +0000 |
commit | 55576105d3c9b0a116d7707a6c46b67f7b20a0ce (patch) | |
tree | 5f17795a5bcd6d4aec0c394f92f1a9fbc7f1a5c9 /sal/rtl | |
parent | ... and ssl3 too (diff) | |
download | core-55576105d3c9b0a116d7707a6c46b67f7b20a0ce.tar.gz core-55576105d3c9b0a116d7707a6c46b67f7b20a0ce.zip |
add OUString::toUInt64()
Change-Id: I2051e161219d424d2c2b69faf6f939cfe21fa5f7
Reviewed-on: https://gerrit.libreoffice.org/1980
Reviewed-by: Luboš Luňák <l.lunak@suse.cz>
Tested-by: Luboš Luňák <l.lunak@suse.cz>
Diffstat (limited to 'sal/rtl')
-rw-r--r-- | sal/rtl/source/strtmpl.cxx | 153 |
1 files changed, 83 insertions, 70 deletions
diff --git a/sal/rtl/source/strtmpl.cxx b/sal/rtl/source/strtmpl.cxx index 619f64e4a6ed..f5cbddd8e6f7 100644 --- a/sal/rtl/source/strtmpl.cxx +++ b/sal/rtl/source/strtmpl.cxx @@ -24,6 +24,8 @@ #include <string.h> #include <sal/log.hxx> +#include <limits> +#include <boost/static_assert.hpp> /* inline void rtl_str_ImplCopy( IMPL_RTL_STRCODE* pDest, @@ -914,97 +916,108 @@ sal_Bool SAL_CALL IMPL_RTL_STRNAME( toBoolean )( const IMPL_RTL_STRCODE* pStr ) } /* ----------------------------------------------------------------------- */ +namespace { + template <typename T> static inline T IMPL_RTL_STRNAME( toInt )( const IMPL_RTL_STRCODE* pStr, + sal_Int16 nRadix ) + { + BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_signed); + sal_Bool bNeg; + sal_Int16 nDigit; + T n = 0; -sal_Int32 SAL_CALL IMPL_RTL_STRNAME( toInt32 )( const IMPL_RTL_STRCODE* pStr, - sal_Int16 nRadix ) - SAL_THROW_EXTERN_C() -{ - sal_Bool bNeg; - sal_Int16 nDigit; - sal_Int32 n = 0; + if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) + nRadix = 10; - if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) - nRadix = 10; + /* Skip whitespaces */ + while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) ) + pStr++; - /* Skip whitespaces */ - while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) ) - pStr++; + if ( *pStr == '-' ) + { + bNeg = sal_True; + pStr++; + } + else + { + if ( *pStr == '+' ) + pStr++; + bNeg = sal_False; + } + + while ( *pStr ) + { + nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix ); + if ( nDigit < 0 ) + break; + + n *= nRadix; + n += nDigit; - if ( *pStr == '-' ) - { - bNeg = sal_True; - pStr++; - } - else - { - if ( *pStr == '+' ) pStr++; - bNeg = sal_False; + } + + if ( bNeg ) + return -n; + else + return n; } - while ( *pStr ) + + template <typename T> static inline T IMPL_RTL_STRNAME( toUInt )( const IMPL_RTL_STRCODE* pStr, + sal_Int16 nRadix ) { - nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix ); - if ( nDigit < 0 ) - break; + BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed); + sal_Int16 nDigit; + T n = 0; - n *= nRadix; - n += nDigit; + if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) + nRadix = 10; - pStr++; - } + /* Skip whitespaces */ + while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) ) + ++pStr; + + // skip optional explicit sign + if ( *pStr == '+' ) + ++pStr; + + while ( *pStr ) + { + nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix ); + if ( nDigit < 0 ) + break; + + n *= nRadix; + n += nDigit; + + ++pStr; + } - if ( bNeg ) - return -n; - else return n; + } } -/* ----------------------------------------------------------------------- */ +sal_Int32 SAL_CALL IMPL_RTL_STRNAME( toInt32 )( const IMPL_RTL_STRCODE* pStr, + sal_Int16 nRadix ) + SAL_THROW_EXTERN_C() +{ + return IMPL_RTL_STRNAME( toInt )<sal_Int32>(pStr, nRadix); +} sal_Int64 SAL_CALL IMPL_RTL_STRNAME( toInt64 )( const IMPL_RTL_STRCODE* pStr, sal_Int16 nRadix ) SAL_THROW_EXTERN_C() { - sal_Bool bNeg; - sal_Int16 nDigit; - sal_Int64 n = 0; - - if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) - nRadix = 10; - - /* Skip whitespaces */ - while ( *pStr && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) ) - pStr++; - - if ( *pStr == '-' ) - { - bNeg = sal_True; - pStr++; - } - else - { - if ( *pStr == '+' ) - pStr++; - bNeg = sal_False; - } - - while ( *pStr ) - { - nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix ); - if ( nDigit < 0 ) - break; - - n *= nRadix; - n += nDigit; + return IMPL_RTL_STRNAME( toInt )<sal_Int64>(pStr, nRadix); +} - pStr++; - } +/* ----------------------------------------------------------------------- */ - if ( bNeg ) - return -n; - else - return n; +sal_uInt64 SAL_CALL IMPL_RTL_STRNAME( toUInt64 )( const IMPL_RTL_STRCODE* pStr, + sal_Int16 nRadix ) + SAL_THROW_EXTERN_C() +{ + return IMPL_RTL_STRNAME( toUInt )<sal_uInt64>(pStr, nRadix); } /* ======================================================================= */ |