Skip to content

Commit ff536f8

Browse files
committed
---
yaml --- r: 63095 b: refs/heads/snap-stage3 c: 239b81f h: refs/heads/master i: 63093: 85e6916 63091: 398f102 63087: 5a0f3fb v: v3
1 parent f9e4877 commit ff536f8

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 83b68a2f69471e0217422cf30d6c45ec4fe28b70
4+
refs/heads/snap-stage3: 239b81f111d522b1fbb0dbefdde16715ef68a204
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/doc/tutorial.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,8 @@ loop {
569569
This code prints out a weird sequence of numbers and stops as soon as
570570
it finds one that can be divided by five.
571571

572-
Rust also has a `for` construct. It's different from C's `for` and it works
573-
best when iterating over collections. See the section on [closures](#closures)
574-
to find out how to use `for` and higher-order functions for enumerating
575-
elements of a collection.
572+
For more involved iteration, such as enumerating the elements of a
573+
collection, Rust uses [higher-order functions](#closures).
576574

577575
# Data structures
578576

@@ -1395,7 +1393,6 @@ assert!(crayons.len() == 3);
13951393
assert!(!crayons.is_empty());
13961394
13971395
// Iterate over a vector, obtaining a pointer to each element
1398-
// (`for` is explained in the next section)
13991396
for crayons.each |crayon| {
14001397
let delicious_crayon_wax = unwrap_crayon(*crayon);
14011398
eat_crayon_wax(delicious_crayon_wax);

branches/snap-stage3/src/libstd/str.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ use vec::{OwnedVector, OwnedCopyableVector};
3939

4040
#[cfg(not(test))] use cmp::{Eq, Ord, Equiv, TotalEq};
4141

42+
/*
43+
Section: Conditions
44+
*/
45+
condition! {
46+
not_utf8: (~str) -> ~str;
47+
}
48+
4249
/*
4350
Section: Creating a string
4451
*/
@@ -48,11 +55,20 @@ Section: Creating a string
4855
*
4956
* # Failure
5057
*
51-
* Fails if invalid UTF-8
58+
* Raises the `not_utf8` condition if invalid UTF-8
5259
*/
53-
pub fn from_bytes(vv: &const [u8]) -> ~str {
54-
assert!(is_utf8(vv));
55-
return unsafe { raw::from_bytes(vv) };
60+
61+
pub fn from_bytes(vv: &[u8]) -> ~str {
62+
use str::not_utf8::cond;
63+
64+
if !is_utf8(vv) {
65+
let first_bad_byte = vec::find(vv, |b| !is_utf8([*b])).get();
66+
cond.raise(fmt!("from_bytes: input is not UTF-8; first bad byte is %u",
67+
first_bad_byte as uint))
68+
}
69+
else {
70+
return unsafe { raw::from_bytes(vv) }
71+
}
5672
}
5773

5874
/**
@@ -3563,9 +3579,10 @@ mod tests {
35633579
}
35643580
35653581
#[test]
3566-
#[should_fail]
35673582
#[ignore(cfg(windows))]
35683583
fn test_from_bytes_fail() {
3584+
use str::not_utf8::cond;
3585+
35693586
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
35703587
0xe0_u8, 0xb9_u8, 0x84_u8,
35713588
0xe0_u8, 0xb8_u8, 0x97_u8,
@@ -3577,7 +3594,15 @@ mod tests {
35773594
0x20_u8, 0x4e_u8, 0x61_u8,
35783595
0x6d_u8];
35793596
3580-
let _x = from_bytes(bb);
3597+
let mut error_happened = false;
3598+
let _x = do cond.trap(|err| {
3599+
assert_eq!(err, ~"from_bytes: input is not UTF-8; first bad byte is 255");
3600+
error_happened = true;
3601+
~""
3602+
}).in {
3603+
from_bytes(bb)
3604+
};
3605+
assert!(error_happened);
35813606
}
35823607
35833608
#[test]

0 commit comments

Comments
 (0)