| 264 | } |
| 265 | |
| 266 | fn make_regex(vm: &VirtualMachine, pattern: &str, flags: PyRegexFlags) -> PyResult<PyPattern> { |
| 267 | let unicode = if flags.unicode && flags.ascii { |
| 268 | return Err(vm.new_value_error("ASCII and UNICODE flags are incompatible")); |
| 269 | } else { |
| 270 | !flags.ascii |
| 271 | }; |
| 272 | let r = RegexBuilder::new(pattern) |
| 273 | .case_insensitive(flags.ignorecase) |
| 274 | .multi_line(flags.multiline) |
| 275 | .dot_matches_new_line(flags.dotall) |
| 276 | .ignore_whitespace(flags.verbose) |
| 277 | .unicode(unicode) |
| 278 | .build() |
| 279 | .map_err(|err| match err { |
| 280 | regex::Error::Syntax(s) => vm.new_value_error(format!("Error in regex: {}", s)), |
| 281 | err => vm.new_value_error(format!("Error in regex: {:?}", err)), |
| 282 | })?; |
| 283 | Ok(PyPattern { |
| 284 | regex: r, |
| 285 | pattern: pattern.to_owned(), |
| 286 | }) |
| 287 | } |
| 288 | |
| 289 | /// Take a found regular expression and convert it to proper match object. |
| 290 | fn create_match(haystack: PyStrRef, captures: Captures) -> PyMatch { |