LibreOffice
LibreOffice 5.1 SDK C/C++ API Reference
stringutils.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #ifndef INCLUDED_RTL_STRINGUTILS_HXX
11 #define INCLUDED_RTL_STRINGUTILS_HXX
12 
13 #include <sal/config.h>
14 
15 #include <cstddef>
16 #include <cstring>
17 
18 #include <sal/types.h>
19 
20 // The unittest uses slightly different code to help check that the proper
21 // calls are made. The class is put into a different namespace to make
22 // sure the compiler generates a different (if generating also non-inline)
23 // copy of the function and does not merge them together. The class
24 // is "brought" into the proper rtl namespace by a typedef below.
25 #ifdef RTL_STRING_UNITTEST
26 #define rtl rtlunittest
27 #endif
28 
29 namespace rtl
30 {
31 
32 #ifdef RTL_STRING_UNITTEST
33 #undef rtl
34 #endif
35 
36 #if defined LIBO_INTERNAL_ONLY
37 
74 template<char C> struct SAL_WARN_UNUSED OUStringLiteral1_ {
75  static_assert(
76  static_cast<unsigned char>(C) < 0x80,
77  "non-ASCII character in OUStringLiteral1");
78  char const c = C;
79 };
80 #if defined _MSC_VER && _MSC_VER <= 1900 && !defined __clang__
81  // Visual Studio 2015
82 template<char C> using OUStringLiteral1 = OUStringLiteral1_<C>;
83 #pragma warning(disable: 4239)
84 #else
85 template<char C> using OUStringLiteral1 = OUStringLiteral1_<C> const;
86 #endif
87 
89 #endif
90 
91 namespace libreoffice_internal
92 {
93 /*
94 These templates use SFINAE (Substitution failure is not an error) to help distinguish the various
95 plain C string types: char*, const char*, char[N], const char[N], char[] and const char[].
96 There are 2 cases:
97 1) Only string literal (i.e. const char[N]) is wanted, not any of the others.
98  In this case it is necessary to distinguish between const char[N] and char[N], as the latter
99  would be automatically converted to the const variant, which is not wanted (not a string literal
100  with known size of the content). In this case ConstCharArrayDetector is used to ensure the function
101  is called only with const char[N] arguments. There's no other plain C string type overload.
102 2) All plain C string types are wanted, and const char[N] needs to be handled differently.
103  In this case const char[N] would match const char* argument type (not exactly sure why, but it's
104  consistent in all of gcc, clang and msvc). Using a template with a reference to const of the type
105  avoids this problem, and CharPtrDetector ensures that the function is called only with char pointer
106  arguments. The const in the argument is necessary to handle the case when something is explicitly
107  cast to const char*. Additionally (non-const) char[N] needs to be handled, but with the reference
108  being const, it would also match const char[N], so another overload with a reference to non-const
109  and NonConstCharArrayDetector are used to ensure the function is called only with (non-const) char[N].
110 Additionally, char[] and const char[] (i.e. size unknown) are rather tricky. Their usage with 'T&' would
111 mean it would be 'char(&)[]', which seems to be invalid. But gcc and clang somehow manage when it is
112 a template. while msvc complains about no conversion from char[] to char[1]. And the reference cannot
113 be avoided, because 'const char[]' as argument type would match also 'const char[N]'
114 So char[] and const char[] should always be used with their contents specified (which automatically
115 turns them into char[N] or const char[N]), or char* and const char* should be used.
116 */
117 struct Dummy {};
118 template< typename T1, typename T2 = void >
120 {
121  static const bool ok = false;
122 };
123 template< typename T >
124 struct CharPtrDetector< const char*, T >
125 {
126  typedef T Type;
127  static const bool ok = true;
128 };
129 template< typename T >
130 struct CharPtrDetector< char*, T >
131 {
132  typedef T Type;
133  static const bool ok = true;
134 };
135 
136 template< typename T1, typename T2 >
138 {
139 };
140 template< typename T, int N >
141 struct NonConstCharArrayDetector< char[ N ], T >
142 {
143  typedef T Type;
144 };
145 #ifdef RTL_STRING_UNITTEST
146 // never use, until all compilers handle this
147 template< typename T >
148 struct NonConstCharArrayDetector< char[], T >
149 {
150  typedef T Type;
151 };
152 template< typename T >
153 struct NonConstCharArrayDetector< const char[], T >
154 {
155  typedef T Type;
156 };
157 #endif
158 
159 template< typename T1, typename T2 = void >
161 {
162  static const bool ok = false;
163 };
164 template< std::size_t N, typename T >
165 struct ConstCharArrayDetector< const char[ N ], T >
166 {
167  typedef T Type;
168  static const std::size_t length = N - 1;
169  static const bool ok = true;
170  static bool isValid(char const (& literal)[N])
171  { return std::strlen(literal) == length; }
172  static char const * toPointer(char const (& literal)[N]) { return literal; }
173 };
174 #if defined LIBO_INTERNAL_ONLY
175 template<char C, typename T> struct ConstCharArrayDetector<
176 #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 \
177  && !defined __clang__
178  OUStringLiteral1_<C> const,
179 #else
180  OUStringLiteral1<C>,
181 #endif
182  T>
183 {
184  typedef T Type;
185  static const std::size_t length = 1;
186  static const bool ok = true;
187  static bool isValid(OUStringLiteral1_<C>) { return true; }
188  static char const * toPointer(OUStringLiteral1_<C> const & literal)
189  { return &literal.c; }
190 };
191 #endif
192 
193 // this one is used to rule out only const char[N]
194 template< typename T >
196 {
197  typedef Dummy Type;
198 };
199 template< int N >
200 struct ExceptConstCharArrayDetector< const char[ N ] >
201 {
202 };
203 // this one is used to rule out only const char[N]
204 // (const will be brought in by 'const T&' in the function call)
205 // msvc needs const char[N] here (not sure whether gcc or msvc
206 // are right, it doesn't matter).
207 template< typename T >
209 {
210  typedef Dummy Type;
211 };
212 template< int N >
213 struct ExceptCharArrayDetector< char[ N ] >
214 {
215 };
216 template< int N >
217 struct ExceptCharArrayDetector< const char[ N ] >
218 {
219 };
220 #if defined LIBO_INTERNAL_ONLY && defined _MSC_VER && _MSC_VER <= 1900
221  // Visual Studio 2015
222 template<char C> struct ExceptCharArrayDetector<OUStringLiteral1<C>> {};
223 #endif
224 
225 template< typename T1, typename T2 = void >
227 {
228  static const bool ok = false;
229 };
230 template< typename T >
232 {
233  typedef T Type;
234  static const bool ok = true;
235 };
236 template< typename T >
238 {
239  typedef T Type;
240  static const bool ok = true;
241 };
242 
243 // SFINAE helper class
244 template< typename T, bool >
245 struct Enable
246  {
247  };
248 
249 template< typename T >
250 struct Enable< T, true >
251  {
252  typedef T Type;
253  };
254 
255 
256 } /* Namespace */
257 
258 } /* Namespace */
259 
260 #endif // INCLUDED_RTL_STRINGUTILS_HXX
261 
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool isValid(char const (&literal)[N])
Definition: stringutils.hxx:170
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:612
Definition: stringutils.hxx:245
T Type
Definition: stringutils.hxx:252
static char const * toPointer(char const (&literal)[N])
Definition: stringutils.hxx:172
Definition: stringutils.hxx:119
sal_uInt16 sal_Unicode
Definition: types.h:155
Definition: bootstrap.hxx:29
Definition: stringutils.hxx:117
Dummy Type
Definition: stringutils.hxx:210
Dummy Type
Definition: stringutils.hxx:197