From 6c07af93436973036781669e47a0ac8fa77f82d6 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 22 Apr 2023 00:49:16 +0200 Subject: [PATCH] Shorthand for escaping with default options Should probably do this on the C++ side too. --- fish-rust/src/builtins/abbr.rs | 4 ++-- fish-rust/src/common.rs | 5 +++++ fish-rust/src/trace.rs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/fish-rust/src/builtins/abbr.rs b/fish-rust/src/builtins/abbr.rs index 422692c32..60f270d4a 100644 --- a/fish-rust/src/builtins/abbr.rs +++ b/fish-rust/src/builtins/abbr.rs @@ -4,7 +4,7 @@ builtin_unknown_option, io_streams_t, BUILTIN_ERR_TOO_MANY_ARGUMENTS, STATUS_CMD_ERROR, STATUS_CMD_OK, STATUS_INVALID_ARGS, }; -use crate::common::{escape_string, valid_func_name, EscapeStringStyle}; +use crate::common::{escape, escape_string, valid_func_name, EscapeStringStyle}; use crate::env::status::{ENV_NOT_FOUND, ENV_OK}; use crate::env::EnvMode; use crate::ffi::parser_t; @@ -415,7 +415,7 @@ fn abbr_erase(opts: &Options, parser: &mut parser_t) -> Option { result = Some(ENV_NOT_FOUND); } // Erase the old uvar - this makes `abbr -e` work. - let esc_src = escape_string(arg, EscapeStringStyle::Script(Default::default())); + let esc_src = escape(arg); if !esc_src.is_empty() { let var_name = WString::from_str("_fish_abbr_") + esc_src.as_utfstr(); let ret = parser.remove_var(&var_name, EnvMode::UNIVERSAL.into()); diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 02efe4ba2..f7a153ca0 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -140,6 +140,11 @@ pub struct UnescapeFlags: u32 { } } +/// Replace special characters with backslash escape sequences. Newline is replaced with `\n`, etc. +pub fn escape(s: &wstr) -> WString { + escape_string(s, EscapeStringStyle::Script(EscapeFlags::default())) +} + /// Replace special characters with backslash escape sequences. Newline is replaced with `\n`, etc. pub fn escape_string(s: &wstr, style: EscapeStringStyle) -> WString { match style { diff --git a/fish-rust/src/trace.rs b/fish-rust/src/trace.rs index 43d3c3797..01b8780ba 100644 --- a/fish-rust/src/trace.rs +++ b/fish-rust/src/trace.rs @@ -1,5 +1,5 @@ use crate::{ - common::{escape_string, EscapeStringStyle}, + common::escape, ffi::{self, parser_t, wcharz_t, wcstring_list_ffi_t}, global_safety::RelaxedAtomicBool, wchar::{self, wstr, L}, @@ -61,7 +61,7 @@ pub fn trace_argv(parser: &parser_t, command: &wstr, args: &[&wstr]) { } for arg in args { trace_text.push(' '); - trace_text.push_utfstr(&escape_string(arg, EscapeStringStyle::default())); + trace_text.push_utfstr(&escape(arg)); } trace_text.push('\n'); ffi::log_extra_to_flog_file(&trace_text.to_ffi());