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*.
This commit is contained in:
Stephen Kelly 2013-11-20 02:12:48 +01:00
parent b7cecfcb21
commit 7d4b2b2ef3
1 changed files with 25 additions and 1 deletions

View File

@ -377,13 +377,31 @@ static thisClass* SafeDownCast(cmObject *c) \
return 0;\ 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 \ #if defined(_MSC_VER) && _MSC_VER < 1300 \
|| defined(__GNUC__) && __GNUC__ < 3 || defined(__GNUC__) && __GNUC__ < 3 \
|| defined(__BORLANDC__)
#define cmArrayBegin(a) a #define cmArrayBegin(a) a
#define cmArraySize(a) (sizeof(a)/sizeof(*a)) #define cmArraySize(a) (sizeof(a)/sizeof(*a))
#define cmArrayEnd(a) a + cmArraySize(a) #define cmArrayEnd(a) a + cmArraySize(a)
#define cmHasLiteralPrefix(STR1, STR2) \
cmHasLiteralPrefixImpl(STR1, "" STR2 "", sizeof(STR2) - 1)
#else #else
template<typename T, size_t N> template<typename T, size_t N>
@ -393,6 +411,12 @@ const T* cmArrayEnd(const T (&a)[N]) { return a + N; }
template<typename T, size_t N> template<typename T, size_t N>
size_t cmArraySize(const T (&)[N]) { return N; } size_t cmArraySize(const T (&)[N]) { return N; }
template<typename T, size_t N>
bool cmHasLiteralPrefix(T str1, const char (&str2)[N])
{
return cmHasLiteralPrefixImpl(str1, str2, N - 1);
}
#endif #endif
struct cmStrCmp { struct cmStrCmp {