Skip to content

Commit 403b27a

Browse files
committed
Switch to more idiomatic builder definition.
Specifically, use mutable references instead of passing ownership.
1 parent 1f7f5c9 commit 403b27a

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

regex-capi/src/rure.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ ffi_fn! {
118118
let mut builder = bytes::RegexBuilder::new(pat);
119119
if !options.is_null() {
120120
let options = unsafe { &*options };
121-
builder = builder.size_limit(options.size_limit);
122-
builder = builder.dfa_size_limit(options.dfa_size_limit);
121+
builder.size_limit(options.size_limit);
122+
builder.dfa_size_limit(options.dfa_size_limit);
123123
}
124-
builder = builder.case_insensitive(flags & RURE_FLAG_CASEI > 0);
125-
builder = builder.multi_line(flags & RURE_FLAG_MULTI > 0);
126-
builder = builder.dot_matches_new_line(flags & RURE_FLAG_DOTNL > 0);
127-
builder = builder.swap_greed(flags & RURE_FLAG_SWAP_GREED > 0);
128-
builder = builder.ignore_whitespace(flags & RURE_FLAG_SPACE > 0);
129-
builder = builder.unicode(flags & RURE_FLAG_UNICODE > 0);
130-
match builder.compile() {
124+
builder.case_insensitive(flags & RURE_FLAG_CASEI > 0);
125+
builder.multi_line(flags & RURE_FLAG_MULTI > 0);
126+
builder.dot_matches_new_line(flags & RURE_FLAG_DOTNL > 0);
127+
builder.swap_greed(flags & RURE_FLAG_SWAP_GREED > 0);
128+
builder.ignore_whitespace(flags & RURE_FLAG_SPACE > 0);
129+
builder.unicode(flags & RURE_FLAG_UNICODE > 0);
130+
match builder.build() {
131131
Ok(re) => {
132132
let mut capture_names = HashMap::new();
133133
for (i, name) in re.capture_names().enumerate() {

src/re_builder.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ impl RegexBuilder {
7171
/// Note that calling `as_str` on the resulting `Regex` will produce the
7272
/// pattern given to `new` verbatim. Notably, it will not incorporate any
7373
/// of the flags set on this builder.
74-
pub fn compile(self) -> Result<Regex, Error> {
75-
ExecBuilder::new_options(self.0)
74+
pub fn build(&self) -> Result<Regex, Error> {
75+
ExecBuilder::new_options(self.0.clone())
7676
.only_utf8($only_utf8)
7777
.build()
7878
.map(Regex::from)
7979
}
8080

8181
/// Set the value for the case insensitive (`i`) flag.
82-
pub fn case_insensitive(mut self, yes: bool) -> RegexBuilder {
82+
pub fn case_insensitive(&mut self, yes: bool) -> &mut RegexBuilder {
8383
self.0.case_insensitive = yes;
8484
self
8585
}
8686

8787
/// Set the value for the multi-line matching (`m`) flag.
88-
pub fn multi_line(mut self, yes: bool) -> RegexBuilder {
88+
pub fn multi_line(&mut self, yes: bool) -> &mut RegexBuilder {
8989
self.0.multi_line = yes;
9090
self
9191
}
@@ -97,27 +97,27 @@ impl RegexBuilder {
9797
/// N.B. "matches anything" means "any byte" for `regex::bytes::Regex`
9898
/// expressions and means "any Unicode codepoint" for `regex::Regex`
9999
/// expressions.
100-
pub fn dot_matches_new_line(mut self, yes: bool) -> RegexBuilder {
100+
pub fn dot_matches_new_line(&mut self, yes: bool) -> &mut RegexBuilder {
101101
self.0.dot_matches_new_line = yes;
102102
self
103103
}
104104

105105
/// Set the value for the greedy swap (`U`) flag.
106-
pub fn swap_greed(mut self, yes: bool) -> RegexBuilder {
106+
pub fn swap_greed(&mut self, yes: bool) -> &mut RegexBuilder {
107107
self.0.swap_greed = yes;
108108
self
109109
}
110110

111111
/// Set the value for the ignore whitespace (`x`) flag.
112-
pub fn ignore_whitespace(mut self, yes: bool) -> RegexBuilder {
112+
pub fn ignore_whitespace(&mut self, yes: bool) -> &mut RegexBuilder {
113113
self.0.ignore_whitespace = yes;
114114
self
115115
}
116116

117117
/// Set the value for the Unicode (`u`) flag.
118118
///
119119
/// For byte based regular expressions, this is disabled by default.
120-
pub fn unicode(mut self, yes: bool) -> RegexBuilder {
120+
pub fn unicode(&mut self, yes: bool) -> &mut RegexBuilder {
121121
self.0.unicode = yes;
122122
self
123123
}
@@ -127,7 +127,7 @@ impl RegexBuilder {
127127
/// This roughly corresponds to the number of bytes occupied by a single
128128
/// compiled program. If the program exceeds this number, then a
129129
/// compilation error is returned.
130-
pub fn size_limit(mut self, limit: usize) -> RegexBuilder {
130+
pub fn size_limit(&mut self, limit: usize) -> &mut RegexBuilder {
131131
self.0.size_limit = limit;
132132
self
133133
}
@@ -141,7 +141,7 @@ impl RegexBuilder {
141141
/// limit. In particular, if a regex is used from multiple threads
142142
/// simulanteously, then each thread may use up to the number of bytes
143143
/// specified here.
144-
pub fn dfa_size_limit(mut self, limit: usize) -> RegexBuilder {
144+
pub fn dfa_size_limit(&mut self, limit: usize) -> &mut RegexBuilder {
145145
self.0.dfa_size_limit = limit;
146146
self
147147
}

src/re_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Regex {
117117
///
118118
/// If an invalid expression is given, then an error is returned.
119119
pub fn new(re: &str) -> Result<Regex, Error> {
120-
RegexBuilder::new(re).compile()
120+
RegexBuilder::new(re).build()
121121
}
122122

123123
/// Returns true if and only if the regex matches the string given.

src/re_unicode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl Regex {
182182
///
183183
/// If an invalid expression is given, then an error is returned.
184184
pub fn new(re: &str) -> Result<Regex, Error> {
185-
RegexBuilder::new(re).compile()
185+
RegexBuilder::new(re).build()
186186
}
187187

188188
/// Returns true if and only if the regex matches the string given.

0 commit comments

Comments
 (0)