use libc;
use ptr;
use ptr::RawPtr;
+ use option::{Option, Some, None};
use str::{is_utf8, OwnedStr, StrSlice};
use vec;
use vec::{MutableVector, ImmutableVector, OwnedVector};
/// Removes the last byte from a string and returns it.
/// The caller must preserve the valid UTF-8 property.
- pub unsafe fn pop_byte(s: &mut ~str) -> u8 {
+ pub unsafe fn pop_byte(s: &mut ~str) -> Option<u8> {
let len = s.len();
- assert!((len > 0u));
- let b = s[len - 1u];
- s.set_len(len - 1);
- return b;
+ if len == 0u {
+ return None;
+ } else {
+ let b = s[len - 1u];
+ s.set_len(len - 1);
+ return Some(b);
+ }
}
/// Removes the first byte from a string and returns it.
/// The caller must preserve the valid UTF-8 property.
- pub unsafe fn shift_byte(s: &mut ~str) -> u8 {
+ pub unsafe fn shift_byte(s: &mut ~str) -> Option<u8> {
let len = s.len();
- assert!((len > 0u));
- let b = s[0];
- *s = s.slice(1, len).to_owned();
- return b;
+ if len == 0u {
+ return None;
+ } else {
+ let b = s[0];
+ *s = s.slice(1, len).to_owned();
+ return Some(b);
+ }
}
/// Access the str in its vector representation.
/// assert_eq!(c, 'ö');
/// assert_eq!(s2, "we 老虎 Léopard");
/// ```
- fn slice_shift_char(&self) -> (char, &'a str);
+ fn slice_shift_char(&self) -> (Option<char>, &'a str);
/// Levenshtein Distance between two strings.
fn lev_distance(&self, t: &str) -> uint;
}
#[inline]
- fn slice_shift_char(&self) -> (char, &'a str) {
- let CharRange {ch, next} = self.char_range_at(0u);
- let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
- return (ch, next_s);
+ fn slice_shift_char(&self) -> (Option<char>, &'a str) {
+ if self.is_empty() {
+ return (None, *self);
+ } else {
+ let CharRange {ch, next} = self.char_range_at(0u);
+ let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
+ return (Some(ch), next_s);
+ }
}
fn lev_distance(&self, t: &str) -> uint {
/// # Failure
///
/// If the string does not contain any characters
- fn pop_char(&mut self) -> char;
+ fn pop_char(&mut self) -> Option<char>;
/// Remove the first character from a string and return it
///
/// # Failure
///
/// If the string does not contain any characters
- fn shift_char(&mut self) -> char;
+ fn shift_char(&mut self) -> Option<char>;
/// Prepend a char to a string
fn unshift_char(&mut self, ch: char);
}
#[inline]
- fn pop_char(&mut self) -> char {
+ fn pop_char(&mut self) -> Option<char> {
let end = self.len();
- assert!(end > 0u);
- let CharRange {ch, next} = self.char_range_at_reverse(end);
- unsafe { self.set_len(next); }
- return ch;
+ if end == 0u {
+ return None;
+ } else {
+ let CharRange {ch, next} = self.char_range_at_reverse(end);
+ unsafe { self.set_len(next); }
+ return Some(ch);
+ }
}
#[inline]
- fn shift_char(&mut self) -> char {
- let CharRange {ch, next} = self.char_range_at(0u);
- *self = self.slice(next, self.len()).to_owned();
- return ch;
+ fn shift_char(&mut self) -> Option<char> {
+ if self.is_empty() {
+ return None;
+ } else {
+ let CharRange {ch, next} = self.char_range_at(0u);
+ *self = self.slice(next, self.len()).to_owned();
+ return Some(ch);
+ }
}
#[inline]
let mut data = ~"ประเทศไทย中华";
let cc = data.pop_char();
assert_eq!(~"ประเทศไทย中", data);
- assert_eq!('华', cc);
+ assert_eq!(Some('华'), cc);
}
#[test]
let mut data2 = ~"华";
let cc2 = data2.pop_char();
assert_eq!(~"", data2);
- assert_eq!('华', cc2);
+ assert_eq!(Some('华'), cc2);
}
#[test]
- #[should_fail]
- fn test_pop_char_fail() {
+ fn test_pop_char_empty() {
let mut data = ~"";
- let _cc3 = data.pop_char();
+ let cc3 = data.pop_char();
+ assert_eq!(~"", data);
+ assert_eq!(None, cc3);
}
#[test]
let mut data = ~"ประเทศไทย中";
let cc = data.shift_char();
assert_eq!(~"ระเทศไทย中", data);
- assert_eq!('ป', cc);
+ assert_eq!(Some('ป'), cc);
}
#[test]
let mut s = ~"ABC";
let b = unsafe{raw::shift_byte(&mut s)};
assert_eq!(s, ~"BC");
- assert_eq!(b, 65u8);
+ assert_eq!(b, Some(65u8));
}
#[test]
let mut s = ~"ABC";
let b = unsafe{raw::pop_byte(&mut s)};
assert_eq!(s, ~"AB");
- assert_eq!(b, 67u8);
+ assert_eq!(b, Some(67u8));
}
#[test]