Skip to content

Commit

Permalink
rename preconversor -> conversion to follow cpython name
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Feb 28, 2023
1 parent a1b74b0 commit 71e1195
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 54 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"memmap",
"Manually",
"rustc",
"splitn",
"unistd",
"unic",
// Python
Expand Down
14 changes: 7 additions & 7 deletions common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -73,7 +73,7 @@ pub enum CFormatType {
Number(CNumberType),
Float(CFloatType),
Character,
String(CFormatPreconversor),
String(CFormatConversion),
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 26 additions & 26 deletions common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>, &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<FormatPreconversor> {
impl FormatConversion {
pub fn from_char(c: char) -> Option<FormatConversion> {
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<FormatPreconversor> {
fn from_string(text: &str) -> Option<FormatConversion> {
let mut chars = text.chars();
if chars.next() != Some('!') {
return None;
}

FormatPreconversor::from_char(chars.next()?)
FormatConversion::from_char(chars.next()?)
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ impl FormatParse for FormatType {

#[derive(Debug, PartialEq)]
pub struct FormatSpec {
preconversor: Option<FormatPreconversor>,
conversion: Option<FormatConversion>,
fill: Option<char>,
align: Option<FormatAlign>,
sign: Option<FormatSign>,
Expand Down Expand Up @@ -270,7 +270,7 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), FormatSpecError>
impl FormatSpec {
pub fn parse(text: &str) -> Result<Self, FormatSpecError> {
// 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);
Expand All @@ -289,7 +289,7 @@ impl FormatSpec {
}

Ok(FormatSpec {
preconversor,
conversion,
fill,
align,
sign,
Expand Down Expand Up @@ -752,7 +752,7 @@ impl FieldName {
pub enum FormatPart {
Field {
field_name: String,
preconversion_spec: Option<char>,
conversion_spec: Option<char>,
format_spec: String,
},
Literal(String),
Expand Down Expand Up @@ -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
Expand All @@ -831,7 +831,7 @@ impl FormatString {

Ok(FormatPart::Field {
field_name: arg_part.to_owned(),
preconversion_spec,
conversion_spec,
format_spec,
})
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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(),
},
],
Expand Down Expand Up @@ -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()),
Expand Down
18 changes: 9 additions & 9 deletions vm/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ fn spec_format_bytes(
obj: PyObjectRef,
) -> PyResult<Vec<u8>> {
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 {
Expand Down Expand Up @@ -127,12 +127,12 @@ fn spec_format_string(
idx: &usize,
) -> PyResult<String> {
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 {}",
Expand Down
16 changes: 8 additions & 8 deletions vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } =
Expand All @@ -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,
Expand Down Expand Up @@ -162,14 +162,14 @@ pub(crate) fn format_map(
pub fn call_object_format(
vm: &VirtualMachine,
argument: PyObjectRef,
preconversion_spec: Option<char>,
conversion_spec: Option<char>,
format_spec: &str,
) -> PyResult<PyStrRef> {
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,
Expand Down
8 changes: 4 additions & 4 deletions vm/src/stdlib/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ mod _string {
literal: String,
field_name: Option<String>,
format_spec: Option<String>,
preconversion_spec: Option<char>,
conversion_spec: Option<char>,
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)
}
Expand All @@ -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,
));
}
Expand Down

0 comments on commit 71e1195

Please sign in to comment.