Skip to content

Commit 4e09cc7

Browse files
authored
Support constexpr for char type via builtins or mem-functions (#34, thanks to @mcskatkat) (#37)
1 parent cbd8321 commit 4e09cc7

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

include/nonstd/string_view.hpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,24 @@ using std::operator<<;
299299

300300
// Presence of compiler intrinsics:
301301

302-
#define nssv_HAVE_BUILTIN_FN ( nssv_COMPILER_MSVC_VERSION >= 142 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
303-
#define nssv_HAVE_BUILTIN_MEMCMP nssv_HAVE_BUILTIN_FN
304-
#define nssv_HAVE_BUILTIN_STRLEN nssv_HAVE_BUILTIN_FN
302+
#define nssv_HAVE_BUILTIN_VER ( nssv_COMPILER_MSVC_VERSION >= 142 || nssv_COMPILER_GNUC_VERSION > 0 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
303+
#ifdef __has_builtin
304+
#define nssv_HAVE_BUILTIN( x ) __has_builtin( x )
305+
#else
306+
#define nssv_HAVE_BUILTIN( x ) 0
307+
#endif
308+
309+
#if nssv_HAVE_BUILTIN(__builtin_memcmp) || nssv_HAVE_BUILTIN_VER
310+
#define nssv_BUILTIN_MEMCMP __builtin_memcmp
311+
#else
312+
#define nssv_BUILTIN_MEMCMP memcmp
313+
#endif
314+
315+
#if nssv_HAVE_BUILTIN(__builtin_strlen) || nssv_HAVE_BUILTIN_VER
316+
#define nssv_BUILTIN_STRLEN __builtin_strlen
317+
#else
318+
#define nssv_BUILTIN_STRLEN strlen
319+
#endif
305320

306321
// C++ feature usage:
307322

@@ -439,7 +454,7 @@ inline nssv_constexpr14 int compare( CharT const * s1, CharT const * s2, std::si
439454

440455
inline nssv_constexpr14 int compare( char const * s1, char const * s2, std::size_t count )
441456
{
442-
return __builtin_memcmp( s1, s2, count );
457+
return nssv_BUILTIN_MEMCMP( s1, s2, count );
443458
}
444459

445460
#endif
@@ -448,9 +463,9 @@ inline nssv_constexpr14 int compare( char const * s1, char const * s2, std::size
448463

449464
// specialization of length() for char, see also generic length() further below:
450465

451-
inline nssv_constexpr int length( char const * s )
466+
inline nssv_constexpr std::size_t length( char const * s )
452467
{
453-
return __builtin_strlen( s );
468+
return nssv_BUILTIN_STRLEN( s );
454469
}
455470

456471
#endif

0 commit comments

Comments
 (0)