From f60e92126296c0d87130ea20b9d4680d51fca62d Mon Sep 17 00:00:00 2001
From: Thomas Schenker <thomas.schenker@protonmail.com>
Date: Fri, 7 Jul 2023 19:42:28 +0200
Subject: find_library: Improve regex readability in --debug-find output

If there is only one possible library prefix/suffix, omit the capture
group around it in the debug output.
---
 Source/cmFindLibraryCommand.cxx                              | 10 ++++++++--
 Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt |  2 +-
 Tests/RunCMake/find_library/FromScriptMode-stderr.txt        |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/Source/cmFindLibraryCommand.cxx b/Source/cmFindLibraryCommand.cxx
index 9eb0603..6a571b4 100644
--- a/Source/cmFindLibraryCommand.cxx
+++ b/Source/cmFindLibraryCommand.cxx
@@ -251,8 +251,14 @@ struct cmFindLibraryHelper
   void DebugLibraryFailed(std::string const& name, std::string const& path)
   {
     if (this->DebugMode) {
-      auto regexName =
-        cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr);
+      // To improve readability of the debug output, if there is only one
+      // prefix/suffix, use the plain prefix/suffix instead of the regex.
+      const auto& prefix = (this->Prefixes.size() == 1) ? this->Prefixes[0]
+                                                        : this->PrefixRegexStr;
+      const auto& suffix = (this->Suffixes.size() == 1) ? this->Suffixes[0]
+                                                        : this->SuffixRegexStr;
+
+      auto regexName = cmStrCat(prefix, name, suffix);
       this->DebugSearches.FailedAt(path, regexName);
     }
   }
diff --git a/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt b/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt
index 185720b..b0bf460 100644
--- a/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt
+++ b/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt
@@ -1,4 +1,4 @@
 .*find_library considered the following locations.*
-.*\(lib\)library_no_exist\(\\.tbd\|\\.dylib\|\\.so\|\\.a\).*
+.*liblibrary_no_exist\(\\.tbd\|\\.dylib\|\\.so\|\\.a\).*
 .*The item was found at.*
 .*lib/libcreated.a.*
diff --git a/Tests/RunCMake/find_library/FromScriptMode-stderr.txt b/Tests/RunCMake/find_library/FromScriptMode-stderr.txt
index 046f680..6f25184 100644
--- a/Tests/RunCMake/find_library/FromScriptMode-stderr.txt
+++ b/Tests/RunCMake/find_library/FromScriptMode-stderr.txt
@@ -1,4 +1,4 @@
 .*find_library considered the following locations.*
-.*\(lib\)library_no_exist\(\\.so\|\\.a\).*
+.*liblibrary_no_exist\(\\.so\|\\.a\).*
 .*The item was found at.*
 .*lib/libcreated.a.*
-- 
cgit v0.12