This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathfoundation-number.cpp
More file actions
392 lines (296 loc) · 10.8 KB
/
foundation-number.cpp
File metadata and controls
392 lines (296 loc) · 10.8 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-stdlib.h>
#include <errno.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
// Forward declarations of internal utility functions
static bool __MCNumberParseNativeString(const char *p_string, uindex_t p_length, bool p_full_string, bool p_integer_only, uindex_t &r_length_used, MCNumberRef &r_number);
static bool __MCNumberParseOffset(MCStringRef p_string, uindex_t offset, uindex_t char_count, bool p_integer_only, MCNumberRef &r_number);
static bool __MCNumberParseUnicodeChars(const unichar_t *p_chars, uindex_t p_char_count, bool p_integer_only, MCNumberRef& r_number);
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCNumberCreateWithInteger(integer_t p_value, MCNumberRef& r_number)
{
__MCNumber *self;
if (!__MCValueCreate(kMCValueTypeCodeNumber, self))
return false;
self -> integer = p_value;
r_number = self;
return true;
}
MC_DLLEXPORT_DEF
bool MCNumberCreateWithReal(real64_t p_value, MCNumberRef& r_number)
{
__MCNumber *self;
if (!__MCValueCreate(kMCValueTypeCodeNumber, self))
return false;
self -> real = p_value;
self -> flags |= (1 << 0);
r_number = self;
return true;
}
MC_DLLEXPORT_DEF
bool MCNumberCreateWithUnsignedInteger(uinteger_t p_value, MCNumberRef& r_number)
{
if (p_value <= INTEGER_MAX)
return MCNumberCreateWithInteger((integer_t)p_value, r_number);
return MCNumberCreateWithReal((real64_t)p_value, r_number);
}
MC_DLLEXPORT_DEF
bool MCNumberIsInteger(MCNumberRef self)
{
__MCAssertIsNumber(self);
return (self -> flags & kMCNumberFlagIsReal) == 0;
}
MC_DLLEXPORT_DEF
bool MCNumberIsReal(MCNumberRef self)
{
__MCAssertIsNumber(self);
return (self -> flags & kMCNumberFlagIsReal) != 0;
}
MC_DLLEXPORT_DEF
real64_t MCNumberFetchAsReal(MCNumberRef self)
{
if (MCNumberIsReal(self))
return self -> real;
return (real64_t)self -> integer;
}
MC_DLLEXPORT_DEF
integer_t MCNumberFetchAsInteger(MCNumberRef self)
{
if (MCNumberIsInteger(self))
return self -> integer;
return self -> real < 0.0 ? (integer_t)(self -> real - 0.5) : (integer_t)(self -> real + 0.5);
}
MC_DLLEXPORT_DEF
uinteger_t MCNumberFetchAsUnsignedInteger(MCNumberRef self)
{
if (MCNumberIsInteger(self))
return self -> integer >= 0 ? self -> integer : 0;
return self -> real >= 0.0 ? (uinteger_t)(self -> real + 0.5) : (uinteger_t)0.0;
}
MC_DLLEXPORT_DEF
bool MCNumberStrictFetchAsIndex(MCNumberRef self,
index_t& r_index)
{
static_assert(sizeof(index_t) == sizeof(integer_t), "index_t is not the same size as integer_t");
if (MCNumberIsInteger(self))
{
r_index = self->integer;
return true;
}
index_t t_as_int = (index_t)(self -> real);
if (self->real - t_as_int != 0.0)
{
return false;
}
r_index = t_as_int;
return true;
}
compare_t MCNumberCompareTo(MCNumberRef self, MCNumberRef p_other_self)
{
// First determine the storage types of both numbers.
bool t_self_is_integer, t_other_self_is_integer;
t_self_is_integer = MCNumberIsInteger(self);
t_other_self_is_integer = MCNumberIsInteger(p_other_self);
// If both are stored as integers then compare.
if (t_self_is_integer && t_other_self_is_integer)
return self -> integer - p_other_self -> integer;
// Otherwise fetch both as reals.
double x, y;
x = t_self_is_integer ? (double)self -> integer : self -> real;
y = t_other_self_is_integer ? (double)p_other_self -> integer : p_other_self -> real;
// TODO: Handle nan / infinity / etc.
if (x < y)
return -1;
if (x > y)
return 1;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCNumberParseOffset(MCStringRef p_string, uindex_t offset, uindex_t char_count, MCNumberRef &r_number)
{
return __MCNumberParseOffset(p_string, offset, char_count, false, r_number);
}
MC_DLLEXPORT_DEF
bool MCNumberParse(MCStringRef p_string, MCNumberRef &r_number)
{
return __MCNumberParseOffset(p_string, 0, MCStringGetLength(p_string), false, r_number);
}
MC_DLLEXPORT_DEF
bool MCNumberParseInteger(MCStringRef p_string, MCNumberRef &r_number)
{
return __MCNumberParseOffset(p_string, 0, MCStringGetLength(p_string), true, r_number);
}
MC_DLLEXPORT_DEF
bool MCNumberParseUnicodeChars(const unichar_t *p_chars, uindex_t p_char_count, MCNumberRef& r_number)
{
return __MCNumberParseUnicodeChars(p_chars, p_char_count, false, r_number);
}
MC_DLLEXPORT_DEF
bool MCNumberParseOffsetPartial(MCStringRef p_string, uindex_t offset, uindex_t &r_chars_used, MCNumberRef &r_number)
{
bool t_success;
t_success = true;
char *t_buffer;
t_buffer = nil;
const char *t_native_string;
t_native_string = nil;
uindex_t t_length;
t_length = MCStringGetLength(p_string);
if (offset > t_length)
offset = t_length;
if (MCStringIsNative(p_string))
t_native_string = (const char*)MCStringGetNativeCharPtr(p_string) + offset;
else
{
t_success = MCMemoryNewArray(t_length - offset + 1, t_buffer);
uindex_t t_native_char_count;
if (t_success)
t_success = MCUnicodeCharsMapToNative(MCStringGetCharPtr(p_string) + offset, t_length - offset, (char_t*)t_buffer, t_native_char_count, '?');
t_native_string = t_buffer;
}
if (t_success)
t_success = __MCNumberParseNativeString(t_native_string, t_length - offset, false, false, r_chars_used, r_number);
MCMemoryDeleteArray(t_buffer);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
static bool __MCNumberParseNativeString(const char *p_string, uindex_t p_length, bool p_full_string, bool p_integer_only, uindex_t &r_length_used, MCNumberRef &r_number)
{
bool t_success;
t_success = true;
MCNumberRef t_number;
t_number = nil;
uinteger_t t_base;
t_base = 10;
const char *t_string;
t_string = p_string;
if (!p_integer_only)
{
if (p_length > 2 &&
p_string[0] == '0' &&
(p_string[1] == 'x' || p_string[1] == 'X'))
{
// If the string begins with 0x then parse as hex, and discard first two chars
t_base = 16;
t_string += 2;
}
}
errno = 0;
char *t_end;
t_end = nil;
integer_t t_integer;
#if defined(__LP64__)
long t_long;
t_long = strtol(t_string, &t_end, t_base);
if (t_long > UINTEGER_MAX)
errno = ERANGE;
t_integer = (integer_t)t_long;
#elif defined(__LP32__) || defined(__LLP64__)
t_integer = strtol(t_string, &t_end, t_base);
#endif
t_success = (errno != ERANGE) && (p_full_string ? (t_end - p_string == (ptrdiff_t)p_length) : (t_end != t_string));
if (!t_success && p_integer_only)
return false;
if (t_success)
t_success = MCNumberCreateWithInteger(t_integer, t_number);
// If parsing as base 10 unsigned integer failed, try to parse as real.
else if (t_base == 10)
{
errno = 0;
real64_t t_real;
t_real = strtod(p_string, &t_end);
// SN-2014-10-06: [[ Bug 13594 ]] check that no error was encountered
t_success = (errno != ERANGE) && (p_full_string ? (t_end - p_string == (ptrdiff_t)p_length) : (t_end != t_string));
if (t_success)
t_success = MCNumberCreateWithReal(t_real, t_number);
}
if (t_success)
{
r_number = t_number;
r_length_used = t_end - p_string;
}
return t_success;
}
static bool __MCNumberParseOffset(MCStringRef p_string, uindex_t offset, uindex_t char_count, bool p_integer_only, MCNumberRef &r_number)
{
uindex_t length = MCStringGetLength(p_string);
if (offset > length)
offset = length;
if (char_count > length - offset)
char_count = length - offset;
if (!MCStringIsNative(p_string))
return __MCNumberParseUnicodeChars(MCStringGetCharPtr(p_string) + offset, char_count, p_integer_only, r_number);
bool t_success;
t_success = false;
uindex_t t_length_used;
t_length_used = 0;
t_success = __MCNumberParseNativeString((const char*)MCStringGetNativeCharPtr(p_string) + offset, char_count, true, p_integer_only, t_length_used, r_number);
return t_success;
}
static bool __MCNumberParseUnicodeChars(const unichar_t *p_chars, uindex_t p_char_count, bool p_integer_only, MCNumberRef& r_number)
{
char *t_native_chars;
if (!MCMemoryNewArray(p_char_count + 1, t_native_chars))
return false;
uindex_t t_native_char_count;
MCUnicodeCharsMapToNative(p_chars, p_char_count, (char_t *)t_native_chars, t_native_char_count, '?');
bool t_success;
t_success = false;
uindex_t t_length_used;
t_length_used = 0;
t_success = __MCNumberParseNativeString(t_native_chars, p_char_count, true, p_integer_only, t_length_used, r_number);
MCMemoryDeleteArray(t_native_chars);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool __MCNumberCopyDescription(__MCNumber *self, MCStringRef& r_string)
{
if (MCNumberIsInteger(self))
return MCStringFormat(r_string, "%d", self -> integer);
return MCStringFormat(r_string, "%lf", self -> real);
}
hash_t __MCNumberHash(__MCNumber *self)
{
if (MCNumberIsInteger(self))
return MCHashInteger(self -> integer);
return MCHashDouble(self -> real);
}
bool __MCNumberIsEqualTo(__MCNumber *self, __MCNumber *p_other_self)
{
return MCNumberCompareTo(self, p_other_self) == 0;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF MCNumberRef kMCZero;
MC_DLLEXPORT_DEF MCNumberRef kMCOne;
MC_DLLEXPORT_DEF MCNumberRef kMCMinusOne;
bool __MCNumberInitialize(void)
{
if (!MCNumberCreateWithInteger(0, kMCZero))
return false;
if (!MCNumberCreateWithInteger(1, kMCOne))
return false;
if (!MCNumberCreateWithInteger(-1, kMCMinusOne))
return false;
return true;
}
void __MCNumberFinalize(void)
{
MCValueRelease(kMCZero);
MCValueRelease(kMCOne);
MCValueRelease(kMCMinusOne);
}
////////////////////////////////////////////////////////////////////////////////