|
10 | 10 |
|
11 | 11 | #include "Versions.h"
|
12 | 12 |
|
| 13 | +#include <array> |
13 | 14 | #include <algorithm>
|
14 |
| -#include <cctype> |
15 | 15 | #include <iterator>
|
16 |
| -#include <mutex> |
17 | 16 |
|
18 | 17 | const char *swt_getTestingLibraryVersion(void) {
|
19 | 18 | #if defined(SWT_TESTING_LIBRARY_VERSION)
|
20 | 19 | // The current environment explicitly specifies a version string to return.
|
21 | 20 | return SWT_TESTING_LIBRARY_VERSION;
|
22 | 21 | #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[] = { |
25 | 25 | #pragma clang diagnostic push
|
26 | 26 | #pragma clang diagnostic ignored "-Wc23-extensions"
|
27 |
| -#embed "../../version.txt" suffix(, '\0') |
| 27 | +#embed "../../version.txt" |
28 | 28 | #pragma clang diagnostic pop
|
29 |
| - }; |
| 29 | + }; |
30 | 30 |
|
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 | + }(); |
37 | 38 |
|
38 |
| - return version; |
| 39 | + return version.data(); |
39 | 40 | #else
|
40 | 41 | #warning SWT_TESTING_LIBRARY_VERSION not defined and version.txt not found: testing library version is unavailable
|
41 | 42 | return nullptr;
|
|
0 commit comments