about summary refs log tree commit diff
path: root/src/libstd/str.rs
AgeCommit message (Collapse)AuthorLines
2013-08-07Forbid `priv` where it has no effectAlex Crichton-10/+10
This is everywhere except struct fields and enum variants.
2013-08-07Merge remote-tracking branch 'remotes/origin/master' into ↵Erick Tryzelaar-11/+14
remove-str-trailing-nulls
2013-08-07auto merge of #8294 : erickt/rust/map-move, r=bblumbors-1/+1
According to #7887, we've decided to use the syntax of `fn map<U>(f: &fn(&T) -> U) -> U`, which passes a reference to the closure, and to `fn map_move<U>(f: &fn(T) -> U) -> U` which moves the value into the closure. This PR adds these `.map_move()` functions to `Option` and `Result`. In addition, it has these other minor features: * Replaces a couple uses of `option.get()`, `result.get()`, and `result.get_err()` with `option.unwrap()`, `result.unwrap()`, and `result.unwrap_err()`. (See #8268 and #8288 for a more thorough adaptation of this functionality. * Removes `option.take_map()` and `option.take_map_default()`. These two functions can be easily written as `.take().map_move(...)`. * Adds a better error message to `result.unwrap()` and `result.unwrap_err()`.
2013-08-07core: option.map_consume -> option.map_moveErick Tryzelaar-1/+1
2013-08-07auto merge of #8305 : huonw/rust/triage-fixes, r=cmrbors-11/+14
The two deletions are because the test cases are very old (still using `class` and modes!), and, as far as I can tell (since they are so old), the areas they test are well tested by other rpass tests.
2013-08-07std: adjust str::test_add so that the macro expands to all 3 items (#8012).Huon Wilson-11/+14
Closes #3682.
2013-08-06Merge remote-tracking branch 'remotes/origin/master' into ↵Erick Tryzelaar-3/+3
remove-str-trailing-nulls
2013-08-06std: update str.push_byte to work without str trailing nullsErick Tryzelaar-1/+10
2013-08-06Merge commit 'd89ff7eef969aee6b493bc846b64d68358fafbcd' into ↵Erick Tryzelaar-1/+8
remove-str-trailing-nulls
2013-08-06auto merge of #8308 : blake2-ppc/rust/str-slice-bytes, r=alexcrichtonbors-1/+1
`fn slice_bytes` is marked unsafe since it allows violating the valid string encoding property; but the function did also allow extending the lifetime of the slice by mistake, since it's returning `&str`. Use the annotation `slice_bytes<'a>(&'a str, ...) -> &'a str` so that all uses of `slice_bytes` are region checked correctly.
2013-08-05Updated std::Option, std::Either and std::ResultMarvin Löbel-2/+2
- Made naming schemes consistent between Option, Result and Either - Changed Options Add implementation to work like the maybe monad (return None if any of the inputs is None) - Removed duplicate Option::get and renamed all related functions to use the term `unwrap` instead
2013-08-05std: Use correct lifetime parameter on str::raw::slice_bytesblake2-ppc-1/+1
fn slice_bytes is marked unsafe since it allows violating the valid string encoding property; but the function did also allow extending the lifetime of the slice by mistake, since it's returning `&str`. Use the annotation `slice_bytes<'a>(&'a str, ...) -> &'a str` so that all uses of slice_bytes are region checked correctly.
2013-08-05auto merge of #8289 : sfackler/rust/push_byte, r=ericktbors-1/+8
It was previously pushing the byte on top of the string's null terminator. I added a test to make sure it doesn't break in the future.
2013-08-04Merge remote-tracking branch 'remotes/origin/master' into str-remove-nullErick Tryzelaar-67/+99
2013-08-04Remove trailing null from stringsErick Tryzelaar-10/+374
2013-08-04std: merge str::raw::from_buf and str::raw::from_c_strErick Tryzelaar-22/+12
2013-08-04std: replace str::as_c_str with std::c_strErick Tryzelaar-98/+0
2013-08-04std: remove str::from_bytes_with_nullErick Tryzelaar-82/+0
2013-08-04std: add test for str::as_c_strErick Tryzelaar-0/+23
2013-08-04std: remove str::NullTerminatedStrErick Tryzelaar-54/+1
2013-08-04std: add str.to_c_str()Erick Tryzelaar-0/+26
2013-08-04Fixed str::raw::push_byteSteven Fackler-1/+8
It was previously pushing the byte on top of the string's null terminator. I added a test to make sure it doesn't break in the future.
2013-08-04auto merge of #8237 : blake2-ppc/rust/faster-utf8, r=brsonbors-35/+67
Use unchecked vec indexing since the vector bounds are checked by the loop. Iterators are not easy to use in this case since we skip 1-4 bytes each lap. This part of the commit speeds up is_utf8 for ASCII input. Check codepoint ranges by checking the byte ranges manually instead of computing a full decoding for multibyte encodings. This is easy to read and corresponds to the UTF-8 syntax in the RFC. No changes to what we accept. A comment notes that surrogate halves are accepted. Before: test str::bench::is_utf8_100_ascii ... bench: 165 ns/iter (+/- 3) test str::bench::is_utf8_100_multibyte ... bench: 218 ns/iter (+/- 5) After: test str::bench::is_utf8_100_ascii ... bench: 130 ns/iter (+/- 1) test str::bench::is_utf8_100_multibyte ... bench: 156 ns/iter (+/- 3) An improvement upon the previous pull #8133
2013-08-03remove obsolete `foreach` keywordDaniel Micay-30/+30
this has been replaced by `for`
2013-08-02std: Speed up str::is_utf8blake2-ppc-35/+67
Use unchecked vec indexing since the vector bounds are checked by the loop. Iterators are not easy to use in this case since we skip 1-4 bytes each lap. This part of the commit speeds up is_utf8 for ASCII input. Check codepoint ranges by checking the byte ranges manually instead of computing a full decoding for multibyte encodings. This is easy to read and corresponds to the UTF-8 syntax in the RFC. No changes to what we accept. A comment notes that surrogate halves are accepted. Before: test str::bench::is_utf8_100_ascii ... bench: 165 ns/iter (+/- 3) test str::bench::is_utf8_100_multibyte ... bench: 218 ns/iter (+/- 5) After: test str::bench::is_utf8_100_ascii ... bench: 130 ns/iter (+/- 1) test str::bench::is_utf8_100_multibyte ... bench: 156 ns/iter (+/- 3)
2013-08-01str: Add method .into_owned(self) -> ~str to StrKevin Ballard-0/+12
The method .into_owned() is meant to be used as an optimization when you need to get a ~str from a Str, but don't want to unnecessarily copy it if it's already a ~str. This is meant to ease functions that look like fn foo<S: Str>(strs: &[S]) Previously they could work with the strings as slices using .as_slice(), but producing ~str required copying the string, even if the vector turned out be a &[~str] already.
2013-08-01std: Change `Times` trait to use `do` instead of `for`blake2-ppc-1/+1
Change the former repetition:: for 5.times { } to:: do 5.times { } .times() cannot be broken with `break` or `return` anymore; for those cases, use a numerical range loop instead.
2013-08-01migrate many `for` loops to `foreach`Daniel Micay-31/+32
2013-08-01make `in` and `foreach` get treated as keywordsDaniel Micay-1/+1
2013-07-30std: Mark the static constants in str.rs as privateblake2-ppc-10/+10
static variables are pub by default, which is not reflected in our code (we need to use priv).
2013-07-30std: Add from_bytes test for utf-8 using codepoints above 0xffffblake2-ppc-0/+3
2013-07-30std: Deny overlong encodings in UTF-8blake2-ppc-8/+45
An 'overlong encoding' is a codepoint encoded non-minimally using the utf-8 format. Denying these enforce each codepoint to have only one valid representation in utf-8. An example is byte sequence 0xE0 0x80 0x80 which could be interpreted as U+0, but it's an overlong encoding since the canonical form is just 0x00. Another example is 0xE0 0x80 0xAF which was previously accepted and is an overlong encoding of the solidus "/". Directory traversal characters like / and . form the most compelling argument for why this commit is security critical. Factor out common UTF-8 decoding expressions as macros. This commit will partly duplicate UTF-8 decoding, so it is now present in both fn is_utf8() and .char_range_at(); the latter using an assumption of a valid str.
2013-07-30std: Disallow bytes 0xC0, 0xC1 (192, 193) in utf-8blake2-ppc-1/+1
Bytes 0xC0, 0xC1 can only be used to start 2-byte codepoint encodings, that are 'overlong encodings' of codepoints below 128. The reference given in a comment -- https://tools.ietf.org/html/rfc3629 -- does in fact already exclude these bytes, so no additional comment should be needed in the code.
2013-07-30auto merge of #8121 : thestinger/rust/offset, r=alexcrichtonbors-19/+19
Closes #8118, #7136 ~~~rust extern mod extra; use std::vec; use std::ptr; fn bench_from_elem(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = vec::from_elem(1024, 0u8); } } fn bench_set_memory(b: &mut extra::test::BenchHarness) { do b.iter { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { let vp = vec::raw::to_mut_ptr(v); ptr::set_memory(vp, 0, 1024); vec::raw::set_len(&mut v, 1024); } } } fn bench_vec_repeat(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = ~[0u8, ..1024]; } } ~~~ Before: test bench_from_elem ... bench: 415 ns/iter (+/- 17) test bench_set_memory ... bench: 85 ns/iter (+/- 4) test bench_vec_repeat ... bench: 83 ns/iter (+/- 3) After: test bench_from_elem ... bench: 84 ns/iter (+/- 2) test bench_set_memory ... bench: 84 ns/iter (+/- 5) test bench_vec_repeat ... bench: 84 ns/iter (+/- 3)
2013-07-30Added str::char_offset_iter() and str::rev_char_offset_iter()Marvin Löbel-590/+489
Renamed bytes_iter to byte_iter to match other iterators Refactored str Iterators to use DoubleEnded Iterators and typedefs instead of wrapper structs Reordered the Iterator section Whitespace fixup Moved clunky `each_split_within` function to the one place in the tree where it's actually needed Replaced all block doccomments in str with line doccomments
2013-07-30implement pointer arithmetic with GEPDaniel Micay-19/+19
Closes #8118, #7136 ~~~rust extern mod extra; use std::vec; use std::ptr; fn bench_from_elem(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = vec::from_elem(1024, 0u8); } } fn bench_set_memory(b: &mut extra::test::BenchHarness) { do b.iter { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { let vp = vec::raw::to_mut_ptr(v); ptr::set_memory(vp, 0, 1024); vec::raw::set_len(&mut v, 1024); } } } fn bench_vec_repeat(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = ~[0u8, ..1024]; } } ~~~ Before: test bench_from_elem ... bench: 415 ns/iter (+/- 17) test bench_set_memory ... bench: 85 ns/iter (+/- 4) test bench_vec_repeat ... bench: 83 ns/iter (+/- 3) After: test bench_from_elem ... bench: 84 ns/iter (+/- 2) test bench_set_memory ... bench: 84 ns/iter (+/- 5) test bench_vec_repeat ... bench: 84 ns/iter (+/- 3)
2013-07-30std: Implement Extendable for hashmap, str and trieblake2-ppc-3/+24
2013-07-29std: Rename Iterator adaptor types to drop the -Iterator suffixblake2-ppc-4/+3
Drop the "Iterator" suffix for the the structs in std::iterator. Filter, Zip, Chain etc. are shorter type names for when iterator pipelines need their types written out in full in return value types, so it's easier to read and write. the iterator module already forms enough namespace.
2013-07-29std: Implement FromIterator for ~strblake2-ppc-1/+23
FromIterator initially only implemented for Iterator<char>, which is the type of the main iterator.
2013-07-28Refactored vec and str iterators to remove prefixesjmgrosen-45/+45
2013-07-27auto merge of #8036 : sfackler/rust/container-impls, r=msullivanbors-8/+0
A couple of implementations of Container::is_empty weren't exactly self.len() == 0 so I left them alone (e.g. Treemap).
2013-07-26Consolidate raw representations of rust valuesAlex Crichton-29/+31
This moves the raw struct layout of closures, vectors, boxes, and strings into a new `unstable::raw` module. This is meant to be a centralized location to find information for the layout of these values. As safe method, `repr`, is provided to convert a rust value to its raw representation. Unsafe methods to convert back are not provided because they are rarely used and too numerous to write an implementation for each (not much of a common pattern).
2013-07-25Added default impls for container methodsSteven Fackler-8/+0
A couple of implementations of Container::is_empty weren't exactly self.len() == 0 so I left them alone (e.g. Treemap).
2013-07-24auto merge of #7996 : erickt/rust/cleanup-strs, r=ericktbors-178/+152
This is a cleanup pull request that does: * removes `os::as_c_charp` * moves `str::as_buf` and `str::as_c_str` into `StrSlice` * converts some functions from `StrSlice::as_buf` to `StrSlice::as_c_str` * renames `StrSlice::as_buf` to `StrSlice::as_imm_buf` (and adds `StrSlice::as_mut_buf` to match `vec.rs`. * renames `UniqueStr::as_bytes_with_null_consume` to `UniqueStr::to_bytes` * and other misc cleanups and minor optimizations
2013-07-24Change 'print(fmt!(...))' to printf!/printfln! in src/lib*Birunthan Mohanathas-1/+1
2013-07-23std: make str::append move selfErick Tryzelaar-6/+5
This eliminates a copy and fixes a FIXME.
2013-07-23std: inline str::with_capacity and vec::with_capacityErick Tryzelaar-5/+3
2013-07-23std: simplify str::as_imm_buf and vec::as_{imm,mut}_bufErick Tryzelaar-5/+2
2013-07-23str: move as_mut_buf into OwnedStr, and make it `self`Erick Tryzelaar-18/+18
2013-07-23std: remove str::to_owned and str::raw::slice_bytes_ownedErick Tryzelaar-41/+22