Debian Codegraph @

rpython/rtyper/lltypesystem/ll_str.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem.lltype import GcArray, Array, Char, malloc
2 from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
3 from rpython.rlib import jit
4
5 CHAR_ARRAY = GcArray(Char)
6
7 def ll_unsigned(i):
8 if isinstance(i, r_longlong) or isinstance(i, r_ulonglong):
9 return r_ulonglong(i)
10 else:
11 return r_uint(i)
12
13 @jit.elidable
14 def ll_int2dec(val):
15 from rpython.rtyper.lltypesystem.rstr import mallocstr
16
17 sign = int(val < 0)
18 if sign:
19 val = ll_unsigned(-val)
20 else:
21 val = ll_unsigned(val)
22 len = 0
23 i = val
24 while i:
25 len += 1
26 i //= 10
27
28 total_len = sign + len + int(val == 0)
29 result = mallocstr(total_len)
30 if sign:
31 result.chars[0] = '-'
32 elif val == 0:
33 result.chars[0] = '0'
34
35 j = 0
36 while j < len:
37 result.chars[total_len - j - 1] = chr(val % 10 + ord('0'))
38 val //= 10
39 j += 1
40 return result
41
42 hex_chars = malloc(Array(Char), 16, immortal=True)
43
44 for i in range(16):
45 hex_chars[i] = "%x" % i
46
47 @jit.elidable
48 def ll_int2hex(i, addPrefix):
49 from rpython.rtyper.lltypesystem.rstr import mallocstr
50 temp = malloc(CHAR_ARRAY, 20)
51 len = 0
52 sign = 0
53 if i < 0:
54 sign = 1
55 i = ll_unsigned(-i)
56 else:
57 i = ll_unsigned(i)
58 if i == 0:
59 len = 1
60 temp[0] = '0'
61 else:
62 while i:
63 temp[len] = hex_chars[i & 0xf]
64 i >>= 4
65 len += 1
66 len += sign
67 if addPrefix:
68 len += 2
69 result = mallocstr(len)
70 result.hash = 0
71 j = 0
72 if sign:
73 result.chars[0] = '-'
74 j = 1
75 if addPrefix:
76 result.chars[j] = '0'
77 result.chars[j+1] = 'x'
78 j += 2
79 while j < len:
80 result.chars[j] = temp[len-j-1]
81 j += 1
82 return result
83
84 @jit.elidable
85 def ll_int2oct(i, addPrefix):
86 from rpython.rtyper.lltypesystem.rstr import mallocstr
87 if i == 0:
88 result = mallocstr(1)
89 result.hash = 0
90 result.chars[0] = '0'
91 return result
92 temp = malloc(CHAR_ARRAY, 25)
93 len = 0
94 sign = 0
95 if i < 0:
96 sign = 1
97 i = ll_unsigned(-i)
98 else:
99 i = ll_unsigned(i)
100 while i:
101 temp[len] = hex_chars[i & 0x7]
102 i >>= 3
103 len += 1
104 len += sign
105 if addPrefix:
106 len += 1
107 result = mallocstr(len)
108 result.hash = 0
109 j = 0
110 if sign:
111 result.chars[0] = '-'
112 j = 1
113 if addPrefix:
114 result.chars[j] = '0'
115 j += 1
116 while j < len:
117 result.chars[j] = temp[len-j-1]
118 j += 1
119 return result
120
121 @jit.elidable
122 def ll_int2bin(i, addPrefix):
123 from rpython.rtyper.lltypesystem.rstr import mallocstr
124 temp = malloc(CHAR_ARRAY, 67)
125 len = 0
126 sign = 0
127 if i < 0:
128 sign = 1
129 i = ll_unsigned(-i)
130 else:
131 i = ll_unsigned(i)
132 if i == 0:
133 len = 1
134 temp[0] = '0'
135 else:
136 while i:
137 temp[len] = hex_chars[i & 0b1]
138 i >>= 1
139 len += 1
140 len += sign
141 if addPrefix:
142 len += 2
143 result = mallocstr(len)
144 result.hash = 0
145 j = 0
146 if sign:
147 result.chars[0] = '-'
148 j = 1
149 if addPrefix:
150 result.chars[j] = '0'
151 result.chars[j+1] = 'b'
152 j += 2
153 while j < len:
154 result.chars[j] = temp[len-j-1]
155 j += 1
156 return result