1 //
2 // Copyright (c) 2006, Brian Frank and Andy Frank
3 // Licensed under the Academic Free License version 3.0
4 //
5 // History:
6 // 2 Dec 05 Brian Frank Creation
7 //
8
9 **
10 ** Int is used to represent a signed 64-bit integer.
11 **
12 const final class Int : Num
13 {
14
15 //////////////////////////////////////////////////////////////////////////
16 // Constructor
17 //////////////////////////////////////////////////////////////////////////
18
19 **
20 ** Parse a Str into a Int using the specified radix.
21 ** If invalid format and checked is false return null,
22 ** otherwise throw ParseErr.
23 **
24 static Int fromStr(Str s, Int radix := 10, Bool checked := true)
25
26 **
27 ** Generate a random number. If range is null then all 2^64
28 ** integer values (both negative and positive) are produced with
29 ** equal probability. If range is non-null, then the result
30 ** is guaranteed to be inclusive of the range.
31 **
32 ** Examples:
33 ** r := Int.random
34 ** r := Int.random(0..100)
35 **
36 static Int random(Range r := null)
37
38 **
39 ** Private constructor.
40 **
41 private new make()
42
43 //////////////////////////////////////////////////////////////////////////
44 // Obj Overrides
45 //////////////////////////////////////////////////////////////////////////
46
47 **
48 ** Return true if same integer value.
49 **
50 override Bool equals(Obj obj)
51
52 **
53 ** Compare based on integer value.
54 **
55 override Int compare(Obj obj)
56
57 **
58 ** Return this.
59 **
60 override Int hash()
61
62 //////////////////////////////////////////////////////////////////////////
63 // Operations
64 //////////////////////////////////////////////////////////////////////////
65
66 **
67 ** Negative of this. Shortcut is -a.
68 **
69 Int negate()
70
71 **
72 ** Bitwise inverse of this. Shortcut is ~a.
73 **
74 Int tilde()
75
76 **
77 ** Multiply this with b. Shortcut is a*b.
78 **
79 Int star(Int b)
80
81 **
82 ** Divide this by b. Shortcut is a/b.
83 **
84 Int slash(Int b)
85
86 **
87 ** Return remainder of this divided by b. Shortcut is a%b.
88 **
89 Int percent(Int b)
90
91 **
92 ** Add this with b. Shortcut is a+b.
93 **
94 Int plus(Int b)
95
96 **
97 ** Subtract b from this. Shortcut is a-b.
98 **
99 Int minus(Int b)
100
101 **
102 ** Bitwise-and of this and b. Shortcut is a&b.
103 **
104 Int amp(Int b)
105
106 **
107 ** Bitwise-or of this and b. Shortcut is a|b.
108 **
109 Int pipe(Int b)
110
111 **
112 ** Bitwise-exclusive-or of this and b. Shortcut is a^b.
113 **
114 Int caret(Int b)
115
116 **
117 ** Bitwise left shift of this by b. Shortcut is a<<b.
118 **
119 Int lshift(Int b)
120
121 **
122 ** Bitwise right shift of this by b. Shortcut is a>>b.
123 **
124 Int rshift(Int b)
125
126 **
127 ** Increment by one. Shortcut is ++a or a++.
128 **
129 Int increment()
130
131 **
132 ** Decrement by one. Shortcut is --a or a--.
133 **
134 Int decrement()
135
136 /////////////////////////////////////////////////////////////////////////
137 // Num
138 //////////////////////////////////////////////////////////////////////////
139
140 **
141 ** Return this.
142 **
143 override Int toInt()
144
145 **
146 ** Convert this Int to a Float.
147 **
148 override Float toFloat()
149
150 /////////////////////////////////////////////////////////////////////////
151 // Math
152 //////////////////////////////////////////////////////////////////////////
153
154 **
155 ** Return the absolute value of this integer. If this value is
156 ** positive then return this, otherwise return the negation.
157 **
158 Int abs()
159
160 **
161 ** Return the smaller of this and the specified Int values.
162 **
163 Int min(Int that)
164
165 **
166 ** Return the larger of this and the specified Int values.
167 **
168 Int max(Int that)
169
170 **
171 ** Return if this integer is evenly divisible by two.
172 **
173 Bool isEven()
174
175 **
176 ** Return if this integer is not evenly divisible by two.
177 **
178 Bool isOdd()
179
180 /////////////////////////////////////////////////////////////////////////
181 // Char
182 //////////////////////////////////////////////////////////////////////////
183
184 **
185 ** Return if this Unicode char is whitespace: space \t \n \r \f
186 **
187 Bool isSpace()
188
189 **
190 ** Return if this Unicode char is an ASCII alpha char: isUpper||isLower
191 **
192 Bool isAlpha()
193
194 **
195 ** Return if this Unicode char is an ASCII alpha-numeric char: isAlpha||isDigit
196 **
197 Bool isAlphaNum()
198
199 **
200 ** Return if this Unicode char is an ASCII uppercase alphabetic char: A-Z
201 **
202 Bool isUpper()
203
204 **
205 ** Return if this Unicode char is an ASCII lowercase alphabetic char: a-z
206 **
207 Bool isLower()
208
209 **
210 ** If this Unicode char is an ASCII lowercase char, then return
211 ** it as uppercase, otherwise return this.
212 **
213 ** Example:
214 ** 'a'.upper -> 'A'
215 ** '4'.upper -> '4'
216 **
217 Int upper()
218
219 **
220 ** If this Unicode char is an ASCII uppercase char, then return
221 ** it as lowercase, otherwise return this.
222 **
223 ** Example:
224 ** 'A'.lower -> 'a'
225 ** 'h'.lower -> 'h'
226 **
227 Int lower()
228
229 **
230 ** Return if this Unicode char is an digit in the specified radix.
231 ** A decimal radix of ten returns true for '0'-'9'. A radix of 16
232 ** also returns true for 'a'-'f' and 'A'-'F'.
233 **
234 ** Example:
235 ** '3'.toDigit -> true
236 ** 3.toDigit -> false
237 ** 'B'.toDigit(16) -> true
238 **
239 Bool isDigit(Int radix := 10)
240
241 **
242 ** Convert this number into a Unicode char '0'-'9'. If radix is
243 ** is greater than 10, then use a lower case letter. Return null if
244 ** this number cannot be represented as a single digit character for
245 ** the specified radix.
246 **
247 ** Example:
248 ** 3.toDigit -> '3'
249 ** 15.toDigit(16) -> 'f'
250 ** 99.toDigit -> null
251 **
252 Int toDigit(Int radix := 10)
253
254 **
255 ** Convert a Unicode digit character into a number for the specified
256 ** radix. Return null if this char is not a valid digit.
257 **
258 ** Example:
259 ** '3'.fromDigit -> 3
260 ** 'f'.fromDigit(16) -> 15
261 ** '%'.fromDigit -> null
262 **
263 Int fromDigit(Int radix := 10)
264
265 /////////////////////////////////////////////////////////////////////////
266 // Locale
267 //////////////////////////////////////////////////////////////////////////
268
269 **
270 ** Return if this Unicode char is an uppercase letter in
271 ** the current locale. See also `localeIsLower` and `isUpper`.
272 **
273 Bool localeIsUpper()
274
275 **
276 ** Return if this Unicode char is a lowercase letter in
277 ** the current locale. See also `localeIsUpper` and `isLower`.
278 **
279 Bool localeIsLower()
280
281 **
282 ** If this Unicode char is a lowercase char, then return
283 ** it as uppercase according to the current locale. Note that
284 ** Unicode contains some case conversion rules that don't work
285 ** correctly on a single character, so `Str.localeLower` should
286 ** be preferred. See also `localeLower` and `upper`.
287 **
288 Int localeUpper()
289
290 **
291 ** If this Unicode char is an uppercase char, then return
292 ** it as lowercase according to the current locale. Note that
293 ** Unicode contains some case conversion rules that don't work
294 ** correctly on a single character, so `Str.localeLower` should
295 ** be preferred. See also `localeUpper` and `lower`.
296 **
297 Int localeLower()
298
299 /////////////////////////////////////////////////////////////////////////
300 // Conversion
301 //////////////////////////////////////////////////////////////////////////
302
303 **
304 ** Return decimal string representation.
305 **
306 override Str toStr()
307
308 **
309 ** Return hexdecimal string representation.
310 **
311 Str toHex()
312
313 **
314 ** Map as a Unicode code point to a single character Str.
315 **
316 Str toChar()
317
318 /////////////////////////////////////////////////////////////////////////
319 // Closures
320 //////////////////////////////////////////////////////////////////////////
321
322 **
323 ** Call the specified function to this times passing the current counter.
324 **
325 Void times(|Int i| c)
326
327 }