summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@collabora.com>2019-09-13 15:13:55 +0200
committerLuboš Luňák <l.lunak@collabora.com>2019-09-24 12:58:14 +0200
commit2f5f45921b05904a4be1ff633be09c62cb44ff08 (patch)
treeddd0cd5ef0349706f935d8360db88343347639a3 /include
parenta7d40f575463467698df76f041e558cb3bea7c85 (diff)
support O(U)String::number() for fast string concatenation
When I did the fast string concatenation, I didn't add any support for number(), which simply returned a O(U)String, and so it did the extra allocation/deallocation, although that could be avoided. In order to support this, number() now returns a special temporary return type, similarly to O(U)StringConcat, which allows delaying the concatenation the same way. Also similarly, the change of the return type in some cases requires explicit cast to the actual string type. Usage of OString::getStr() is so extensive in the codebase that I actually added it to the helper class, after that it's only relatively few cases. Change-Id: Iba6e158010e1e458089698c426803052b6f46031 Reviewed-on: https://gerrit.libreoffice.org/78873 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Diffstat (limited to 'include')
-rw-r--r--include/com/sun/star/uno/Any.hxx19
-rw-r--r--include/rtl/strbuf.hxx27
-rw-r--r--include/rtl/string.hxx64
-rw-r--r--include/rtl/stringconcat.hxx158
-rw-r--r--include/rtl/ustrbuf.hxx29
-rw-r--r--include/rtl/ustring.h2
-rw-r--r--include/rtl/ustring.hxx64
7 files changed, 362 insertions, 1 deletions
diff --git a/include/com/sun/star/uno/Any.hxx b/include/com/sun/star/uno/Any.hxx
index d7b3420baecc..05f032fc5b81 100644
--- a/include/com/sun/star/uno/Any.hxx
+++ b/include/com/sun/star/uno/Any.hxx
@@ -249,6 +249,14 @@ template<typename T1, typename T2>
Any toAny(rtl::OUStringConcat<T1, T2> && value)
{ return makeAny(std::move(value)); }
+template<typename T>
+Any makeAny(rtl::OUStringNumber<T> && value)
+{ return Any(OUString(std::move(value))); }
+
+template<typename T>
+Any toAny(rtl::OUStringNumber<T> && value)
+{ return makeAny(std::move(value)); }
+
template<typename T> bool fromAny(Any const & any, T * value) {
assert(value != nullptr);
return any >>= *value;
@@ -295,6 +303,17 @@ inline void SAL_CALL operator <<= ( Any & rAny, rtl::OUStringConcat< C1, C2 >&&
}
template<typename T1, typename T2>
void operator <<=(Any &, rtl::OUStringConcat<T1, T2> const &) = delete;
+template< class C >
+inline void SAL_CALL operator <<= ( Any & rAny, rtl::OUStringNumber< C >&& value )
+{
+ const rtl::OUString str( std::move(value) );
+ const Type & rType = ::cppu::getTypeFavourUnsigned(&str);
+ ::uno_type_any_assign(
+ &rAny, const_cast< rtl::OUString * >( &str ), rType.getTypeLibType(),
+ cpp_acquire, cpp_release );
+}
+template<typename T>
+void operator <<=(Any &, rtl::OUStringNumber<T> const &) = delete;
#endif
#if defined LIBO_INTERNAL_ONLY
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx
index 2b8e6506b6b6..832c48959d47 100644
--- a/include/rtl/strbuf.hxx
+++ b/include/rtl/strbuf.hxx
@@ -222,6 +222,15 @@ public:
*end = '\0';
pData->length = l;
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OStringBuffer( OStringNumber< T >&& n )
+ : OStringBuffer( OString( n ))
+ {}
#endif
/** Assign to this a copy of value.
@@ -287,6 +296,13 @@ public:
pData->length = n;
return *this;
}
+
+ /** @overload @internal */
+ template<typename T>
+ OStringBuffer & operator =(OStringNumber<T> && n)
+ {
+ return *this = OStringBuffer( std::move ( n ));
+ }
#endif
/**
@@ -560,6 +576,17 @@ public:
pData->length = l;
return *this;
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OStringBuffer& append( OStringNumber< T >&& c )
+ {
+ return append( c.buf, c.length );
+ }
+
#endif
/**
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index d06a1b727394..68450da21fa9 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -280,6 +280,15 @@ public:
*end = '\0';
}
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OString( OStringNumber< T >&& n )
+ : OString( n.buf, n.length )
+ {}
#endif
#ifdef LIBO_INTERNAL_ONLY
@@ -383,6 +392,25 @@ public:
}
template<typename T1, typename T2> void operator +=(
OStringConcat<T1, T2> &&) && = delete;
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OString& operator+=( OStringNumber< T >&& n ) & {
+ sal_Int32 l = n.length;
+ if( l == 0 )
+ return *this;
+ l += pData->length;
+ rtl_string_ensureCapacity( &pData, l );
+ char* end = addDataHelper( pData->buffer + pData->length, n.buf, n.length );
+ *end = '\0';
+ pData->length = l;
+ return *this;
+ }
+ template<typename T> void operator +=(
+ OStringNumber<T> &&) && = delete;
#endif
/**
@@ -1602,6 +1630,41 @@ public:
return rtl_str_toDouble( pData->buffer );
}
+#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
+
+ static OStringNumber< int > number( int i, sal_Int16 radix = 10 )
+ {
+ return OStringNumber< int >( i, radix );
+ }
+ static OStringNumber< long long > number( long long ll, sal_Int16 radix = 10 )
+ {
+ return OStringNumber< long long >( ll, radix );
+ }
+ static OStringNumber< unsigned long long > number( unsigned long long ll, sal_Int16 radix = 10 )
+ {
+ return OStringNumber< unsigned long long >( ll, radix );
+ }
+ static OStringNumber< unsigned long long > number( unsigned int i, sal_Int16 radix = 10 )
+ {
+ return number( static_cast< unsigned long long >( i ), radix );
+ }
+ static OStringNumber< long long > number( long i, sal_Int16 radix = 10)
+ {
+ return number( static_cast< long long >( i ), radix );
+ }
+ static OStringNumber< unsigned long long > number( unsigned long i, sal_Int16 radix = 10 )
+ {
+ return number( static_cast< unsigned long long >( i ), radix );
+ }
+ static OStringNumber< float > number( float f )
+ {
+ return OStringNumber< float >( f );
+ }
+ static OStringNumber< double > number( double d )
+ {
+ return OStringNumber< double >( d );
+ }
+#else
/**
Returns the string representation of the integer argument.
@@ -1686,6 +1749,7 @@ public:
rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) );
return OString( pNewData, SAL_NO_ACQUIRE );
}
+#endif
/**
Returns the string representation of the sal_Bool argument.
diff --git a/include/rtl/stringconcat.hxx b/include/rtl/stringconcat.hxx
index 220d0a2cfc9e..50522636ea3d 100644
--- a/include/rtl/stringconcat.hxx
+++ b/include/rtl/stringconcat.hxx
@@ -11,6 +11,8 @@
#define INCLUDED_RTL_STRINGCONCAT_HXX
#include "rtl/stringutils.hxx"
+#include "rtl/string.h"
+#include "rtl/ustring.h"
#include <cstddef>
#include <string.h>
@@ -168,6 +170,9 @@ template<> struct ToStringHelper<OUStringLiteral1_> {
Objects returned by operator+, instead of OString. These objects (possibly recursively) keep a representation of the whole
concatenation operation.
+
+If you get a build error related to this class, you most probably need to explicitly convert the result of a string
+concatenation to OString.
*/
template< typename T1, typename T2 >
struct OStringConcat
@@ -189,6 +194,9 @@ struct OStringConcat
Objects returned by operator+, instead of OUString. These objects (possibly recursively) keep a representation of the whole
concatenation operation.
+
+If you get a build error related to this class, you most probably need to explicitly convert the result of a string
+concatenation to OUString.
*/
template< typename T1, typename T2 >
struct OUStringConcat
@@ -302,6 +310,156 @@ int operator+( const StringConcatInvalid&, const T& )
}
#endif
+/**
+ @internal
+
+Objects returned by OString::number(), instead of OString. These objects keep a representation of the number() operation.
+
+If you get a build error related to this class, you most probably need to explicitly convert the result of calling
+OString::number() to OString.
+*/
+template< typename T >
+struct OStringNumber;
+
+template<>
+struct OStringNumber< int >
+{
+ OStringNumber( int i, sal_Int16 radix )
+ : length( rtl_str_valueOfInt32( buf, i, radix ))
+ {}
+ // OString::number(value).getStr() is very common (writing xml code, ...),
+ // so implement that one also here, to avoid having to explicitly to convert
+ // to OString in all such places
+ const char * getStr() const SAL_RETURNS_NONNULL { return buf; }
+ char buf[RTL_STR_MAX_VALUEOFINT32];
+ const sal_Int32 length;
+};
+
+template<>
+struct OStringNumber< long long >
+{
+ OStringNumber( long long ll, sal_Int16 radix )
+ : length( rtl_str_valueOfInt64( buf, ll, radix ))
+ {}
+ const char * getStr() const SAL_RETURNS_NONNULL { return buf; }
+ char buf[RTL_STR_MAX_VALUEOFINT64];
+ const sal_Int32 length;
+};
+
+template<>
+struct OStringNumber< unsigned long long >
+{
+ OStringNumber( unsigned long long ll, sal_Int16 radix )
+ : length( rtl_str_valueOfUInt64( buf, ll, radix ))
+ {}
+ const char * getStr() const SAL_RETURNS_NONNULL { return buf; }
+ char buf[RTL_STR_MAX_VALUEOFUINT64];
+ const sal_Int32 length;
+};
+
+template<>
+struct OStringNumber< float >
+{
+ OStringNumber( float f )
+ : length( rtl_str_valueOfFloat( buf, f ))
+ {}
+ const char * getStr() const SAL_RETURNS_NONNULL { return buf; }
+ char buf[RTL_STR_MAX_VALUEOFFLOAT];
+ const sal_Int32 length;
+};
+
+template<>
+struct OStringNumber< double >
+{
+ OStringNumber( double d )
+ : length( rtl_str_valueOfDouble( buf, d ))
+ {}
+ const char * getStr() const SAL_RETURNS_NONNULL { return buf; }
+ char buf[RTL_STR_MAX_VALUEOFDOUBLE];
+ const sal_Int32 length;
+};
+
+template< typename T >
+struct ToStringHelper< OStringNumber< T > >
+ {
+ static int length( const OStringNumber< T >& n ) { return n.length; }
+ static char* addData( char* buffer, const OStringNumber< T >& n ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, n.buf, n.length ); }
+ static const bool allowOStringConcat = true;
+ static const bool allowOUStringConcat = false;
+ };
+
+
+/**
+ @internal
+
+Objects returned by OUString::number(), instead of OUString. These objects keep a representation of the number() operation.
+
+If you get a build error related to this class, you most probably need to explicitly convert the result of calling
+OUString::number() to OUString.
+*/
+template< typename T >
+struct OUStringNumber;
+
+template<>
+struct OUStringNumber< int >
+{
+ OUStringNumber( int i, sal_Int16 radix )
+ : length( rtl_ustr_valueOfInt32( buf, i, radix ))
+ {}
+ sal_Unicode buf[RTL_USTR_MAX_VALUEOFINT32];
+ const sal_Int32 length;
+};
+
+template<>
+struct OUStringNumber< long long >
+{
+ OUStringNumber( long long ll, sal_Int16 radix )
+ : length( rtl_ustr_valueOfInt64( buf, ll, radix ))
+ {}
+ sal_Unicode buf[RTL_USTR_MAX_VALUEOFINT64];
+ const sal_Int32 length;
+};
+
+template<>
+struct OUStringNumber< unsigned long long >
+{
+ OUStringNumber( unsigned long long ll, sal_Int16 radix )
+ : length( rtl_ustr_valueOfUInt64( buf, ll, radix ))
+ {}
+ sal_Unicode buf[RTL_USTR_MAX_VALUEOFUINT64];
+ const sal_Int32 length;
+};
+
+template<>
+struct OUStringNumber< float >
+{
+ OUStringNumber( float f )
+ : length( rtl_ustr_valueOfFloat( buf, f ))
+ {}
+ sal_Unicode buf[RTL_USTR_MAX_VALUEOFFLOAT];
+ const sal_Int32 length;
+};
+
+template<>
+struct OUStringNumber< double >
+{
+ OUStringNumber( double d )
+ : length( rtl_ustr_valueOfDouble( buf, d ))
+ {}
+ sal_Unicode buf[RTL_USTR_MAX_VALUEOFDOUBLE];
+ const sal_Int32 length;
+};
+
+template< typename T >
+struct ToStringHelper< OUStringNumber< T > >
+ {
+ static int length( const OUStringNumber< T >& n ) { return n.length; }
+ static sal_Unicode* addData( sal_Unicode* buffer, const OUStringNumber< T >& n ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, n.buf, n.length ); }
+ static const bool allowOStringConcat = false;
+ static const bool allowOUStringConcat = true;
+ };
+
+
} // namespace
#endif
diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx
index 94bf9c789a61..4016eaf210d9 100644
--- a/include/rtl/ustrbuf.hxx
+++ b/include/rtl/ustrbuf.hxx
@@ -224,6 +224,18 @@ public:
pData->length = l;
// TODO realloc in case pData->>length is noticeably smaller than l ?
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OUStringBuffer( OUStringNumber< T >&& n )
+ : pData(NULL)
+ , nCapacity( n.length + 16 )
+ {
+ rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
+ }
#endif
/** Assign to this a copy of value.
*/
@@ -328,6 +340,13 @@ public:
pData->length = n;
return *this;
}
+
+ /** @overload @internal */
+ template<typename T>
+ OUStringBuffer & operator =(OUStringNumber<T> && n)
+ {
+ return *this = OUStringBuffer( std::move( n ));
+ }
#endif
/**
@@ -658,6 +677,16 @@ public:
pData->length = l;
return *this;
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OUStringBuffer& append( OUStringNumber< T >&& c )
+ {
+ return append( c.buf, c.length );
+ }
#endif
/**
diff --git a/include/rtl/ustring.h b/include/rtl/ustring.h
index 90aff10dd169..35e4a7d4ad67 100644
--- a/include/rtl/ustring.h
+++ b/include/rtl/ustring.h
@@ -1000,7 +1000,7 @@ SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_ustr_valueOfInt64(
*/
SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_ustr_valueOfUInt64(
sal_Unicode * str, sal_uInt64 l, sal_Int16 radix ) SAL_THROW_EXTERN_C();
-#define RTL_USTR_MAX_VALUEOFINT64 RTL_STR_MAX_VALUEOFINT64
+#define RTL_USTR_MAX_VALUEOFUINT64 RTL_STR_MAX_VALUEOFUINT64
/** Create the string representation of a float.
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index 18b717fd2b04..a6d9e22c37b5 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -409,6 +409,15 @@ public:
// TODO realloc in case pData->length is noticeably smaller than l?
}
}
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OUString( OUStringNumber< T >&& n )
+ : OUString( n.buf, n.length )
+ {}
#endif
#if defined LIBO_INTERNAL_ONLY
@@ -632,6 +641,25 @@ public:
}
template<typename T1, typename T2> void operator +=(
OUStringConcat<T1, T2> &&) && = delete;
+
+ /**
+ @overload
+ @internal
+ */
+ template< typename T >
+ OUString& operator+=( OUStringNumber< T >&& n ) & {
+ sal_Int32 l = n.length;
+ if( l == 0 )
+ return *this;
+ l += pData->length;
+ rtl_uString_ensureCapacity( &pData, l );
+ sal_Unicode* end = addDataHelper( pData->buffer + pData->length, n.buf, n.length );
+ *end = '\0';
+ pData->length = l;
+ return *this;
+ }
+ template<typename T> void operator +=(
+ OUStringNumber<T> &&) && = delete;
#endif
/**
@@ -3345,6 +3373,41 @@ public:
return aTarget;
}
+#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
+
+ static OUStringNumber< int > number( int i, sal_Int16 radix = 10 )
+ {
+ return OUStringNumber< int >( i, radix );
+ }
+ static OUStringNumber< long long > number( long long ll, sal_Int16 radix = 10 )
+ {
+ return OUStringNumber< long long >( ll, radix );
+ }
+ static OUStringNumber< unsigned long long > number( unsigned long long ll, sal_Int16 radix = 10 )
+ {
+ return OUStringNumber< unsigned long long >( ll, radix );
+ }
+ static OUStringNumber< unsigned long long > number( unsigned int i, sal_Int16 radix = 10 )
+ {
+ return number( static_cast< unsigned long long >( i ), radix );
+ }
+ static OUStringNumber< long long > number( long i, sal_Int16 radix = 10)
+ {
+ return number( static_cast< long long >( i ), radix );
+ }
+ static OUStringNumber< unsigned long long > number( unsigned long i, sal_Int16 radix = 10 )
+ {
+ return number( static_cast< unsigned long long >( i ), radix );
+ }
+ static OUStringNumber< float > number( float f )
+ {
+ return OUStringNumber< float >( f );
+ }
+ static OUStringNumber< double > number( double d )
+ {
+ return OUStringNumber< double >( d );
+ }
+#else
/**
Returns the string representation of the integer argument.
@@ -3432,6 +3495,7 @@ public:
rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfDouble( aBuf, d ) );
return OUString( pNewData, SAL_NO_ACQUIRE );
}
+#endif
/**
Returns the string representation of the sal_Bool argument.