From 0c9cdf30ed91fb4705efed578f90af3226d8fe9f Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Thu, 3 Sep 2020 11:50:25 -0400 Subject: cmStringAlgorithms: Add cmStrLen() --- Source/cmStringAlgorithms.h | 7 +++++++ Tests/CMakeLib/testStringAlgorithms.cxx | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 6508069..81667db 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -33,6 +33,13 @@ inline bool cmNonempty(std::string const* str) return str && !str->empty(); } +/** Returns length of a literal string. */ +template +constexpr size_t cmStrLen(const char (&/*str*/)[N]) +{ + return N - 1; +} + /** Callable string comparison struct. */ struct cmStrCmp { diff --git a/Tests/CMakeLib/testStringAlgorithms.cxx b/Tests/CMakeLib/testStringAlgorithms.cxx index 63826cf..c2706c1 100644 --- a/Tests/CMakeLib/testStringAlgorithms.cxx +++ b/Tests/CMakeLib/testStringAlgorithms.cxx @@ -226,5 +226,15 @@ int testStringAlgorithms(int /*unused*/, char* /*unused*/ []) "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; } -- cgit v0.12