blob: c9955e1d2e084cf9652cf7e59fdf9b9d3ad73197 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Copyright 2020 The Chromium Authors. All rights reserved.
// Copyright 2014 Blake Embrey (hello@blakeembrey.com)
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.
#include "third_party/liburlpattern/utils.h"
namespace liburlpattern {
namespace {
constexpr absl::string_view kRegexpSpecialCharacters(".+*?^${}()[]|/\\");
constexpr absl::string_view kPatternSpecialCharacters("+*?:{}()\\");
void EscapeStringAndAppendInternal(absl::string_view input,
std::string& append_target,
absl::string_view special_chars) {
for (auto& c : input) {
if (special_chars.find(c) != std::string::npos)
append_target += '\\';
append_target += c;
}
}
} // namespace
size_t EscapedRegexpStringLength(absl::string_view input) {
size_t count = input.size();
for (auto& c : input) {
if (kRegexpSpecialCharacters.find(c) != std::string::npos)
count += 1;
}
return count;
}
void EscapeRegexpStringAndAppend(absl::string_view input,
std::string& append_target) {
return EscapeStringAndAppendInternal(input, append_target,
kRegexpSpecialCharacters);
}
void EscapePatternStringAndAppend(absl::string_view input,
std::string& append_target) {
return EscapeStringAndAppendInternal(input, append_target,
kPatternSpecialCharacters);
}
std::string EscapeRegexpString(absl::string_view input) {
std::string result;
result.reserve(EscapedRegexpStringLength(input));
EscapeRegexpStringAndAppend(input, result);
return result;
}
} // namespace liburlpattern
|