@@ -82,13 +82,22 @@ impl<const N: usize> Debug for AsciiNumber<N> {
8282 }
8383}
8484
85+ const fn const_max ( a : usize , b : usize ) -> usize {
86+ if a > b {
87+ a
88+ } else {
89+ b
90+ }
91+ }
92+
8593macro_rules! impl_numtoa_const_for_base_on_type {
8694 (
8795 $type_name: ty,
8896 $base: expr,
8997 $core_function_name: ident,
9098 $base_n_function_name: ident,
9199 $padded_function_name: ident,
100+ $filled_function_name: ident,
92101 $required_space_constant_name: ident,
93102 $needed_space_bytes: expr
94103) => {
@@ -115,6 +124,18 @@ macro_rules! impl_numtoa_const_for_base_on_type {
115124 let _ = $core_function_name( num, $base, & mut string) ;
116125 return AsciiNumber { string, start: 0 } ;
117126 }
127+
128+ #[ doc = concat!( "converts the specified [" , stringify!( $type_name) , "] to its ASCII representation in base " , $base, ", left-filled to the specified length with the provided byte" ) ]
129+ pub const fn $filled_function_name<const LENGTH : usize >(
130+ num: $type_name,
131+ fill: u8 ,
132+ ) -> AsciiNumber <{ Self :: $required_space_constant_name } > {
133+ const { assert!( LENGTH < { Self :: $required_space_constant_name } ) }
134+ let mut string = [ fill; Self :: $required_space_constant_name] ;
135+ let start = Self :: $required_space_constant_name
136+ - const_max( LENGTH , $core_function_name( num, $base, & mut string) . len( ) ) ;
137+ return AsciiNumber { string, start } ;
138+ }
118139 } ;
119140}
120141
@@ -127,6 +148,7 @@ macro_rules! impl_numtoa_const_for_base_n {
127148 numtoa_u8,
128149 u8 ,
129150 u8_padded,
151+ u8_filled,
130152 REQUIRED_SPACE_U8 ,
131153 required_space( $base_value as u128 , u8 :: MAX as u128 , false )
132154 ) ;
@@ -136,6 +158,7 @@ macro_rules! impl_numtoa_const_for_base_n {
136158 numtoa_u16,
137159 u16 ,
138160 u16_padded,
161+ u16_filled,
139162 REQUIRED_SPACE_U16 ,
140163 required_space( $base_value as u128 , u16 :: MAX as u128 , false )
141164 ) ;
@@ -145,6 +168,7 @@ macro_rules! impl_numtoa_const_for_base_n {
145168 numtoa_u32,
146169 u32 ,
147170 u32_padded,
171+ u32_filled,
148172 REQUIRED_SPACE_U32 ,
149173 required_space( $base_value as u128 , u32 :: MAX as u128 , false )
150174 ) ;
@@ -154,6 +178,7 @@ macro_rules! impl_numtoa_const_for_base_n {
154178 numtoa_u64,
155179 u64 ,
156180 u64_padded,
181+ u64_filled,
157182 REQUIRED_SPACE_U64 ,
158183 required_space( $base_value as u128 , u64 :: MAX as u128 , false )
159184 ) ;
@@ -163,6 +188,7 @@ macro_rules! impl_numtoa_const_for_base_n {
163188 numtoa_u128,
164189 u128 ,
165190 u128_padded,
191+ u128_filled,
166192 REQUIRED_SPACE_U128 ,
167193 required_space( $base_value as u128 , u128 :: MAX as u128 , false )
168194 ) ;
@@ -172,6 +198,7 @@ macro_rules! impl_numtoa_const_for_base_n {
172198 numtoa_usize,
173199 usize ,
174200 usize_padded,
201+ usize_filled,
175202 REQUIRED_SPACE_USIZE ,
176203 required_space( $base_value as u128 , usize :: MAX as u128 , false )
177204 ) ;
@@ -181,6 +208,7 @@ macro_rules! impl_numtoa_const_for_base_n {
181208 numtoa_i8,
182209 i8 ,
183210 i8_padded,
211+ i8_filled,
184212 REQUIRED_SPACE_I8 ,
185213 required_space( $base_value as u128 , i8 :: MIN . unsigned_abs( ) as u128 , true )
186214 ) ;
@@ -190,6 +218,7 @@ macro_rules! impl_numtoa_const_for_base_n {
190218 numtoa_i16,
191219 i16 ,
192220 i16_padded,
221+ i16_filled,
193222 REQUIRED_SPACE_I16 ,
194223 required_space( $base_value as u128 , i16 :: MIN . unsigned_abs( ) as u128 , true )
195224 ) ;
@@ -199,6 +228,7 @@ macro_rules! impl_numtoa_const_for_base_n {
199228 numtoa_i32,
200229 i32 ,
201230 i32_padded,
231+ i32_filled,
202232 REQUIRED_SPACE_I32 ,
203233 required_space( $base_value as u128 , i32 :: MIN . unsigned_abs( ) as u128 , true )
204234 ) ;
@@ -208,6 +238,7 @@ macro_rules! impl_numtoa_const_for_base_n {
208238 numtoa_i64,
209239 i64 ,
210240 i64_padded,
241+ i64_filled,
211242 REQUIRED_SPACE_I64 ,
212243 required_space( $base_value as u128 , i64 :: MIN . unsigned_abs( ) as u128 , true )
213244 ) ;
@@ -217,6 +248,7 @@ macro_rules! impl_numtoa_const_for_base_n {
217248 numtoa_i128,
218249 i128 ,
219250 i128_padded,
251+ i128_filled,
220252 REQUIRED_SPACE_I128 ,
221253 required_space( $base_value as u128 , i128 :: MIN . unsigned_abs( ) as u128 , true )
222254 ) ;
@@ -226,6 +258,7 @@ macro_rules! impl_numtoa_const_for_base_n {
226258 numtoa_isize,
227259 isize ,
228260 isize_padded,
261+ isize_filled,
229262 REQUIRED_SPACE_ISIZE ,
230263 required_space( $base_value as u128 , isize :: MIN . unsigned_abs( ) as u128 , true )
231264 ) ;
@@ -272,6 +305,16 @@ fn str_convenience_base10_padded() {
272305 ) ;
273306}
274307
308+ #[ test]
309+ fn str_convenience_base10_filled ( ) {
310+ assert_eq ! ( "12" , BaseN :: <10 >:: i32_filled:: <1 >( 12 , b'0' ) . as_str( ) ) ;
311+ assert_eq ! ( "00012" , BaseN :: <10 >:: i32_filled:: <5 >( 12 , b'0' ) . as_str( ) ) ;
312+ assert_eq ! (
313+ "123456" ,
314+ BaseN :: <10 >:: i32_filled:: <5 >( 123456 , b'0' ) . as_str( )
315+ ) ;
316+ }
317+
275318#[ test]
276319fn str_convenience_base16 ( ) {
277320 assert_eq ! ( "3E87B" , BaseN :: <16 >:: i32 ( 256123 ) . as_str( ) ) ;
@@ -285,6 +328,16 @@ fn str_convenience_base16_padded() {
285328 ) ;
286329}
287330
331+ #[ test]
332+ fn str_convenience_base16_filled ( ) {
333+ assert_eq ! ( "7B" , BaseN :: <16 >:: i32_filled:: <1 >( 123 , b'0' ) . as_str( ) ) ;
334+ assert_eq ! ( "0007B" , BaseN :: <16 >:: i32_filled:: <5 >( 123 , b'0' ) . as_str( ) ) ;
335+ assert_eq ! (
336+ "BC614E" ,
337+ BaseN :: <16 >:: i32_filled:: <5 >( 12345678 , b'0' ) . as_str( )
338+ ) ;
339+ }
340+
288341#[ test]
289342fn str_convenience_wacky_padding ( ) {
290343 assert_eq ! (
@@ -297,6 +350,16 @@ fn str_convenience_wacky_padding() {
297350 ) ;
298351}
299352
353+ #[ test]
354+ fn str_convenience_wacky_filling ( ) {
355+ assert_eq ! (
356+ "##-3E87B" ,
357+ BaseN :: <16 >:: i32_filled:: <8 >( -256123 , b'#' ) . as_str( )
358+ ) ;
359+ assert_eq ! ( "@-3" , BaseN :: <10 >:: i8_filled:: <3 >( -3 , b'@' ) . as_str( ) ) ;
360+ assert_eq ! ( "-11" , BaseN :: <10 >:: i8_filled:: <3 >( -11 , b'@' ) . as_str( ) ) ;
361+ }
362+
300363#[ test]
301364fn base10_i8_all_base10 ( ) {
302365 for i in i8:: MIN ..i8:: MAX {
0 commit comments