Skip to content

Commit b34dd3b

Browse files
jtraceysylvestre
authored andcommitted
quoting_style: use and return OsStrings
This exposes the non-UTF-8 functionality to callers. Support in `argument`, `spec`, and `wc` are implemented, as their usage is simple. A wrapper only returning valid unicode is used in `ls`, since proper handling of OsStrings there is more involved (outputs that escape non-unicode work now though).
1 parent e894e57 commit b34dd3b

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

src/uu/ls/src/ls.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::os::windows::fs::MetadataExt;
2121
use std::{
2222
cmp::Reverse,
2323
error::Error,
24-
ffi::OsString,
24+
ffi::{OsStr, OsString},
2525
fmt::{Display, Write as FmtWrite},
2626
fs::{self, DirEntry, FileType, Metadata, ReadDir},
2727
io::{stdout, BufWriter, ErrorKind, Stdout, Write},
@@ -55,7 +55,7 @@ use uucore::libc::{dev_t, major, minor};
5555
#[cfg(unix)]
5656
use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR};
5757
use uucore::line_ending::LineEnding;
58-
use uucore::quoting_style::{escape_dir_name, escape_name, QuotingStyle};
58+
use uucore::quoting_style::QuotingStyle;
5959
use uucore::{
6060
display::Quotable,
6161
error::{set_exit_code, UError, UResult},
@@ -3542,3 +3542,15 @@ fn calculate_padding_collection(
35423542

35433543
padding_collections
35443544
}
3545+
3546+
fn escape_name(name: &OsStr, style: &QuotingStyle) -> String {
3547+
uucore::quoting_style::escape_name(name, style)
3548+
.to_string_lossy()
3549+
.to_string()
3550+
}
3551+
3552+
fn escape_dir_name(name: &OsStr, style: &QuotingStyle) -> String {
3553+
uucore::quoting_style::escape_dir_name(name, style)
3554+
.to_string_lossy()
3555+
.to_string()
3556+
}

src/uu/wc/src/wc.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod word_count;
1313
use std::{
1414
borrow::{Borrow, Cow},
1515
cmp::max,
16-
ffi::OsString,
16+
ffi::{OsStr, OsString},
1717
fs::{self, File},
1818
io::{self, Write},
1919
iter,
@@ -259,7 +259,7 @@ impl<'a> Input<'a> {
259259
match self {
260260
Self::Path(path) => Some(match path.to_str() {
261261
Some(s) if !s.contains('\n') => Cow::Borrowed(s),
262-
_ => Cow::Owned(escape_name(path.as_os_str(), QS_ESCAPE)),
262+
_ => Cow::Owned(escape_name_wrapper(path.as_os_str())),
263263
}),
264264
Self::Stdin(StdinKind::Explicit) => Some(Cow::Borrowed(STDIN_REPR)),
265265
Self::Stdin(StdinKind::Implicit) => None,
@@ -269,7 +269,7 @@ impl<'a> Input<'a> {
269269
/// Converts input into the form that appears in errors.
270270
fn path_display(&self) -> String {
271271
match self {
272-
Self::Path(path) => escape_name(path.as_os_str(), QS_ESCAPE),
272+
Self::Path(path) => escape_name_wrapper(path.as_os_str()),
273273
Self::Stdin(_) => String::from("standard input"),
274274
}
275275
}
@@ -361,7 +361,7 @@ impl WcError {
361361
Some((input, idx)) => {
362362
let path = match input {
363363
Input::Stdin(_) => STDIN_REPR.into(),
364-
Input::Path(path) => escape_name(path.as_os_str(), QS_ESCAPE).into(),
364+
Input::Path(path) => escape_name_wrapper(path.as_os_str()).into(),
365365
};
366366
Self::ZeroLengthFileNameCtx { path, idx }
367367
}
@@ -762,6 +762,8 @@ fn files0_iter_file<'a>(path: &Path) -> UResult<impl Iterator<Item = InputIterIt
762762
format!(
763763
"cannot open {} for reading",
764764
escape_name(path.as_os_str(), QS_QUOTE_ESCAPE)
765+
.into_string()
766+
.expect("All escaped names with the escaping option return valid strings.")
765767
)
766768
})),
767769
}
@@ -793,9 +795,9 @@ fn files0_iter<'a>(
793795
Ok(Input::Path(PathBuf::from(s).into()))
794796
}
795797
}
796-
Err(e) => Err(e.map_err_context(|| {
797-
format!("{}: read error", escape_name(&err_path, QS_ESCAPE))
798-
}) as Box<dyn UError>),
798+
Err(e) => Err(e
799+
.map_err_context(|| format!("{}: read error", escape_name_wrapper(&err_path)))
800+
as Box<dyn UError>),
799801
}),
800802
);
801803
// Loop until there is an error; yield that error and then nothing else.
@@ -808,6 +810,12 @@ fn files0_iter<'a>(
808810
})
809811
}
810812

