瀏覽代碼

cmStringAlgorithms: Add cmStrLen()

Kyle Edwards 5 年之前
父節點
當前提交
0c9cdf30ed
共有 2 個文件被更改,包括 17 次插入0 次删除
  1. 7 0
      Source/cmStringAlgorithms.h
  2. 10 0
      Tests/CMakeLib/testStringAlgorithms.cxx

+ 7 - 0
Source/cmStringAlgorithms.h

@@ -33,6 +33,13 @@ inline bool cmNonempty(std::string const* str)
   return str && !str->empty();
   return str && !str->empty();
 }
 }
 
 
+/** Returns length of a literal string.  */
+template <size_t N>
+constexpr size_t cmStrLen(const char (&/*str*/)[N])
+{
+  return N - 1;
+}
+
 /** Callable string comparison struct.  */
 /** Callable string comparison struct.  */
 struct cmStrCmp
 struct cmStrCmp
 {
 {

+ 10 - 0
Tests/CMakeLib/testStringAlgorithms.cxx

@@ -226,5 +226,15 @@ int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
               "cmStrToULong rejects trailing content.");
               "cmStrToULong rejects trailing content.");
   }
   }
 
 
+  // ----------------------------------------------------------------------
+  // Test cmStrLen
+  {
+    constexpr auto len = cmStrLen("Hello world!");
+    assert_ok(len == 12,
+              "cmStrLen returns length of non-empty literal string");
+    assert_ok(cmStrLen("") == 0,
+              "cmStrLen returns length of empty literal string");
+  }
+
   return failed;
   return failed;
 }
 }