+2
-2
| [package] | ||
| name = "arrayvec" | ||
| version = "0.3.20" | ||
| version = "0.3.21" | ||
| authors = ["bluss"] | ||
@@ -14,3 +14,3 @@ license = "MIT/Apache-2.0" | ||
| [dependencies.odds] | ||
| version = "0.2.12" | ||
| version = "0.2.23" | ||
| default-features = false | ||
@@ -17,0 +17,0 @@ |
+10
-0
@@ -25,2 +25,7 @@ | ||
| - 0.3.21 | ||
| - Use ``encode_utf8`` from crate odds | ||
| - Add constructor ``ArrayString::from_byte_string`` | ||
| - 0.3.20 | ||
@@ -94,2 +99,7 @@ | ||
| - 0.1.9 | ||
| - Fix issue in recent nightly where ``repr(u8)`` did not work. Use | ||
| a better way to get rid of the enum layout optimization. | ||
| - 0.1.8 | ||
@@ -96,0 +106,0 @@ |
+20
-2
@@ -8,8 +8,9 @@ use std::borrow::Borrow; | ||
| use std::str; | ||
| use std::str::Utf8Error; | ||
| use std::slice; | ||
| use array::Array; | ||
| use array::{Array, ArrayExt}; | ||
| use array::Index; | ||
| use CapacityError; | ||
| use char_ext::encode_utf8; | ||
| use odds::char::encode_utf8; | ||
@@ -71,2 +72,19 @@ /// A string with a fixed capacity. | ||
| /// Create a new `ArrayString` from a byte string literal. | ||
| /// | ||
| /// **Errors** if the byte string literal is not valid UTF-8. | ||
| /// | ||
| /// ``` | ||
| /// use arrayvec::ArrayString; | ||
| /// | ||
| /// let string = ArrayString::from_byte_string(b"hello world").unwrap(); | ||
| /// ``` | ||
| pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> { | ||
| let mut arraystr = Self::new(); | ||
| let s = try!(str::from_utf8(b.as_slice())); | ||
| let _result = arraystr.push_str(s); | ||
| debug_assert!(_result.is_ok()); | ||
| Ok(arraystr) | ||
| } | ||
| /// Return the capacity of the `ArrayString`. | ||
@@ -73,0 +91,0 @@ /// |
+13
-0
@@ -22,2 +22,15 @@ | ||
| use std::slice::{from_raw_parts}; | ||
| pub trait ArrayExt : Array { | ||
| #[inline(always)] | ||
| fn as_slice(&self) -> &[Self::Item] { | ||
| unsafe { | ||
| from_raw_parts(self.as_ptr(), Self::capacity()) | ||
| } | ||
| } | ||
| } | ||
| impl<A> ArrayExt for A where A: Array { } | ||
| #[cfg(feature = "use_generic_array")] | ||
@@ -24,0 +37,0 @@ unsafe impl<T, U> Array for ::generic_array::GenericArray<T, U> |
+0
-1
@@ -59,3 +59,2 @@ //! **arrayvec** provides the types `ArrayVec` and `ArrayString`: | ||
| mod array_string; | ||
| mod char_ext; | ||
@@ -62,0 +61,0 @@ pub use array::Array; |
+8
-0
@@ -350,2 +350,10 @@ extern crate arrayvec; | ||
| #[test] | ||
| fn test_string_from_bytes() { | ||
| let text = "hello world"; | ||
| let u = ArrayString::from_byte_string(b"hello world").unwrap(); | ||
| assert_eq!(&u, text); | ||
| assert_eq!(u.len(), text.len()); | ||
| } | ||
| #[test] | ||
| fn test_string_clone() { | ||
@@ -352,0 +360,0 @@ let text = "hi"; |
| // Copyright 2012-2016 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. | ||
| // | ||
| // Original authors: alexchrichton | ||
| // UTF-8 ranges and tags for encoding characters | ||
| const TAG_CONT: u8 = 0b1000_0000; | ||
| const TAG_TWO_B: u8 = 0b1100_0000; | ||
| const TAG_THREE_B: u8 = 0b1110_0000; | ||
| const TAG_FOUR_B: u8 = 0b1111_0000; | ||
| const MAX_ONE_B: u32 = 0x80; | ||
| const MAX_TWO_B: u32 = 0x800; | ||
| const MAX_THREE_B: u32 = 0x10000; | ||
| /// Placeholder | ||
| pub struct EncodeError; | ||
| /// Encode a char into buf | ||
| #[inline] | ||
| pub fn encode_utf8(ch: char, buf: &mut [u8]) -> Result<usize, EncodeError> | ||
| { | ||
| let code = ch as u32; | ||
| if code < MAX_ONE_B && buf.len() >= 1 { | ||
| buf[0] = code as u8; | ||
| return Ok(1); | ||
| } else if code < MAX_TWO_B && buf.len() >= 2 { | ||
| buf[0] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B; | ||
| buf[1] = (code & 0x3F) as u8 | TAG_CONT; | ||
| return Ok(2); | ||
| } else if code < MAX_THREE_B && buf.len() >= 3 { | ||
| buf[0] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B; | ||
| buf[1] = (code >> 6 & 0x3F) as u8 | TAG_CONT; | ||
| buf[2] = (code & 0x3F) as u8 | TAG_CONT; | ||
| return Ok(3); | ||
| } else if buf.len() >= 4 { | ||
| buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B; | ||
| buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT; | ||
| buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT; | ||
| buf[3] = (code & 0x3F) as u8 | TAG_CONT; | ||
| return Ok(4); | ||
| }; | ||
| Err(EncodeError) | ||
| } | ||