builtin string: add names to RegexError enum fields

This commit is contained in:
Johannes Altmanninger
2026-04-28 21:09:41 +08:00
parent 81e8eebd8d
commit ab2678082e
3 changed files with 32 additions and 15 deletions

View File

@@ -152,9 +152,16 @@ enum StringError<'a> {
}
enum RegexError {
Compile(WString, pcre2::Error),
InvalidCaptureGroupName(WString),
InvalidEscape(WString),
Compile {
pattern: WString,
error: pcre2::Error,
},
InvalidCaptureGroupName {
name: WString,
},
InvalidEscape {
replacement: WString,
},
}
impl RegexError {
@@ -163,14 +170,15 @@ fn print_error(&self, args: &[&wstr], streams: &mut IoStreams) {
let subcmd = args[0];
use RegexError::*;
match self {
Compile(pattern, e) => {
Compile { pattern, error } => {
// TODO: This is misaligned if `pattern` contains characters which are not exactly 1
// terminal cell wide.
let mut marker: WString =
" ".repeat(e.offset().unwrap_or(0).saturating_sub(1)).into();
let mut marker: WString = " "
.repeat(error.offset().unwrap_or(0).saturating_sub(1))
.into();
marker.push('^');
err_fmt!(Error::REGEX_COMPILE, e.error_message())
err_fmt!(Error::REGEX_COMPILE, error.error_message())
.append_to_msg('\n')
.append_to_msg(&err_raw!(pattern).subcmd(cmd, subcmd).to_string())
.append_to_msg('\n')
@@ -178,15 +186,15 @@ fn print_error(&self, args: &[&wstr], streams: &mut IoStreams) {
.subcmd(cmd, subcmd)
.finish(streams);
}
InvalidCaptureGroupName(name) => {
InvalidCaptureGroupName { name } => {
err_fmt!(
"Modification of read-only variable \"%s\" is not allowed",
name
)
.finish(streams);
}
InvalidEscape(pattern) => {
err_fmt!("Invalid escape sequence in pattern \"%s\"", pattern)
InvalidEscape { replacement } => {
err_fmt!("Invalid escape sequence in pattern \"%s\"", replacement)
.subcmd(cmd, subcmd)
.finish(streams);
}

View File

@@ -234,7 +234,10 @@ fn new(
// the capture group names are valid variable names
.block_utf_pattern_directive(true)
.build(pattern.as_char_slice())
.map_err(|e| RegexError::Compile(pattern.to_owned(), e))?;
.map_err(|error| RegexError::Compile {
pattern: pattern.to_owned(),
error,
})?;
Self::validate_capture_group_names(regex.capture_names())?;
@@ -315,7 +318,7 @@ fn validate_capture_group_names(
for name in capture_group_names.iter().filter_map(|n| n.as_ref()) {
let wname = str2wcstring(name);
if EnvVar::flags_for(&wname).contains(EnvVarFlags::READ_ONLY) {
return Err(RegexError::InvalidCaptureGroupName(wname));
return Err(RegexError::InvalidCaptureGroupName { name: wname });
}
}
Ok(())

View File

@@ -183,13 +183,19 @@ fn new(
// allowed to be user-controlled here
.block_utf_pattern_directive(true)
.build(pattern.as_char_slice())
.map_err(|e| RegexError::Compile(pattern.to_owned(), e))?;
.map_err(|error| RegexError::Compile {
pattern: pattern.to_owned(),
error,
})?;
let replacement = if feature_test(FeatureFlag::StringReplaceBackslash) {
replacement.to_owned()
} else {
Self::interpret_escape(replacement)
.ok_or_else(|| RegexError::InvalidEscape(replacement.to_owned()))?
Self::interpret_escape(replacement).ok_or_else(|| {
RegexError::InvalidEscape {
replacement: replacement.to_owned(),
}
})?
};
Self::Regex {
replacement,