22//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
33
44use crate :: common:: cformat:: * ;
5+ use crate :: common:: wtf8:: { CodePoint , Wtf8 , Wtf8Buf } ;
56use crate :: {
67 AsObject , PyObjectRef , PyResult , TryFromBorrowedObject , TryFromObject , VirtualMachine ,
78 builtins:: {
@@ -125,13 +126,13 @@ fn spec_format_string(
125126 spec : & CFormatSpec ,
126127 obj : PyObjectRef ,
127128 idx : usize ,
128- ) -> PyResult < String > {
129+ ) -> PyResult < Wtf8Buf > {
129130 match & spec. format_type {
130131 CFormatType :: String ( conversion) => {
131132 let result = match conversion {
132133 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 ( ) ,
134+ CFormatConversion :: Str => obj. str ( vm) ?. as_wtf8 ( ) . to_owned ( ) ,
135+ CFormatConversion :: Repr => obj. repr ( vm) ?. as_wtf8 ( ) . to_owned ( ) ,
135136 CFormatConversion :: Bytes => {
136137 // idx is the position of the %, we want the position of the b
137138 return Err ( vm. new_value_error ( format ! (
@@ -146,16 +147,18 @@ fn spec_format_string(
146147 CNumberType :: DecimalD | CNumberType :: DecimalI | CNumberType :: DecimalU => {
147148 match_class ! ( match & obj {
148149 ref i @ PyInt => {
149- Ok ( spec. format_number( i. as_bigint( ) ) )
150+ Ok ( spec. format_number( i. as_bigint( ) ) . into ( ) )
150151 }
151152 ref f @ PyFloat => {
152- Ok ( spec. format_number( & try_f64_to_bigint( f. to_f64( ) , vm) ?) )
153+ Ok ( spec
154+ . format_number( & try_f64_to_bigint( f. to_f64( ) , vm) ?)
155+ . into( ) )
153156 }
154157 obj => {
155158 if let Some ( method) = vm. get_method( obj. clone( ) , identifier!( vm, __int__) ) {
156159 let result = method?. call( ( ) , vm) ?;
157160 if let Some ( i) = result. payload:: <PyInt >( ) {
158- return Ok ( spec. format_number( i. as_bigint( ) ) ) ;
161+ return Ok ( spec. format_number( i. as_bigint( ) ) . into ( ) ) ;
159162 }
160163 }
161164 Err ( vm. new_type_error( format!(
@@ -168,7 +171,7 @@ fn spec_format_string(
168171 }
169172 _ => {
170173 if let Some ( i) = obj. payload :: < PyInt > ( ) {
171- Ok ( spec. format_number ( i. as_bigint ( ) ) )
174+ Ok ( spec. format_number ( i. as_bigint ( ) ) . into ( ) )
172175 } else {
173176 Err ( vm. new_type_error ( format ! (
174177 "%{} format: an integer is required, not {}" ,
@@ -180,21 +183,21 @@ fn spec_format_string(
180183 } ,
181184 CFormatType :: Float ( _) => {
182185 let value = ArgIntoFloat :: try_from_object ( vm, obj) ?;
183- Ok ( spec. format_float ( value. into ( ) ) )
186+ Ok ( spec. format_float ( value. into ( ) ) . into ( ) )
184187 }
185188 CFormatType :: Character ( CCharacterType :: Character ) => {
186189 if let Some ( i) = obj. payload :: < PyInt > ( ) {
187190 let ch = i
188191 . as_bigint ( )
189192 . to_u32 ( )
190- . and_then ( char :: from_u32)
193+ . and_then ( CodePoint :: from_u32)
191194 . ok_or_else ( || {
192195 vm. new_overflow_error ( "%c arg not in range(0x110000)" . to_owned ( ) )
193196 } ) ?;
194197 return Ok ( spec. format_char ( ch) ) ;
195198 }
196199 if let Some ( s) = obj. payload :: < PyStr > ( ) {
197- if let Ok ( ch) = s. as_str ( ) . chars ( ) . exactly_one ( ) {
200+ if let Ok ( ch) = s. as_wtf8 ( ) . code_points ( ) . exactly_one ( ) {
198201 return Ok ( spec. format_char ( ch) ) ;
199202 }
200203 }
@@ -374,17 +377,16 @@ pub(crate) fn cformat_bytes(
374377
375378pub ( crate ) fn cformat_string (
376379 vm : & VirtualMachine ,
377- format_string : & str ,
380+ format_string : & Wtf8 ,
378381 values_obj : PyObjectRef ,
379- ) -> PyResult < String > {
380- let format = format_string
381- . parse :: < CFormatString > ( )
382+ ) -> PyResult < Wtf8Buf > {
383+ let format = CFormatWtf8 :: parse_from_wtf8 ( format_string)
382384 . map_err ( |err| vm. new_value_error ( err. to_string ( ) ) ) ?;
383385 let ( num_specifiers, mapping_required) = format
384386 . check_specifiers ( )
385387 . ok_or_else ( || specifier_error ( vm) ) ?;
386388
387- let mut result = String :: new ( ) ;
389+ let mut result = Wtf8Buf :: new ( ) ;
388390
389391 let is_mapping = values_obj. class ( ) . has_attr ( identifier ! ( vm, __getitem__) )
390392 && !values_obj. fast_isinstance ( vm. ctx . types . tuple_type )
@@ -399,7 +401,7 @@ pub(crate) fn cformat_string(
399401 {
400402 for ( _, part) in format. iter ( ) {
401403 match part {
402- CFormatPart :: Literal ( literal) => result. push_str ( literal) ,
404+ CFormatPart :: Literal ( literal) => result. push_wtf8 ( literal) ,
403405 CFormatPart :: Spec ( _) => unreachable ! ( ) ,
404406 }
405407 }
@@ -415,11 +417,11 @@ pub(crate) fn cformat_string(
415417 return if is_mapping {
416418 for ( idx, part) in format {
417419 match part {
418- CFormatPart :: Literal ( literal) => result. push_str ( & literal) ,
420+ CFormatPart :: Literal ( literal) => result. push_wtf8 ( & literal) ,
419421 CFormatPart :: Spec ( CFormatSpecKeyed { mapping_key, spec } ) => {
420422 let value = values_obj. get_item ( & mapping_key. unwrap ( ) , vm) ?;
421423 let part_result = spec_format_string ( vm, & spec, value, idx) ?;
422- result. push_str ( & part_result) ;
424+ result. push_wtf8 ( & part_result) ;
423425 }
424426 }
425427 }
@@ -439,7 +441,7 @@ pub(crate) fn cformat_string(
439441
440442 for ( idx, part) in format {
441443 match part {
442- CFormatPart :: Literal ( literal) => result. push_str ( & literal) ,
444+ CFormatPart :: Literal ( literal) => result. push_wtf8 ( & literal) ,
443445 CFormatPart :: Spec ( CFormatSpecKeyed { mut spec, .. } ) => {
444446 try_update_quantity_from_tuple (
445447 vm,
@@ -456,7 +458,7 @@ pub(crate) fn cformat_string(
456458 }
457459 } ?;
458460 let part_result = spec_format_string ( vm, & spec, value, idx) ?;
459- result. push_str ( & part_result) ;
461+ result. push_wtf8 ( & part_result) ;
460462 }
461463 }
462464 }
0 commit comments