@@ -112,7 +112,6 @@ struct Fraction {
112112struct Parser < ' a > {
113113 iter : Chars < ' a > ,
114114 src : & ' a str ,
115- current : Duration ,
116115}
117116
118117impl Parser < ' _ > {
@@ -146,10 +145,11 @@ impl Parser<'_> {
146145 }
147146 let start = off;
148147 let mut off = self . off ( ) ;
148+ let mut out = Duration :: ZERO ;
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 Ok ( unit) = Unit :: from_str ( & self . src [ start..end] ) else {
241243 return Err ( Error :: UnknownUnit {
@@ -259,7 +261,7 @@ impl Parser<'_> {
259261 Unit :: Month => ( n. mul ( 2_630_016 ) ?, 0 ) , // 30.44d
260262 Unit :: Year => ( n. mul ( 31_557_600 ) ?, 0 ) , // 365.25d
261263 } ;
262- self . add_current ( sec, nsec) ?;
264+ add_current ( sec, nsec, out ) ?;
263265
264266 // add the fractional part
265267 if let Some ( Fraction {
@@ -279,21 +281,22 @@ impl Parser<'_> {
279281 Unit :: Month => ( n. mul ( 2_630_016 ) ?. div ( d) ?, 0 ) , // 30.44d
280282 Unit :: Year => ( n. mul ( 31_557_600 ) ?. div ( d) ?, 0 ) , // 365.25d
281283 } ;
282- self . add_current ( sec, nsec) ?;
284+ add_current ( sec, nsec, out ) ?;
283285 }
286+
284287 Ok ( ( ) )
285288 }
289+ }
286290
287- fn add_current ( & mut self , mut sec : u64 , nsec : u64 ) -> Result < ( ) , Error > {
288- let mut nsec = ( self . current . subsec_nanos ( ) as u64 ) . add ( nsec) ?;
289- if nsec > 1_000_000_000 {
290- sec = sec. add ( nsec / 1_000_000_000 ) ?;
291- nsec %= 1_000_000_000 ;
292- }
293- sec = self . current . as_secs ( ) . add ( sec) ?;
294- self . current = Duration :: new ( sec, nsec as u32 ) ;
295- Ok ( ( ) )
291+ fn add_current ( mut sec : u64 , nsec : u64 , out : & mut Duration ) -> Result < ( ) , Error > {
292+ let mut nsec = ( out. subsec_nanos ( ) as u64 ) . add ( nsec) ?;
293+ if nsec > 1_000_000_000 {
294+ sec = sec. add ( nsec / 1_000_000_000 ) ?;
295+ nsec %= 1_000_000_000 ;
296296 }
297+ sec = out. as_secs ( ) . add ( sec) ?;
298+ * out = Duration :: new ( sec, nsec as u32 ) ;
299+ Ok ( ( ) )
297300}
298301
299302enum Unit {
@@ -362,7 +365,6 @@ pub fn parse_duration(s: &str) -> Result<Duration, Error> {
362365 Parser {
363366 iter : s. chars ( ) ,
364367 src : s,
365- current : Duration :: ZERO ,
366368 }
367369 . parse ( )
368370}
0 commit comments