Repository navigation
Allow try_update_quantity_from_tuple to set left alignment when needed #4766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
84d91ab
88bb322
653cf2e
c3881dd
b7bb7a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -204,12 +204,18 @@ fn spec_format_string( | |||||||
| fn try_update_quantity_from_element( | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now quantity and precision has different spec, but they are handled by same function. my suggestion is:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your feedback! Yeah, I think that would help ease the potential confusion. |
||||||||
| vm: &VirtualMachine, | ||||||||
| element: Option<&PyObjectRef>, | ||||||||
| ) -> PyResult<CFormatQuantity> { | ||||||||
| ) -> PyResult<(CFormatQuantity, CFormatAlign)> { | ||||||||
| match element { | ||||||||
| Some(width_obj) => { | ||||||||
| if let Some(i) = width_obj.payload::<PyInt>() { | ||||||||
| let i = i.try_to_primitive::<i32>(vm)?.unsigned_abs(); | ||||||||
| Ok(CFormatQuantity::Amount(i as usize)) | ||||||||
| let i = i.try_to_primitive::<i32>(vm)?; | ||||||||
| let align = if i < 0 { | ||||||||
| CFormatAlign::Left | ||||||||
| } else { | ||||||||
| CFormatAlign::Right | ||||||||
| }; | ||||||||
| let i = i.unsigned_abs(); | ||||||||
| Ok((CFormatQuantity::Amount(i as usize), align)) | ||||||||
| } else { | ||||||||
| Err(vm.new_type_error("* wants int".to_owned())) | ||||||||
| } | ||||||||
|
|
@@ -222,12 +228,16 @@ fn try_update_quantity_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>( | |||||||
| vm: &VirtualMachine, | ||||||||
| elements: &mut I, | ||||||||
| q: &mut Option<CFormatQuantity>, | ||||||||
| f: &mut CConversionFlags, | ||||||||
| ) -> PyResult<()> { | ||||||||
| let Some(CFormatQuantity::FromValuesTuple) = q else { | ||||||||
| return Ok(()); | ||||||||
| }; | ||||||||
| let quantity = try_update_quantity_from_element(vm, elements.next())?; | ||||||||
| let (quantity, align) = try_update_quantity_from_element(vm, elements.next())?; | ||||||||
| *q = Some(quantity); | ||||||||
| if let CFormatAlign::Left = align { | ||||||||
| f.insert(CConversionFlags::LEFT_ADJUST); | ||||||||
| } | ||||||||
| Ok(()) | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -239,7 +249,7 @@ fn try_update_precision_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>( | |||||||
| let Some(CFormatPrecision::Quantity(CFormatQuantity::FromValuesTuple)) = p else { | ||||||||
| return Ok(()); | ||||||||
| }; | ||||||||
| let quantity = try_update_quantity_from_element(vm, elements.next())?; | ||||||||
| let (quantity, _) = try_update_quantity_from_element(vm, elements.next())?; | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this also lends to the fact that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a fair point. I think it could be possible to add a new, separate function that only checks the sign of the given element and set the flag directly, without messing with return types. I did what I did yesterday mainly because I originally did not know that I could just set left adjustment to spec flags directly, so I wanted to have some means of storing the alignment data to later reference when I'm actually formatting the string. However, I think I forgot to adjust later when I discovered what I can do with CConversionFlags. Let me work on the fix real quick. |
||||||||
| *p = Some(CFormatPrecision::Quantity(quantity)); | ||||||||
| Ok(()) | ||||||||
| } | ||||||||
|
|
@@ -322,7 +332,12 @@ pub(crate) fn cformat_bytes( | |||||||
| match part { | ||||||||
| CFormatPart::Literal(literal) => result.append(literal), | ||||||||
| CFormatPart::Spec(spec) => { | ||||||||
| try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.min_field_width)?; | ||||||||
| try_update_quantity_from_tuple( | ||||||||
| vm, | ||||||||
| &mut value_iter, | ||||||||
| &mut spec.min_field_width, | ||||||||
| &mut spec.flags, | ||||||||
| )?; | ||||||||
| try_update_precision_from_tuple(vm, &mut value_iter, &mut spec.precision)?; | ||||||||
|
|
||||||||
| let value = match value_iter.next() { | ||||||||
|
|
@@ -416,7 +431,12 @@ pub(crate) fn cformat_string( | |||||||
| match part { | ||||||||
| CFormatPart::Literal(literal) => result.push_str(literal), | ||||||||
| CFormatPart::Spec(spec) => { | ||||||||
| try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.min_field_width)?; | ||||||||
| try_update_quantity_from_tuple( | ||||||||
| vm, | ||||||||
| &mut value_iter, | ||||||||
| &mut spec.min_field_width, | ||||||||
| &mut spec.flags, | ||||||||
| )?; | ||||||||
| try_update_precision_from_tuple(vm, &mut value_iter, &mut spec.precision)?; | ||||||||
|
|
||||||||
| let value = match value_iter.next() { | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
along with YunWon's suggestion, I'd document this. We currently don't do this as much as we'd probably like but it's relatively straight-forward here.