@@ -112,12 +112,12 @@ struct Fraction {
112112struct Parser < ' a > {
113113 iter : Chars < ' a > ,
114114 src : & ' a str ,
115- current : Duration ,
116115}
117116
118117impl Parser < ' _ > {
119118 fn parse ( mut self ) -> Result < Duration , Error > {
120119 let mut n = self . parse_first_char ( ) ?. ok_or ( Error :: Empty ) ?; // integer part
120+ let mut out = Duration :: ZERO ;
121121 ' outer: loop {
122122 let mut frac = None ; // fractional part
123123 let mut off = self . off ( ) ;
@@ -149,7 +149,7 @@ impl Parser<'_> {
149149 while let Some ( c) = self . iter . next ( ) {
150150 match c {
151151 '0' ..='9' => {
152- self . parse_unit ( n, frac, start, off) ?;
152+ self . parse_unit ( n, frac, start, off, & mut out ) ?;
153153 n = c as u64 - '0' as u64 ;
154154 continue ' outer;
155155 }
@@ -161,10 +161,11 @@ impl Parser<'_> {
161161 }
162162 off = self . off ( ) ;
163163 }
164- self . parse_unit ( n, frac, start, off) ?;
164+
165+ self . parse_unit ( n, frac, start, off, & mut out) ?;
165166 n = match self . parse_first_char ( ) ? {
166167 Some ( n) => n,
167- None => return Ok ( self . current ) ,
168+ None => return Ok ( out ) ,
168169 } ;
169170 }
170171 }
@@ -236,6 +237,7 @@ impl Parser<'_> {
236237 frac : Option < Fraction > ,
237238 start : usize ,
238239 end : usize ,
240+ out : & mut Duration ,
239241 ) -> Result < ( ) , Error > {
240242 let unit = match Unit :: from_str ( & self . src [ start..end] ) {
241243 Ok ( u) => u,
@@ -262,7 +264,7 @@ impl Parser<'_> {
262264 Unit :: Month => ( n. mul ( 2_630_016 ) ?, 0 ) , // 30.44d
263265 Unit :: Year => ( n. mul ( 31_557_600 ) ?, 0 ) , // 365.25d
264266 } ;
265- self . add_current ( sec, nsec) ?;
267+ add_current ( sec, nsec, out ) ?;
266268
267269 // add the fractional part
268270 if let Some ( Fraction {
@@ -282,21 +284,22 @@ impl Parser<'_> {
282284 Unit :: Month => ( n. mul ( 2_630_016 ) ?. div ( d) ?, 0 ) , // 30.44d
283285 Unit :: Year => ( n. mul ( 31_557_600 ) ?. div ( d) ?, 0 ) , // 365.25d
284286 } ;
285- self . add_current ( sec, nsec) ?;
287+ add_current ( sec, nsec, out ) ?;
286288 }
289+
287290 Ok ( ( ) )
288291 }
292+ }
289293
290- fn add_current ( & mut self , mut sec : u64 , nsec : u64 ) -> Result < ( ) , Error > {
291- let mut nsec = ( self . current . subsec_nanos ( ) as u64 ) . add ( nsec) ?;
292- if nsec > 1_000_000_000 {
293- sec = sec. add ( nsec / 1_000_000_000 ) ?;
294- nsec %= 1_000_000_000 ;
295- }
296- sec = self . current . as_secs ( ) . add ( sec) ?;
297- self . current = Duration :: new ( sec, nsec as u32 ) ;
298- Ok ( ( ) )
294+ fn add_current ( mut sec : u64 , nsec : u64 , out : & mut Duration ) -> Result < ( ) , Error > {
295+ let mut nsec = ( out. subsec_nanos ( ) as u64 ) . add ( nsec) ?;
296+ if nsec > 1_000_000_000 {
297+ sec = sec. add ( nsec / 1_000_000_000 ) ?;
298+ nsec %= 1_000_000_000 ;
299299 }
300+ sec = out. as_secs ( ) . add ( sec) ?;
301+ * out = Duration :: new ( sec, nsec as u32 ) ;
302+ Ok ( ( ) )
300303}
301304
302305enum Unit {
@@ -365,7 +368,6 @@ pub fn parse_duration(s: &str) -> Result<Duration, Error> {
365368 Parser {
366369 iter : s. chars ( ) ,
367370 src : s,
368- current : Duration :: ZERO ,
369371 }
370372 . parse ( )
371373}
0 commit comments