2014-04-02 18:54:22 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! An owned, growable string that enforces that its contents are valid UTF-8.
|
|
|
|
|
|
|
|
use c_vec::CVec;
|
|
|
|
use char::Char;
|
2014-05-16 12:45:16 -05:00
|
|
|
use cmp::Equiv;
|
2014-05-11 05:49:09 -05:00
|
|
|
use container::{Container, Mutable};
|
2014-05-20 01:19:56 -05:00
|
|
|
use default::Default;
|
2014-04-02 18:54:22 -05:00
|
|
|
use fmt;
|
2014-05-19 19:23:26 -05:00
|
|
|
use from_str::FromStr;
|
2014-04-02 18:54:22 -05:00
|
|
|
use io::Writer;
|
|
|
|
use iter::{Extendable, FromIterator, Iterator, range};
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
use mem;
|
2014-04-02 18:54:22 -05:00
|
|
|
use option::{None, Option, Some};
|
|
|
|
use ptr::RawPtr;
|
2014-05-08 15:42:40 -05:00
|
|
|
use ptr;
|
2014-05-14 18:55:24 -05:00
|
|
|
use result::{Result, Ok, Err};
|
2014-05-19 19:23:26 -05:00
|
|
|
use slice::Vector;
|
|
|
|
use str::{CharRange, Str, StrSlice, StrAllocating};
|
2014-04-10 05:55:34 -05:00
|
|
|
use str;
|
2014-04-02 18:54:22 -05:00
|
|
|
use vec::Vec;
|
|
|
|
|
2014-04-10 05:55:34 -05:00
|
|
|
/// A growable string stored as a UTF-8 encoded buffer.
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq, PartialOrd, TotalEq, TotalOrd)]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub struct String {
|
2014-04-02 18:54:22 -05:00
|
|
|
vec: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl String {
|
2014-04-20 23:49:39 -05:00
|
|
|
/// Creates a new string buffer initialized with the empty string.
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn new() -> String {
|
|
|
|
String {
|
2014-04-02 18:54:22 -05:00
|
|
|
vec: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new string buffer with the given capacity.
|
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn with_capacity(capacity: uint) -> String {
|
|
|
|
String {
|
2014-04-02 18:54:22 -05:00
|
|
|
vec: Vec::with_capacity(capacity),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new string buffer from length, capacity, and a pointer.
|
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub unsafe fn from_raw_parts(length: uint, capacity: uint, ptr: *mut u8) -> String {
|
|
|
|
String {
|
2014-04-02 18:54:22 -05:00
|
|
|
vec: Vec::from_raw_parts(length, capacity, ptr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new string buffer from the given string.
|
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn from_str(string: &str) -> String {
|
|
|
|
String {
|
2014-04-02 18:54:22 -05:00
|
|
|
vec: Vec::from_slice(string.as_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 23:59:56 -05:00
|
|
|
#[allow(missing_doc)]
|
|
|
|
#[deprecated = "obsoleted by the removal of ~str"]
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn from_owned_str(string: String) -> String {
|
2014-05-19 19:23:26 -05:00
|
|
|
string
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-14 18:55:24 -05:00
|
|
|
/// Returns the vector as a string buffer, if possible, taking care not to
|
|
|
|
/// copy it.
|
|
|
|
///
|
|
|
|
/// Returns `Err` with the original vector if the vector contains invalid
|
|
|
|
/// UTF-8.
|
2014-04-10 05:55:34 -05:00
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn from_utf8(vec: Vec<u8>) -> Result<String, Vec<u8>> {
|
2014-04-10 05:55:34 -05:00
|
|
|
if str::is_utf8(vec.as_slice()) {
|
2014-05-22 18:57:53 -05:00
|
|
|
Ok(String { vec: vec })
|
2014-04-10 05:55:34 -05:00
|
|
|
} else {
|
2014-05-14 18:55:24 -05:00
|
|
|
Err(vec)
|
2014-04-10 05:55:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the underlying byte buffer, encoded as UTF-8.
|
|
|
|
#[inline]
|
|
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
|
|
self.vec
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
/// Pushes the given string onto this buffer; then, returns `self` so that it can be used
|
|
|
|
/// again.
|
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn append(mut self, second: &str) -> String {
|
2014-04-02 18:54:22 -05:00
|
|
|
self.push_str(second);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a string buffer by repeating a character `length` times.
|
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn from_char(length: uint, ch: char) -> String {
|
2014-04-02 18:54:22 -05:00
|
|
|
if length == 0 {
|
2014-05-22 18:57:53 -05:00
|
|
|
return String::new()
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut buf = String::new();
|
2014-04-02 18:54:22 -05:00
|
|
|
buf.push_char(ch);
|
|
|
|
let size = buf.len() * length;
|
|
|
|
buf.reserve(size);
|
|
|
|
for _ in range(1, length) {
|
|
|
|
buf.push_char(ch)
|
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes the given string onto this string buffer.
|
|
|
|
#[inline]
|
|
|
|
pub fn push_str(&mut self, string: &str) {
|
|
|
|
self.vec.push_all(string.as_bytes())
|
|
|
|
}
|
|
|
|
|
2014-04-10 05:55:34 -05:00
|
|
|
/// Push `ch` onto the given string `count` times.
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn grow(&mut self, count: uint, ch: char) {
|
|
|
|
for _ in range(0, count) {
|
|
|
|
self.push_char(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of bytes that this string buffer can hold without reallocating.
|
|
|
|
#[inline]
|
|
|
|
pub fn byte_capacity(&self) -> uint {
|
|
|
|
self.vec.capacity()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves capacity for at least `extra` additional bytes in this string buffer.
|
|
|
|
#[inline]
|
|
|
|
pub fn reserve_additional(&mut self, extra: uint) {
|
|
|
|
self.vec.reserve_additional(extra)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves capacity for at least `capacity` bytes in this string buffer.
|
|
|
|
#[inline]
|
|
|
|
pub fn reserve(&mut self, capacity: uint) {
|
|
|
|
self.vec.reserve(capacity)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves capacity for exactly `capacity` bytes in this string buffer.
|
|
|
|
#[inline]
|
|
|
|
pub fn reserve_exact(&mut self, capacity: uint) {
|
|
|
|
self.vec.reserve_exact(capacity)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shrinks the capacity of this string buffer to match its length.
|
|
|
|
#[inline]
|
|
|
|
pub fn shrink_to_fit(&mut self) {
|
|
|
|
self.vec.shrink_to_fit()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the given character to the end of the string.
|
|
|
|
#[inline]
|
|
|
|
pub fn push_char(&mut self, ch: char) {
|
|
|
|
let cur_len = self.len();
|
|
|
|
unsafe {
|
|
|
|
// This may use up to 4 bytes.
|
|
|
|
self.vec.reserve_additional(4);
|
|
|
|
|
|
|
|
// Attempt to not use an intermediate buffer by just pushing bytes
|
|
|
|
// directly onto this string.
|
|
|
|
let mut c_vector = CVec::new(self.vec.as_mut_ptr().offset(cur_len as int), 4);
|
|
|
|
let used = ch.encode_utf8(c_vector.as_mut_slice());
|
|
|
|
self.vec.set_len(cur_len + used);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes the given bytes onto this string buffer. This is unsafe because it does not check
|
|
|
|
/// to ensure that the resulting string will be valid UTF-8.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn push_bytes(&mut self, bytes: &[u8]) {
|
|
|
|
self.vec.push_all(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Works with the underlying buffer as a byte slice.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.vec.as_slice()
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:23:26 -05:00
|
|
|
/// Works with the underlying buffer as a mutable byte slice. Unsafe
|
|
|
|
/// because this can be used to violate the UTF-8 property.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn as_mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
|
|
|
|
self.vec.as_mut_slice()
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
/// Shorten a string to the specified length (which must be <= the current length)
|
|
|
|
#[inline]
|
|
|
|
pub fn truncate(&mut self, len: uint) {
|
|
|
|
assert!(self.as_slice().is_char_boundary(len));
|
|
|
|
self.vec.truncate(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends a byte to this string buffer. The caller must preserve the valid UTF-8 property.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn push_byte(&mut self, byte: u8) {
|
|
|
|
self.push_bytes([byte])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the last byte from the string buffer and returns it. Returns `None` if this string
|
|
|
|
/// buffer is empty.
|
|
|
|
///
|
|
|
|
/// The caller must preserve the valid UTF-8 property.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn pop_byte(&mut self) -> Option<u8> {
|
|
|
|
let len = self.len();
|
|
|
|
if len == 0 {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
let byte = self.as_slice()[len - 1];
|
|
|
|
self.vec.set_len(len - 1);
|
|
|
|
Some(byte)
|
|
|
|
}
|
|
|
|
|
2014-05-08 15:42:40 -05:00
|
|
|
/// Removes the last character from the string buffer and returns it. Returns `None` if this
|
|
|
|
/// string buffer is empty.
|
|
|
|
#[inline]
|
|
|
|
pub fn pop_char(&mut self) -> Option<char> {
|
|
|
|
let len = self.len();
|
|
|
|
if len == 0 {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
let CharRange {ch, next} = self.as_slice().char_range_at_reverse(len);
|
|
|
|
unsafe {
|
|
|
|
self.vec.set_len(next);
|
|
|
|
}
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
/// Removes the first byte from the string buffer and returns it. Returns `None` if this string
|
|
|
|
/// buffer is empty.
|
|
|
|
///
|
|
|
|
/// The caller must preserve the valid UTF-8 property.
|
|
|
|
pub unsafe fn shift_byte(&mut self) -> Option<u8> {
|
2014-05-08 15:42:40 -05:00
|
|
|
self.vec.shift()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the first character from the string buffer and returns it. Returns `None` if this
|
|
|
|
/// string buffer is empty.
|
|
|
|
///
|
|
|
|
/// # Warning
|
|
|
|
///
|
|
|
|
/// This is a O(n) operation as it requires copying every element in the buffer.
|
|
|
|
pub fn shift_char (&mut self) -> Option<char> {
|
2014-04-02 18:54:22 -05:00
|
|
|
let len = self.len();
|
|
|
|
if len == 0 {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
2014-05-08 15:42:40 -05:00
|
|
|
let CharRange {ch, next} = self.as_slice().char_range_at(0);
|
|
|
|
let new_len = len - next;
|
|
|
|
unsafe {
|
|
|
|
ptr::copy_memory(self.vec.as_mut_ptr(), self.vec.as_ptr().offset(next as int), new_len);
|
|
|
|
self.vec.set_len(new_len);
|
|
|
|
}
|
|
|
|
Some(ch)
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
2014-04-12 07:44:31 -05:00
|
|
|
|
|
|
|
/// Views the string buffer as a mutable sequence of bytes.
|
|
|
|
///
|
|
|
|
/// Callers must preserve the valid UTF-8 property.
|
|
|
|
pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
|
|
|
|
&mut self.vec
|
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl Container for String {
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> uint {
|
|
|
|
self.vec.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl Mutable for String {
|
2014-05-11 05:49:09 -05:00
|
|
|
#[inline]
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.vec.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl FromIterator<char> for String {
|
|
|
|
fn from_iter<I:Iterator<char>>(iterator: I) -> String {
|
|
|
|
let mut buf = String::new();
|
2014-04-02 18:54:22 -05:00
|
|
|
buf.extend(iterator);
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl Extendable<char> for String {
|
2014-04-02 18:54:22 -05:00
|
|
|
fn extend<I:Iterator<char>>(&mut self, mut iterator: I) {
|
|
|
|
for ch in iterator {
|
|
|
|
self.push_char(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl Str for String {
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
|
|
|
fn as_slice<'a>(&'a self) -> &'a str {
|
|
|
|
unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
mem::transmute(self.vec.as_slice())
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-01 01:06:36 -05:00
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl StrAllocating for String {
|
2014-04-12 07:44:31 -05:00
|
|
|
#[inline]
|
2014-05-25 05:17:19 -05:00
|
|
|
fn into_string(self) -> String {
|
2014-05-19 19:23:26 -05:00
|
|
|
self
|
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl Default for String {
|
|
|
|
fn default() -> String {
|
|
|
|
String::new()
|
2014-05-20 01:19:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl fmt::Show for String {
|
2014-04-02 18:54:22 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.as_slice().fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl<H:Writer> ::hash::Hash<H> for String {
|
2014-04-02 18:54:22 -05:00
|
|
|
#[inline]
|
|
|
|
fn hash(&self, hasher: &mut H) {
|
|
|
|
self.as_slice().hash(hasher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl<'a, S: Str> Equiv<S> for String {
|
2014-05-16 12:45:16 -05:00
|
|
|
#[inline]
|
|
|
|
fn equiv(&self, other: &S) -> bool {
|
|
|
|
self.as_slice() == other.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
impl FromStr for String {
|
2014-05-19 19:23:26 -05:00
|
|
|
#[inline]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn from_str(s: &str) -> Option<String> {
|
2014-05-25 05:17:19 -05:00
|
|
|
Some(s.to_string())
|
2014-05-19 19:23:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate test;
|
2014-05-11 05:49:09 -05:00
|
|
|
use container::{Container, Mutable};
|
2014-03-31 20:16:35 -05:00
|
|
|
use self::test::Bencher;
|
2014-04-02 18:54:22 -05:00
|
|
|
use str::{Str, StrSlice};
|
2014-05-22 18:57:53 -05:00
|
|
|
use super::String;
|
2014-04-02 18:54:22 -05:00
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_with_capacity(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-05-22 18:57:53 -05:00
|
|
|
String::with_capacity(100)
|
2014-04-02 18:54:22 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_push_str(b: &mut Bencher) {
|
2014-04-02 18:54:22 -05:00
|
|
|
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut r = String::new();
|
2014-04-02 18:54:22 -05:00
|
|
|
r.push_str(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_bytes() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("ABC");
|
2014-04-02 18:54:22 -05:00
|
|
|
unsafe {
|
|
|
|
s.push_bytes([ 'D' as u8 ]);
|
|
|
|
}
|
|
|
|
assert_eq!(s.as_slice(), "ABCD");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_str() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::new();
|
2014-04-02 18:54:22 -05:00
|
|
|
s.push_str("");
|
|
|
|
assert_eq!(s.as_slice().slice_from(0), "");
|
|
|
|
s.push_str("abc");
|
|
|
|
assert_eq!(s.as_slice().slice_from(0), "abc");
|
|
|
|
s.push_str("ประเทศไทย中华Việt Nam");
|
|
|
|
assert_eq!(s.as_slice().slice_from(0), "abcประเทศไทย中华Việt Nam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_char() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut data = String::from_str("ประเทศไทย中");
|
2014-04-02 18:54:22 -05:00
|
|
|
data.push_char('华');
|
|
|
|
data.push_char('b'); // 1 byte
|
|
|
|
data.push_char('¢'); // 2 byte
|
|
|
|
data.push_char('€'); // 3 byte
|
|
|
|
data.push_char('𤭢'); // 4 byte
|
|
|
|
assert_eq!(data.as_slice(), "ประเทศไทย中华b¢€𤭢");
|
|
|
|
}
|
|
|
|
|
2014-05-08 15:42:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_pop_char() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
|
2014-05-08 15:42:40 -05:00
|
|
|
assert_eq!(data.pop_char().unwrap(), '𤭢'); // 4 bytes
|
|
|
|
assert_eq!(data.pop_char().unwrap(), '€'); // 3 bytes
|
|
|
|
assert_eq!(data.pop_char().unwrap(), '¢'); // 2 bytes
|
|
|
|
assert_eq!(data.pop_char().unwrap(), 'b'); // 1 bytes
|
|
|
|
assert_eq!(data.pop_char().unwrap(), '华');
|
|
|
|
assert_eq!(data.as_slice(), "ประเทศไทย中");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_shift_char() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut data = String::from_str("𤭢€¢b华ประเทศไทย中");
|
2014-05-08 15:42:40 -05:00
|
|
|
assert_eq!(data.shift_char().unwrap(), '𤭢'); // 4 bytes
|
|
|
|
assert_eq!(data.shift_char().unwrap(), '€'); // 3 bytes
|
|
|
|
assert_eq!(data.shift_char().unwrap(), '¢'); // 2 bytes
|
|
|
|
assert_eq!(data.shift_char().unwrap(), 'b'); // 1 bytes
|
|
|
|
assert_eq!(data.shift_char().unwrap(), '华');
|
|
|
|
assert_eq!(data.as_slice(), "ประเทศไทย中");
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_str_truncate() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("12345");
|
2014-04-02 18:54:22 -05:00
|
|
|
s.truncate(5);
|
|
|
|
assert_eq!(s.as_slice(), "12345");
|
|
|
|
s.truncate(3);
|
|
|
|
assert_eq!(s.as_slice(), "123");
|
|
|
|
s.truncate(0);
|
|
|
|
assert_eq!(s.as_slice(), "");
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("12345");
|
2014-04-02 18:54:22 -05:00
|
|
|
let p = s.as_slice().as_ptr();
|
|
|
|
s.truncate(3);
|
|
|
|
s.push_str("6");
|
|
|
|
let p_ = s.as_slice().as_ptr();
|
|
|
|
assert_eq!(p_, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_str_truncate_invalid_len() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("12345");
|
2014-04-02 18:54:22 -05:00
|
|
|
s.truncate(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_str_truncate_split_codepoint() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("\u00FC"); // ü
|
2014-04-02 18:54:22 -05:00
|
|
|
s.truncate(1);
|
|
|
|
}
|
2014-05-11 05:49:09 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_str_clear() {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str("12345");
|
2014-05-11 05:49:09 -05:00
|
|
|
s.clear();
|
|
|
|
assert_eq!(s.len(), 0);
|
|
|
|
assert_eq!(s.as_slice(), "");
|
|
|
|
}
|
2014-04-02 18:54:22 -05:00
|
|
|
}
|