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 bitflags:: bitflags;
3+ use bitflags:: { bitflags, bitflags_match } ;
44use itertools:: Itertools ;
55use malachite_bigint:: { BigInt , Sign } ;
66use num_traits:: Signed ;
@@ -137,13 +137,12 @@ bitflags! {
137137impl CConversionFlags {
138138 #[ inline]
139139 pub fn sign_string ( & self ) -> & ' static str {
140- if self . contains ( CConversionFlags :: SIGN_CHAR ) {
141- "+"
142- } else if self . contains ( CConversionFlags :: BLANK_SIGN ) {
143- " "
144- } else {
145- ""
140+ bitflags_match ! ( * self , {
141+ Self :: SIGN_CHAR => "+" ,
142+ Self :: BLANK_SIGN => " " ,
143+ _ => "" ,
146144 }
145+ )
147146 }
148147}
149148
@@ -172,12 +171,15 @@ pub trait FormatChar: Copy + Into<CodePoint> + From<u8> {
172171
173172impl FormatBuf for String {
174173 type Char = char ;
174+
175175 fn chars ( & self ) -> impl Iterator < Item = Self :: Char > {
176176 ( * * self ) . chars ( )
177177 }
178+
178179 fn len ( & self ) -> usize {
179180 self . len ( )
180181 }
182+
181183 fn concat ( mut self , other : Self ) -> Self {
182184 self . extend ( [ other] ) ;
183185 self
@@ -188,19 +190,23 @@ impl FormatChar for char {
188190 fn to_char_lossy ( self ) -> char {
189191 self
190192 }
193+
191194 fn eq_char ( self , c : char ) -> bool {
192195 self == c
193196 }
194197}
195198
196199impl FormatBuf for Wtf8Buf {
197200 type Char = CodePoint ;
201+
198202 fn chars ( & self ) -> impl Iterator < Item = Self :: Char > {
199203 self . code_points ( )
200204 }
205+
201206 fn len ( & self ) -> usize {
202207 ( * * self ) . len ( )
203208 }
209+
204210 fn concat ( mut self , other : Self ) -> Self {
205211 self . extend ( [ other] ) ;
206212 self
@@ -211,19 +217,23 @@ impl FormatChar for CodePoint {
211217 fn to_char_lossy ( self ) -> char {
212218 self . to_char_lossy ( )
213219 }
220+
214221 fn eq_char ( self , c : char ) -> bool {
215222 self == c
216223 }
217224}
218225
219226impl FormatBuf for Vec < u8 > {
220227 type Char = u8 ;
228+
221229 fn chars ( & self ) -> impl Iterator < Item = Self :: Char > {
222230 self . iter ( ) . copied ( )
223231 }
232+
224233 fn len ( & self ) -> usize {
225234 self . len ( )
226235 }
236+
227237 fn concat ( mut self , other : Self ) -> Self {
228238 self . extend ( other) ;
229239 self
@@ -234,6 +244,7 @@ impl FormatChar for u8 {
234244 fn to_char_lossy ( self ) -> char {
235245 self . into ( )
236246 }
247+
237248 fn eq_char ( self , c : char ) -> bool {
238249 char:: from ( self ) == c
239250 }
@@ -394,6 +405,7 @@ impl CFormatSpec {
394405 Some ( & ( CFormatQuantity :: Amount ( 1 ) . into ( ) ) ) ,
395406 )
396407 }
408+
397409 pub fn format_bytes ( & self , bytes : & [ u8 ] ) -> Vec < u8 > {
398410 let bytes = if let Some ( CFormatPrecision :: Quantity ( CFormatQuantity :: Amount ( precision) ) ) =
399411 self . precision
@@ -707,12 +719,12 @@ pub enum CFormatPart<T> {
707719
708720impl < T > CFormatPart < T > {
709721 #[ inline]
710- pub fn is_specifier ( & self ) -> bool {
722+ pub const fn is_specifier ( & self ) -> bool {
711723 matches ! ( self , CFormatPart :: Spec { .. } )
712724 }
713725
714726 #[ inline]
715- pub fn has_key ( & self ) -> bool {
727+ pub const fn has_key ( & self ) -> bool {
716728 match self {
717729 CFormatPart :: Spec ( s) => s. mapping_key . is_some ( ) ,
718730 _ => false ,
@@ -804,6 +816,7 @@ impl<S> CFormatStrOrBytes<S> {
804816impl < S > IntoIterator for CFormatStrOrBytes < S > {
805817 type Item = ( usize , CFormatPart < S > ) ;
806818 type IntoIter = std:: vec:: IntoIter < Self :: Item > ;
819+
807820 fn into_iter ( self ) -> Self :: IntoIter {
808821 self . parts . into_iter ( )
809822 }
0 commit comments