From 7d4b2b2ef36c73c36eefd464fbb17bf34ebdb5fe Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 20 Nov 2013 02:12:48 +0100 Subject: cmStandardIncludes: Add new cmHasLiteralPrefix function. This allows avoiding error-prone hard-coding of literal string lengths. Borland is not able to process the template version of this method. Make it use the macro version instead. This means that Borland will also use the macro versions of cmArray*. --- Source/cmStandardIncludes.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Source/cmStandardIncludes.h b/Source/cmStandardIncludes.h index 18d017d..1ccec68 100644 --- a/Source/cmStandardIncludes.h +++ b/Source/cmStandardIncludes.h @@ -377,13 +377,31 @@ static thisClass* SafeDownCast(cmObject *c) \ return 0;\ } +inline bool cmHasLiteralPrefixImpl(const std::string &str1, + const char *str2, + size_t N) +{ + return strncmp(str1.c_str(), str2, N) == 0; +} + +inline bool cmHasLiteralPrefixImpl(const char* str1, + const char *str2, + size_t N) +{ + return strncmp(str1, str2, N) == 0; +} + #if defined(_MSC_VER) && _MSC_VER < 1300 \ - || defined(__GNUC__) && __GNUC__ < 3 + || defined(__GNUC__) && __GNUC__ < 3 \ + || defined(__BORLANDC__) #define cmArrayBegin(a) a #define cmArraySize(a) (sizeof(a)/sizeof(*a)) #define cmArrayEnd(a) a + cmArraySize(a) +#define cmHasLiteralPrefix(STR1, STR2) \ + cmHasLiteralPrefixImpl(STR1, "" STR2 "", sizeof(STR2) - 1) + #else template @@ -393,6 +411,12 @@ const T* cmArrayEnd(const T (&a)[N]) { return a + N; } template size_t cmArraySize(const T (&)[N]) { return N; } +template +bool cmHasLiteralPrefix(T str1, const char (&str2)[N]) +{ + return cmHasLiteralPrefixImpl(str1, str2, N - 1); +} + #endif struct cmStrCmp { -- cgit v0.12