Update for the version 0.7 of the utf8 crate.

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/7/head
Alexey Galakhov 8 years ago
parent c6f47503d6
commit b057cf0096
  1. 2
      Cargo.toml
  2. 53
      src/protocol/message.rs

@ -23,7 +23,7 @@ log = "*"
rand = "*" rand = "*"
sha1 = "*" sha1 = "*"
url = "*" url = "*"
utf-8 = "0.6" utf-8 = "*"
[dependencies.native-tls] [dependencies.native-tls]
optional = true optional = true

@ -8,34 +8,67 @@ use error::Result;
mod string_collect { mod string_collect {
use utf8; use utf8;
use utf8::DecodeError;
use error::{Error, Result}; use error::{Error, Result};
pub struct StringCollector { pub struct StringCollector {
data: String, data: String,
decoder: utf8::Decoder, incomplete: Option<utf8::Incomplete>,
} }
impl StringCollector { impl StringCollector {
pub fn new() -> Self { pub fn new() -> Self {
StringCollector { StringCollector {
data: String::new(), data: String::new(),
decoder: utf8::Decoder::new(), incomplete: None,
} }
} }
pub fn extend<T: AsRef<[u8]>>(&mut self, tail: T) -> Result<()> { pub fn extend<T: AsRef<[u8]>>(&mut self, tail: T) -> Result<()> {
let (sym, text, result) = self.decoder.decode(tail.as_ref()); let mut input: &[u8] = tail.as_ref();
self.data.push_str(&sym);
if let Some(mut incomplete) = self.incomplete.take() {
let fin = if let Some((result, rest)) = incomplete.try_complete(input) {
input = rest;
if let Ok(text) = result {
self.data.push_str(text); self.data.push_str(text);
match result { } else {
utf8::Result::Ok | utf8::Result::Incomplete => return Err(Error::Utf8)
Ok(()), }
utf8::Result::Error { remaining_input_after_error: _ } => true
Err(Error::Utf8), } else {
input = &[];
false
};
if !fin {
self.incomplete = Some(incomplete)
} }
} }
if !input.is_empty() {
match utf8::decode(input) {
Ok(text) => {
self.data.push_str(text);
Ok(())
}
Err(DecodeError::Incomplete { valid_prefix, incomplete_suffix }) => {
self.data.push_str(valid_prefix);
self.incomplete = Some(incomplete_suffix);
Ok(())
}
Err(DecodeError::Invalid { valid_prefix, .. }) => {
self.data.push_str(valid_prefix);
Err(Error::Utf8)
}
}
} else {
Ok(())
}
}
pub fn into_string(self) -> Result<String> { pub fn into_string(self) -> Result<String> {
if self.decoder.has_incomplete_sequence() { if self.incomplete.is_some() {
Err(Error::Utf8) Err(Error::Utf8)
} else { } else {
Ok(self.data) Ok(self.data)

Loading…
Cancel
Save