Skip to content

Commit e3ec122

Browse files
committed
printf: support %q
1 parent 7279bc1 commit e3ec122

File tree

1 file changed

+15
-7
lines changed
  • src/uucore/src/lib/features/tokenize

1 file changed

+15
-7
lines changed

src/uucore/src/lib/features/tokenize/sub.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Subs which have numeric field chars make use of the num_format
1111
//! submodule
1212
use crate::error::{UError, UResult};
13+
use crate::quoting_style::{escape_name, QuotingStyle};
1314
use itertools::{put_back_n, PutBackN};
1415
use std::error::Error;
1516
use std::fmt::Display;
@@ -91,7 +92,7 @@ impl Sub {
9192
// for more dry printing, field characters are grouped
9293
// in initialization of token.
9394
let field_type = match field_char {
94-
's' | 'b' => FieldType::Strf,
95+
's' | 'b' | 'q' => FieldType::Strf,
9596
'd' | 'i' | 'u' | 'o' | 'x' | 'X' => FieldType::Intf,
9697
'f' | 'F' => FieldType::Floatf,
9798
'a' | 'A' => FieldType::CninetyNineHexFloatf,
@@ -189,7 +190,7 @@ impl SubParser {
189190

190191
let mut legal_fields = [
191192
// 'a', 'A', //c99 hex float implementation not yet complete
192-
'b', 'c', 'd', 'e', 'E', 'f', 'F', 'g', 'G', 'i', 'o', 's', 'u', 'x', 'X',
193+
'b', 'c', 'd', 'e', 'E', 'f', 'F', 'g', 'G', 'i', 'o', 'q', 's', 'u', 'x', 'X',
193194
];
194195
let mut specifiers = ['h', 'j', 'l', 'L', 't', 'z'];
195196
legal_fields.sort_unstable();
@@ -260,7 +261,6 @@ impl SubParser {
260261
}
261262
x if legal_fields.binary_search(&x).is_ok() => {
262263
self.field_char = Some(ch);
263-
self.text_so_far.push(ch);
264264
break;
265265
}
266266
x if specifiers.binary_search(&x).is_ok() => {
@@ -331,7 +331,7 @@ impl SubParser {
331331
if (field_char == 's' && self.min_width_tmp == Some(String::from("0")))
332332
|| (field_char == 'c'
333333
&& (self.min_width_tmp == Some(String::from("0")) || self.past_decimal))
334-
|| (field_char == 'b'
334+
|| ((field_char == 'b' || field_char == 'q')
335335
&& (self.min_width_tmp.is_some()
336336
|| self.past_decimal
337337
|| self.second_field_tmp.is_some()))
@@ -391,6 +391,7 @@ impl Sub {
391391
// if %s just return arg
392392
// if %b use UnescapedText module's unescape-fn
393393
// if %c return first char of arg
394+
// if %q return arg which non-printable characters are escaped
394395
FieldType::Strf | FieldType::Charf => {
395396
match pf_arg {
396397
Some(arg_string) => {
@@ -404,11 +405,18 @@ impl Sub {
404405
UnescapedText::from_it_core(writer, &mut a_it, true);
405406
None
406407
}
407-
// for 'c': get iter of string vals,
408+
'q' => Some(escape_name(
409+
arg_string.as_ref(),
410+
&QuotingStyle::Shell {
411+
escape: true,
412+
always_quote: false,
413+
show_control: false,
414+
},
415+
)),
408416
// get opt<char> of first val
409417
// and map it to opt<String>
410-
/* 'c' | */
411-
_ => arg_string.chars().next().map(|x| x.to_string()),
418+
'c' => arg_string.chars().next().map(|x| x.to_string()),
419+
_ => unreachable!(),
412420
}
413421
}
414422
None => None,

0 commit comments

Comments
 (0)