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 227
Expand file tree
/
Copy pathfoundation-math.cpp
More file actions
125 lines (100 loc) · 3.12 KB
/
foundation-math.cpp
File metadata and controls
125 lines (100 loc) · 3.12 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
/* 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-math.h>
#include "foundation-auto.h"
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
bool MCMathConvertFromBase10(uint32_t p_value, bool p_negative, integer_t p_dest_base, MCStringRef& r_result)
{
char_t result[64];
char_t *dptr = &result[63];
do
{
uint16_t digit = p_value % p_dest_base;
p_value /= p_dest_base;
if (digit >= 10)
*dptr-- = digit - 10 + 'A';
else
*dptr-- = digit + '0';
}
while (p_value);
if (p_negative)
*dptr-- = '-';
dptr++;
return MCStringCreateWithNativeChars(dptr, 64 - (dptr - result), r_result);
}
bool MCMathConvertToBase10(MCStringRef p_source, integer_t p_source_base, bool& r_negative, uinteger_t& r_result, bool& r_error)
{
uint32_t t_value;
t_value = 0;
bool t_negative;
t_negative = false;
MCAutoStringRefAsNativeChars t_auto_native;
const char_t* t_native;
uindex_t t_length;
if (!t_auto_native . Lock(p_source, t_native, t_length))
return false;
// MW-2008-01-31: [[ Bug 5841 ]] Added in some more strict error checking to
// stop baseConvert attempting to convert strings with digits outside the
// source-base.
bool t_error;
t_error = false;
if (!t_error && t_length == 0)
t_error = true;
uint32_t i;
if (!t_error)
{
if (t_native[0] == '+')
i = 1;
else if (t_native[0] == '-')
{
i = 1;
t_negative = true;
}
else
i = 0;
}
while(!t_error && i < t_length)
{
t_value *= p_source_base;
char_t source = MCNativeCharUppercase(t_native[i]);
if (isdigit((uint8_t)source))
{
if (source - '0' >= p_source_base)
t_error = true;
else
t_value += source - '0';
}
else if (source >= 'A' && source < 'A' + p_source_base - 10)
t_value += source - 'A' + 10;
else
t_error = true;
i += 1;
}
if (t_error)
{
r_error = true;
return false;
}
r_negative = t_negative;
r_result = t_value;
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool __MCMathInitialize()
{
return true;
}
void __MCMathFinalize()
{
}
////////////////////////////////////////////////////////////////////////////////