Skip to content

Commit 96edfa4

Browse files
committed
---
yaml --- r: 15822 b: refs/heads/try c: cb6ed42 h: refs/heads/master v: v3
1 parent ad6e674 commit 96edfa4

File tree

6 files changed

+121
-167
lines changed

6 files changed

+121
-167
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 0e21a05e6c0bc0bfa905291a916b38bdb9daf90d
5+
refs/heads/try: cb6ed427174031f67f226da292aa85f8127f4513
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/core.rc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ mod i64 {
9595
mod uint {
9696
import inst::{
9797
div_ceil, div_round, div_floor, hash, iterate,
98-
next_power_of_two, parse_buf, from_str, to_str, str
98+
next_power_of_two
9999
};
100100
export div_ceil, div_round, div_floor, hash, iterate,
101-
next_power_of_two, parse_buf, from_str, to_str, str;
101+
next_power_of_two;
102102

103103
#[path = "uint.rs"]
104104
mod inst;
@@ -131,9 +131,6 @@ mod u32 {
131131
#[doc = "Operations and constants for `u64`"]
132132
#[path = "uint-template"]
133133
mod u64 {
134-
import inst::{ to_str, str, from_str };
135-
export to_str, str, from_str;
136-
137134
#[path = "u64.rs"]
138135
mod inst;
139136
}

branches/try/src/libcore/uint-template.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export is_positive, is_negative;
88
export is_nonpositive, is_nonnegative;
99
export range;
1010
export compl;
11+
export to_str, from_str, from_str_radix, str, parse_buf;
1112

1213
const min_value: T = 0 as T;
1314
const max_value: T = 0 as T - 1 as T;
@@ -43,3 +44,118 @@ fn range(lo: T, hi: T, it: fn(T)) {
4344
pure fn compl(i: T) -> T {
4445
max_value ^ i
4546
}
47+
48+
#[doc = "
49+
Parse a buffer of bytes
50+
51+
# Arguments
52+
53+
* buf - A byte buffer
54+
* radix - The base of the number
55+
56+
# Failure
57+
58+
`buf` must not be empty
59+
"]
60+
fn parse_buf(buf: [u8], radix: uint) -> option<T> {
61+
if vec::len(buf) == 0u { ret none; }
62+
let mut i = vec::len(buf) - 1u;
63+
let mut power = 1u as T;
64+
let mut n = 0u as T;
65+
loop {
66+
alt char::to_digit(buf[i] as char, radix) {
67+
some(d) { n += d as T * power; }
68+
none { ret none; }
69+
}
70+
power *= radix as T;
71+
if i == 0u { ret some(n); }
72+
i -= 1u;
73+
};
74+
}
75+
76+
#[doc = "Parse a string to an int"]
77+
fn from_str(s: str) -> option<T> { parse_buf(str::bytes(s), 10u) }
78+
79+
#[doc = "Parse a string as an unsigned integer."]
80+
fn from_str_radix(buf: str, radix: u64) -> option<u64> {
81+
if str::len(buf) == 0u { ret none; }
82+
let mut i = str::len(buf) - 1u;
83+
let mut power = 1u64, n = 0u64;
84+
loop {
85+
alt char::to_digit(buf[i] as char, radix as uint) {
86+
some(d) { n += d as u64 * power; }
87+
none { ret none; }
88+
}
89+
power *= radix;
90+
if i == 0u { ret some(n); }
91+
i -= 1u;
92+
};
93+
}
94+
95+
#[doc = "Convert to a string in a given base"]
96+
fn to_str(num: T, radix: uint) -> str {
97+
assert (0u < radix && radix <= 16u);
98+
let mut n = num;
99+
let radix = radix as T;
100+
fn digit(n: T) -> char {
101+
ret alt n {
102+
0u as T { '0' }
103+
1u as T { '1' }
104+
2u as T { '2' }
105+
3u as T { '3' }
106+
4u as T { '4' }
107+
5u as T { '5' }
108+
6u as T { '6' }
109+
7u as T { '7' }
110+
8u as T { '8' }
111+
9u as T { '9' }
112+
10u as T { 'a' }
113+
11u as T { 'b' }
114+
12u as T { 'c' }
115+
13u as T { 'd' }
116+
14u as T { 'e' }
117+
15u as T { 'f' }
118+
_ { fail }
119+
};
120+
}
121+
if n == 0u as T { ret "0"; }
122+
let mut s: str = "";
123+
while n != 0u as T {
124+
s += str::from_byte(digit(n % radix) as u8);
125+
n /= radix;
126+
}
127+
let mut s1: str = "";
128+
let mut len: uint = str::len(s);
129+
while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); }
130+
ret s1;
131+
}
132+
133+
#[doc = "Convert to a string"]
134+
fn str(i: T) -> str { ret to_str(i, 10u); }
135+
136+
#[test]
137+
fn test_from_str() {
138+
assert from_str("0") == some(0u as T);
139+
assert from_str("3") == some(3u as T);
140+
assert from_str("10") == some(10u as T);
141+
assert from_str("123456789") == some(123456789u as T);
142+
assert from_str("00100") == some(100u as T);
143+
144+
assert from_str("") == none;
145+
assert from_str(" ") == none;
146+
assert from_str("x") == none;
147+
}
148+
149+
#[test]
150+
fn test_parse_buf() {
151+
import str::bytes;
152+
assert parse_buf(bytes("123"), 10u) == some(123u as T);
153+
assert parse_buf(bytes("1001"), 2u) == some(9u as T);
154+
assert parse_buf(bytes("123"), 8u) == some(83u as T);
155+
assert parse_buf(bytes("123"), 16u) == some(291u as T);
156+
assert parse_buf(bytes("ffff"), 16u) == some(65535u as T);
157+
assert parse_buf(bytes("z"), 36u) == some(35u as T);
158+
159+
assert parse_buf(str::bytes("Z"), 10u) == none;
160+
assert parse_buf(str::bytes("_"), 2u) == none;
161+
}
Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1 @@
11
type T = u64;
2-
3-
// Type-specific functions here. These must be reexported by the
4-
// parent module so that they appear in core::u8 and not core::u8::u8;
5-
6-
7-
// FIXME: Surely we can generalize this to apply to all uint types
8-
#[doc = "Convert to a string in a given base"]
9-
fn to_str(n: u64, radix: uint) -> str {
10-
assert (0u < radix && radix <= 16u);
11-
12-
let r64 = radix as u64;
13-
14-
fn digit(n: u64) -> str {
15-
ret alt n {
16-
0u64 { "0" }
17-
1u64 { "1" }
18-
2u64 { "2" }
19-
3u64 { "3" }
20-
4u64 { "4" }
21-
5u64 { "5" }
22-
6u64 { "6" }
23-
7u64 { "7" }
24-
8u64 { "8" }
25-
9u64 { "9" }
26-
10u64 { "a" }
27-
11u64 { "b" }
28-
12u64 { "c" }
29-
13u64 { "d" }
30-
14u64 { "e" }
31-
15u64 { "f" }
32-
_ { fail }
33-
};
34-
}
35-
36-
if n == 0u64 { ret "0"; }
37-
38-
let mut s = "";
39-
40-
let mut n = n;
41-
while n > 0u64 { s = digit(n % r64) + s; n /= r64; }
42-
ret s;
43-
}
44-
45-
#[doc = "Convert to a string"]
46-
fn str(n: u64) -> str { ret to_str(n, 10u); }
47-
48-
#[doc = "Parse a string as an unsigned integer."]
49-
fn from_str(buf: str, radix: u64) -> option<u64> {
50-
if str::len(buf) == 0u { ret none; }
51-
let mut i = str::len(buf) - 1u;
52-
let mut power = 1u64, n = 0u64;
53-
loop {
54-
alt char::to_digit(buf[i] as char, radix as uint) {
55-
some(d) { n += d as u64 * power; }
56-
none { ret none; }
57-
}
58-
power *= radix;
59-
if i == 0u { ret some(n); }
60-
i -= 1u;
61-
};
62-
}

branches/try/src/libcore/uint-template/uint.rs

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -89,104 +89,6 @@ fn next_power_of_two(n: uint) -> uint {
8989
ret tmp + 1u;
9090
}
9191

92-
#[doc = "
93-
Parse a buffer of bytes
94-
95-
# Arguments
96-
97-
* buf - A byte buffer
98-
* radix - The base of the number
99-
100-
# Failure
101-
102-
`buf` must not be empty
103-
"]
104-
fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
105-
if vec::len(buf) == 0u { ret none; }
106-
let mut i = vec::len(buf) - 1u;
107-
let mut power = 1u;
108-
let mut n = 0u;
109-
loop {
110-
alt char::to_digit(buf[i] as char, radix) {
111-
some(d) { n += d * power; }
112-
none { ret none; }
113-
}
114-
power *= radix;
115-
if i == 0u { ret some(n); }
116-
i -= 1u;
117-
};
118-
}
119-
120-
#[doc = "Parse a string to an int"]
121-
fn from_str(s: str) -> option<uint> { parse_buf(str::bytes(s), 10u) }
122-
123-
#[doc = "Convert to a string in a given base"]
124-
fn to_str(num: uint, radix: uint) -> str {
125-
let mut n = num;
126-
assert (0u < radix && radix <= 16u);
127-
fn digit(n: uint) -> char {
128-
ret alt n {
129-
0u { '0' }
130-
1u { '1' }
131-
2u { '2' }
132-
3u { '3' }
133-
4u { '4' }
134-
5u { '5' }
135-
6u { '6' }
136-
7u { '7' }
137-
8u { '8' }
138-
9u { '9' }
139-
10u { 'a' }
140-
11u { 'b' }
141-
12u { 'c' }
142-
13u { 'd' }
143-
14u { 'e' }
144-
15u { 'f' }
145-
_ { fail }
146-
};
147-
}
148-
if n == 0u { ret "0"; }
149-
let mut s: str = "";
150-
while n != 0u {
151-
s += str::from_byte(digit(n % radix) as u8);
152-
n /= radix;
153-
}
154-
let mut s1: str = "";
155-
let mut len: uint = str::len(s);
156-
while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); }
157-
ret s1;
158-
}
159-
160-
#[doc = "Convert to a string"]
161-
fn str(i: uint) -> str { ret to_str(i, 10u); }
162-
163-
#[test]
164-
fn test_from_str() {
165-
assert uint::from_str("0") == some(0u);
166-
assert uint::from_str("3") == some(3u);
167-
assert uint::from_str("10") == some(10u);
168-
assert uint::from_str("123456789") == some(123456789u);
169-
assert uint::from_str("00100") == some(100u);
170-
171-
assert uint::from_str("") == none;
172-
assert uint::from_str(" ") == none;
173-
assert uint::from_str("x") == none;
174-
}
175-
176-
#[Test]
177-
fn test_parse_buf() {
178-
import str::bytes;
179-
assert uint::parse_buf(bytes("123"), 10u) == some(123u);
180-
assert uint::parse_buf(bytes("1001"), 2u) == some(9u);
181-
assert uint::parse_buf(bytes("123"), 8u) == some(83u);
182-
assert uint::parse_buf(bytes("123"), 16u) == some(291u);
183-
assert uint::parse_buf(bytes("ffff"), 16u) == some(65535u);
184-
assert uint::parse_buf(bytes("z"), 36u) == some(35u);
185-
186-
assert uint::parse_buf(str::bytes("Z"), 10u) == none;
187-
assert uint::parse_buf(str::bytes("_"), 2u) == none;
188-
}
189-
19092
#[test]
19193
fn test_next_power_of_two() {
19294
assert (uint::next_power_of_two(0u) == 0u);

branches/try/src/librustsyntax/parse/lexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn scan_number(c: char, rdr: reader) -> token::token {
223223
if str::len(num_str) == 0u {
224224
rdr.fatal("no valid digits found for number");
225225
}
226-
let parsed = option::get(u64::from_str(num_str, base as u64));
226+
let parsed = option::get(u64::from_str_radix(num_str, base as u64));
227227
alt tp {
228228
either::left(t) { ret token::LIT_INT(parsed as i64, t); }
229229
either::right(t) { ret token::LIT_UINT(parsed, t); }
@@ -271,7 +271,7 @@ fn scan_number(c: char, rdr: reader) -> token::token {
271271
if str::len(num_str) == 0u {
272272
rdr.fatal("no valid digits found for number");
273273
}
274-
let parsed = option::get(u64::from_str(num_str, base as u64));
274+
let parsed = option::get(u64::from_str_radix(num_str, base as u64));
275275
ret token::LIT_INT(parsed as i64, ast::ty_i);
276276
}
277277
}

0 commit comments

Comments
 (0)