Skip to content

Commit dc5ff64

Browse files
committed
Made more stuff pure.
escape functions in char, io.with_str_reader, base64 and md5sum, cell.empty_cell and is_empty.
1 parent e0805cb commit dc5ff64

File tree

5 files changed

+105
-101
lines changed

5 files changed

+105
-101
lines changed

src/libcore/char.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,18 @@ pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
126126
* - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
127127
* - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
128128
*/
129-
pub fn escape_unicode(c: char) -> ~str {
129+
pub pure fn escape_unicode(c: char) -> ~str {
130130
let s = u32::to_str(c as u32, 16u);
131131
let (c, pad) = (if c <= '\xff' { ('x', 2u) }
132132
else if c <= '\uffff' { ('u', 4u) }
133133
else { ('U', 8u) });
134134
assert str::len(s) <= pad;
135135
let mut out = ~"\\";
136-
str::push_str(&mut out, str::from_char(c));
137-
for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); }
138-
str::push_str(&mut out, s);
136+
unsafe {
137+
str::push_str(&mut out, str::from_char(c));
138+
for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); }
139+
str::push_str(&mut out, s);
140+
}
139141
move out
140142
}
141143

@@ -151,7 +153,7 @@ pub fn escape_unicode(c: char) -> ~str {
151153
* - Any other chars in the range [0x20,0x7e] are not escaped.
152154
* - Any other chars are given hex unicode escapes; see `escape_unicode`.
153155
*/
154-
pub fn escape_default(c: char) -> ~str {
156+
pub pure fn escape_default(c: char) -> ~str {
155157
match c {
156158
'\t' => ~"\\t",
157159
'\r' => ~"\\r",

src/libcore/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
512512
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
513513
}
514514

515-
pub fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
515+
pub pure fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
516516
str::byte_slice(s, |bytes| with_bytes_reader(bytes, f))
517517
}
518518

src/libstd/base64.rs

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,73 @@
22
use io::Reader;
33

44
pub trait ToBase64 {
5-
fn to_base64() -> ~str;
5+
pure fn to_base64() -> ~str;
66
}
77

88
impl &[u8]: ToBase64 {
9-
fn to_base64() -> ~str {
9+
pure fn to_base64() -> ~str {
1010
let chars = str::chars(
1111
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
1212
);
1313

14-
let len = self.len();
1514
let mut s = ~"";
16-
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
17-
18-
let mut i = 0u;
19-
20-
while i < len - (len % 3u) {
21-
let n = (self[i] as uint) << 16u |
22-
(self[i + 1u] as uint) << 8u |
23-
(self[i + 2u] as uint);
24-
25-
// This 24-bit number gets separated into four 6-bit numbers.
26-
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
27-
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
28-
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
29-
str::push_char(&mut s, chars[n & 63u]);
30-
31-
i += 3u;
32-
}
33-
34-
// Heh, would be cool if we knew this was exhaustive
35-
// (the dream of bounded integer types)
36-
match len % 3 {
37-
0 => (),
38-
1 => {
39-
let n = (self[i] as uint) << 16u;
40-
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
41-
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
42-
str::push_char(&mut s, '=');
43-
str::push_char(&mut s, '=');
44-
}
45-
2 => {
46-
let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u;
47-
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
48-
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
49-
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
50-
str::push_char(&mut s, '=');
51-
}
52-
_ => fail ~"Algebra is broken, please alert the math police"
15+
unsafe {
16+
let len = self.len();
17+
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
18+
19+
let mut i = 0u;
20+
21+
while i < len - (len % 3u) {
22+
let n = (self[i] as uint) << 16u |
23+
(self[i + 1u] as uint) << 8u |
24+
(self[i + 2u] as uint);
25+
26+
// This 24-bit number gets separated into four 6-bit numbers.
27+
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
28+
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
29+
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
30+
str::push_char(&mut s, chars[n & 63u]);
31+
32+
i += 3u;
33+
}
34+
35+
// Heh, would be cool if we knew this was exhaustive
36+
// (the dream of bounded integer types)
37+
match len % 3 {
38+
0 => (),
39+
1 => {
40+
let n = (self[i] as uint) << 16u;
41+
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
42+
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
43+
str::push_char(&mut s, '=');
44+
str::push_char(&mut s, '=');
45+
}
46+
2 => {
47+
let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u;
48+
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
49+
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
50+
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
51+
str::push_char(&mut s, '=');
52+
}
53+
_ => fail ~"Algebra is broken, please alert the math police"
54+
}
5355
}
54-
5556
s
5657
}
5758
}
5859

5960
impl &str: ToBase64 {
60-
fn to_base64() -> ~str {
61+
pure fn to_base64() -> ~str {
6162
str::to_bytes(self).to_base64()
6263
}
6364
}
6465

6566
pub trait FromBase64 {
66-
fn from_base64() -> ~[u8];
67+
pure fn from_base64() -> ~[u8];
6768
}
6869

6970
impl ~[u8]: FromBase64 {
70-
fn from_base64() -> ~[u8] {
71+
pure fn from_base64() -> ~[u8] {
7172
if self.len() % 4u != 0u { fail ~"invalid base64 length"; }
7273

7374
let len = self.len();
@@ -80,55 +81,56 @@ impl ~[u8]: FromBase64 {
8081

8182
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
8283

83-
let mut i = 0u;
84-
while i < len {
85-
let mut n = 0u;
86-
87-
for iter::repeat(4u) {
88-
let ch = self[i] as char;
89-
n <<= 6u;
90-
91-
if ch >= 'A' && ch <= 'Z' {
92-
n |= (ch as uint) - 0x41u;
93-
} else if ch >= 'a' && ch <= 'z' {
94-
n |= (ch as uint) - 0x47u;
95-
} else if ch >= '0' && ch <= '9' {
96-
n |= (ch as uint) + 0x04u;
97-
} else if ch == '+' {
98-
n |= 0x3Eu;
99-
} else if ch == '/' {
100-
n |= 0x3Fu;
101-
} else if ch == '=' {
102-
match len - i {
103-
1u => {
104-
r.push(((n >> 16u) & 0xFFu) as u8);
105-
r.push(((n >> 8u ) & 0xFFu) as u8);
106-
return copy r;
107-
}
108-
2u => {
109-
r.push(((n >> 10u) & 0xFFu) as u8);
110-
return copy r;
111-
}
112-
_ => fail ~"invalid base64 padding"
84+
unsafe {
85+
let mut i = 0u;
86+
while i < len {
87+
let mut n = 0u;
88+
89+
for iter::repeat(4u) {
90+
let ch = self[i] as char;
91+
n <<= 6u;
92+
93+
if ch >= 'A' && ch <= 'Z' {
94+
n |= (ch as uint) - 0x41u;
95+
} else if ch >= 'a' && ch <= 'z' {
96+
n |= (ch as uint) - 0x47u;
97+
} else if ch >= '0' && ch <= '9' {
98+
n |= (ch as uint) + 0x04u;
99+
} else if ch == '+' {
100+
n |= 0x3Eu;
101+
} else if ch == '/' {
102+
n |= 0x3Fu;
103+
} else if ch == '=' {
104+
match len - i {
105+
1u => {
106+
r.push(((n >> 16u) & 0xFFu) as u8);
107+
r.push(((n >> 8u ) & 0xFFu) as u8);
108+
return copy r;
109+
}
110+
2u => {
111+
r.push(((n >> 10u) & 0xFFu) as u8);
112+
return copy r;
113+
}
114+
_ => fail ~"invalid base64 padding"
115+
}
116+
} else {
117+
fail ~"invalid base64 character";
113118
}
114-
} else {
115-
fail ~"invalid base64 character";
116-
}
117-
118-
i += 1u;
119-
};
120-
121-
r.push(((n >> 16u) & 0xFFu) as u8);
122-
r.push(((n >> 8u ) & 0xFFu) as u8);
123-
r.push(((n ) & 0xFFu) as u8);
119+
120+
i += 1u;
121+
};
122+
123+
r.push(((n >> 16u) & 0xFFu) as u8);
124+
r.push(((n >> 8u ) & 0xFFu) as u8);
125+
r.push(((n ) & 0xFFu) as u8);
126+
}
124127
}
125-
126128
r
127129
}
128130
}
129131

130132
impl ~str: FromBase64 {
131-
fn from_base64() -> ~[u8] {
133+
pure fn from_base64() -> ~[u8] {
132134
str::to_bytes(self).from_base64()
133135
}
134136
}

src/libstd/cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn Cell<T>(value: T) -> Cell<T> {
1212
Cell { value: Some(move value) }
1313
}
1414

15-
pub fn empty_cell<T>() -> Cell<T> {
15+
pub pure fn empty_cell<T>() -> Cell<T> {
1616
Cell { value: None }
1717
}
1818

@@ -37,7 +37,7 @@ impl<T> Cell<T> {
3737
}
3838

3939
/// Returns true if the cell is empty and false if the cell is full.
40-
fn is_empty() -> bool {
40+
pure fn is_empty() -> bool {
4141
self.value.is_none()
4242
}
4343

src/libstd/md4.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[forbid(deprecated_mode)];
22

3-
pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
3+
pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
44
// subtle: if orig_len is merely uint, then the code below
55
// which performs shifts by 32 bits or more has undefined
66
// results.
@@ -10,14 +10,14 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
1010
let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]);
1111
let mut bitlen = orig_len + 8u64;
1212
while (bitlen + 64u64) % 512u64 > 0u64 {
13-
msg.push(0u8);
13+
unsafe {msg.push(0u8);}
1414
bitlen += 8u64;
1515
}
1616

1717
// append length
1818
let mut i = 0u64;
1919
while i < 8u64 {
20-
msg.push((orig_len >> (i * 8u64)) as u8);
20+
unsafe {msg.push((orig_len >> (i * 8u64)) as u8);}
2121
i += 1u64;
2222
}
2323

@@ -26,7 +26,7 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
2626
let mut c = 0x98badcfeu32;
2727
let mut d = 0x10325476u32;
2828

29-
fn rot(r: int, x: u32) -> u32 {
29+
pure fn rot(r: int, x: u32) -> u32 {
3030
let r = r as u32;
3131
(x << r) | (x >> (32u32 - r))
3232
}
@@ -84,9 +84,9 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
8484
return {a: a, b: b, c: c, d: d};
8585
}
8686

87-
pub fn md4_str(msg: &[u8]) -> ~str {
87+
pub pure fn md4_str(msg: &[u8]) -> ~str {
8888
let {a, b, c, d} = md4(msg);
89-
fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
89+
pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
9090
f(a); f(b); f(c); f(d);
9191
}
9292
let mut result = ~"";
@@ -102,7 +102,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
102102
result
103103
}
104104

105-
pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
105+
pub pure fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
106106

107107
#[test]
108108
fn test_md4() {

0 commit comments

Comments
 (0)