Skip to content

Commit 5f7fffb

Browse files
authored
Merge pull request RustPython#4596 from youknowone/format-clean
clean up common/src/format.rs
2 parents f251540 + 71e1195 commit 5f7fffb

File tree

6 files changed

+58
-57
lines changed

6 files changed

+58
-57
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"memmap",
3737
"Manually",
3838
"rustc",
39+
"splitn",
3940
"unistd",
4041
"unic",
4142
// Python

common/src/cformat.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl fmt::Display for CFormatError {
4646
}
4747
}
4848

49-
pub type CFormatPreconversor = super::format::FormatPreconversor;
49+
pub type CFormatConversion = super::format::FormatConversion;
5050

5151
#[derive(Debug, PartialEq)]
5252
pub enum CFormatCase {
@@ -73,7 +73,7 @@ pub enum CFormatType {
7373
Number(CNumberType),
7474
Float(CFloatType),
7575
Character,
76-
String(CFormatPreconversor),
76+
String(CFormatConversion),
7777
}
7878

7979
#[derive(Debug, PartialEq)]
@@ -503,10 +503,10 @@ where
503503
'g' => CFormatType::Float(General(Lowercase)),
504504
'G' => CFormatType::Float(General(Uppercase)),
505505
'c' => CFormatType::Character,
506-
'r' => CFormatType::String(CFormatPreconversor::Repr),
507-
's' => CFormatType::String(CFormatPreconversor::Str),
508-
'b' => CFormatType::String(CFormatPreconversor::Bytes),
509-
'a' => CFormatType::String(CFormatPreconversor::Ascii),
506+
'r' => CFormatType::String(CFormatConversion::Repr),
507+
's' => CFormatType::String(CFormatConversion::Str),
508+
'b' => CFormatType::String(CFormatConversion::Bytes),
509+
'a' => CFormatType::String(CFormatConversion::Ascii),
510510
_ => return Err((CFormatErrorType::UnsupportedFormatChar(c), index)),
511511
};
512512
Ok((format_type, c))
@@ -1031,7 +1031,7 @@ mod tests {
10311031
(
10321032
18,
10331033
CFormatPart::Spec(CFormatSpec {
1034-
format_type: CFormatType::String(CFormatPreconversor::Str),
1034+
format_type: CFormatType::String(CFormatConversion::Str),
10351035
format_char: 's',
10361036
mapping_key: None,
10371037
min_field_width: None,

common/src/format.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,43 @@ trait FormatParse {
1212
}
1313

1414
#[derive(Debug, Copy, Clone, PartialEq)]
15-
pub enum FormatPreconversor {
15+
pub enum FormatConversion {
1616
Str,
1717
Repr,
1818
Ascii,
1919
Bytes,
2020
}
2121

22-
impl FormatParse for FormatPreconversor {
22+
impl FormatParse for FormatConversion {
2323
fn parse(text: &str) -> (Option<Self>, &str) {
24-
let Some(preconversor) = Self::from_string(text) else {
24+
let Some(conversion) = Self::from_string(text) else {
2525
return (None, text);
2626
};
2727
let mut chars = text.chars();
2828
chars.next(); // Consume the bang
2929
chars.next(); // Consume one r,s,a char
30-
(Some(preconversor), chars.as_str())
30+
(Some(conversion), chars.as_str())
3131
}
3232
}
3333

34-
impl FormatPreconversor {
35-
pub fn from_char(c: char) -> Option<FormatPreconversor> {
34+
impl FormatConversion {
35+
pub fn from_char(c: char) -> Option<FormatConversion> {
3636
match c {
37-
's' => Some(FormatPreconversor::Str),
38-
'r' => Some(FormatPreconversor::Repr),
39-
'a' => Some(FormatPreconversor::Ascii),
40-
'b' => Some(FormatPreconversor::Bytes),
37+
's' => Some(FormatConversion::Str),
38+
'r' => Some(FormatConversion::Repr),
39+
'a' => Some(FormatConversion::Ascii),
40+
'b' => Some(FormatConversion::Bytes),
4141
_ => None,
4242
}
4343
}
4444

45-
fn from_string(text: &str) -> Option<FormatPreconversor> {
45+
fn from_string(text: &str) -> Option<FormatConversion> {
4646
let mut chars = text.chars();
4747
if chars.next() != Some('!') {
4848
return None;
4949
}
5050

51-
FormatPreconversor::from_char(chars.next()?)
51+
FormatConversion::from_char(chars.next()?)
5252
}
5353
}
5454

@@ -182,7 +182,7 @@ impl FormatParse for FormatType {
182182

183183
#[derive(Debug, PartialEq)]
184184
pub struct FormatSpec {
185-
preconversor: Option<FormatPreconversor>,
185+
conversion: Option<FormatConversion>,
186186
fill: Option<char>,
187187
align: Option<FormatAlign>,
188188
sign: Option<FormatSign>,
@@ -270,7 +270,7 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), FormatSpecError>
270270
impl FormatSpec {
271271
pub fn parse(text: &str) -> Result<Self, FormatSpecError> {
272272
// get_integer in CPython
273-
let (preconversor, text) = FormatPreconversor::parse(text);
273+
let (conversion, text) = FormatConversion::parse(text);
274274
let (mut fill, mut align, text) = parse_fill_and_align(text);
275275
let (sign, text) = FormatSign::parse(text);
276276
let (alternate_form, text) = parse_alternate_form(text);
@@ -289,7 +289,7 @@ impl FormatSpec {
289289
}
290290

291291
Ok(FormatSpec {
292-
preconversor,
292+
conversion,
293293
fill,
294294
align,
295295
sign,
@@ -517,7 +517,7 @@ impl FormatSpec {
517517
} else {
518518
""
519519
};
520-
let raw_magnitude_str: Result<String, FormatSpecError> = match self.format_type {
520+
let raw_magnitude_str = match self.format_type {
521521
Some(FormatType::Binary) => self.format_int_radix(magnitude, 2),
522522
Some(FormatType::Decimal) => self.format_int_radix(magnitude, 10),
523523
Some(FormatType::Octal) => self.format_int_radix(magnitude, 8),
@@ -548,7 +548,7 @@ impl FormatSpec {
548548
_ => Err(FormatSpecError::UnableToConvert),
549549
},
550550
None => self.format_int_radix(magnitude, 10),
551-
};
551+
}?;
552552
let format_sign = self.sign.unwrap_or(FormatSign::Minus);
553553
let sign_str = match num.sign() {
554554
Sign::Minus => "-",
@@ -559,7 +559,7 @@ impl FormatSpec {
559559
},
560560
};
561561
let sign_prefix = format!("{sign_str}{prefix}");
562-
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, &sign_prefix);
562+
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str, &sign_prefix);
563563
self.format_sign_and_align(
564564
&BorrowedStr::from_bytes(magnitude_str.as_bytes()),
565565
&sign_prefix,
@@ -752,7 +752,7 @@ impl FieldName {
752752
pub enum FormatPart {
753753
Field {
754754
field_name: String,
755-
preconversion_spec: Option<char>,
755+
conversion_spec: Option<char>,
756756
format_spec: String,
757757
},
758758
Literal(String),
@@ -813,12 +813,12 @@ impl FormatString {
813813
String::new()
814814
};
815815

816-
// On parts[0] can still be the preconversor (!r, !s, !a)
816+
// On parts[0] can still be the conversion (!r, !s, !a)
817817
let parts: Vec<&str> = arg_part.splitn(2, '!').collect();
818-
// before the bang is a keyword or arg index, after the comma is maybe a conversor spec.
818+
// before the bang is a keyword or arg index, after the comma is maybe a conversion spec.
819819
let arg_part = parts[0];
820820

821-
let preconversion_spec = parts
821+
let conversion_spec = parts
822822
.get(1)
823823
.map(|conversion| {
824824
// conversions are only every one character
@@ -831,7 +831,7 @@ impl FormatString {
831831

832832
Ok(FormatPart::Field {
833833
field_name: arg_part.to_owned(),
834-
preconversion_spec,
834+
conversion_spec,
835835
format_spec,
836836
})
837837
}
@@ -936,7 +936,7 @@ mod tests {
936936
#[test]
937937
fn test_width_only() {
938938
let expected = Ok(FormatSpec {
939-
preconversor: None,
939+
conversion: None,
940940
fill: None,
941941
align: None,
942942
sign: None,
@@ -952,7 +952,7 @@ mod tests {
952952
#[test]
953953
fn test_fill_and_width() {
954954
let expected = Ok(FormatSpec {
955-
preconversor: None,
955+
conversion: None,
956956
fill: Some('<'),
957957
align: Some(FormatAlign::Right),
958958
sign: None,
@@ -968,7 +968,7 @@ mod tests {
968968
#[test]
969969
fn test_all() {
970970
let expected = Ok(FormatSpec {
971-
preconversor: None,
971+
conversion: None,
972972
fill: Some('<'),
973973
align: Some(FormatAlign::Right),
974974
sign: Some(FormatSign::Minus),
@@ -1034,13 +1034,13 @@ mod tests {
10341034
FormatPart::Literal("abcd".to_owned()),
10351035
FormatPart::Field {
10361036
field_name: "1".to_owned(),
1037-
preconversion_spec: None,
1037+
conversion_spec: None,
10381038
format_spec: String::new(),
10391039
},
10401040
FormatPart::Literal(":".to_owned()),
10411041
FormatPart::Field {
10421042
field_name: "key".to_owned(),
1043-
preconversion_spec: None,
1043+
conversion_spec: None,
10441044
format_spec: String::new(),
10451045
},
10461046
],
@@ -1069,7 +1069,7 @@ mod tests {
10691069
FormatPart::Literal("{".to_owned()),
10701070
FormatPart::Field {
10711071
field_name: "key".to_owned(),
1072-
preconversion_spec: None,
1072+
conversion_spec: None,
10731073
format_spec: String::new(),
10741074
},
10751075
FormatPart::Literal("}ddfe".to_owned()),

vm/src/cformat.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ fn spec_format_bytes(
2121
obj: PyObjectRef,
2222
) -> PyResult<Vec<u8>> {
2323
match &spec.format_type {
24-
CFormatType::String(preconversor) => match preconversor {
24+
CFormatType::String(conversion) => match conversion {
2525
// Unlike strings, %r and %a are identical for bytes: the behaviour corresponds to
2626
// %a for strings (not %r)
27-
CFormatPreconversor::Repr | CFormatPreconversor::Ascii => {
27+
CFormatConversion::Repr | CFormatConversion::Ascii => {
2828
let b = builtins::ascii(obj, vm)?.into();
2929
Ok(b)
3030
}
31-
CFormatPreconversor::Str | CFormatPreconversor::Bytes => {
31+
CFormatConversion::Str | CFormatConversion::Bytes => {
3232
if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, &obj) {
3333
Ok(buffer.contiguous_or_collect(|bytes| spec.format_bytes(bytes)))
3434
} else {
@@ -127,12 +127,12 @@ fn spec_format_string(
127127
idx: &usize,
128128
) -> PyResult<String> {
129129
match &spec.format_type {
130-
CFormatType::String(preconversor) => {
131-
let result = match preconversor {
132-
CFormatPreconversor::Ascii => builtins::ascii(obj, vm)?.into(),
133-
CFormatPreconversor::Str => obj.str(vm)?.as_str().to_owned(),
134-
CFormatPreconversor::Repr => obj.repr(vm)?.as_str().to_owned(),
135-
CFormatPreconversor::Bytes => {
130+
CFormatType::String(conversion) => {
131+
let result = match conversion {
132+
CFormatConversion::Ascii => builtins::ascii(obj, vm)?.into(),
133+
CFormatConversion::Str => obj.str(vm)?.as_str().to_owned(),
134+
CFormatConversion::Repr => obj.repr(vm)?.as_str().to_owned(),
135+
CFormatConversion::Bytes => {
136136
// idx is the position of the %, we want the position of the b
137137
return Err(vm.new_value_error(format!(
138138
"unsupported format character 'b' (0x62) at index {}",

vm/src/format.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn format_internal(
6868
let result_string: &str = match part {
6969
FormatPart::Field {
7070
field_name,
71-
preconversion_spec,
71+
conversion_spec,
7272
format_spec,
7373
} => {
7474
let FieldName { field_type, parts } =
@@ -94,7 +94,7 @@ fn format_internal(
9494
FormatString::from_str(format_spec).map_err(|e| e.to_pyexception(vm))?;
9595
let format_spec = format_internal(vm, &nested_format, field_func)?;
9696

97-
pystr = call_object_format(vm, argument, *preconversion_spec, &format_spec)?;
97+
pystr = call_object_format(vm, argument, *conversion_spec, &format_spec)?;
9898
pystr.as_ref()
9999
}
100100
FormatPart::Literal(literal) => literal,
@@ -162,14 +162,14 @@ pub(crate) fn format_map(
162162
pub fn call_object_format(
163163
vm: &VirtualMachine,
164164
argument: PyObjectRef,
165-
preconversion_spec: Option<char>,
165+
conversion_spec: Option<char>,
166166
format_spec: &str,
167167
) -> PyResult<PyStrRef> {
168-
let argument = match preconversion_spec.and_then(FormatPreconversor::from_char) {
169-
Some(FormatPreconversor::Str) => argument.str(vm)?.into(),
170-
Some(FormatPreconversor::Repr) => argument.repr(vm)?.into(),
171-
Some(FormatPreconversor::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(),
172-
Some(FormatPreconversor::Bytes) => {
168+
let argument = match conversion_spec.and_then(FormatConversion::from_char) {
169+
Some(FormatConversion::Str) => argument.str(vm)?.into(),
170+
Some(FormatConversion::Repr) => argument.repr(vm)?.into(),
171+
Some(FormatConversion::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(),
172+
Some(FormatConversion::Bytes) => {
173173
vm.call_method(&argument, identifier!(vm, decode).as_str(), ())?
174174
}
175175
None => argument,

vm/src/stdlib/string.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ mod _string {
2121
literal: String,
2222
field_name: Option<String>,
2323
format_spec: Option<String>,
24-
preconversion_spec: Option<char>,
24+
conversion_spec: Option<char>,
2525
vm: &VirtualMachine,
2626
) -> PyObjectRef {
2727
let tuple = (
2828
literal,
2929
field_name,
3030
format_spec,
31-
preconversion_spec.map(|c| c.to_string()),
31+
conversion_spec.map(|c| c.to_string()),
3232
);
3333
tuple.to_pyobject(vm)
3434
}
@@ -44,14 +44,14 @@ mod _string {
4444
match part {
4545
FormatPart::Field {
4646
field_name,
47-
preconversion_spec,
47+
conversion_spec,
4848
format_spec,
4949
} => {
5050
result.push(create_format_part(
5151
mem::take(&mut literal),
5252
Some(field_name),
5353
Some(format_spec),
54-
preconversion_spec,
54+
conversion_spec,
5555
vm,
5656
));
5757
}

0 commit comments

Comments
 (0)