Skip to content

Commit ef5f208

Browse files
committed
---
yaml --- r: 151604 b: refs/heads/try2 c: cb115ac h: refs/heads/master v: v3
1 parent f0d0665 commit ef5f208

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: b8e3f3a41715a7de7e32eb32456aa25132c8ff46
8+
refs/heads/try2: cb115ac2d4f57d8b590c8d46d8f9e2958ed9a527
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/strbuf.rs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use iter::{Extendable, FromIterator, Iterator, range};
1919
use mem;
2020
use option::{None, Option, Some};
2121
use ptr::RawPtr;
22+
use ptr;
2223
use slice::{OwnedVector, Vector, CloneableVector};
23-
use str::{OwnedStr, Str, StrSlice, StrAllocating};
24+
use str::{CharRange, OwnedStr, Str, StrSlice, StrAllocating};
2425
use str;
2526
use vec::Vec;
2627

@@ -215,19 +216,49 @@ impl StrBuf {
215216
Some(byte)
216217
}
217218

219+
/// Removes the last character from the string buffer and returns it. Returns `None` if this
220+
/// string buffer is empty.
221+
#[inline]
222+
pub fn pop_char(&mut self) -> Option<char> {
223+
let len = self.len();
224+
if len == 0 {
225+
return None
226+
}
227+
228+
let CharRange {ch, next} = self.as_slice().char_range_at_reverse(len);
229+
unsafe {
230+
self.vec.set_len(next);
231+
}
232+
Some(ch)
233+
}
234+
218235
/// Removes the first byte from the string buffer and returns it. Returns `None` if this string
219236
/// buffer is empty.
220237
///
221238
/// The caller must preserve the valid UTF-8 property.
222239
pub unsafe fn shift_byte(&mut self) -> Option<u8> {
240+
self.vec.shift()
241+
}
242+
243+
/// Removes the first character from the string buffer and returns it. Returns `None` if this
244+
/// string buffer is empty.
245+
///
246+
/// # Warning
247+
///
248+
/// This is a O(n) operation as it requires copying every element in the buffer.
249+
pub fn shift_char (&mut self) -> Option<char> {
223250
let len = self.len();
224251
if len == 0 {
225252
return None
226253
}
227254

228-
let byte = self.as_slice()[0];
229-
*self = self.as_slice().slice(1, len).into_strbuf();
230-
Some(byte)
255+
let CharRange {ch, next} = self.as_slice().char_range_at(0);
256+
let new_len = len - next;
257+
unsafe {
258+
ptr::copy_memory(self.vec.as_mut_ptr(), self.vec.as_ptr().offset(next as int), new_len);
259+
self.vec.set_len(new_len);
260+
}
261+
Some(ch)
231262
}
232263

233264
/// Views the string buffer as a mutable sequence of bytes.
@@ -357,6 +388,28 @@ mod tests {
357388
assert_eq!(data.as_slice(), "ประเทศไทย中华b¢€𤭢");
358389
}
359390

391+
#[test]
392+
fn test_pop_char() {
393+
let mut data = StrBuf::from_str("ประเทศไทย中华b¢€𤭢");
394+
assert_eq!(data.pop_char().unwrap(), '𤭢'); // 4 bytes
395+
assert_eq!(data.pop_char().unwrap(), '€'); // 3 bytes
396+
assert_eq!(data.pop_char().unwrap(), '¢'); // 2 bytes
397+
assert_eq!(data.pop_char().unwrap(), 'b'); // 1 bytes
398+
assert_eq!(data.pop_char().unwrap(), '华');
399+
assert_eq!(data.as_slice(), "ประเทศไทย中");
400+
}
401+
402+
#[test]
403+
fn test_shift_char() {
404+
let mut data = StrBuf::from_str("𤭢€¢b华ประเทศไทย中");
405+
assert_eq!(data.shift_char().unwrap(), '𤭢'); // 4 bytes
406+
assert_eq!(data.shift_char().unwrap(), '€'); // 3 bytes
407+
assert_eq!(data.shift_char().unwrap(), '¢'); // 2 bytes
408+
assert_eq!(data.shift_char().unwrap(), 'b'); // 1 bytes
409+
assert_eq!(data.shift_char().unwrap(), '华');
410+
assert_eq!(data.as_slice(), "ประเทศไทย中");
411+
}
412+
360413
#[test]
361414
fn test_str_truncate() {
362415
let mut s = StrBuf::from_str("12345");

0 commit comments

Comments
 (0)