Repository: serde-rs/serde
File: serde_json/src/de.rs (Rust)

Code before:
        match value {
            Ok(value) => Ok(value),
            Err(Error::SyntaxError(code, _, _)) => Err(self.error(code)),
            Err(err) => Err(err),
        }
    }

    fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
        for c in ident {
            if Some(*c) != try!(self.next_char()) {
                return Err(self.error(ErrorCode::ExpectedSomeIdent));
            }
        }

        try!(self.bump());
        Ok(())
    }

    fn parse_integer<V>(&mut self, pos: bool, visitor: V) -> Result<V::Value>
        where V: de::Visitor,
    {
        match self.ch_or_null() {
            b'0' => {
                try!(self.bump());

                // There can be only one leading '0'.
                match self.ch_or_null() {
                    b'0' ... b'9' => {
                        Err(self.error(ErrorCode::InvalidNumber))
                    }
                    _ => {
                        self.parse_number(pos, 0, visitor)
                    }
                }
            },
            c @ b'1' ... b'9' => {
                try!(self.bump());

                let mut res: u64 = (c as u64) - ('0' as u64);

                loop {
                    match self.ch_or_null() {
                        c @ b'0' ... b'9' => {
                            try!(self.bump());

                            let digit = (c as u64) - ('0' as u64);

                            // We need to be careful with overflow. If we can, try to keep the
                            // number as a `u64` until we grow too large. At that point, switch to
                            // parsing the value as a `f64`.
                            match res.checked_mul(10) {

Review comment (performance):
is this additional complexity really necessary? Does it matter for deserialization if there are multiple leading zeros?


Code after:
        match value {
            Ok(value) => Ok(value),
            Err(Error::SyntaxError(code, _, _)) => Err(self.error(code)),
            Err(err) => Err(err),
        }
    }

    fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
        for c in ident {
            if Some(*c) != try!(self.next_char()) {
                return Err(self.error(ErrorCode::ExpectedSomeIdent));
            }
        }

        try!(self.bump());
        Ok(())
    }

    fn parse_integer<V>(&mut self, pos: bool, visitor: V) -> Result<V::Value>
        where V: de::Visitor,
    {
        match self.ch_or_null() {
            b'0' => {
                try!(self.bump());

                // There can be only one leading '0'.
                match self.ch_or_null() {
                    b'0' ... b'9' => {
                        Err(self.error(ErrorCode::InvalidNumber))
                    }
                    _ => {
                        self.parse_number(pos, 0, visitor)
                    }
                }
            },
            c @ b'1' ... b'9' => {
                try!(self.bump());

                let mut res: u64 = (c as u64) - ('0' as u64);

                loop {
                    match self.ch_or_null() {
                        c @ b'0' ... b'9' => {
                            try!(self.bump());

                            let digit = (c as u64) - ('0' as u64);

                            // We need to be careful with overflow. If we can, try to keep the
                            // number as a `u64` until we grow too large. At that point, switch to
                            // parsing the value as a `f64`.
                            match res.checked_mul(10).and_then(|val| val.checked_add(digit)) {