File tree Expand file tree Collapse file tree 1 file changed +3
-22
lines changed Expand file tree Collapse file tree 1 file changed +3
-22
lines changed Original file line number Diff line number Diff line change @@ -22,19 +22,7 @@ impl<'a> Part<'a> {
22
22
pub fn len ( & self ) -> usize {
23
23
match * self {
24
24
Part :: Zero ( nzeroes) => nzeroes,
25
- Part :: Num ( v) => {
26
- if v < 1_000 {
27
- if v < 10 {
28
- 1
29
- } else if v < 100 {
30
- 2
31
- } else {
32
- 3
33
- }
34
- } else {
35
- if v < 10_000 { 4 } else { 5 }
36
- }
37
- }
25
+ Part :: Num ( v) => v. checked_ilog10 ( ) . unwrap_or_default ( ) as usize + 1 ,
38
26
Part :: Copy ( buf) => buf. len ( ) ,
39
27
}
40
28
}
@@ -82,21 +70,14 @@ pub struct Formatted<'a> {
82
70
impl < ' a > Formatted < ' a > {
83
71
/// Returns the exact byte length of combined formatted result.
84
72
pub fn len ( & self ) -> usize {
85
- let mut len = self . sign . len ( ) ;
86
- for part in self . parts {
87
- len += part. len ( ) ;
88
- }
89
- len
73
+ self . sign . len ( ) + self . parts . iter ( ) . map ( |part| part. len ( ) ) . sum :: < usize > ( )
90
74
}
91
75
92
76
/// Writes all formatted parts into the supplied buffer.
93
77
/// Returns the number of written bytes, or `None` if the buffer is not enough.
94
78
/// (It may still leave partially written bytes in the buffer; do not rely on that.)
95
79
pub fn write ( & self , out : & mut [ u8 ] ) -> Option < usize > {
96
- if out. len ( ) < self . sign . len ( ) {
97
- return None ;
98
- }
99
- out[ ..self . sign . len ( ) ] . copy_from_slice ( self . sign . as_bytes ( ) ) ;
80
+ out. get_mut ( ..self . sign . len ( ) ) ?. copy_from_slice ( self . sign . as_bytes ( ) ) ;
100
81
101
82
let mut written = self . sign . len ( ) ;
102
83
for part in self . parts {
You can’t perform that action at this time.
0 commit comments