Skip to content

Commit 638258d

Browse files
Kimundibrson
authored andcommitted
---
yaml --- r: 41467 b: refs/heads/snap-stage3 c: 26e72bf h: refs/heads/master i: 41465: 92cd18c 41463: 24b32cb v: v3
1 parent 3a317bc commit 638258d

File tree

7 files changed

+134
-80
lines changed

7 files changed

+134
-80
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: 09bb07bed9166105ea961a42b5fff7739ae0d2e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 5b0335ec8e14ede924c45eac3b08c66191bfc04a
4+
refs/heads/snap-stage3: 26e72bf92bb0f9cf4d10a5edb07dbbd5c09f0e24
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/num/int-template.rs

Lines changed: 126 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use T = self::inst::T;
1717
use char;
1818
use cmp::{Eq, Ord};
1919
use cmp;
20+
use to_str::ToStr;
2021
use from_str::FromStr;
22+
use num::{ToStrRadix, FromStrRadix};
2123
use num;
2224
use num::Num::from_int;
2325
use prelude::*;
@@ -212,70 +214,87 @@ impl T: num::Round {
212214
pure fn fract(&self) -> T { 0 }
213215
}
214216
215-
/**
216-
* Parse a buffer of bytes
217-
*
218-
* # Arguments
219-
*
220-
* * buf - A byte buffer
221-
* * radix - The base of the number
222-
*/
223-
pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
224-
if vec::len(buf) == 0u { return None; }
225-
let mut i = vec::len(buf) - 1u;
226-
let mut start = 0u;
227-
let mut power = 1 as T;
228-
229-
if buf[0] == ('-' as u8) {
230-
power = -1 as T;
231-
start = 1u;
232-
}
233-
let mut n = 0 as T;
234-
loop {
235-
match char::to_digit(buf[i] as char, radix) {
236-
Some(d) => n += (d as T) * power,
237-
None => return None
238-
}
239-
power *= radix as T;
240-
if i <= start { return Some(n); }
241-
i -= 1u;
242-
};
217+
// String conversion functions and impl str -> num
218+
219+
/// Parse a string as a number in base 10.
220+
#[inline(always)]
221+
pub pure fn from_str(s: &str) -> Option<T> {
222+
num::from_str_common(s, 10u, true, false, false,
223+
num::ExpNone, false)
243224
}
244225
245-
/// Parse a string to an int
226+
/// Parse a string as a number in the given base.
246227
#[inline(always)]
247-
pub pure fn from_str(s: &str) -> Option<T>
248-
{
249-
parse_bytes(str::to_bytes(s), 10u)
228+
pub pure fn from_str_radix(s: &str, radix: uint) -> Option<T> {
229+
num::from_str_common(s, radix, true, false, false,
230+
num::ExpNone, false)
231+
}
232+
233+
/// Parse a byte slice as a number in the given base.
234+
#[inline(always)]
235+
pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
236+
num::from_str_bytes_common(buf, radix, true, false, false,
237+
num::ExpNone, false)
250238
}
251239
252240
impl T : FromStr {
253241
#[inline(always)]
254-
static pure fn from_str(s: &str) -> Option<T> { from_str(s) }
242+
static pure fn from_str(s: &str) -> Option<T> {
243+
from_str(s)
244+
}
255245
}
256246
257-
/// Convert to a string in a given base
258-
#[inline(always)]
259-
pub pure fn to_str(n: T, radix: uint) -> ~str {
260-
do to_str_bytes(n, radix) |slice| {
261-
do vec::as_imm_buf(slice) |p, len| {
262-
unsafe { str::raw::from_buf_len(p, len) }
263-
}
247+
impl T : FromStrRadix {
248+
#[inline(always)]
249+
static pure fn from_str_radix(&self, s: &str, radix: uint) -> Option<T> {
250+
from_str_radix(s, radix)
264251
}
265252
}
266253
254+
// String conversion functions and impl num -> str
255+
256+
/// Convert to a string as a byte slice in a given base.
267257
#[inline(always)]
268258
pub pure fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
269-
if n < 0 as T {
270-
uint::to_str_bytes(true, -n as uint, radix, f)
271-
} else {
272-
uint::to_str_bytes(false, n as uint, radix, f)
273-
}
259+
let (buf, _) = num::to_str_bytes_common(&n, radix, false, false,
260+
num::SignNeg, num::DigAll);
261+
f(buf)
274262
}
275263
276-
/// Convert to a string
264+
/// Convert to a string in base 10.
277265
#[inline(always)]
278-
pub pure fn str(i: T) -> ~str { return to_str(i, 10u); }
266+
pub pure fn to_str(num: T) -> ~str {
267+
let (buf, _) = num::to_str_common(&num, 10u, false, false,
268+
num::SignNeg, num::DigAll);
269+
buf
270+
}
271+
272+
/// Convert to a string in a given base.
273+
#[inline(always)]
274+
pub pure fn to_str_radix(num: T, radix: uint) -> ~str {
275+
let (buf, _) = num::to_str_common(&num, radix, false, false,
276+
num::SignNeg, num::DigAll);
277+
buf
278+
}
279+
280+
/// Convert to a string.
281+
/// *Deprecated*, use to_str() instead.
282+
#[inline(always)]
283+
pub pure fn str(i: T) -> ~str { to_str(i) }
284+
285+
impl T : ToStr {
286+
#[inline(always)]
287+
pure fn to_str() -> ~str {
288+
to_str(self)
289+
}
290+
}
291+
292+
impl T : ToStrRadix {
293+
#[inline(always)]
294+
pure fn to_str_radix(&self, radix: uint) -> ~str {
295+
to_str_radix(*self, radix)
296+
}
297+
}
279298
280299
#[test]
281300
fn test_from_str() {
@@ -322,11 +341,66 @@ fn test_parse_bytes() {
322341
323342
#[test]
324343
fn test_to_str() {
325-
assert (to_str(0 as T, 10u) == ~"0");
326-
assert (to_str(1 as T, 10u) == ~"1");
327-
assert (to_str(-1 as T, 10u) == ~"-1");
328-
assert (to_str(127 as T, 16u) == ~"7f");
329-
assert (to_str(100 as T, 10u) == ~"100");
344+
assert (to_str_radix(0 as T, 10u) == ~"0");
345+
assert (to_str_radix(1 as T, 10u) == ~"1");
346+
assert (to_str_radix(-1 as T, 10u) == ~"-1");
347+
assert (to_str_radix(127 as T, 16u) == ~"7f");
348+
assert (to_str_radix(100 as T, 10u) == ~"100");
349+
350+
}
351+
352+
#[test]
353+
fn test_int_to_str_overflow() {
354+
let mut i8_val: i8 = 127_i8;
355+
assert (i8::to_str(i8_val) == ~"127");
356+
357+
i8_val += 1 as i8;
358+
assert (i8::to_str(i8_val) == ~"-128");
359+
360+
let mut i16_val: i16 = 32_767_i16;
361+
assert (i16::to_str(i16_val) == ~"32767");
362+
363+
i16_val += 1 as i16;
364+
assert (i16::to_str(i16_val) == ~"-32768");
365+
366+
let mut i32_val: i32 = 2_147_483_647_i32;
367+
assert (i32::to_str(i32_val) == ~"2147483647");
368+
369+
i32_val += 1 as i32;
370+
assert (i32::to_str(i32_val) == ~"-2147483648");
371+
372+
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
373+
assert (i64::to_str(i64_val) == ~"9223372036854775807");
374+
375+
i64_val += 1 as i64;
376+
assert (i64::to_str(i64_val) == ~"-9223372036854775808");
377+
}
378+
379+
#[test]
380+
fn test_int_from_str_overflow() {
381+
let mut i8_val: i8 = 127_i8;
382+
assert (i8::from_str(~"127") == Some(i8_val));
383+
384+
i8_val += 1 as i8;
385+
assert (i8::from_str(~"-128") == Some(i8_val));
386+
387+
let mut i16_val: i16 = 32_767_i16;
388+
assert (i16::from_str(~"32767") == Some(i16_val));
389+
390+
i16_val += 1 as i16;
391+
assert (i16::from_str(~"-32768") == Some(i16_val));
392+
393+
let mut i32_val: i32 = 2_147_483_647_i32;
394+
assert (i32::from_str(~"2147483647") == Some(i32_val));
395+
396+
i32_val += 1 as i32;
397+
assert (i32::from_str(~"-2147483648") == Some(i32_val));
398+
399+
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
400+
assert (i64::from_str(~"9223372036854775807") == Some(i64_val));
401+
402+
i64_val += 1 as i64;
403+
assert (i64::from_str(~"-9223372036854775808") == Some(i64_val));
330404
}
331405
332406
#[test]

branches/snap-stage3/src/libcore/to_str.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ use vec;
2424

2525
pub trait ToStr { pub pure fn to_str() -> ~str; }
2626

27-
impl int: ToStr {
28-
#[inline(always)]
29-
pure fn to_str() -> ~str { ::int::str(self) }
30-
}
31-
impl i8: ToStr {
32-
#[inline(always)]
33-
pure fn to_str() -> ~str { ::i8::str(self) }
34-
}
35-
impl i16: ToStr {
36-
#[inline(always)]
37-
pure fn to_str() -> ~str { ::i16::str(self) }
38-
}
39-
impl i32: ToStr {
40-
#[inline(always)]
41-
pure fn to_str() -> ~str { ::i32::str(self) }
42-
}
43-
impl i64: ToStr {
44-
#[inline(always)]
45-
pure fn to_str() -> ~str { ::i64::str(self) }
46-
}
4727
impl uint: ToStr {
4828
#[inline(always)]
4929
pure fn to_str() -> ~str { ::uint::str(self) }

branches/snap-stage3/src/librustc/driver/driver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,21 @@ pub fn pretty_print_input(sess: Session, +cfg: ast::crate_cfg, input: input,
382382
match node {
383383
pprust::node_item(s, item) => {
384384
pp::space(s.s);
385-
pprust::synth_comment(s, int::to_str(item.id, 10u));
385+
pprust::synth_comment(s, int::to_str(item.id));
386386
}
387387
pprust::node_block(s, ref blk) => {
388388
pp::space(s.s);
389389
pprust::synth_comment(
390-
s, ~"block " + int::to_str((*blk).node.id, 10u));
390+
s, ~"block " + int::to_str((*blk).node.id));
391391
}
392392
pprust::node_expr(s, expr) => {
393393
pp::space(s.s);
394-
pprust::synth_comment(s, int::to_str(expr.id, 10u));
394+
pprust::synth_comment(s, int::to_str(expr.id));
395395
pprust::pclose(s);
396396
}
397397
pprust::node_pat(s, pat) => {
398398
pp::space(s.s);
399-
pprust::synth_comment(s, ~"pat " + int::to_str(pat.id, 10u));
399+
pprust::synth_comment(s, ~"pat " + int::to_str(pat.id));
400400
}
401401
}
402402
}

branches/snap-stage3/src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn encode_discriminant(ecx: @encode_ctxt, ebml_w: writer::Encoder,
245245
fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: writer::Encoder,
246246
disr_val: int) {
247247
ebml_w.start_tag(tag_disr_val);
248-
ebml_w.writer.write(str::to_bytes(int::to_str(disr_val,10u)));
248+
ebml_w.writer.write(str::to_bytes(int::to_str(disr_val)));
249249
ebml_w.end_tag();
250250
}
251251

branches/snap-stage3/src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ pub fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
679679
let variant_cx =
680680
sub_block(cx,
681681
~"enum-iter-variant-" +
682-
int::to_str(variant.disr_val, 10u));
682+
int::to_str(variant.disr_val));
683683
AddCase(llswitch, C_int(ccx, variant.disr_val), variant_cx.llbb);
684684
let variant_cx =
685685
iter_variant(variant_cx, llunion_a_ptr, *variant,

branches/snap-stage3/src/test/run-pass/reflect-visit-data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl my_visitor: TyVisitor {
517517
}
518518
fn visit_int(&self) -> bool {
519519
do self.get::<int>() |i| {
520-
self.vals += ~[int::to_str(i, 10u)];
520+
self.vals += ~[int::to_str(i)];
521521
};
522522
true
523523
}

0 commit comments

Comments
 (0)