瀏覽代碼

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*.
Stephen Kelly 12 年之前
父節點
當前提交
7d4b2b2ef3
共有 1 個文件被更改,包括 25 次插入1 次删除
  1. 25 1
      Source/cmStandardIncludes.h

+ 25 - 1
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<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>
 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
 
 struct cmStrCmp {