Skip to content

Commit be7d423

Browse files
committed
Constant-fold the whole version.txt file
1 parent afa1d9c commit be7d423

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Sources/_TestingInternals/Versions.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,33 @@
1010

1111
#include "Versions.h"
1212

13+
#include <array>
1314
#include <algorithm>
14-
#include <cctype>
1515
#include <iterator>
16-
#include <mutex>
1716

1817
const char *swt_getTestingLibraryVersion(void) {
1918
#if defined(SWT_TESTING_LIBRARY_VERSION)
2019
// The current environment explicitly specifies a version string to return.
2120
return SWT_TESTING_LIBRARY_VERSION;
2221
#elif __has_embed("../../version.txt")
23-
// Read the version from version.txt at the root of the package's repository.
24-
static char version[] = {
22+
static constinit auto version = [] () constexpr {
23+
// Read the version from version.txt at the root of the package's repo.
24+
char version[] = {
2525
#pragma clang diagnostic push
2626
#pragma clang diagnostic ignored "-Wc23-extensions"
27-
#embed "../../version.txt" suffix(, '\0')
27+
#embed "../../version.txt"
2828
#pragma clang diagnostic pop
29-
};
29+
};
3030

31-
// Zero any trailing characters (e.g. the copyright block.)
32-
static std::once_flag once;
33-
std::call_once(once, [] {
34-
auto i = std::find_if(std::begin(version), std::end(version), isspace);
35-
std::fill(i, std::end(version), '\0');
36-
});
31+
// Copy the first line from the C string into a C array so that we can
32+
// return it from this closure.
33+
std::array<char, std::size(version) + 1> result {};
34+
auto i = std::find(std::begin(version), std::end(version), '\n');
35+
std::copy(std::begin(version), i, result.begin());
36+
return result;
37+
}();
3738

38-
return version;
39+
return version.data();
3940
#else
4041
#warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable
4142
return nullptr;

0 commit comments

Comments
 (0)