aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Barnard2015-08-28 16:50:01 +0700
committerEdward Barnard2015-08-28 16:50:01 +0700
commit0c2ad6b0d12524b7042d45a92c1b20d8082c662c (patch)
tree099fb4ea08c347764dc3f9ec90ff5409bdfeeed3 /src
parentb0b34ef55e1d8a3783b7ddfbf5612b9176b20e66 (diff)
downloadrust-plist-0c2ad6b0d12524b7042d45a92c1b20d8082c662c.tar.bz2
Add size hints for arrays and dictionaries
Diffstat (limited to 'src')
-rw-r--r--src/binary.rs8
-rw-r--r--src/lib.rs26
-rw-r--r--src/xml.rs8
3 files changed, 24 insertions, 18 deletions
diff --git a/src/binary.rs b/src/binary.rs
index ef293cb..826ba0b 100644
--- a/src/binary.rs
+++ b/src/binary.rs
@@ -198,7 +198,7 @@ impl<R: Read+Seek> StreamingParser<R> {
object_refs: object_refs
});
- Some(PlistEvent::StartArray)
+ Some(PlistEvent::StartArray(Some(len)))
},
(0xd, n) => { // Dict
let len = try!(self.read_object_len(n));
@@ -214,7 +214,7 @@ impl<R: Read+Seek> StreamingParser<R> {
object_refs: object_refs
});
- Some(PlistEvent::StartDictionary)
+ Some(PlistEvent::StartDictionary(Some(len)))
},
(_, _) => return Err(ParserError::InvalidData)
};
@@ -251,9 +251,9 @@ mod tests {
let events: Vec<PlistEvent> = streaming_parser.collect();
let comparison = &[
- StartDictionary,
+ StartDictionary(Some(5)),
StringValue("Lines".to_owned()),
- StartArray,
+ StartArray(Some(2)),
StringValue("It is a tale told by an idiot,".to_owned()),
StringValue("Full of sound and fury, signifying nothing.".to_owned()),
EndArray,
diff --git a/src/lib.rs b/src/lib.rs
index c80ffb1..6168295 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -26,10 +26,10 @@ pub enum Plist {
#[derive(Debug, PartialEq)]
pub enum PlistEvent {
- StartArray,
+ StartArray(Option<u64>),
EndArray,
- StartDictionary,
+ StartDictionary(Option<u64>),
EndDictionary,
BooleanValue(bool),
@@ -169,8 +169,8 @@ impl<T:Iterator<Item=PlistEvent>> Builder<T> {
fn build_value(&mut self) -> BuilderResult<Plist> {
match self.token.take() {
- Some(PlistEvent::StartArray) => Ok(Plist::Array(try!(self.build_array()))),
- Some(PlistEvent::StartDictionary) => Ok(Plist::Dictionary(try!(self.build_dict()))),
+ Some(PlistEvent::StartArray(len)) => Ok(Plist::Array(try!(self.build_array(len)))),
+ Some(PlistEvent::StartDictionary(len)) => Ok(Plist::Dictionary(try!(self.build_dict(len)))),
Some(PlistEvent::BooleanValue(b)) => Ok(Plist::Boolean(b)),
Some(PlistEvent::DataValue(d)) => Ok(Plist::Data(d)),
@@ -187,8 +187,11 @@ impl<T:Iterator<Item=PlistEvent>> Builder<T> {
}
}
- fn build_array(&mut self) -> Result<Vec<Plist>, BuilderError> {
- let mut values = Vec::new();
+ fn build_array(&mut self, len: Option<u64>) -> Result<Vec<Plist>, BuilderError> {
+ let mut values = match len {
+ Some(len) => Vec::with_capacity(len as usize),
+ None => Vec::new()
+ };
loop {
self.bump();
@@ -200,8 +203,11 @@ impl<T:Iterator<Item=PlistEvent>> Builder<T> {
}
}
- fn build_dict(&mut self) -> Result<HashMap<String, Plist>, BuilderError> {
- let mut values = HashMap::new();
+ fn build_dict(&mut self, len: Option<u64>) -> Result<HashMap<String, Plist>, BuilderError> {
+ let mut values = match len {
+ Some(len) => HashMap::with_capacity(len as usize),
+ None => HashMap::new()
+ };
loop {
@@ -234,11 +240,11 @@ mod tests {
// Input
let events = vec![
- StartDictionary,
+ StartDictionary(None),
StringValue("Author".to_owned()),
StringValue("William Shakespeare".to_owned()),
StringValue("Lines".to_owned()),
- StartArray,
+ StartArray(None),
StringValue("It is a tale told by an idiot,".to_owned()),
StringValue("Full of sound and fury, signifying nothing.".to_owned()),
EndArray,
diff --git a/src/xml.rs b/src/xml.rs
index a15db70..b6f8928 100644
--- a/src/xml.rs
+++ b/src/xml.rs
@@ -47,8 +47,8 @@ impl<R: Read> Iterator for StreamingParser<R> {
match &name.local_name[..] {
"plist" => (),
- "array" => return Some(PlistEvent::StartArray),
- "dict" => return Some(PlistEvent::StartDictionary),
+ "array" => return Some(PlistEvent::StartArray(None)),
+ "dict" => return Some(PlistEvent::StartDictionary(None)),
"key" => return Some(self.read_content(|s| PlistEvent::StringValue(s))),
"true" => return Some(PlistEvent::BooleanValue(true)),
"false" => return Some(PlistEvent::BooleanValue(false)),
@@ -118,11 +118,11 @@ mod tests {
let events: Vec<PlistEvent> = streaming_parser.collect();
let comparison = &[
- StartDictionary,
+ StartDictionary(None),
StringValue("Author".to_owned()),
StringValue("William Shakespeare".to_owned()),
StringValue("Lines".to_owned()),
- StartArray,
+ StartArray(None),
StringValue("It is a tale told by an idiot,".to_owned()),
StringValue("Full of sound and fury, signifying nothing.".to_owned()),
EndArray,