diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-03-28 21:33:11 +0200 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-03-28 23:00:53 +0200 |
commit | 81e7364f52b6135776d4999be287524d508a7d08 (patch) | |
tree | 0fd03aa32200b1c8d7e29708aa071b6cf364af4f /sal | |
parent | 48cd6ac1ec7e92a9c9a21f5365db4f9ff5fde93c (diff) |
move string helper types to stringutils.hxx
Diffstat (limited to 'sal')
-rw-r--r-- | sal/Package_inc.mk | 1 | ||||
-rw-r--r-- | sal/inc/rtl/oustringostreaminserter.hxx | 13 | ||||
-rw-r--r-- | sal/inc/rtl/strbuf.hxx | 26 | ||||
-rw-r--r-- | sal/inc/rtl/string.hxx | 81 | ||||
-rw-r--r-- | sal/inc/rtl/stringutils.hxx | 110 | ||||
-rw-r--r-- | sal/inc/rtl/ustrbuf.hxx | 26 | ||||
-rw-r--r-- | sal/inc/rtl/ustring.hxx | 40 |
7 files changed, 227 insertions, 70 deletions
diff --git a/sal/Package_inc.mk b/sal/Package_inc.mk index 3ac23bccb2c5..5350faf19f46 100644 --- a/sal/Package_inc.mk +++ b/sal/Package_inc.mk @@ -92,6 +92,7 @@ $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/strbuf.h,rtl/strbuf.h)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/strbuf.hxx,rtl/strbuf.hxx)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/string.h,rtl/string.h)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/string.hxx,rtl/string.hxx)) +$(eval $(call gb_Package_add_file,sal_inc,inc/rtl/stringutils.hxx,rtl/stringutils.hxx)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/tencinfo.h,rtl/tencinfo.h)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/textcvt.h,rtl/textcvt.h)) $(eval $(call gb_Package_add_file,sal_inc,inc/rtl/textenc.h,rtl/textenc.h)) diff --git a/sal/inc/rtl/oustringostreaminserter.hxx b/sal/inc/rtl/oustringostreaminserter.hxx index 579ffa0cd045..632bc23492ce 100644 --- a/sal/inc/rtl/oustringostreaminserter.hxx +++ b/sal/inc/rtl/oustringostreaminserter.hxx @@ -42,8 +42,21 @@ @since LibreOffice 3.5. */ +// The unittest uses slightly different code to help check that the proper +// calls are made. The class is put into a different namespace to make +// sure the compiler generates a different (if generating also non-inline) +// copy of the function and does not merge them together. The class +// is "brought" into the proper rtl namespace by a typedef below. +#ifdef RTL_STRING_UNITTEST +#define rtl rtlunittest +#endif + namespace rtl { +#ifdef RTL_STRING_UNITTEST +#undef rtl +#endif + template< typename charT, typename traits > std::basic_ostream<charT, traits> & operator <<( std::basic_ostream<charT, traits> & stream, rtl::OUString const & string) diff --git a/sal/inc/rtl/strbuf.hxx b/sal/inc/rtl/strbuf.hxx index a92ea1d1d7e1..62250f3faaa0 100644 --- a/sal/inc/rtl/strbuf.hxx +++ b/sal/inc/rtl/strbuf.hxx @@ -35,12 +35,30 @@ #include <rtl/strbuf.h> #include <rtl/string.hxx> +#include <rtl/stringutils.hxx> #ifdef __cplusplus +// The unittest uses slightly different code to help check that the proper +// calls are made. The class is put into a different namespace to make +// sure the compiler generates a different (if generating also non-inline) +// copy of the function and does not merge them together. The class +// is "brought" into the proper rtl namespace by a typedef below. +#ifdef RTL_STRING_UNITTEST +#define rtl rtlunittest +#endif + namespace rtl { +#ifdef RTL_STRING_UNITTEST +#undef rtl +// helper macro to make functions appear more readable +#define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true; +#else +#define RTL_STRING_CONST_FUNCTION +#endif + /** A string buffer implements a mutable sequence of characters. <p> String buffers are safe for use by multiple threads. The methods @@ -715,6 +733,14 @@ private: } +#ifdef RTL_STRING_UNITTEST +namespace rtl +{ +typedef rtlunittest::OStringBuffer OStringBuffer; +} +#undef RTL_STRING_CONST_FUNCTION +#endif + #endif /* __cplusplus */ #endif /* _RTL_STRBUF_HXX_ */ diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx index 68f41bdf6398..72af04ee79bc 100644 --- a/sal/inc/rtl/string.hxx +++ b/sal/inc/rtl/string.hxx @@ -37,6 +37,8 @@ #include <rtl/memory.h> #include <rtl/textenc.h> #include <rtl/string.h> +#include <rtl/stringutils.hxx> + #include "sal/log.hxx" #if !defined EXCEPTIONS_OFF @@ -89,65 +91,6 @@ namespace rtl use this class. */ -namespace internal -{ -/* -These templates use SFINAE (Substitution failure is not an error) to help distinguish the various -plain C string types: char*, const char*, char[N] and const char[N]. There are 2 cases: -1) Only string literal (i.e. const char[N]) is wanted, not any of the others. - In this case it is necessary to distinguish between const char[N] and char[N], as the latter - would be automatically converted to the const variant, which is not wanted (not a string literal - with known size of the content). In this case ConstCharArrayDetector is used to ensure the function - is called only with const char[N] arguments. There's no other plain C string type overload. -2) All plain C string types are wanted, and const char[N] needs to be handled differently. - In this case const char[N] would match const char* argument type (not exactly sure why, but it's - consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type - avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer - arguments. The const in the argument is necessary to handle the case when something is explicitly - cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference - being const, it would also match const char[N], so another overload with a reference to non-const - and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N]. -*/ -struct Dummy {}; -template< typename T1, typename T2 > -struct CharPtrDetector -{ -}; -template< typename T > -struct CharPtrDetector< const char*, T > -{ - typedef T Type; -}; -template< typename T > -struct CharPtrDetector< char*, T > -{ - typedef T Type; -}; - -template< typename T1, typename T2 > -struct NonConstCharArrayDetector -{ -}; -template< typename T, int N > -struct NonConstCharArrayDetector< char[ N ], T > -{ - typedef T Type; -}; -// This is similar, only it helps to detect const char[]. Without using a template, -// (non-const) char[] would be automatically converted to const char[], which is unwanted -// here (size of the content is not known). -template< typename T1, typename T2 > -struct ConstCharArrayDetector -{ -}; -template< int N, typename T > -struct ConstCharArrayDetector< const char[ N ], T > -{ - typedef T Type; - static const int size = N; -}; -} - class OString { public: @@ -1486,6 +1429,19 @@ public: /* ======================================================================= */ +} /* Namespace */ + +#ifdef RTL_STRING_UNITTEST +namespace rtl +{ +typedef rtlunittest::OString OString; +} +#undef RTL_STRING_CONST_FUNCTION +#endif + +namespace rtl +{ + /** A helper to use OStrings with hash maps. Instances of this class are unary function objects that can be used as @@ -1510,13 +1466,6 @@ struct OStringHash } /* Namespace */ -#ifdef RTL_STRING_UNITTEST -namespace rtl -{ -typedef rtlunittest::OString OString; -} -#endif - #endif /* _RTL_STRING_HXX_ */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/inc/rtl/stringutils.hxx b/sal/inc/rtl/stringutils.hxx new file mode 100644 index 000000000000..866bf232d37f --- /dev/null +++ b/sal/inc/rtl/stringutils.hxx @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * Major Contributor(s): + * [ Copyright (C) 2012 Lubos Lunak <l.lunak@suse.cz> (initial developer) ] + * + * All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +#ifndef _RTL_STRINGUTILS_HXX_ +#define _RTL_STRINGUTILS_HXX_ + +#include "sal/config.h" + +// The unittest uses slightly different code to help check that the proper +// calls are made. The class is put into a different namespace to make +// sure the compiler generates a different (if generating also non-inline) +// copy of the function and does not merge them together. The class +// is "brought" into the proper rtl namespace by a typedef below. +#ifdef RTL_STRING_UNITTEST +#define rtl rtlunittest +#endif + +namespace rtl +{ + +#ifdef RTL_STRING_UNITTEST +#undef rtl +#endif +namespace internal +{ +/* +These templates use SFINAE (Substitution failure is not an error) to help distinguish the various +plain C string types: char*, const char*, char[N] and const char[N]. There are 2 cases: +1) Only string literal (i.e. const char[N]) is wanted, not any of the others. + In this case it is necessary to distinguish between const char[N] and char[N], as the latter + would be automatically converted to the const variant, which is not wanted (not a string literal + with known size of the content). In this case ConstCharArrayDetector is used to ensure the function + is called only with const char[N] arguments. There's no other plain C string type overload. +2) All plain C string types are wanted, and const char[N] needs to be handled differently. + In this case const char[N] would match const char* argument type (not exactly sure why, but it's + consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type + avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer + arguments. The const in the argument is necessary to handle the case when something is explicitly + cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference + being const, it would also match const char[N], so another overload with a reference to non-const + and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N]. +*/ +struct Dummy {}; +template< typename T1, typename T2 > +struct CharPtrDetector +{ +}; +template< typename T > +struct CharPtrDetector< const char*, T > +{ + typedef T Type; +}; +template< typename T > +struct CharPtrDetector< char*, T > +{ + typedef T Type; +}; + +template< typename T1, typename T2 > +struct NonConstCharArrayDetector +{ +}; +template< typename T, int N > +struct NonConstCharArrayDetector< char[ N ], T > +{ + typedef T Type; +}; + +template< typename T1, typename T2 > +struct ConstCharArrayDetector +{ +}; +template< int N, typename T > +struct ConstCharArrayDetector< const char[ N ], T > +{ + typedef T Type; + static const int size = N; +}; +} + +} /* Namespace */ + +#endif /* _RTL_STRINGUTILS_HXX_ */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/inc/rtl/ustrbuf.hxx b/sal/inc/rtl/ustrbuf.hxx index adb17616ac7a..15bbe67ad04b 100644 --- a/sal/inc/rtl/ustrbuf.hxx +++ b/sal/inc/rtl/ustrbuf.hxx @@ -36,10 +36,28 @@ #include <osl/diagnose.h> #include <rtl/ustrbuf.h> #include <rtl/ustring.hxx> +#include <rtl/stringutils.hxx> + +// The unittest uses slightly different code to help check that the proper +// calls are made. The class is put into a different namespace to make +// sure the compiler generates a different (if generating also non-inline) +// copy of the function and does not merge them together. The class +// is "brought" into the proper rtl namespace by a typedef below. +#ifdef RTL_STRING_UNITTEST +#define rtl rtlunittest +#endif namespace rtl { +#ifdef RTL_STRING_UNITTEST +#undef rtl +// helper macro to make functions appear more readable +#define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true; +#else +#define RTL_STRING_CONST_FUNCTION +#endif + /** A string buffer implements a mutable sequence of characters. <p> String buffers are safe for use by multiple threads. The methods @@ -866,6 +884,14 @@ private: } +#ifdef RTL_STRING_UNITTEST +namespace rtl +{ +typedef rtlunittest::OUStringBuffer OUStringBuffer; +} +#undef RTL_STRING_CONST_FUNCTION +#endif + #endif /* _RTL_USTRBUF_HXX_ */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx index ef47be621867..dadef0f978e6 100644 --- a/sal/inc/rtl/ustring.hxx +++ b/sal/inc/rtl/ustring.hxx @@ -36,6 +36,7 @@ #include "osl/diagnose.h" #include <rtl/ustring.h> #include <rtl/string.hxx> +#include <rtl/stringutils.hxx> #include <rtl/memory.h> #include "sal/log.hxx" @@ -45,8 +46,26 @@ #include <new> #endif +// The unittest uses slightly different code to help check that the proper +// calls are made. The class is put into a different namespace to make +// sure the compiler generates a different (if generating also non-inline) +// copy of the function and does not merge them together. The class +// is "brought" into the proper rtl namespace by a typedef below. +#ifdef RTL_STRING_UNITTEST +#define rtl rtlunittest +#endif + namespace rtl { + +#ifdef RTL_STRING_UNITTEST +#undef rtl +// helper macro to make functions appear more readable +#define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true; +#else +#define RTL_STRING_CONST_FUNCTION +#endif + /* ======================================================================= */ /** @@ -897,7 +916,7 @@ public: @since LibreOffice 3.6 */ - bool endsWith(rtl::OUString const & str) const { + bool endsWith(OUString const & str) const { return str.getLength() <= getLength() && match(str, getLength() - str.getLength()); } @@ -1485,7 +1504,7 @@ public: @since LibreOffice 3.6 */ template< int N > - OUString replaceFirst( const char (&from)[ N ], rtl::OUString const & to, + OUString replaceFirst( const char (&from)[ N ], OUString const & to, sal_Int32 * index = 0) const { rtl_uString * s = 0; @@ -1500,7 +1519,7 @@ public: * @internal */ template< int N > - OUString replaceFirst( char (&literal)[ N ], rtl::OUString const & to, + OUString replaceFirst( char (&literal)[ N ], OUString const & to, sal_Int32 * index = 0) const; /** @@ -2133,6 +2152,19 @@ public: /* ======================================================================= */ +} /* Namespace */ + +#ifdef RTL_STRING_UNITTEST +namespace rtl +{ +typedef rtlunittest::OUString OUString; +} +#undef RTL_STRING_CONST_FUNCTION +#endif + +namespace rtl +{ + /** A helper to use OUStrings with hash maps. Instances of this class are unary function objects that can be used as @@ -2149,7 +2181,7 @@ struct OUStringHash a hash code for the string. This hash code should not be stored persistently, as its computation may change in later revisions. */ - size_t operator()(const rtl::OUString& rString) const + size_t operator()(const OUString& rString) const { return (size_t)rString.hashCode(); } }; |