|
13 | 13 | #include <array>
|
14 | 14 | #include <algorithm>
|
15 | 15 | #include <iterator>
|
| 16 | +#include <mutex> |
16 | 17 |
|
17 | 18 | const char *swt_getTestingLibraryVersion(void) {
|
18 | 19 | #if defined(SWT_TESTING_LIBRARY_VERSION)
|
19 | 20 | // The current environment explicitly specifies a version string to return.
|
20 | 21 | return SWT_TESTING_LIBRARY_VERSION;
|
21 | 22 | #elif __clang_major__ >= 17 && defined(__has_embed)
|
22 | 23 | #if __has_embed("../../VERSION.txt")
|
23 |
| - static constinit auto version = [] () constexpr { |
24 |
| - // Read the version from version.txt at the root of the package's repo. |
25 |
| - char version[] = { |
| 24 | + // Read the version from version.txt at the root of the package's repo. |
| 25 | + static char version[] = { |
26 | 26 | #pragma clang diagnostic push
|
27 | 27 | #pragma clang diagnostic ignored "-Wc23-extensions"
|
28 | 28 | #embed "../../VERSION.txt"
|
29 | 29 | #pragma clang diagnostic pop
|
30 |
| - }; |
| 30 | + }; |
31 | 31 |
|
32 |
| - // Copy the first line from the C string into a C array so that we can |
33 |
| - // return it from this closure. |
34 |
| - std::array<char, std::size(version) + 1> result {}; |
| 32 | + // Zero out the newline character and anything after it. |
| 33 | + static std::once_flag once; |
| 34 | + std::call_once(once, [] { |
35 | 35 | auto i = std::find_if(std::begin(version), std::end(version), [] (char c) {
|
36 | 36 | return c == '\r' || c == '\n';
|
37 | 37 | });
|
38 |
| - std::copy(std::begin(version), i, result.begin()); |
39 |
| - return result; |
40 |
| - }(); |
| 38 | + std::fill(i, std::end(version), '\0'); |
| 39 | + }); |
41 | 40 |
|
42 |
| - return version.data(); |
| 41 | + return version; |
43 | 42 | #else
|
44 | 43 | #warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable
|
45 | 44 | return nullptr;
|
|
0 commit comments