813+
fn escape_name_wrapper(name: &OsStr) -> String {
814+
escape_name(name, QS_ESCAPE)
815+
.into_string()
816+
.expect("All escaped names with the escaping option return valid strings.")
817+
}
818+
811819
fn wc(inputs: &Inputs, settings: &Settings) -> UResult<()> {
812820
let mut total_word_count = WordCount::default();
813821
let mut num_inputs: usize = 0;

src/uucore/src/lib/features/format/argument.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T
112112
Default::default()
113113
}
114114
ParseError::PartialMatch(v, rest) => {
115-
if input.starts_with('\'') {
115+
let bytes = input.as_encoded_bytes();
116+
if !bytes.is_empty() && bytes[0] == b'\'' {
116117
show_warning!(
117118
"{}: character(s) following character constant have been ignored",
118119
&rest,

src/uucore/src/lib/features/format/spec.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,20 @@ impl Spec {
353353
writer.write_all(&parsed).map_err(FormatError::IoError)
354354
}
355355
Self::QuotedString => {
356-
let s = args.get_str();
357-
writer
358-
.write_all(
359-
escape_name(
360-
s.as_ref(),
361-
&QuotingStyle::Shell {
362-
escape: true,
363-
always_quote: false,
364-
show_control: false,
365-
},
366-
)
367-
.as_bytes(),
368-
)
369-
.map_err(FormatError::IoError)
356+
let s = escape_name(
357+
args.get_str().as_ref(),
358+
&QuotingStyle::Shell {
359+
escape: true,
360+
always_quote: false,
361+
show_control: false,
362+
},
363+
);
364+
#[cfg(unix)]
365+
let bytes = std::os::unix::ffi::OsStringExt::into_vec(s);
366+
#[cfg(not(unix))]
367+
let bytes = s.to_string_lossy().as_bytes().to_owned();
368+
369+
writer.write_all(&bytes).map_err(FormatError::IoError)
370370
}
371371
Self::SignedInt {
372372
width,

src/uucore/src/lib/features/quoting_style.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
//! Set of functions for escaping names according to different quoting styles.
77
88
use std::char::from_digit;
9-
use std::ffi::OsStr;
9+
use std::ffi::{OsStr, OsString};
1010
use std::fmt;
11+
#[cfg(unix)]
12+
use std::os::unix::ffi::{OsStrExt, OsStringExt};
1113

1214
// These are characters with special meaning in the shell (e.g. bash).
1315
// The first const contains characters that only have a special meaning when they appear at the beginning of a name.
@@ -454,17 +456,37 @@ fn escape_name_inner(name: &[u8], style: &QuotingStyle, dirname: bool) -> Vec<u8
454456
}
455457

456458
/// Escape a filename with respect to the given style.
457-
pub fn escape_name(name: &OsStr, style: &QuotingStyle) -> String {
458-
let name = name.to_string_lossy();
459-
String::from_utf8_lossy(&escape_name_inner(name.as_bytes(), style, false)).to_string()
459+
pub fn escape_name(name: &OsStr, style: &QuotingStyle) -> OsString {
460+
#[cfg(unix)]
461+
{
462+
let name = name.as_bytes();
463+
OsStringExt::from_vec(escape_name_inner(name, style, false))
464+
}
465+
#[cfg(not(unix))]
466+
{
467+
let name = name.to_string_lossy();
468+
String::from_utf8_lossy(&escape_name_inner(name.as_bytes(), style, false))
469+
.to_string()
470+
.into()
471+
}
460472
}
461473

462474
/// Escape a directory name with respect to the given style.
463475
/// This is mainly meant to be used for ls' directory name printing and is not
464476
/// likely to be used elsewhere.
465-
pub fn escape_dir_name(dir_name: &OsStr, style: &QuotingStyle) -> String {
466-
let dir_name = dir_name.to_string_lossy();
467-
String::from_utf8_lossy(&escape_name_inner(dir_name.as_bytes(), style, true)).to_string()
477+
pub fn escape_dir_name(dir_name: &OsStr, style: &QuotingStyle) -> OsString {
478+
#[cfg(unix)]
479+
{
480+
let name = dir_name.as_bytes();
481+
OsStringExt::from_vec(escape_name_inner(name, style, true))
482+
}
483+
#[cfg(not(unix))]
484+
{
485+
let name = dir_name.to_string_lossy();
486+
String::from_utf8_lossy(&escape_name_inner(name.as_bytes(), style, true))
487+
.to_string()
488+
.into()
489+
}
468490
}
469491

470492
impl fmt::Display for QuotingStyle {

0 commit comments

Comments
 (0)