/// Write a string into the buffer
///
/// # Examples
///
/// ```
/// # use bitbuffer::{BitReadBuffer, LittleEndian, Result};
/// #
/// # fn main() -> Result<()> {
/// # use bitbuffer::{BitWriteStream, LittleEndian};
///
/// let mut data = Vec::new();
/// let mut stream = BitWriteStream::new(&mut data, LittleEndian);
/// stream.write_string("zero terminated string", None)?;
/// stream.write_string("fixed size string, zero padded", Some(64))?;
/// #
/// #     Ok(())
/// # }
/// ```
Write a string into the buffer
Write a string into the buffer
pub fn write_string(&mut self, string: &str, length: Option<usize>) -> Result<()> {
        match length {
            Some(length) => {
                if length < string.len() {
                    return Err(BitError::StringToLong {
                        string_length: string.len(),
                        requested_length: length,
                    });
                }
                self.write_bytes(&string.as_bytes())?;
                for _ in 0..(length - string.len()) {
                    self.push_bits(0, 8)
                }
            }
            None => {
                self.write_bytes(&string.as_bytes())?;
                self.push_bits(0, 8)
            }
        }
        Ok(())
    }
pub fn write_string ( & mut self , string : & str , length : Option < usize > ) -> Result < ( ) > { match length { Some ( length ) => { if length < string . len ( ) { return Err ( BitError :: StringToLong { string_length : string . len ( ) , requested_length : length , } ) ; } self . write_bytes ( & string . as_bytes ( ) ) ? ; for _ in 0 .. ( length - string . len ( ) ) { self . push_bits ( 0 , 8 ) } } None => { self . write_bytes ( & string . as_bytes ( ) ) ? ; self . push_bits ( 0 , 8 ) } } Ok ( ( ) ) }
Write a string into the buffer
Write a string into the buffer
&mut self
string
&str
length
Option<usize>
examples
None
use bitbuffer::{bitreadbuffer, littleendian, result};
None
fn main() -> result<()> {
None
use bitbuffer::{bitwritestream, littleendian};
let mut data = Vec::new();
let mut stream = BitWriteStream::new(&mut data, LittleEndian);
stream.write_string("zero terminated string", None)?;
stream.write_string("fixed size string, zero padded", Some(64))?.
let mut data = Vec :: new () ; let mut stream = BitWriteStream :: new ( &mut data LittleEndian ) ; stream . write_string ( " zero terminated string " None ) ? ; stream . write_string ( " fixed size string zero padded " Some ( 64 )) ? .
ok(())
None
}
None