|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include <iterator> // std::back_inserter |
4 | | -#include <string> // std::string |
5 | | -#include <memory> // std::unique_ptr |
6 | 3 | #include <array> // std::array |
7 | | -#include <algorithm> // std::transform |
| 4 | +#include <string> // std::string |
| 5 | +#include <utility> // std::pair |
| 6 | +#include <algorithm> // std::ranges::transform |
8 | 7 | #include <cctype> // std::toupper |
9 | 8 |
|
10 | 9 | #include "serialize_result_type.h" |
@@ -35,36 +34,49 @@ namespace sqlite_orm { |
35 | 34 |
|
36 | 35 | namespace internal { |
37 | 36 |
|
38 | | - inline const serialize_result_type& to_string(journal_mode value) { |
39 | | - static const std::array<serialize_result_type, 6> res = { |
| 37 | + inline const serialize_result_type& journal_mode_to_string(journal_mode value) { |
| 38 | +#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED |
| 39 | + static constexpr std::array<serialize_result_type, 6> idx2str = { |
| 40 | +#else |
| 41 | + static const std::array<serialize_result_type, 6> idx2str = { |
| 42 | +#endif |
40 | 43 | "DELETE", |
41 | 44 | "TRUNCATE", |
42 | 45 | "PERSIST", |
43 | 46 | "MEMORY", |
44 | 47 | "WAL", |
45 | 48 | "OFF", |
46 | 49 | }; |
47 | | - return res.at(static_cast<int>(value)); |
| 50 | + return idx2str.at(static_cast<int>(value)); |
48 | 51 | } |
49 | 52 |
|
50 | | - inline std::pair<bool, journal_mode> journal_mode_from_string(const std::string& string) { |
51 | | - std::string upperString; |
52 | | - std::transform(string.begin(), string.end(), std::back_inserter(upperString), [](char c) { |
53 | | - return static_cast<char>(std::toupper(static_cast<int>(c))); |
54 | | - }); |
55 | | - static const std::array<journal_mode, 6> allValues = {{ |
| 53 | + inline std::pair<bool, journal_mode> journal_mode_from_string(std::string string) { |
| 54 | + static constexpr std::array<journal_mode, 6> journalModes = {{ |
56 | 55 | journal_mode::DELETE, |
57 | 56 | journal_mode::TRUNCATE, |
58 | 57 | journal_mode::PERSIST, |
59 | 58 | journal_mode::MEMORY, |
60 | 59 | journal_mode::WAL, |
61 | 60 | journal_mode::OFF, |
62 | 61 | }}; |
63 | | - for(auto journalMode: allValues) { |
64 | | - if(to_string(journalMode) == upperString) { |
| 62 | +#if __cpp_lib_ranges >= 201911L |
| 63 | + std::ranges::transform(string, string.begin(), [](unsigned char c) noexcept { |
| 64 | + return std::toupper(c); |
| 65 | + }); |
| 66 | + if(auto found = std::ranges::find(journalModes, string, journal_mode_to_string); |
| 67 | + found != journalModes.end()) SQLITE_ORM_CPP_LIKELY { |
| 68 | + return {true, *found}; |
| 69 | + } |
| 70 | +#else |
| 71 | + std::transform(string.begin(), string.end(), string.begin(), [](unsigned char c) noexcept { |
| 72 | + return std::toupper(c); |
| 73 | + }); |
| 74 | + for(auto journalMode: journalModes) { |
| 75 | + if(journal_mode_to_string(journalMode) == string) { |
65 | 76 | return {true, journalMode}; |
66 | 77 | } |
67 | 78 | } |
| 79 | +#endif |
68 | 80 | return {false, journal_mode::OFF}; |
69 | 81 | } |
70 | 82 | } |
|
0 commit comments