File: s2n.c

package info (click to toggle)
fis-gtm 6.3-007-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,284 kB
  • sloc: ansic: 328,861; asm: 5,182; csh: 5,102; sh: 1,918; awk: 291; makefile: 69; sed: 13
file content (187 lines) | stat: -rwxr-xr-x 4,624 bytes parent folder | download | duplicates (2)
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
/****************************************************************
 *								*
 * Copyright (c) 2001-2017 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"

#include "arit.h"
#include "stringpool.h"

#define DIGIT(x)	((x >='0') && (x <= '9'))
#define NUM_MASK	(MV_NM | MV_INT | MV_NUM_APPROX)

error_def(ERR_NUMOFLOW);

LITREF mval literal_null;
LITREF int4 ten_pwr[];

char *s2n (mval *u)
{
	boolean_t	digit, dot, dotseen, exp, exneg, isdot, tail;
	char		*c, *d, *eos, *w;
	int		expdigits, i, j, k, sign, x, y, z, zero;
	DCL_THREADGBL_ACCESS;

	SETUP_THREADGBL_ACCESS;
	i = 0;
	assertpro(MV_DEFINED(u));
	c = u->str.addr;
	if (0 == u->str.len)
	{	/* Substitute pre-converted NULL/0 value */
		TREF(s2n_intlit) = 1;
		*u = literal_null;
		return c;
	}
	eos = u->str.addr + u->str.len;				/* End of string marker */
	sign = 0;
	while (c < eos && (('-'== *c) || ('+' == *c)))
		sign += (('-' == *c++) ? 1 : 2);		/* Sign is odd: negative, even: positive */
	for (zero = 0; (c < eos) && ('0'== *c ); zero++, c++)	/* Eliminate leading zeroes */
		 ;
	dot = ((c < eos) && ('.' == *c));
	if (dot)
		c++;
	for (y = 0; (c < eos) && ('0' == *c ); c++, y--)	/* Eliminate leading zeroes of possible fractional part */
		;
	z = u->m[0] = u->m[1] = 0;				/* R0 */
	d = c + 9;
	for  (w = ((d < eos) ? d : eos); (c < w) && DIGIT(*c); c++, z++)
	{
		if ('0' == *c)
		{
			u->m[1] *= 10;
			i++;
		} else
		{
			i = 0;
			u->m[1] = (u->m[1] * 10) + (*c - '0');
		}
	}							/* R1 */
	if ((c < w) && ('.' == *c) && !dot)
	{
		y = z;
		c++;
		d++;
		dot = TRUE;
		if (w < eos)
			w++;
	}
	for (; (c < w) && DIGIT(*c); c++)
	{
		if ('0' == *c)
		{
			u->m[1] *= 10;
			i++;
		} else
		{
			i = 0;
			u->m[1] = (u->m[1] * 10) + (*c - '0');
		}
	}							/* R2 */
	k = (int4)(d - c);
	if (c < eos)
	{
		d = c + 9;
		for (w = ((d < eos) ? d : eos); (c < w) && DIGIT(*c); z++)
			u->m[0] = (u->m[0] * 10) + (*c++ - '0');
		if ((c < w) && ('.' == *c) && !dot )
		{
			y = z;
			c++;
			d++;
			dot = TRUE;
			if (w < eos)
				w++;
		}
		while ((c < w) && DIGIT(*c))
			u->m[0] = (u->m[0] * 10) + (*c++ - '0');
		u->m[0] *= ten_pwr[d - c];
		for (; (c < eos) && ('0' == *c); c++, z++)
			;
	}
	tail = (c != eos) || (dot && (('0' == *(c - 1)) || ('.' == *(c - 1))));
	for (dotseen = dot; (c < eos) && (((isdot = ('.' == *c)) && !dot) || DIGIT(*c)); c++)
	{
		dotseen = (dotseen || isdot);
		if (!dotseen)
			z++;
	}
	digit = (0 != z) || (0 != y) || (0 != zero);
	x = 0;
	exp = (c < eos) && ('E' == *c) && digit;
	if (exp && ((c + 1) < eos))
	{
		w = c;	/* save pointer to return in case expression following E is not a valid exponent */
		c++;
		exneg = ('-' == *c);
		if (exneg || ('+' == *c))
			c++;
		d = c;	/* save pointer to see if any progress occurs in the below for loop */
		for (; (c < eos) && ('0' == *c); c++)
			;	/* Do not count leading 0s towards MAX_DIGITS_IN_EXP */
		for (expdigits = 0; (c < eos) && DIGIT(*c); c++)
		{
			if ((MAX_DIGITS_IN_EXP + 1) > expdigits)
			{
				x = (x * 10) + (*c - '0');
				expdigits++;
			}
		}
		if (!expdigits && (d == c))
		{
			c = w;	/* if we did not see any digit following the E (and optional + or -) reset parse to E */
			assert(0 == x);
		} else if (exneg)
			x = -x;
	}
	TREF(s2n_intlit) = (0 != sign) || dot || exp;
	if (digit)
	{
		x += (dot ? y : z);
		j = x + k - 6;
		i += j;
		if ((0 == u->m[0]) && (6 >= x) && (0 <= i))
		{
			u->mvtype |= (tail || (1 < sign) || ((0 != zero) && (1 != u->str.len)))
				? (MV_NM | MV_INT | MV_NUM_APPROX) : (MV_NM | MV_INT);
			if (0 > j)
				u->m[1] /= ((sign & 1) ? -ten_pwr[-j] : ten_pwr[-j]);
			else
				u->m[1] *= ((sign & 1) ? -ten_pwr[j] : ten_pwr[j]);
		} else
		{
			u->m[1] *= ten_pwr[k];
			x += MV_XBIAS;
			if ((EXPLO > x) || (0 == u->m[1]))
			{
				u->mvtype |= MV_NM | MV_INT | MV_NUM_APPROX;
				u->m[1] = 0;
			} else if (EXPHI <= x)
			{
				u->mvtype &= ~NUM_MASK;
				if (!TREF(compile_time))
					rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) ERR_NUMOFLOW);
			} else
			{
				u->e = x;
				u->sgn = sign & 1;
				u->mvtype |= (tail || (1 < sign) || ((0 != zero) && (1 != u->str.len)))
					? (MV_NM | MV_NUM_APPROX) : MV_NM;
			}
		}
		assert(MANT_HI > u->m[1]);
	} else
	{
		u->mvtype |= (MV_NM | MV_INT | MV_NUM_APPROX);
		u->m[1] = 0;
	}
	return c;
}