aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/lib.rs b/src/lib.rs
index d2374ab..f049d05 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -145,18 +145,14 @@ impl Tftp {
}
fn get_tftp_str(&self, buf: &[u8]) -> Option<String> {
- let mut iter = buf.iter();
-
- let len = match iter.position(|&x| x == 0) {
- Some(l) => l,
- None => return None,
- };
- let val = match String::from_utf8(buf[0..len].to_vec()) {
- Ok(v) => v,
- Err(_) => return None,
- };
-
- Some(val)
+ /* make sure the null-terminator exists */
+ buf.iter().find(|&x| *x == 0)?;
+
+ /* build string from buffer */
+ String::from_utf8(buf.iter()
+ .take_while(|&x| *x != 0)
+ .map(|&x| x)
+ .collect()).ok()
}
/// Read::read can possibly return less bytes than the requested buffer size,