/usr/include/libreoffice/rtl/stringconcat.hxx is in libreoffice-dev-common 1:6.0.3-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_RTL_STRINGCONCAT_HXX
#define INCLUDED_RTL_STRINGCONCAT_HXX
#include "rtl/stringutils.hxx"
#include <cstddef>
#include <string.h>
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
#if defined RTL_STRING_UNITTEST_CONCAT
extern bool rtl_string_unittest_invalid_concat;
#endif
#ifdef RTL_STRING_UNITTEST
#define rtl rtlunittest
#endif
namespace rtl
{
#ifdef RTL_STRING_UNITTEST
#undef rtl
#endif
/*
Implementation of efficient string concatenation.
The whole system is built around two basic template classes:
- ToStringHelper< T > - for each T it can give the length of the resulting string representation and can write
this string representation to a buffer
- O(U)StringConcat< T1, T2 > - operator+ now, instead of creating O(U)String object, returns only this helper object,
that keeps a reference to both operator+ operands; only when converted to O(U)String it will actually create
the resulting string object using ToStringHelper, creating directly the resulting object without any string
intermediate objects
As all the code is inline methods, it allows for extensive optimization and will usually result in very effective code
(even surpassing strlen/strcat and equalling handwritten), while allowing for very easy and intuitive syntax.
*/
/**
@internal
Helper class for converting a given type to a string representation.
*/
template< typename T >
struct ToStringHelper
{
/// Return length of the string representation of the given object (if not known exactly, it needs to be the maximum).
static int length( const T& );
/// Add 8-bit representation of the given object to the given buffer and return position right after the added data.
static char* addData( char* buffer, const T& ) SAL_RETURNS_NONNULL;
/// Add Unicode representation of the given object to the given buffer and return position right after the added data.
static sal_Unicode* addData( sal_Unicode* buffer, const T& ) SAL_RETURNS_NONNULL;
/// If true, T can be used in concatenation resulting in OString.
static const bool allowOStringConcat = false;
/// If true, T can be used in concatenation resulting in OUString.
static const bool allowOUStringConcat = false;
};
inline
char* addDataHelper( char* buffer, const char* data, int length )
{
memcpy( buffer, data, length );
return buffer + length;
}
inline
sal_Unicode* addDataHelper( sal_Unicode* buffer, const sal_Unicode* data, int length )
{
memcpy( buffer, data, length * sizeof( sal_Unicode ));
return buffer + length;
}
inline
sal_Unicode* addDataLiteral( sal_Unicode* buffer, const char* data, int length )
{
while( length-- > 0 )
*buffer++ = *data++;
return buffer;
}
inline
char* addDataCString( char* buffer, const char* str )
{
while( *str != '\0' )
*buffer++ = *str++;
return buffer;
}
inline
sal_Unicode* addDataUString( sal_Unicode* buffer, const sal_Unicode* str )
{
while( *str != '\0' )
*buffer++ = *str++;
return buffer;
}
template<>
struct ToStringHelper< const char* >
{
static int length( const char* str ) {
return sal::static_int_cast<int>(strlen( str ));
}
static char* addData( char* buffer, const char* str ) { return addDataCString( buffer, str ); }
static const bool allowOStringConcat = true;
static const bool allowOUStringConcat = false;
};
template<>
struct ToStringHelper< char* >
{
static int length( const char* str ) {
return sal::static_int_cast<int>(strlen( str ));
}
static char* addData( char* buffer, const char* str ) { return addDataCString( buffer, str ); }
static const bool allowOStringConcat = true;
static const bool allowOUStringConcat = false;
};
template< int N >
struct ToStringHelper< char[ N ] >
{
static int length( const char str[ N ] ) {
return sal::static_int_cast<int>(strlen( str ));
}
static char* addData( char* buffer, const char str[ N ] ) { return addDataCString( buffer, str ); }
static sal_Unicode* addData( sal_Unicode* buffer, const char str[ N ] ) { return addDataLiteral( buffer, str, N - 1 ); }
static const bool allowOStringConcat = true;
static const bool allowOUStringConcat = false;
};
template< int N >
struct ToStringHelper< const char[ N ] >
{
static int length( const char str[ N ] ) { (void)str; assert( strlen( str ) == N - 1 ); return N - 1; }
static char* addData( char* buffer, const char str[ N ] ) { return addDataHelper( buffer, str, N - 1 ); }
static sal_Unicode* addData( sal_Unicode* buffer, const char str[ N ] ) { return addDataLiteral( buffer, str, N - 1 ); }
static const bool allowOStringConcat = true;
static const bool allowOUStringConcat = true;
};
template<std::size_t N> struct ToStringHelper<sal_Unicode const[N]> {
static int length(sal_Unicode const[N]) { return N - 1; }
static sal_Unicode * addData(sal_Unicode * buffer, sal_Unicode const str[N])
{ return addDataHelper(buffer, str, N - 1); }
static bool const allowOStringConcat = false;
static bool const allowOUStringConcat = true;
};
template<> struct ToStringHelper<OUStringLiteral1_> {
static int length(OUStringLiteral1_) { return 1; }
static sal_Unicode * addData(
sal_Unicode * buffer, OUStringLiteral1_ literal)
{ return addDataHelper(buffer, &literal.c, 1); }
static bool const allowOStringConcat = false;
static bool const allowOUStringConcat = true;
};
/**
@internal
Objects returned by operator+, instead of OString. These objects (possibly recursively) keep a representation of the whole
concatenation operation.
*/
template< typename T1, typename T2 >
struct OStringConcat
{
public:
OStringConcat( const T1& left_, const T2& right_ ) : left( left_ ), right( right_ ) {}
int length() const { return ToStringHelper< T1 >::length( left ) + ToStringHelper< T2 >::length( right ); }
char* addData( char* buffer ) const SAL_RETURNS_NONNULL { return ToStringHelper< T2 >::addData( ToStringHelper< T1 >::addData( buffer, left ), right ); }
// NOTE here could be functions that would forward to the "real" temporary OString. Note however that e.g. getStr()
// is not so simple, as the OString temporary must live long enough (i.e. can't be created here in a function, a wrapper
// temporary object containing it must be returned instead).
private:
const T1& left;
const T2& right;
};
/**
@internal
Objects returned by operator+, instead of OUString. These objects (possibly recursively) keep a representation of the whole
concatenation operation.
*/
template< typename T1, typename T2 >
struct OUStringConcat
{
public:
OUStringConcat( const T1& left_, const T2& right_ ) : left( left_ ), right( right_ ) {}
int length() const { return ToStringHelper< T1 >::length( left ) + ToStringHelper< T2 >::length( right ); }
sal_Unicode* addData( sal_Unicode* buffer ) const SAL_RETURNS_NONNULL { return ToStringHelper< T2 >::addData( ToStringHelper< T1 >::addData( buffer, left ), right ); }
private:
const T1& left;
const T2& right;
};
template< typename T1, typename T2 >
struct ToStringHelper< OStringConcat< T1, T2 > >
{
static int length( const OStringConcat< T1, T2 >& c ) { return c.length(); }
static char* addData( char* buffer, const OStringConcat< T1, T2 >& c ) SAL_RETURNS_NONNULL { return c.addData( buffer ); }
static const bool allowOStringConcat = ToStringHelper< T1 >::allowOStringConcat && ToStringHelper< T2 >::allowOStringConcat;
static const bool allowOUStringConcat = false;
};
template< typename T1, typename T2 >
struct ToStringHelper< OUStringConcat< T1, T2 > >
{
static int length( const OUStringConcat< T1, T2 >& c ) { return c.length(); }
static sal_Unicode* addData( sal_Unicode* buffer, const OUStringConcat< T1, T2 >& c ) SAL_RETURNS_NONNULL { return c.addData( buffer ); }
static const bool allowOStringConcat = false;
static const bool allowOUStringConcat = ToStringHelper< T1 >::allowOUStringConcat && ToStringHelper< T2 >::allowOUStringConcat;
};
template< typename T1, typename T2 >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OStringConcat< T1, T2 >, ToStringHelper< T1 >::allowOStringConcat && ToStringHelper< T2 >::allowOStringConcat >::Type operator+( const T1& left, const T2& right )
{
return OStringConcat< T1, T2 >( left, right );
}
// char[N] and const char[N] need to be done explicitly, otherwise the compiler likes to treat them the same way for some reason
template< typename T, int N >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OStringConcat< T, const char[ N ] >, ToStringHelper< T >::allowOStringConcat >::Type operator+( const T& left, const char (&right)[ N ] )
{
return OStringConcat< T, const char[ N ] >( left, right );
}
template< typename T, int N >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OStringConcat< const char[ N ], T >, ToStringHelper< T >::allowOStringConcat >::Type operator+( const char (&left)[ N ], const T& right )
{
return OStringConcat< const char[ N ], T >( left, right );
}
template< typename T, int N >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OStringConcat< T, char[ N ] >, ToStringHelper< T >::allowOStringConcat >::Type operator+( const T& left, char (&right)[ N ] )
{
return OStringConcat< T, char[ N ] >( left, right );
}
template< typename T, int N >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OStringConcat< char[ N ], T >, ToStringHelper< T >::allowOStringConcat >::Type operator+( char (&left)[ N ], const T& right )
{
return OStringConcat< char[ N ], T >( left, right );
}
template< typename T1, typename T2 >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OUStringConcat< T1, T2 >, ToStringHelper< T1 >::allowOUStringConcat && ToStringHelper< T2 >::allowOUStringConcat >::Type operator+( const T1& left, const T2& right )
{
return OUStringConcat< T1, T2 >( left, right );
}
template< typename T1, typename T2 >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OUStringConcat< T1, T2 >, ToStringHelper< T1 >::allowOUStringConcat && ToStringHelper< T2 >::allowOUStringConcat && libreoffice_internal::ConstCharArrayDetector< T1, void >::ok >::Type operator+( T1& left, const T2& right )
{
return OUStringConcat< T1, T2 >( left, right );
}
template< typename T1, typename T2 >
inline
SAL_WARN_UNUSED_RESULT
typename libreoffice_internal::Enable< OUStringConcat< T1, T2 >, ToStringHelper< T1 >::allowOUStringConcat && ToStringHelper< T2 >::allowOUStringConcat && libreoffice_internal::ConstCharArrayDetector< T2, void >::ok >::Type operator+( const T1& left, T2& right )
{
return OUStringConcat< T1, T2 >( left, right );
}
#ifdef RTL_STRING_UNITTEST_CONCAT
// Special overload to catch the remaining invalid combinations. The helper struct must
// be used to make this operator+ overload a worse choice than all the existing overloads above.
struct StringConcatInvalid
{
template< typename T >
StringConcatInvalid( const T& ) {}
};
template< typename T >
inline
int operator+( const StringConcatInvalid&, const T& )
{
rtl_string_unittest_invalid_concat = true;
return 0; // doesn't matter
}
#endif
} // namespace
#endif
#endif
|