11//! Implementation of Printf-Style string formatting
22//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
3- use crate :: float_ops;
43use bitflags:: bitflags;
54use num_bigint:: { BigInt , Sign } ;
65use num_traits:: Signed ;
6+ use rustpython_literal:: { float, format:: Case } ;
77use std:: {
88 cmp, fmt,
99 iter:: { Enumerate , Peekable } ,
@@ -48,24 +48,18 @@ impl fmt::Display for CFormatError {
4848
4949pub type CFormatConversion = super :: format:: FormatConversion ;
5050
51- #[ derive( Debug , PartialEq ) ]
52- pub enum CFormatCase {
53- Lowercase ,
54- Uppercase ,
55- }
56-
5751#[ derive( Debug , PartialEq ) ]
5852pub enum CNumberType {
5953 Decimal ,
6054 Octal ,
61- Hex ( CFormatCase ) ,
55+ Hex ( Case ) ,
6256}
6357
6458#[ derive( Debug , PartialEq ) ]
6559pub enum CFloatType {
66- Exponent ( CFormatCase ) ,
67- PointDecimal ( CFormatCase ) ,
68- General ( CFormatCase ) ,
60+ Exponent ( Case ) ,
61+ PointDecimal ( Case ) ,
62+ General ( Case ) ,
6963}
7064
7165#[ derive( Debug , PartialEq ) ]
@@ -283,14 +277,13 @@ impl CFormatSpec {
283277 }
284278
285279 pub fn format_number ( & self , num : & BigInt ) -> String {
286- use CFormatCase :: { Lowercase , Uppercase } ;
287280 use CNumberType :: * ;
288281 let magnitude = num. abs ( ) ;
289282 let prefix = if self . flags . contains ( CConversionFlags :: ALTERNATE_FORM ) {
290283 match self . format_type {
291284 CFormatType :: Number ( Octal ) => "0o" ,
292- CFormatType :: Number ( Hex ( Lowercase ) ) => "0x" ,
293- CFormatType :: Number ( Hex ( Uppercase ) ) => "0X" ,
285+ CFormatType :: Number ( Hex ( Case :: Lower ) ) => "0x" ,
286+ CFormatType :: Number ( Hex ( Case :: Upper ) ) => "0X" ,
294287 _ => "" ,
295288 }
296289 } else {
@@ -300,8 +293,8 @@ impl CFormatSpec {
300293 let magnitude_string: String = match self . format_type {
301294 CFormatType :: Number ( Decimal ) => magnitude. to_str_radix ( 10 ) ,
302295 CFormatType :: Number ( Octal ) => magnitude. to_str_radix ( 8 ) ,
303- CFormatType :: Number ( Hex ( Lowercase ) ) => magnitude. to_str_radix ( 16 ) ,
304- CFormatType :: Number ( Hex ( Uppercase ) ) => {
296+ CFormatType :: Number ( Hex ( Case :: Lower ) ) => magnitude. to_str_radix ( 16 ) ,
297+ CFormatType :: Number ( Hex ( Case :: Upper ) ) => {
305298 let mut result = magnitude. to_str_radix ( 16 ) ;
306299 result. make_ascii_uppercase ( ) ;
307300 result
@@ -359,42 +352,30 @@ impl CFormatSpec {
359352
360353 let magnitude_string = match & self . format_type {
361354 CFormatType :: Float ( CFloatType :: PointDecimal ( case) ) => {
362- let case = match case {
363- CFormatCase :: Lowercase => float_ops:: Case :: Lower ,
364- CFormatCase :: Uppercase => float_ops:: Case :: Upper ,
365- } ;
366355 let magnitude = num. abs ( ) ;
367- float_ops :: format_fixed (
356+ float :: format_fixed (
368357 precision,
369358 magnitude,
370- case,
359+ * case,
371360 self . flags . contains ( CConversionFlags :: ALTERNATE_FORM ) ,
372361 )
373362 }
374363 CFormatType :: Float ( CFloatType :: Exponent ( case) ) => {
375- let case = match case {
376- CFormatCase :: Lowercase => float_ops:: Case :: Lower ,
377- CFormatCase :: Uppercase => float_ops:: Case :: Upper ,
378- } ;
379364 let magnitude = num. abs ( ) ;
380- float_ops :: format_exponent (
365+ float :: format_exponent (
381366 precision,
382367 magnitude,
383- case,
368+ * case,
384369 self . flags . contains ( CConversionFlags :: ALTERNATE_FORM ) ,
385370 )
386371 }
387372 CFormatType :: Float ( CFloatType :: General ( case) ) => {
388373 let precision = if precision == 0 { 1 } else { precision } ;
389- let case = match case {
390- CFormatCase :: Lowercase => float_ops:: Case :: Lower ,
391- CFormatCase :: Uppercase => float_ops:: Case :: Upper ,
392- } ;
393374 let magnitude = num. abs ( ) ;
394- float_ops :: format_general (
375+ float :: format_general (
395376 precision,
396377 magnitude,
397- case,
378+ * case,
398379 self . flags . contains ( CConversionFlags :: ALTERNATE_FORM ) ,
399380 false ,
400381 )
@@ -480,7 +461,6 @@ where
480461 I : Iterator < Item = T > ,
481462{
482463 use CFloatType :: * ;
483- use CFormatCase :: { Lowercase , Uppercase } ;
484464 use CNumberType :: * ;
485465 let ( index, c) = match iter. next ( ) {
486466 Some ( ( index, c) ) => ( index, c. into ( ) ) ,
@@ -494,14 +474,14 @@ where
494474 let format_type = match c {
495475 'd' | 'i' | 'u' => CFormatType :: Number ( Decimal ) ,
496476 'o' => CFormatType :: Number ( Octal ) ,
497- 'x' => CFormatType :: Number ( Hex ( Lowercase ) ) ,
498- 'X' => CFormatType :: Number ( Hex ( Uppercase ) ) ,
499- 'e' => CFormatType :: Float ( Exponent ( Lowercase ) ) ,
500- 'E' => CFormatType :: Float ( Exponent ( Uppercase ) ) ,
501- 'f' => CFormatType :: Float ( PointDecimal ( Lowercase ) ) ,
502- 'F' => CFormatType :: Float ( PointDecimal ( Uppercase ) ) ,
503- 'g' => CFormatType :: Float ( General ( Lowercase ) ) ,
504- 'G' => CFormatType :: Float ( General ( Uppercase ) ) ,
477+ 'x' => CFormatType :: Number ( Hex ( Case :: Lower ) ) ,
478+ 'X' => CFormatType :: Number ( Hex ( Case :: Upper ) ) ,
479+ 'e' => CFormatType :: Float ( Exponent ( Case :: Lower ) ) ,
480+ 'E' => CFormatType :: Float ( Exponent ( Case :: Upper ) ) ,
481+ 'f' => CFormatType :: Float ( PointDecimal ( Case :: Lower ) ) ,
482+ 'F' => CFormatType :: Float ( PointDecimal ( Case :: Upper ) ) ,
483+ 'g' => CFormatType :: Float ( General ( Case :: Lower ) ) ,
484+ 'G' => CFormatType :: Float ( General ( Case :: Upper ) ) ,
505485 'c' => CFormatType :: Character ,
506486 'r' => CFormatType :: String ( CFormatConversion :: Repr ) ,
507487 's' => CFormatType :: String ( CFormatConversion :: Str ) ,
0 commit comments