@@ -36,5 +36,139 @@ template <auto... Str, auto V, typename... Content, size_t Id, typename... Ts, s
36
36
return pcre_context{ctll::push_front (capture_with_name<Id, id<Str...>, Content...>(), ctll::list<Ts...>()), pcre_parameters<Counter>()};
37
37
}
38
38
39
+ template <size_t To, auto ... Str, typename T>
40
+ static constexpr auto replace_captures_with_id (T, string<Str...>) { // fallback case, no transform
41
+ return T{};
42
+ }
43
+
44
+ template <size_t To, auto ... Str, size_t From, typename ... Content>
45
+ static constexpr auto replace_captures_with_id (capture<From, Content...>, string<Str...>) {
46
+ return capture<To, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
47
+ }
48
+ // named reseted captures must have same Str... for all instances inside the call (otherwise how would we know how to access which by name?)
49
+ template <size_t To, auto ... Str, size_t From, auto ... Str2, typename ... Content>
50
+ static constexpr auto replace_captures_with_id (capture_with_name<From, id<Str2...>, Content...>, string<Str...>) {
51
+ static_assert ((id<Str...>{} == id<Str2...>{}) && " named captures must be the same" );
52
+ return capture_with_name<From, id<Str...>, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
53
+ }
54
+
55
+ template <size_t To, auto ... Str, typename ... Content>
56
+ static constexpr auto replace_captures_with_id (sequence<Content...>, string<Str...>) {
57
+ return sequence<decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
58
+ }
59
+
60
+ template <size_t To, auto ... Str, size_t Id, typename ... Content>
61
+ static constexpr auto replace_captures_with_id (reset_group<Id, Content...>, string<Str...>) {
62
+ return reset_group<To, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
63
+ }
64
+
65
+ template <size_t To, auto ... Str, size_t Id, auto ... Str2, typename ... Content>
66
+ static constexpr auto replace_captures_with_id (reset_group_with_name<Id, id<Str2...>, Content...>, string<Str...>) {
67
+ return reset_group_with_name<To, id<Str...>, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
68
+ }
69
+
70
+ template <size_t To, auto ... Str, typename ... Content>
71
+ static constexpr auto replace_captures_with_id (atomic_group<Content...>, string<Str...>) {
72
+ return atomic_group<decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
73
+ }
74
+
75
+ template <size_t To, auto ... Str, typename ... Content>
76
+ static constexpr auto replace_captures_with_id (lookahead_positive<Content...>, string<Str...>) {
77
+ return lookahead_positive<decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
78
+ }
79
+
80
+ template <size_t To, auto ... Str, typename ... Content>
81
+ static constexpr auto replace_captures_with_id (lookahead_negative<Content...>, string<Str...>) {
82
+ return lookahead_negative<decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
83
+ }
84
+
85
+ template <size_t To, auto ... Str, size_t A, size_t B, typename ... Content>
86
+ static constexpr auto replace_captures_with_id (possessive_repeat<A, B, Content...>, string<Str...>) {
87
+ return possessive_repeat<A, B, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
88
+ }
89
+
90
+ template <size_t To, auto ... Str, size_t A, size_t B, typename ... Content>
91
+ static constexpr auto replace_captures_with_id (repeat<A, B, Content...>, string<Str...>) {
92
+ return repeat<A, B, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
93
+ }
94
+
95
+ template <size_t To, auto ... Str, size_t A, size_t B, typename ... Content>
96
+ static constexpr auto replace_captures_with_id (lazy_repeat<A, B, Content...>, string<Str...>) {
97
+ return lazy_repeat<A, B, decltype (replace_captures_with_id<To>(Content{}, string<Str...>{}))...>{};
98
+ }
99
+
100
+ template <size_t To, auto ... Str, typename ... Opts>
101
+ static constexpr auto replace_captures_with_id (select<Opts...>, string<Str...>) {
102
+ return select <decltype (replace_captures_with_id<To>(Opts{}, string<Str...>{}))...>{};
103
+ }
104
+
105
+ // get name (might be a utility already written)
106
+ template <size_t Idx, auto ... Str>
107
+ static constexpr string<Str...> get_capture_name (captured_content<Idx, id<Str...>>) noexcept {
108
+ return string<Str...>{};
109
+ }
110
+
111
+ template <size_t Idx>
112
+ static constexpr string<> get_capture_name (captured_content<Idx>) noexcept {
113
+ return string<>{};
114
+ }
115
+
116
+ template <auto ... Str>
117
+ static constexpr id<Str...> make_id_from_string (string<Str...>) noexcept {
118
+ return id<Str...>{};
119
+ }
120
+
121
+ // find the first named capture
122
+ template <typename H, typename ... Tail>
123
+ static constexpr auto get_capture_with_name (ctll::list<H, Tail...>) noexcept {
124
+ if constexpr (sizeof ...(Tail))
125
+ return get_capture_with_name (ctll::list<Tail...>{});
126
+ else
127
+ return ctll::list<>{};
128
+ }
129
+
130
+ template <size_t Idx, typename Name, typename ... Tail>
131
+ static constexpr auto get_capture_with_name (ctll::list<captured_content<Idx, Name>, Tail...>) noexcept {
132
+ return ctll::list<captured_content<Idx, Name>>{};
133
+ }
134
+
135
+ // reset group start
136
+ template <auto V, typename ... Ts, size_t Counter> static constexpr auto apply (pcre::start_reset_group, ctll::term<V>, pcre_context<ctll::list<Ts...>, pcre_parameters<Counter>>) {
137
+ return pcre_context{ ctll::push_front (reset_start<Counter+1 >(), ctll::list<Ts...>()), pcre_parameters<Counter>() };
138
+ // ctll::list<reset_start<Counter+1>(), Ts...>(), };
139
+ }
140
+
141
+ // reset group end
142
+ template <auto V, typename A, typename ... Ts, size_t Id, size_t Counter> static constexpr auto apply (pcre::make_reset_group, ctll::term<V>, pcre_context<ctll::list<A, reset_start<Id>, Ts...>, pcre_parameters<Counter>>) {
143
+ using first_capture = decltype (ctll::front (find_captures (A{})));
144
+ if constexpr (::std::is_same_v<first_capture, ctll::_nothing>) {
145
+ // no captures to reset... easy case
146
+ return pcre_context{ ctll::list<sequence<A>, Ts...>(), pcre_parameters<Counter>() };
147
+ } else {
148
+ using first_named_capture = decltype (ctll::front (get_capture_with_name (find_captures (A{}))));
149
+ if constexpr (::std::is_same_v<first_named_capture, ctll::_nothing>) {
150
+ return pcre_context{ ctll::list<reset_group<Id, decltype (replace_captures_with_id<Id>(A{}, string<>{}))>, Ts...>(), pcre_parameters<Counter>() };
151
+ } else {
152
+ return pcre_context{ ctll::list<reset_group_with_name<Id, decltype (make_id_from_string (get_capture_name (first_named_capture{}))), decltype (replace_captures_with_id<Id>(A{}, get_capture_name (first_named_capture{})))>, Ts...>(), pcre_parameters<Counter>() };
153
+ }
154
+ }
155
+ }
156
+ // reset group end (sequence)
157
+ template <auto V, typename ... Content, typename ... Ts, size_t Id, size_t Counter> static constexpr auto apply (pcre::make_reset_group, ctll::term<V>, pcre_context<ctll::list<ctre::sequence<Content...>, reset_start<Id>, Ts...>, pcre_parameters<Counter>>) {
158
+ using first_capture = decltype (ctll::front (find_captures (sequence<Content...>{})));
159
+ if constexpr (::std::is_same_v<first_capture, ctll::_nothing>) {
160
+ // no captures to reset... easy case
161
+ return pcre_context{ ctll::list<sequence<Content...>, Ts...>(), pcre_parameters<Counter>() };
162
+ } else {
163
+ using first_named_capture = decltype (ctll::front (get_capture_with_name (find_captures (sequence<Content...>{}))));
164
+ if constexpr (::std::is_same_v<first_named_capture, ctll::_nothing>) {
165
+ return pcre_context{ ctll::list<reset_group<Id, decltype (replace_captures_with_id<Id>(Content{}, string<>{}))...>, Ts...>(), pcre_parameters<Counter>() };
166
+ } else {
167
+ return pcre_context{ ctll::list<reset_group<Id, decltype (make_id_from_string (get_capture_name (first_named_capture{}))), decltype (replace_captures_with_id<Id>(Content{}, get_capture_name (first_named_capture{})))...>, Ts...>(), pcre_parameters<Counter>() };
168
+ }
169
+ }
170
+ }
171
+
172
+
39
173
40
174
#endif
0 commit comments