~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/lltypesystem/ll_str.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pypy.rpython.lltypesystem.lltype import GcArray, Array, Char, malloc
 
2
from pypy.rlib.rarithmetic import r_uint
 
3
 
 
4
CHAR_ARRAY = GcArray(Char)
 
5
 
 
6
def ll_int_str(repr, i):
 
7
    return ll_int2dec(i)
 
8
 
 
9
def ll_int2dec(i):
 
10
    from pypy.rpython.lltypesystem.rstr import mallocstr
 
11
    temp = malloc(CHAR_ARRAY, 20)
 
12
    len = 0
 
13
    sign = 0
 
14
    if i < 0:
 
15
        sign = 1
 
16
        i = r_uint(-i)
 
17
    else:
 
18
        i = r_uint(i)
 
19
    if i == 0:
 
20
        len = 1
 
21
        temp[0] = '0'
 
22
    else:
 
23
        while i:
 
24
            temp[len] = chr(i%10+ord('0'))
 
25
            i //= 10
 
26
            len += 1
 
27
    len += sign
 
28
    result = mallocstr(len)
 
29
    result.hash = 0
 
30
    if sign:
 
31
        result.chars[0] = '-'
 
32
        j = 1
 
33
    else:
 
34
        j = 0
 
35
    while j < len:
 
36
        result.chars[j] = temp[len-j-1]
 
37
        j += 1
 
38
    return result
 
39
 
 
40
hex_chars = malloc(Array(Char), 16, immortal=True)
 
41
 
 
42
for i in range(16):
 
43
    hex_chars[i] = "%x"%i
 
44
 
 
45
def ll_int2hex(i, addPrefix):
 
46
    from pypy.rpython.lltypesystem.rstr import mallocstr
 
47
    temp = malloc(CHAR_ARRAY, 20)
 
48
    len = 0
 
49
    sign = 0
 
50
    if i < 0:
 
51
        sign = 1
 
52
        i = r_uint(-i)
 
53
    else:
 
54
        i = r_uint(i)
 
55
    if i == 0:
 
56
        len = 1
 
57
        temp[0] = '0'
 
58
    else:
 
59
        while i:
 
60
            temp[len] = hex_chars[i & 0xf]
 
61
            i >>= 4
 
62
            len += 1
 
63
    len += sign
 
64
    if addPrefix:
 
65
        len += 2
 
66
    result = mallocstr(len)
 
67
    result.hash = 0
 
68
    j = 0
 
69
    if sign:
 
70
        result.chars[0] = '-'
 
71
        j = 1
 
72
    if addPrefix:
 
73
        result.chars[j] = '0'
 
74
        result.chars[j+1] = 'x'
 
75
        j += 2
 
76
    while j < len:
 
77
        result.chars[j] = temp[len-j-1]
 
78
        j += 1
 
79
    return result
 
80
 
 
81
def ll_int2oct(i, addPrefix):
 
82
    from pypy.rpython.lltypesystem.rstr import mallocstr
 
83
    if i == 0:
 
84
        result = mallocstr(1)
 
85
        result.hash = 0
 
86
        result.chars[0] = '0'
 
87
        return result
 
88
    temp = malloc(CHAR_ARRAY, 25)
 
89
    len = 0
 
90
    sign = 0
 
91
    if i < 0:
 
92
        sign = 1
 
93
        i = r_uint(-i)
 
94
    else:
 
95
        i = r_uint(i)
 
96
    while i:
 
97
        temp[len] = hex_chars[i & 0x7]
 
98
        i >>= 3
 
99
        len += 1
 
100
    len += sign
 
101
    if addPrefix:
 
102
        len += 1
 
103
    result = mallocstr(len)
 
104
    result.hash = 0
 
105
    j = 0
 
106
    if sign:
 
107
        result.chars[0] = '-'
 
108
        j = 1
 
109
    if addPrefix:
 
110
        result.chars[j] = '0'
 
111
        j += 1
 
112
    while j < len:
 
113
        result.chars[j] = temp[len-j-1]
 
114
        j += 1
 
115
    return result
 
116
 
 
117
def ll_float_str(repr, f):
 
118
    from pypy.rpython.lltypesystem.module.ll_strtod import Implementation
 
119
    from pypy.rpython.lltypesystem.rstr import percent_f
 
120
    return Implementation.ll_strtod_formatd(percent_f, f)
 
121