pub fn read_str(&mut self, length: usize) -> Result<String> {
        let mut out = String::with_capacity(length);

        let mut i = length;
        while i > 0 {
            // read first byte
            let r1 = u32::from(self.read_u8()?);
            let ch = if r1 != 0 && r1 < 0x80 {
                // single byte
                i -= 1;
                r1
            } else if r1 >= 0xC0 && r1 < 0xE0 && i >= 2 {
                // 2 bytes
                i -= 2;
                let r2 = u32::from(self.read_u8()?);
                (r1 & 0x1F) << 6 | (r2 & 0x3F)
            } else if r1 >= 0xE0 && r1 < 0xF0 && i >= 3 {
                i -= 3;
                let r2 = u32::from(self.read_u8()?);
                let r3 = u32::from(self.read_u8()?);
                if r1 == 0xED && r2 >= 0xA0 && r2 <= 0xAF {
                    if i >= 3 {
                        i -= 3;

                        self.read_u8()?;
                        let r5 = u32::from(self.read_u8()?);
                        let r6 = u32::from(self.read_u8()?);
                        // r1 and r4 can be ignored
                        0x1_0000 + ((r2 & 0x0F) << 16) + ((r3 & 0x3F) << 10) + ((r5 & 0x0F) << 6)
                            + (r6 & 0x3F)
                    } else {
                        return Err(Error::InvalidUTF8);
                    }
                } else {
                    ((r1 & 0x0F) << 12) + ((r2 & 0x3F) << 6) + (r3 & 0x3F)
                }
            } else {
                // this is not a valid utf8 scalar value
                return Err(Error::InvalidUTF8);
            };

            // convert the u32 to a char and push it to the output string
            let ch = char::from_u32(ch).ok_or(Error::InvalidUTF8)?;
            out.push(ch);
        }

        Ok(out)
    }
/// Decodes a modified UTF-8 string.
/// Length is the amount of bytes the String was encoded in.
/// The length used here may differ from the count of all chars.
Decodes a modified UTF-8 string.
Length is the amount of bytes the String was encoded in.
The length used here may differ from the count of all chars.
Decodes a modified UTF - 8 string . Length is the amount of bytes the String was encoded in . The length used here may differ from the count of all chars .
pub fn read_str(&mut self, length: usize) -> Result<String> {
        let mut out = String::with_capacity(length);
        let mut i = length;
        while i > 0 {
            // read first byte
            let r1 = u32::from(self.read_u8()?);
            let ch = if r1 != 0 && r1 < 0x80 {
                // single byte
                i -= 1;
                r1
            } else if r1 >= 0xC0 && r1 < 0xE0 && i >= 2 {
                // 2 bytes
                i -= 2;
                let r2 = u32::from(self.read_u8()?);
                (r1 & 0x1F) << 6 | (r2 & 0x3F)
            } else if r1 >= 0xE0 && r1 < 0xF0 && i >= 3 {
                i -= 3;
                let r2 = u32::from(self.read_u8()?);
                let r3 = u32::from(self.read_u8()?);
                if r1 == 0xED && r2 >= 0xA0 && r2 <= 0xAF {
                    if i >= 3 {
                        i -= 3;
                        self.read_u8()?;
                        let r5 = u32::from(self.read_u8()?);
                        let r6 = u32::from(self.read_u8()?);
                        // r1 and r4 can be ignored
                        0x1_0000 + ((r2 & 0x0F) << 16) + ((r3 & 0x3F) << 10) + ((r5 & 0x0F) << 6)
                            + (r6 & 0x3F)
                    } else {
                        return Err(Error::InvalidUTF8);
                    }
                } else {
                    ((r1 & 0x0F) << 12) + ((r2 & 0x3F) << 6) + (r3 & 0x3F)
                }
            } else {
                // this is not a valid utf8 scalar value
                return Err(Error::InvalidUTF8);
            };
            // convert the u32 to a char and push it to the output string
            let ch = char::from_u32(ch).ok_or(Error::InvalidUTF8)?;
            out.push(ch);
        }
        Ok(out)
    }
pub fn read_str ( & mut self , length : usize ) -> Result < String > { let mut out = String :: with_capacity ( length ) ; let mut i = length ; while i > 0 { // read first byte let r1 = u32 :: from ( self . read_u8 ( ) ? ) ; let ch = if r1 != 0 && r1 < 0x80 { // single byte i -= 1 ; r1 } else if r1 >= 0xC0 && r1 < 0xE0 && i >= 2 { // 2 bytes i -= 2 ; let r2 = u32 :: from ( self . read_u8 ( ) ? ) ; ( r1 & 0x1F ) << 6 | ( r2 & 0x3F ) } else if r1 >= 0xE0 && r1 < 0xF0 && i >= 3 { i -= 3 ; let r2 = u32 :: from ( self . read_u8 ( ) ? ) ; let r3 = u32 :: from ( self . read_u8 ( ) ? ) ; if r1 == 0xED && r2 >= 0xA0 && r2 <= 0xAF { if i >= 3 { i -= 3 ; self . read_u8 ( ) ? ; let r5 = u32 :: from ( self . read_u8 ( ) ? ) ; let r6 = u32 :: from ( self . read_u8 ( ) ? ) ; // r1 and r4 can be ignored 0x1_0000 + ( ( r2 & 0x0F ) << 16 ) + ( ( r3 & 0x3F ) << 10 ) + ( ( r5 & 0x0F ) << 6 ) + ( r6 & 0x3F ) } else { return Err ( Error :: InvalidUTF8 ) ; } } else { ( ( r1 & 0x0F ) << 12 ) + ( ( r2 & 0x3F ) << 6 ) + ( r3 & 0x3F ) } } else { // this is not a valid utf8 scalar value return Err ( Error :: InvalidUTF8 ) ; } ; // convert the u32 to a char and push it to the output string let ch = char :: from_u32 ( ch ) . ok_or ( Error :: InvalidUTF8 ) ? ; out . push ( ch ) ; } Ok ( out ) }
Decodes a modified UTF-8 string.
Decodes a modified UTF - 8 string .
&mut self
length
usize