| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include "transform.h" |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
const char *start; |
| 29 |
size_t size; |
| 30 |
} pn_group_t; |
| 31 |
|
| 32 |
#define MAX_GROUP (64) |
| 33 |
|
| 34 |
typedef struct { |
| 35 |
size_t groups; |
| 36 |
pn_group_t group[MAX_GROUP]; |
| 37 |
} pn_matcher_t; |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
pn_string_t *pattern; |
| 41 |
pn_string_t *substitution; |
| 42 |
} pn_rule_t; |
| 43 |
|
| 44 |
struct pn_transform_t { |
| 45 |
pn_list_t *rules; |
| 46 |
pn_matcher_t matcher; |
| 47 |
bool matched; |
| 48 |
}; |
| 49 |
|
| 50 |
static void pn_rule_finalize(void *object) |
| 51 |
{ |
| 52 |
pn_rule_t *rule = (pn_rule_t *) object; |
| 53 |
pn_free(rule->pattern); |
| 54 |
pn_free(rule->substitution); |
| 55 |
} |
| 56 |
|
| 57 |
#define CID_pn_rule CID_pn_object |
| 58 |
#define pn_rule_initialize NULL |
| 59 |
#define pn_rule_hashcode NULL |
| 60 |
#define pn_rule_compare NULL |
| 61 |
#define pn_rule_inspect NULL |
| 62 |
|
| 63 |
pn_rule_t *pn_rule(const char *pattern, const char *substitution) |
| 64 |
{ |
| 65 |
static const pn_class_t clazz = PN_CLASS(pn_rule); |
| 66 |
pn_rule_t *rule = (pn_rule_t *) pn_class_new(&clazz, sizeof(pn_rule_t)); |
| 67 |
rule->pattern = pn_string(pattern); |
| 68 |
rule->substitution = pn_string(substitution); |
| 69 |
return rule; |
| 70 |
} |
| 71 |
|
| 72 |
static void pn_transform_finalize(void *object) |
| 73 |
{ |
| 74 |
pn_transform_t *transform = (pn_transform_t *) object; |
| 75 |
pn_free(transform->rules); |
| 76 |
} |
| 77 |
|
| 78 |
#define CID_pn_transform CID_pn_object |
| 79 |
#define pn_transform_initialize NULL |
| 80 |
#define pn_transform_hashcode NULL |
| 81 |
#define pn_transform_compare NULL |
| 82 |
#define pn_transform_inspect NULL |
| 83 |
|
| 84 |
pn_transform_t *pn_transform() |
| 85 |
{ |
| 86 |
static const pn_class_t clazz = PN_CLASS(pn_transform); |
| 87 |
pn_transform_t *transform = (pn_transform_t *) pn_class_new(&clazz, sizeof(pn_transform_t)); |
| 88 |
transform->rules = pn_list(PN_OBJECT, 0); |
| 89 |
transform->matched = false; |
| 90 |
return transform; |
| 91 |
} |
| 92 |
|
| 93 |
void pn_transform_rule(pn_transform_t *transform, const char *pattern, |
| 94 |
const char *substitution) |
| 95 |
{ |
| 96 |
assert(transform); |
| 97 |
pn_rule_t *rule = pn_rule(pattern, substitution); |
| 98 |
pn_list_add(transform->rules, rule); |
| 99 |
pn_decref(rule); |
| 100 |
} |
| 101 |
|
| 102 |
static void pni_sub(pn_matcher_t *matcher, size_t group, const char *text, size_t matched) |
| 103 |
{ |
| 104 |
if (group > matcher->groups) { |
| 105 |
matcher->groups = group; |
| 106 |
} |
| 107 |
matcher->group[group].start = text - matched; |
| 108 |
matcher->group[group].size = matched; |
| 109 |
} |
| 110 |
|
| 111 |
static bool pni_match_r(pn_matcher_t *matcher, const char *pattern, const char *text, size_t group, size_t matched) |
| 112 |
{ |
| 113 |
bool match; |
| 114 |
|
| 115 |
char p = *pattern; |
| 116 |
char c = *text; |
| 117 |
|
| 118 |
switch (p) { |
| 119 |
case '\0': return c == '\0'; |
| 120 |
case '%': |
| 121 |
case '*': |
| 122 |
switch (c) { |
| 123 |
case '\0': |
| 124 |
match = pni_match_r(matcher, pattern + 1, text, group + 1, 0); |
| 125 |
if (match) pni_sub(matcher, group, text, matched); |
| 126 |
return match; |
| 127 |
case '/': |
| 128 |
if (p == '%') { |
| 129 |
match = pni_match_r(matcher, pattern + 1, text, group + 1, 0); |
| 130 |
if (match) pni_sub(matcher, group, text, matched); |
| 131 |
return match; |
| 132 |
} |
| 133 |
// Fallthrough |
| 134 |
default: |
| 135 |
match = pni_match_r(matcher, pattern, text + 1, group, matched + 1); |
| 136 |
if (!match) { |
| 137 |
match = pni_match_r(matcher, pattern + 1, text, group + 1, 0); |
| 138 |
if (match) pni_sub(matcher, group, text, matched); |
| 139 |
} |
| 140 |
return match; |
| 141 |
} |
| 142 |
default: |
| 143 |
return c == p && pni_match_r(matcher, pattern + 1, text + 1, group, 0); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static bool pni_match(pn_matcher_t *matcher, const char *pattern, const char *text) |
| 148 |
{ |
| 149 |
text = text ? text : ""; |
| 150 |
matcher->groups = 0; |
| 151 |
if (pni_match_r(matcher, pattern, text, 1, 0)) { |
| 152 |
matcher->group[0].start = text; |
| 153 |
matcher->group[0].size = strlen(text); |
| 154 |
return true; |
| 155 |
} else { |
| 156 |
matcher->groups = 0; |
| 157 |
return false; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static size_t pni_substitute(pn_matcher_t *matcher, const char *pattern, char *dest, size_t limit) |
| 162 |
{ |
| 163 |
size_t result = 0; |
| 164 |
|
| 165 |
while (*pattern) { |
| 166 |
switch (*pattern) { |
| 167 |
case '$': |
| 168 |
pattern++; |
| 169 |
if (*pattern == '$') { |
| 170 |
if (result < limit) { |
| 171 |
*dest++ = *pattern; |
| 172 |
} |
| 173 |
pattern++; |
| 174 |
result++; |
| 175 |
} else { |
| 176 |
size_t idx = 0; |
| 177 |
while (isdigit(*pattern)) { |
| 178 |
idx *= 10; |
| 179 |
idx += *pattern++ - '0'; |
| 180 |
} |
| 181 |
|
| 182 |
if (idx <= matcher->groups) { |
| 183 |
pn_group_t *group = &matcher->group[idx]; |
| 184 |
for (size_t i = 0; i < group->size; i++) { |
| 185 |
if (result < limit) { |
| 186 |
*dest++ = group->start[i]; |
| 187 |
} |
| 188 |
result++; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
break; |
| 193 |
default: |
| 194 |
if (result < limit) { |
| 195 |
*dest++ = *pattern; |
| 196 |
} |
| 197 |
pattern++; |
| 198 |
result++; |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (result < limit) { |
| 204 |
*dest = '\0'; |
| 205 |
} |
| 206 |
|
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
int pn_transform_apply(pn_transform_t *transform, const char *src, |
| 211 |
pn_string_t *dst) |
| 212 |
{ |
| 213 |
for (size_t i = 0; i < pn_list_size(transform->rules); i++) |
| 214 |
{ |
| 215 |
pn_rule_t *rule = (pn_rule_t *) pn_list_get(transform->rules, i); |
| 216 |
if (pni_match(&transform->matcher, pn_string_get(rule->pattern), src)) { |
| 217 |
transform->matched = true; |
| 218 |
if (!pn_string_get(rule->substitution)) { |
| 219 |
return pn_string_set(dst, NULL); |
| 220 |
} |
| 221 |
|
| 222 |
while (true) { |
| 223 |
size_t capacity = pn_string_capacity(dst); |
| 224 |
size_t n = pni_substitute(&transform->matcher, |
| 225 |
pn_string_get(rule->substitution), |
| 226 |
pn_string_buffer(dst), capacity); |
| 227 |
int err = pn_string_resize(dst, n); |
| 228 |
if (err) return err; |
| 229 |
if (n <= capacity) { |
| 230 |
return 0; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
transform->matched = false; |
| 237 |
return pn_string_set(dst, src); |
| 238 |
} |
| 239 |
|
| 240 |
bool pn_transform_matched(pn_transform_t *transform) |
| 241 |
{ |
| 242 |
return transform->matched; |
| 243 |
} |
| 244 |
|
| 245 |
int pn_transform_get_substitutions(pn_transform_t *transform, |
| 246 |
pn_list_t *substitutions) |
| 247 |
{ |
| 248 |
int size = pn_list_size(transform->rules); |
| 249 |
for (size_t i = 0; i < (size_t)size; i++) { |
| 250 |
pn_rule_t *rule = (pn_rule_t *)pn_list_get(transform->rules, i); |
| 251 |
pn_list_add(substitutions, rule->substitution); |
| 252 |
} |
| 253 |
|
| 254 |
return size; |
| 255 |
} |