You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been trying to reimplement the following logic in Rust: Lua, PHP.
Basically, they use a regular expression to unescape some sequences and throw an exception if it's not a known one. I tried to do a similar thing but ran into an issue that Replacer is expected to be a function that returns String, not Result<String, _>.
I circumvented this by panic!()-ing inside my Replacer and wrapping a replace_all() call in std::panic::catch_unwind() but, AFAIK, it's a pretty terrible solution.
Is there a better approach? (Other than not using regular expressions for deserializing, haha)
The text was updated successfully, but these errors were encountered:
It's hard for me to understand the problem you're trying to solve without seeing a complete Rust program. Could you provide a minimal reproducible example of your problem? For example, based on your description, it's not clear to me why the error needs to come at replacement time instead of being checked for before doing a replacement. I just don't understand enough about what you're trying to do.
Which isn't ideal, especially since there is no way to stop the replacement routine. At this point, I'd recommend not using regex's replacement functionality at all. It's really provided as a convenience. Implementing replacements on top of captures_iter is fairly simple to do, so I'd recommend just doing that:
fnreplace_all(re:&Regex,haystack:&str,replacement:implFn(&Captures) -> Result<String,Error>,) -> Result<String,Error>{letmut new = String::with_capacity(haystack.len());letmut last_match = 0;for caps in re.captures_iter(haystack){let m = caps.get(0).unwrap();
new.push_str(&haystack[last_match..m.start()]);
new.push_str(&replacement(&caps)?);
last_match = m.end();}
new.push_str(&haystack[last_match..]);Ok(new)}
I've been trying to reimplement the following logic in Rust: Lua, PHP.
Basically, they use a regular expression to unescape some sequences and throw an exception if it's not a known one. I tried to do a similar thing but ran into an issue that
Replacer
is expected to be a function that returnsString
, notResult<String, _>
.I circumvented this by
panic!()
-ing inside myReplacer
and wrapping areplace_all()
call instd::panic::catch_unwind()
but, AFAIK, it's a pretty terrible solution.Is there a better approach? (Other than not using regular expressions for deserializing, haha)
The text was updated successfully, but these errors were encountered: