about summary refs log tree commit diff
path: root/src/libcore/u64.rs
blob: 0e1a330c6631176565a0f9d144ef565d69e68583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Module: u64
*/

/*
Const: min_value

Return the minimal value for a u64
*/
const min_value: u64 = 0u64;

/*
Const: max_value

Return the maximal value for a u64
*/
const max_value: u64 = 18446744073709551615u64;

/*
Function: to_str

Convert to a string in a given base
*/
fn to_str(n: u64, radix: uint) -> str {
    assert (0u < radix && radix <= 16u);

    let r64 = radix as u64;

    fn digit(n: u64) -> str {
        ret alt n {
              0u64 { "0" }
              1u64 { "1" }
              2u64 { "2" }
              3u64 { "3" }
              4u64 { "4" }
              5u64 { "5" }
              6u64 { "6" }
              7u64 { "7" }
              8u64 { "8" }
              9u64 { "9" }
              10u64 { "a" }
              11u64 { "b" }
              12u64 { "c" }
              13u64 { "d" }
              14u64 { "e" }
              15u64 { "f" }
              _ { fail }
            };
    }

    if n == 0u64 { ret "0"; }

    let s = "";

    let n = n;
    while n > 0u64 { s = digit(n % r64) + s; n /= r64; }
    ret s;
}

/*
Function: str

Convert to a string
*/
fn str(n: u64) -> str { ret to_str(n, 10u); }

/*
Function: parse_buf

Parse a string as an unsigned integer.
*/
fn from_str(buf: str, radix: u64) -> u64 {
    if str::byte_len(buf) == 0u {
        log_err "parse_buf(): buf is empty";
        fail;
    }
    let i = str::byte_len(buf) - 1u;
    let power = 1u64, n = 0u64;
    while true {
        let digit = char::to_digit(buf[i] as char) as u64;
        if digit >= radix { fail; }
        n += digit * power;
        power *= radix;
        if i == 0u { ret n; }
        i -= 1u;
    }
    fail;
}