diff --git a/.cspell.json b/.cspell.json index ef9daa60d4..8cd83748b4 100644 --- a/.cspell.json +++ b/.cspell.json @@ -36,6 +36,7 @@ "memmap", "Manually", "rustc", + "splitn", "unistd", "unic", // Python diff --git a/common/src/cformat.rs b/common/src/cformat.rs index 2c1eda5c4a..524f93de24 100644 --- a/common/src/cformat.rs +++ b/common/src/cformat.rs @@ -46,7 +46,7 @@ impl fmt::Display for CFormatError { } } -pub type CFormatPreconversor = super::format::FormatPreconversor; +pub type CFormatConversion = super::format::FormatConversion; #[derive(Debug, PartialEq)] pub enum CFormatCase { @@ -73,7 +73,7 @@ pub enum CFormatType { Number(CNumberType), Float(CFloatType), Character, - String(CFormatPreconversor), + String(CFormatConversion), } #[derive(Debug, PartialEq)] @@ -503,10 +503,10 @@ where 'g' => CFormatType::Float(General(Lowercase)), 'G' => CFormatType::Float(General(Uppercase)), 'c' => CFormatType::Character, - 'r' => CFormatType::String(CFormatPreconversor::Repr), - 's' => CFormatType::String(CFormatPreconversor::Str), - 'b' => CFormatType::String(CFormatPreconversor::Bytes), - 'a' => CFormatType::String(CFormatPreconversor::Ascii), + 'r' => CFormatType::String(CFormatConversion::Repr), + 's' => CFormatType::String(CFormatConversion::Str), + 'b' => CFormatType::String(CFormatConversion::Bytes), + 'a' => CFormatType::String(CFormatConversion::Ascii), _ => return Err((CFormatErrorType::UnsupportedFormatChar(c), index)), }; Ok((format_type, c)) @@ -1031,7 +1031,7 @@ mod tests { ( 18, CFormatPart::Spec(CFormatSpec { - format_type: CFormatType::String(CFormatPreconversor::Str), + format_type: CFormatType::String(CFormatConversion::Str), format_char: 's', mapping_key: None, min_field_width: None, diff --git a/common/src/format.rs b/common/src/format.rs index bf3a238b01..ca994c6dbe 100644 --- a/common/src/format.rs +++ b/common/src/format.rs @@ -12,43 +12,43 @@ trait FormatParse { } #[derive(Debug, Copy, Clone, PartialEq)] -pub enum FormatPreconversor { +pub enum FormatConversion { Str, Repr, Ascii, Bytes, } -impl FormatParse for FormatPreconversor { +impl FormatParse for FormatConversion { fn parse(text: &str) -> (Option, &str) { - let Some(preconversor) = Self::from_string(text) else { + let Some(conversion) = Self::from_string(text) else { return (None, text); }; let mut chars = text.chars(); chars.next(); // Consume the bang chars.next(); // Consume one r,s,a char - (Some(preconversor), chars.as_str()) + (Some(conversion), chars.as_str()) } } -impl FormatPreconversor { - pub fn from_char(c: char) -> Option { +impl FormatConversion { + pub fn from_char(c: char) -> Option { match c { - 's' => Some(FormatPreconversor::Str), - 'r' => Some(FormatPreconversor::Repr), - 'a' => Some(FormatPreconversor::Ascii), - 'b' => Some(FormatPreconversor::Bytes), + 's' => Some(FormatConversion::Str), + 'r' => Some(FormatConversion::Repr), + 'a' => Some(FormatConversion::Ascii), + 'b' => Some(FormatConversion::Bytes), _ => None, } } - fn from_string(text: &str) -> Option { + fn from_string(text: &str) -> Option { let mut chars = text.chars(); if chars.next() != Some('!') { return None; } - FormatPreconversor::from_char(chars.next()?) + FormatConversion::from_char(chars.next()?) } } @@ -182,7 +182,7 @@ impl FormatParse for FormatType { #[derive(Debug, PartialEq)] pub struct FormatSpec { - preconversor: Option, + conversion: Option, fill: Option, align: Option, sign: Option, @@ -270,7 +270,7 @@ fn parse_precision(text: &str) -> Result<(Option, &str), FormatSpecError> impl FormatSpec { pub fn parse(text: &str) -> Result { // get_integer in CPython - let (preconversor, text) = FormatPreconversor::parse(text); + let (conversion, text) = FormatConversion::parse(text); let (mut fill, mut align, text) = parse_fill_and_align(text); let (sign, text) = FormatSign::parse(text); let (alternate_form, text) = parse_alternate_form(text); @@ -289,7 +289,7 @@ impl FormatSpec { } Ok(FormatSpec { - preconversor, + conversion, fill, align, sign, @@ -752,7 +752,7 @@ impl FieldName { pub enum FormatPart { Field { field_name: String, - preconversion_spec: Option, + conversion_spec: Option, format_spec: String, }, Literal(String), @@ -813,12 +813,12 @@ impl FormatString { String::new() }; - // On parts[0] can still be the preconversor (!r, !s, !a) + // On parts[0] can still be the conversion (!r, !s, !a) let parts: Vec<&str> = arg_part.splitn(2, '!').collect(); - // before the bang is a keyword or arg index, after the comma is maybe a conversor spec. + // before the bang is a keyword or arg index, after the comma is maybe a conversion spec. let arg_part = parts[0]; - let preconversion_spec = parts + let conversion_spec = parts .get(1) .map(|conversion| { // conversions are only every one character @@ -831,7 +831,7 @@ impl FormatString { Ok(FormatPart::Field { field_name: arg_part.to_owned(), - preconversion_spec, + conversion_spec, format_spec, }) } @@ -936,7 +936,7 @@ mod tests { #[test] fn test_width_only() { let expected = Ok(FormatSpec { - preconversor: None, + conversion: None, fill: None, align: None, sign: None, @@ -952,7 +952,7 @@ mod tests { #[test] fn test_fill_and_width() { let expected = Ok(FormatSpec { - preconversor: None, + conversion: None, fill: Some('<'), align: Some(FormatAlign::Right), sign: None, @@ -968,7 +968,7 @@ mod tests { #[test] fn test_all() { let expected = Ok(FormatSpec { - preconversor: None, + conversion: None, fill: Some('<'), align: Some(FormatAlign::Right), sign: Some(FormatSign::Minus), @@ -1034,13 +1034,13 @@ mod tests { FormatPart::Literal("abcd".to_owned()), FormatPart::Field { field_name: "1".to_owned(), - preconversion_spec: None, + conversion_spec: None, format_spec: String::new(), }, FormatPart::Literal(":".to_owned()), FormatPart::Field { field_name: "key".to_owned(), - preconversion_spec: None, + conversion_spec: None, format_spec: String::new(), }, ], @@ -1069,7 +1069,7 @@ mod tests { FormatPart::Literal("{".to_owned()), FormatPart::Field { field_name: "key".to_owned(), - preconversion_spec: None, + conversion_spec: None, format_spec: String::new(), }, FormatPart::Literal("}ddfe".to_owned()), diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 3148975a80..998ac6744e 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -21,14 +21,14 @@ fn spec_format_bytes( obj: PyObjectRef, ) -> PyResult> { match &spec.format_type { - CFormatType::String(preconversor) => match preconversor { + CFormatType::String(conversion) => match conversion { // Unlike strings, %r and %a are identical for bytes: the behaviour corresponds to // %a for strings (not %r) - CFormatPreconversor::Repr | CFormatPreconversor::Ascii => { + CFormatConversion::Repr | CFormatConversion::Ascii => { let b = builtins::ascii(obj, vm)?.into(); Ok(b) } - CFormatPreconversor::Str | CFormatPreconversor::Bytes => { + CFormatConversion::Str | CFormatConversion::Bytes => { if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, &obj) { Ok(buffer.contiguous_or_collect(|bytes| spec.format_bytes(bytes))) } else { @@ -127,12 +127,12 @@ fn spec_format_string( idx: &usize, ) -> PyResult { match &spec.format_type { - CFormatType::String(preconversor) => { - let result = match preconversor { - CFormatPreconversor::Ascii => builtins::ascii(obj, vm)?.into(), - CFormatPreconversor::Str => obj.str(vm)?.as_str().to_owned(), - CFormatPreconversor::Repr => obj.repr(vm)?.as_str().to_owned(), - CFormatPreconversor::Bytes => { + CFormatType::String(conversion) => { + let result = match conversion { + CFormatConversion::Ascii => builtins::ascii(obj, vm)?.into(), + CFormatConversion::Str => obj.str(vm)?.as_str().to_owned(), + CFormatConversion::Repr => obj.repr(vm)?.as_str().to_owned(), + CFormatConversion::Bytes => { // idx is the position of the %, we want the position of the b return Err(vm.new_value_error(format!( "unsupported format character 'b' (0x62) at index {}", diff --git a/vm/src/format.rs b/vm/src/format.rs index d13dbd9384..7b11d16dc5 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -68,7 +68,7 @@ fn format_internal( let result_string: &str = match part { FormatPart::Field { field_name, - preconversion_spec, + conversion_spec, format_spec, } => { let FieldName { field_type, parts } = @@ -94,7 +94,7 @@ fn format_internal( FormatString::from_str(format_spec).map_err(|e| e.to_pyexception(vm))?; let format_spec = format_internal(vm, &nested_format, field_func)?; - pystr = call_object_format(vm, argument, *preconversion_spec, &format_spec)?; + pystr = call_object_format(vm, argument, *conversion_spec, &format_spec)?; pystr.as_ref() } FormatPart::Literal(literal) => literal, @@ -162,14 +162,14 @@ pub(crate) fn format_map( pub fn call_object_format( vm: &VirtualMachine, argument: PyObjectRef, - preconversion_spec: Option, + conversion_spec: Option, format_spec: &str, ) -> PyResult { - let argument = match preconversion_spec.and_then(FormatPreconversor::from_char) { - Some(FormatPreconversor::Str) => argument.str(vm)?.into(), - Some(FormatPreconversor::Repr) => argument.repr(vm)?.into(), - Some(FormatPreconversor::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(), - Some(FormatPreconversor::Bytes) => { + let argument = match conversion_spec.and_then(FormatConversion::from_char) { + Some(FormatConversion::Str) => argument.str(vm)?.into(), + Some(FormatConversion::Repr) => argument.repr(vm)?.into(), + Some(FormatConversion::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(), + Some(FormatConversion::Bytes) => { vm.call_method(&argument, identifier!(vm, decode).as_str(), ())? } None => argument, diff --git a/vm/src/stdlib/string.rs b/vm/src/stdlib/string.rs index e33eca8fb1..4666d83812 100644 --- a/vm/src/stdlib/string.rs +++ b/vm/src/stdlib/string.rs @@ -21,14 +21,14 @@ mod _string { literal: String, field_name: Option, format_spec: Option, - preconversion_spec: Option, + conversion_spec: Option, vm: &VirtualMachine, ) -> PyObjectRef { let tuple = ( literal, field_name, format_spec, - preconversion_spec.map(|c| c.to_string()), + conversion_spec.map(|c| c.to_string()), ); tuple.to_pyobject(vm) } @@ -44,14 +44,14 @@ mod _string { match part { FormatPart::Field { field_name, - preconversion_spec, + conversion_spec, format_spec, } => { result.push(create_format_part( mem::take(&mut literal), Some(field_name), Some(format_spec), - preconversion_spec, + conversion_spec, vm, )); }