reek-rs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +614 -0
- data/Cargo.toml +24 -0
- data/DIVERGENCES.md +15 -0
- data/LICENSE +22 -0
- data/README.md +125 -0
- data/THIRD_PARTY_NOTICES.md +57 -0
- data/crates/reek-cli/Cargo.toml +22 -0
- data/crates/reek-cli/src/main.rs +277 -0
- data/crates/reek-core/Cargo.toml +21 -0
- data/crates/reek-core/src/ast.rs +449 -0
- data/crates/reek-core/src/code_comment.rs +245 -0
- data/crates/reek-core/src/comments.rs +179 -0
- data/crates/reek-core/src/config.rs +803 -0
- data/crates/reek-core/src/context.rs +891 -0
- data/crates/reek-core/src/detectors/attribute.rs +13 -0
- data/crates/reek-core/src/detectors/boolean_parameter.rs +20 -0
- data/crates/reek-core/src/detectors/class_variable.rs +25 -0
- data/crates/reek-core/src/detectors/control_parameter.rs +110 -0
- data/crates/reek-core/src/detectors/data_clump.rs +114 -0
- data/crates/reek-core/src/detectors/duplicate_method_call.rs +63 -0
- data/crates/reek-core/src/detectors/feature_envy.rs +32 -0
- data/crates/reek-core/src/detectors/instance_variable_assumption.rs +39 -0
- data/crates/reek-core/src/detectors/irresponsible_module.rs +12 -0
- data/crates/reek-core/src/detectors/long_parameter_list.rs +16 -0
- data/crates/reek-core/src/detectors/long_yield_list.rs +20 -0
- data/crates/reek-core/src/detectors/manual_dispatch.rs +18 -0
- data/crates/reek-core/src/detectors/missing_safe_method.rs +34 -0
- data/crates/reek-core/src/detectors/mod.rs +479 -0
- data/crates/reek-core/src/detectors/module_initialize.rs +15 -0
- data/crates/reek-core/src/detectors/nested_iterators.rs +82 -0
- data/crates/reek-core/src/detectors/nil_check.rs +40 -0
- data/crates/reek-core/src/detectors/repeated_conditional.rs +51 -0
- data/crates/reek-core/src/detectors/subclassed_from_core_class.rs +22 -0
- data/crates/reek-core/src/detectors/too_many_constants.rs +18 -0
- data/crates/reek-core/src/detectors/too_many_instance_variables.rs +25 -0
- data/crates/reek-core/src/detectors/too_many_methods.rs +15 -0
- data/crates/reek-core/src/detectors/too_many_statements.rs +16 -0
- data/crates/reek-core/src/detectors/uncommunicative_method_name.rs +32 -0
- data/crates/reek-core/src/detectors/uncommunicative_module_name.rs +33 -0
- data/crates/reek-core/src/detectors/uncommunicative_parameter_name.rs +49 -0
- data/crates/reek-core/src/detectors/uncommunicative_variable_name.rs +138 -0
- data/crates/reek-core/src/detectors/unused_parameters.rs +21 -0
- data/crates/reek-core/src/detectors/unused_private_method.rs +59 -0
- data/crates/reek-core/src/detectors/utility_function.rs +27 -0
- data/crates/reek-core/src/encodings.rs +180 -0
- data/crates/reek-core/src/examiner.rs +165 -0
- data/crates/reek-core/src/lib.rs +21 -0
- data/crates/reek-core/src/node.rs +423 -0
- data/crates/reek-core/src/ruby_format.rs +60 -0
- data/crates/reek-core/src/ruby_inspect.rs +172 -0
- data/crates/reek-core/src/ruby_regexp.rs +243 -0
- data/crates/reek-core/src/single_byte_encodings.rs +6007 -0
- data/crates/reek-core/src/source.rs +280 -0
- data/crates/reek-core/src/translate.rs +3112 -0
- data/crates/reek-core/src/yaml.rs +520 -0
- data/ext/reek_rs/Cargo.toml +18 -0
- data/ext/reek_rs/extconf.rb +6 -0
- data/ext/reek_rs/src/lib.rs +155 -0
- data/lib/reek_rs/version.rb +8 -0
- data/lib/reek_rs.rb +390 -0
- metadata +123 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
//! Ruby's `inspect` for the values `Parser::AST::Node#to_s` prints.
|
|
2
|
+
//!
|
|
3
|
+
//! Reek falls back to `to_s` (the node's s-expression) for the `name` of
|
|
4
|
+
//! nodes it has no extension for, and that text can end up in a smell's
|
|
5
|
+
//! context, as in `def (obj).method`.
|
|
6
|
+
|
|
7
|
+
/// `Symbol#inspect`.
|
|
8
|
+
pub fn symbol_inspect(name: &[u8]) -> String {
|
|
9
|
+
if is_simple_symbol(name) {
|
|
10
|
+
format!(":{}", String::from_utf8_lossy(name))
|
|
11
|
+
} else {
|
|
12
|
+
format!(":{}", string_inspect(name))
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// Whether `Symbol#inspect` prints the name bare: operators, identifiers
|
|
17
|
+
/// (optionally ending in `?`, `!` or `=`), and instance, class and global
|
|
18
|
+
/// variable names.
|
|
19
|
+
fn is_simple_symbol(name: &[u8]) -> bool {
|
|
20
|
+
const OPERATORS: [&[u8]; 29] = [
|
|
21
|
+
b"[]", b"[]=", b"**", b"!", b"!=", b"!~", b"~", b"+", b"-", b"+@", b"-@", b"*", b"/", b"%", b"==", b"===",
|
|
22
|
+
b"=~", b"<", b"<=", b">", b">=", b"<=>", b"<<", b">>", b"&", b"|", b"^", b"`", b"~@",
|
|
23
|
+
];
|
|
24
|
+
if OPERATORS.contains(&name) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
if std::str::from_utf8(name).is_err() {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if let Some(rest) = name.strip_prefix(b"@@").or_else(|| name.strip_prefix(b"@")) {
|
|
31
|
+
return is_identifier(rest);
|
|
32
|
+
}
|
|
33
|
+
if let Some(rest) = name.strip_prefix(b"$") {
|
|
34
|
+
return is_global_name(rest);
|
|
35
|
+
}
|
|
36
|
+
let body = match name.last() {
|
|
37
|
+
Some(b'?' | b'!') => &name[..name.len() - 1],
|
|
38
|
+
Some(b'=') => &name[..name.len() - 1],
|
|
39
|
+
_ => name,
|
|
40
|
+
};
|
|
41
|
+
is_identifier(body) && !(name.ends_with(b"=") && body.last().is_some_and(|last| matches!(last, b'?' | b'!')))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn is_identifier(name: &[u8]) -> bool {
|
|
45
|
+
let Some(first) = name.first() else { return false };
|
|
46
|
+
let identifier_byte = |byte: &u8| byte.is_ascii_alphanumeric() || *byte == b'_' || *byte >= 0x80;
|
|
47
|
+
!first.is_ascii_digit() && name.iter().all(identifier_byte)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn is_global_name(rest: &[u8]) -> bool {
|
|
51
|
+
match rest {
|
|
52
|
+
[] => false,
|
|
53
|
+
[special] if b"~*$?!@/\\;,.=:<>\"&`'+0_".contains(special) => true,
|
|
54
|
+
[b'-', letter] if letter.is_ascii_alphanumeric() || *letter == b'_' => true,
|
|
55
|
+
digits if digits.iter().all(u8::is_ascii_digit) => true,
|
|
56
|
+
name => is_identifier(name),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// `String#inspect` for a UTF-8 string (invalid bytes print as `\xHH`).
|
|
61
|
+
pub fn string_inspect(bytes: &[u8]) -> String {
|
|
62
|
+
let mut out = String::from("\"");
|
|
63
|
+
let mut index = 0;
|
|
64
|
+
while index < bytes.len() {
|
|
65
|
+
let byte = bytes[index];
|
|
66
|
+
let next = bytes.get(index + 1).copied();
|
|
67
|
+
match byte {
|
|
68
|
+
b'"' => out.push_str("\\\""),
|
|
69
|
+
b'\\' => out.push_str("\\\\"),
|
|
70
|
+
b'#' if matches!(next, Some(b'{' | b'$' | b'@')) => out.push_str("\\#"),
|
|
71
|
+
b'\n' => out.push_str("\\n"),
|
|
72
|
+
b'\r' => out.push_str("\\r"),
|
|
73
|
+
b'\t' => out.push_str("\\t"),
|
|
74
|
+
0x0c => out.push_str("\\f"),
|
|
75
|
+
0x0b => out.push_str("\\v"),
|
|
76
|
+
0x08 => out.push_str("\\b"),
|
|
77
|
+
0x07 => out.push_str("\\a"),
|
|
78
|
+
0x1b => out.push_str("\\e"),
|
|
79
|
+
0x00..=0x1f | 0x7f => out.push_str(&format!("\\u{byte:04X}")),
|
|
80
|
+
0x20..=0x7e => out.push(char::from(byte)),
|
|
81
|
+
_ => {
|
|
82
|
+
let length = utf8_length(&bytes[index..]);
|
|
83
|
+
match length.and_then(|length| std::str::from_utf8(&bytes[index..index + length]).ok()) {
|
|
84
|
+
Some(character) => {
|
|
85
|
+
out.push_str(character);
|
|
86
|
+
index += character.len();
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
None => out.push_str(&format!("\\x{byte:02X}")),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
index += 1;
|
|
94
|
+
}
|
|
95
|
+
out.push('"');
|
|
96
|
+
out
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn utf8_length(bytes: &[u8]) -> Option<usize> {
|
|
100
|
+
let length = match bytes.first()? {
|
|
101
|
+
0xc2..=0xdf => 2,
|
|
102
|
+
0xe0..=0xef => 3,
|
|
103
|
+
0xf0..=0xf4 => 4,
|
|
104
|
+
_ => return None,
|
|
105
|
+
};
|
|
106
|
+
(bytes.len() >= length).then_some(length)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// `Rational#inspect` from `Rational#to_s`.
|
|
110
|
+
pub fn rational_inspect(to_s: &str) -> String {
|
|
111
|
+
format!("({to_s})")
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// `Complex#inspect` from `Complex#to_s`: a rational imaginary part
|
|
115
|
+
/// prints as `(n/d)*i`.
|
|
116
|
+
pub fn complex_inspect(to_s: &str) -> String {
|
|
117
|
+
let imaginary_start = to_s[1..].find(['+', '-']).map_or(0, |index| index + 2);
|
|
118
|
+
let (real_and_sign, imaginary) = to_s.split_at(imaginary_start);
|
|
119
|
+
let magnitude = imaginary.strip_suffix('i').unwrap_or(imaginary);
|
|
120
|
+
if magnitude.contains('/') {
|
|
121
|
+
format!("({real_and_sign}({magnitude})*i)")
|
|
122
|
+
} else {
|
|
123
|
+
format!("({to_s})")
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#[cfg(test)]
|
|
128
|
+
mod tests {
|
|
129
|
+
use super::*;
|
|
130
|
+
|
|
131
|
+
#[test]
|
|
132
|
+
fn symbols_match_ruby() {
|
|
133
|
+
let cases: [(&[u8], &str); 16] = [
|
|
134
|
+
(b"foo", ":foo"),
|
|
135
|
+
(b"foo?", ":foo?"),
|
|
136
|
+
(b"Foo=", ":Foo="),
|
|
137
|
+
(b"@@b", ":@@b"),
|
|
138
|
+
(b"$1", ":$1"),
|
|
139
|
+
(b"$-w", ":$-w"),
|
|
140
|
+
(b"a b", ":\"a b\""),
|
|
141
|
+
(b"[]=", ":[]="),
|
|
142
|
+
(b"foo?=", ":\"foo?=\""),
|
|
143
|
+
(b"9a", ":\"9a\""),
|
|
144
|
+
("é".as_bytes(), ":é"),
|
|
145
|
+
(b"", ":\"\""),
|
|
146
|
+
(b"@", ":\"@\""),
|
|
147
|
+
(b"=", ":\"=\""),
|
|
148
|
+
(b"a\nb", ":\"a\\nb\""),
|
|
149
|
+
(b"-@", ":-@"),
|
|
150
|
+
];
|
|
151
|
+
for (name, expected) in cases {
|
|
152
|
+
assert_eq!(symbol_inspect(name), expected, "{:?}", String::from_utf8_lossy(name));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#[test]
|
|
157
|
+
fn strings_match_ruby() {
|
|
158
|
+
assert_eq!(
|
|
159
|
+
string_inspect("a\"b\\c#{d} #$e #@f #g\n\t\u{1b}\u{1}\u{7f} é".as_bytes()),
|
|
160
|
+
"\"a\\\"b\\\\c\\#{d} \\#$e \\#@f #g\\n\\t\\e\\u0001\\u007F é\""
|
|
161
|
+
);
|
|
162
|
+
assert_eq!(string_inspect(b"\xff"), "\"\\xFF\"");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#[test]
|
|
166
|
+
fn numbers_match_ruby() {
|
|
167
|
+
assert_eq!(rational_inspect("3/2"), "(3/2)");
|
|
168
|
+
assert_eq!(complex_inspect("0+3i"), "(0+3i)");
|
|
169
|
+
assert_eq!(complex_inspect("0-2.5i"), "(0-2.5i)");
|
|
170
|
+
assert_eq!(complex_inspect("0+2/1i"), "(0+(2/1)*i)");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
//! Ruby regular expressions, via Oniguruma with Ruby syntax.
|
|
2
|
+
//!
|
|
3
|
+
//! Reek matches names against patterns from its defaults, `.reek.yml` and
|
|
4
|
+
//! source comments, and builds new patterns by interpolation
|
|
5
|
+
//! (`/#{candidate}/`). [`RubyRegexp`] compiles Ruby regexp source with
|
|
6
|
+
//! Ruby's options (`i`, `m`, `x`), matches UTF-8 bytes, and gives the
|
|
7
|
+
//! `Regexp#to_s` form that interpolation embeds.
|
|
8
|
+
|
|
9
|
+
use onig::{EncodedBytes, Regex, RegexOptions, SearchOptions, Syntax};
|
|
10
|
+
|
|
11
|
+
#[derive(Debug)]
|
|
12
|
+
pub struct RubyRegexp {
|
|
13
|
+
regex: Regex,
|
|
14
|
+
source: String,
|
|
15
|
+
ignore_case: bool,
|
|
16
|
+
extended: bool,
|
|
17
|
+
multiline: bool,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl Clone for RubyRegexp {
|
|
21
|
+
fn clone(&self) -> Self {
|
|
22
|
+
let mut options = String::new();
|
|
23
|
+
if self.ignore_case {
|
|
24
|
+
options.push('i');
|
|
25
|
+
}
|
|
26
|
+
if self.extended {
|
|
27
|
+
options.push('x');
|
|
28
|
+
}
|
|
29
|
+
if self.multiline {
|
|
30
|
+
options.push('m');
|
|
31
|
+
}
|
|
32
|
+
RubyRegexp::new(&self.source, &options).expect("recompiling a valid regexp")
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl PartialEq for RubyRegexp {
|
|
37
|
+
/// `Regexp#==`: same source and options.
|
|
38
|
+
fn eq(&self, other: &Self) -> bool {
|
|
39
|
+
self.source == other.source
|
|
40
|
+
&& self.ignore_case == other.ignore_case
|
|
41
|
+
&& self.extended == other.extended
|
|
42
|
+
&& self.multiline == other.multiline
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn utf8(bytes: &[u8]) -> EncodedBytes<'_> {
|
|
47
|
+
EncodedBytes::from_parts(bytes, &raw mut onig_sys::OnigEncodingUTF8)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
impl RubyRegexp {
|
|
51
|
+
/// `Regexp.new(source, options)`; `options` holds any of `i`, `m`, `x`.
|
|
52
|
+
pub fn new(source: &str, options: &str) -> Result<Self, String> {
|
|
53
|
+
let ignore_case = options.contains('i');
|
|
54
|
+
let extended = options.contains('x');
|
|
55
|
+
let multiline = options.contains('m');
|
|
56
|
+
// Ruby's `\w`, `\d` and `\s` match ASCII only.
|
|
57
|
+
let mut flags = RegexOptions::from_bits_retain(
|
|
58
|
+
onig_sys::ONIG_OPTION_WORD_IS_ASCII | onig_sys::ONIG_OPTION_DIGIT_IS_ASCII | onig_sys::ONIG_OPTION_SPACE_IS_ASCII,
|
|
59
|
+
);
|
|
60
|
+
if ignore_case {
|
|
61
|
+
flags |= RegexOptions::REGEX_OPTION_IGNORECASE;
|
|
62
|
+
}
|
|
63
|
+
if extended {
|
|
64
|
+
flags |= RegexOptions::REGEX_OPTION_EXTEND;
|
|
65
|
+
}
|
|
66
|
+
if multiline {
|
|
67
|
+
flags |= RegexOptions::REGEX_OPTION_MULTILINE;
|
|
68
|
+
}
|
|
69
|
+
let regex = Regex::with_options_and_encoding(utf8(source.as_bytes()), flags, Syntax::ruby())
|
|
70
|
+
.map_err(|error| error.description().to_string())?;
|
|
71
|
+
Ok(RubyRegexp { regex, source: source.to_string(), ignore_case, extended, multiline })
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// `Regexp.new(Regexp.quote(text))`.
|
|
75
|
+
pub fn quoted(text: &str) -> Self {
|
|
76
|
+
RubyRegexp::new("e(text), "").expect("a quoted string is a valid regexp")
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// `Regexp#source`.
|
|
80
|
+
pub fn source(&self) -> &str {
|
|
81
|
+
&self.source
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// `regexp =~ text` is truthy.
|
|
85
|
+
pub fn is_match(&self, text: &[u8]) -> bool {
|
|
86
|
+
self.find(text).is_some()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// The first match's byte range.
|
|
90
|
+
pub fn find(&self, text: &[u8]) -> Option<(usize, usize)> {
|
|
91
|
+
let mut region = onig::Region::new();
|
|
92
|
+
self.regex
|
|
93
|
+
.search_with_encoding(utf8(text), 0, text.len(), SearchOptions::SEARCH_OPTION_NONE, Some(&mut region))
|
|
94
|
+
.and_then(|_| region.pos(0))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// The first match's capture groups (`None` for groups that did not
|
|
98
|
+
/// participate), group 0 first.
|
|
99
|
+
pub fn captures(&self, text: &[u8]) -> Option<Vec<Option<(usize, usize)>>> {
|
|
100
|
+
let mut region = onig::Region::new();
|
|
101
|
+
self.regex.search_with_encoding(utf8(text), 0, text.len(), SearchOptions::SEARCH_OPTION_NONE, Some(&mut region))?;
|
|
102
|
+
Some((0..region.len()).map(|index| region.pos(index)).collect())
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// `String#scan`: the capture groups of each successive match. After an
|
|
106
|
+
/// empty match the search moves on one character.
|
|
107
|
+
pub fn scan(&self, text: &[u8]) -> Vec<Vec<Option<(usize, usize)>>> {
|
|
108
|
+
let mut out = Vec::new();
|
|
109
|
+
let mut start = 0;
|
|
110
|
+
while start <= text.len() {
|
|
111
|
+
let mut region = onig::Region::new();
|
|
112
|
+
let found = self.regex.search_with_encoding(
|
|
113
|
+
utf8(text),
|
|
114
|
+
start,
|
|
115
|
+
text.len(),
|
|
116
|
+
SearchOptions::SEARCH_OPTION_NONE,
|
|
117
|
+
Some(&mut region),
|
|
118
|
+
);
|
|
119
|
+
if found.is_none() {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
let (match_start, match_end) = region.pos(0).expect("a match has a range");
|
|
123
|
+
out.push((0..region.len()).map(|index| region.pos(index)).collect());
|
|
124
|
+
start = if match_end > match_start {
|
|
125
|
+
match_end
|
|
126
|
+
} else {
|
|
127
|
+
match_end + text[match_end..].first().map_or(1, |byte| utf8_char_length(*byte))
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
out
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// `String#gsub(regexp, replacement)` with a literal replacement.
|
|
134
|
+
pub fn replace_all(&self, text: &str, replacement: &str) -> String {
|
|
135
|
+
let mut out = String::with_capacity(text.len());
|
|
136
|
+
let mut last = 0;
|
|
137
|
+
for groups in self.scan(text.as_bytes()) {
|
|
138
|
+
let (start, end) = groups[0].expect("a match has a range");
|
|
139
|
+
out.push_str(&text[last..start]);
|
|
140
|
+
out.push_str(replacement);
|
|
141
|
+
last = end;
|
|
142
|
+
}
|
|
143
|
+
out.push_str(&text[last..]);
|
|
144
|
+
out
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// `Regexp#to_s`, as embedded by `/#{regexp}/`: `(?on-off:source)`.
|
|
148
|
+
pub fn to_embedded(&self) -> String {
|
|
149
|
+
let mut on = String::new();
|
|
150
|
+
let mut off = String::new();
|
|
151
|
+
for (flag, letter) in [(self.multiline, 'm'), (self.ignore_case, 'i'), (self.extended, 'x')] {
|
|
152
|
+
if flag {
|
|
153
|
+
on.push(letter);
|
|
154
|
+
} else {
|
|
155
|
+
off.push(letter);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if off.is_empty() {
|
|
159
|
+
format!("(?{on}:{})", self.source)
|
|
160
|
+
} else {
|
|
161
|
+
format!("(?{on}-{off}:{})", self.source)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// `Regexp#inspect`: `/source/options`.
|
|
166
|
+
pub fn inspect(&self) -> String {
|
|
167
|
+
let mut out = format!("/{}/", self.source.replace('/', "\\/"));
|
|
168
|
+
if self.multiline {
|
|
169
|
+
out.push('m');
|
|
170
|
+
}
|
|
171
|
+
if self.ignore_case {
|
|
172
|
+
out.push('i');
|
|
173
|
+
}
|
|
174
|
+
if self.extended {
|
|
175
|
+
out.push('x');
|
|
176
|
+
}
|
|
177
|
+
out
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
fn utf8_char_length(first_byte: u8) -> usize {
|
|
182
|
+
match first_byte {
|
|
183
|
+
0xc0..=0xdf => 2,
|
|
184
|
+
0xe0..=0xef => 3,
|
|
185
|
+
0xf0..=0xf7 => 4,
|
|
186
|
+
_ => 1,
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/// `Regexp.quote`.
|
|
191
|
+
pub fn quote(text: &str) -> String {
|
|
192
|
+
let mut out = String::with_capacity(text.len());
|
|
193
|
+
for character in text.chars() {
|
|
194
|
+
match character {
|
|
195
|
+
'[' | ']' | '{' | '}' | '(' | ')' | '|' | '-' | '*' | '.' | '\\' | '?' | '+' | '^' | '$' | '#' => {
|
|
196
|
+
out.push('\\');
|
|
197
|
+
out.push(character);
|
|
198
|
+
}
|
|
199
|
+
' ' => out.push_str("\\ "),
|
|
200
|
+
'\t' => out.push_str("\\t"),
|
|
201
|
+
'\n' => out.push_str("\\n"),
|
|
202
|
+
'\r' => out.push_str("\\r"),
|
|
203
|
+
'\u{c}' => out.push_str("\\f"),
|
|
204
|
+
'\u{b}' => out.push_str("\\v"),
|
|
205
|
+
other => out.push(other),
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
out
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#[cfg(test)]
|
|
212
|
+
mod tests {
|
|
213
|
+
use super::*;
|
|
214
|
+
|
|
215
|
+
#[test]
|
|
216
|
+
fn ruby_anchors_are_line_anchors() {
|
|
217
|
+
let regexp = RubyRegexp::new("^[a-z]$", "").unwrap();
|
|
218
|
+
assert!(regexp.is_match(b"x"));
|
|
219
|
+
assert!(regexp.is_match(b"ab\nc"));
|
|
220
|
+
assert!(!regexp.is_match(b"ab"));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#[test]
|
|
224
|
+
fn shorthand_classes_are_ascii() {
|
|
225
|
+
assert!(!RubyRegexp::new(r"\w", "").unwrap().is_match("é".as_bytes()));
|
|
226
|
+
assert!(!RubyRegexp::new(r"\s", "").unwrap().is_match("\u{a0}".as_bytes()));
|
|
227
|
+
assert!(RubyRegexp::new("[[:alpha:]]", "").unwrap().is_match("é".as_bytes()));
|
|
228
|
+
assert!(RubyRegexp::new("é", "i").unwrap().is_match("É".as_bytes()));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#[test]
|
|
232
|
+
fn embedding_keeps_options() {
|
|
233
|
+
let regexp = RubyRegexp::new("a", "i").unwrap();
|
|
234
|
+
assert_eq!(regexp.to_embedded(), "(?i-mx:a)");
|
|
235
|
+
let embedded = RubyRegexp::new(®exp.to_embedded(), "").unwrap();
|
|
236
|
+
assert!(embedded.is_match(b"A"));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#[test]
|
|
240
|
+
fn quote_matches_ruby() {
|
|
241
|
+
assert_eq!(quote("a.b c\t-#?[]{}()|*+^$\\/\n"), "a\\.b\\ c\\t\\-\\#\\?\\[\\]\\{\\}\\(\\)\\|\\*\\+\\^\\$\\\\/\\n");
|
|
242
|
+
}
|
|
243
|
+
}
|