2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_number.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-01-13 04:29:42 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program 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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
#define __GBX_NUMBER_C
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
2011-12-30 03:29:00 +01:00
|
|
|
#include "gb_limit.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "gbx_type.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gbx_local.h"
|
2012-04-28 17:08:24 +02:00
|
|
|
#include "gbx_math.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_number.h"
|
|
|
|
|
|
|
|
#define buffer_init COMMON_buffer_init
|
|
|
|
#define get_char COMMON_get_char
|
|
|
|
#define last_char COMMON_last_char
|
|
|
|
#define look_char COMMON_look_char
|
|
|
|
#define put_char COMMON_put_char
|
|
|
|
#define jump_space COMMON_jump_space
|
|
|
|
#define get_current COMMON_get_current
|
|
|
|
#define buffer_pos COMMON_pos
|
|
|
|
#define get_size_left COMMON_get_size_left
|
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
#define IS_PURE_INTEGER(_int64_val) ((_int64_val) == ((int)(_int64_val)))
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-11-02 00:24:07 +01:00
|
|
|
static bool read_integer(int base, bool minus, int64_t *result, bool local)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
uint64_t nbr2, nbr;
|
2011-10-16 21:24:01 +02:00
|
|
|
int d, n, c; //, nmax;
|
2010-03-07 02:38:15 +01:00
|
|
|
char thsep;
|
2010-03-07 18:14:54 +01:00
|
|
|
int ndigit_thsep;
|
|
|
|
bool first_thsep;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
thsep = LOCAL_get(local)->thousand_sep;
|
|
|
|
ndigit_thsep = 0;
|
|
|
|
first_thsep = FALSE;
|
2010-03-07 02:38:15 +01:00
|
|
|
|
|
|
|
n = 0;
|
2010-12-20 04:26:00 +01:00
|
|
|
nbr = 0;
|
2009-07-20 03:10:50 +02:00
|
|
|
|
2011-10-16 21:24:01 +02:00
|
|
|
/*switch (base)
|
2009-07-20 03:10:50 +02:00
|
|
|
{
|
|
|
|
case 2: nmax = 64; break;
|
|
|
|
case 8: nmax = 21; break;
|
|
|
|
case 10: nmax = 19; break;
|
|
|
|
case 16: nmax = 16; break;
|
2011-10-16 21:24:01 +02:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = last_char();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
2010-03-07 18:14:54 +01:00
|
|
|
if (local && base == 10)
|
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c == thsep && (ndigit_thsep == 3 || (!first_thsep && ndigit_thsep >= 1 && ndigit_thsep <= 3)))
|
2010-03-07 18:14:54 +01:00
|
|
|
{
|
|
|
|
c = get_char();
|
|
|
|
first_thsep = TRUE;
|
|
|
|
ndigit_thsep = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-03-07 02:38:15 +01:00
|
|
|
|
|
|
|
if (c >= '0' && c <= '9')
|
2010-03-07 18:14:54 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
d = c - '0';
|
2010-03-07 18:14:54 +01:00
|
|
|
if (local && base == 10)
|
|
|
|
ndigit_thsep++;
|
|
|
|
}
|
2010-12-20 04:26:00 +01:00
|
|
|
else if (c >= 'A' && c <='Z')
|
|
|
|
d = c - 'A' + 10;
|
|
|
|
else if (c >= 'a' && c <='z')
|
|
|
|
d = c - 'a' + 10;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (d >= base)
|
|
|
|
break;
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
2011-11-02 00:24:07 +01:00
|
|
|
nbr2 = nbr * base + d;
|
2010-12-20 04:26:00 +01:00
|
|
|
|
2011-11-02 00:24:07 +01:00
|
|
|
if ((nbr2 / base) != nbr || nbr2 > ((uint64_t)LLONG_MAX + minus))
|
|
|
|
return TRUE;
|
2009-07-20 03:10:50 +02:00
|
|
|
|
2011-11-02 00:24:07 +01:00
|
|
|
nbr = nbr2;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = get_char();
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = last_char();
|
2009-07-20 03:10:50 +02:00
|
|
|
|
2010-03-07 18:14:54 +01:00
|
|
|
if (base == 10)
|
|
|
|
{
|
|
|
|
if (local && first_thsep && ndigit_thsep != 3)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
2009-07-20 03:10:50 +02:00
|
|
|
{
|
2010-01-02 16:04:06 +01:00
|
|
|
if ((c == '&' || c == 'u' || c == 'U') && base != 10)
|
2009-07-20 03:10:50 +02:00
|
|
|
c = get_char();
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 16:04:06 +01:00
|
|
|
if ((base == 16 && n == 4) || (base == 2 && n == 16))
|
|
|
|
{
|
|
|
|
if (nbr >= 0x8000L && nbr <= 0xFFFFL)
|
|
|
|
nbr |= INT64_C(0xFFFFFFFFFFFF0000);
|
|
|
|
}
|
|
|
|
else if ((base == 16 && n == 8) || (base == 2 && n == 32))
|
|
|
|
{
|
|
|
|
if (nbr >= 0x80000000L && nbr <= 0xFFFFFFFFL)
|
|
|
|
nbr |= INT64_C(0xFFFFFFFF00000000);
|
|
|
|
}
|
2009-07-20 03:10:50 +02:00
|
|
|
}
|
|
|
|
}
|
2010-01-02 16:04:06 +01:00
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
if (c > 0 && !isspace(c))
|
|
|
|
return TRUE;
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (n == 0)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
*((int64_t *)result) = nbr;
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
static bool read_float(double *result, bool local)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
LOCAL_INFO *local_info;
|
|
|
|
char point;
|
|
|
|
char thsep;
|
2010-03-07 18:14:54 +01:00
|
|
|
int ndigit_thsep;
|
|
|
|
bool first_thsep;
|
2010-06-02 23:12:23 +02:00
|
|
|
int c, n;
|
|
|
|
|
2012-04-28 04:31:48 +02:00
|
|
|
uint64_t mantisse, mantisse_int;
|
2010-06-02 23:12:23 +02:00
|
|
|
int ndigit_frac;
|
|
|
|
bool frac;
|
2012-04-28 04:31:48 +02:00
|
|
|
bool frac_null;
|
2011-01-04 03:35:48 +01:00
|
|
|
bool nozero;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
int nexp;
|
|
|
|
bool nexp_minus;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-03-07 18:14:54 +01:00
|
|
|
local_info = LOCAL_get(local);
|
|
|
|
point = local_info->decimal_point;
|
2011-05-16 04:16:22 +02:00
|
|
|
thsep = local_info->thousand_sep;
|
|
|
|
ndigit_thsep = 0;
|
|
|
|
first_thsep = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
c = last_char();
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
/* Integer and decimal part */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-02 23:12:23 +02:00
|
|
|
n = 0;
|
|
|
|
mantisse = 0;
|
2012-04-28 04:31:48 +02:00
|
|
|
mantisse_int = 0;
|
2010-06-02 23:12:23 +02:00
|
|
|
frac = FALSE;
|
2012-04-28 04:31:48 +02:00
|
|
|
frac_null = TRUE;
|
2010-06-02 23:12:23 +02:00
|
|
|
ndigit_frac = 0;
|
2010-12-20 04:26:00 +01:00
|
|
|
nexp = 0;
|
|
|
|
nexp_minus = FALSE;
|
2011-01-04 03:35:48 +01:00
|
|
|
nozero = FALSE;
|
2010-06-02 23:12:23 +02:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (c == point)
|
|
|
|
{
|
|
|
|
if (frac)
|
|
|
|
break;
|
|
|
|
c = get_char();
|
2010-06-02 23:12:23 +02:00
|
|
|
frac = TRUE;
|
2012-04-28 04:31:48 +02:00
|
|
|
mantisse_int = mantisse;
|
2010-12-20 04:26:00 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (local && !frac)
|
|
|
|
{
|
|
|
|
if (c == thsep && (ndigit_thsep == 3 || (!first_thsep && ndigit_thsep >= 1 && ndigit_thsep <= 3)))
|
|
|
|
{
|
|
|
|
c = get_char();
|
|
|
|
first_thsep = TRUE;
|
|
|
|
ndigit_thsep = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 23:12:23 +02:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (!isdigit(c) || (c < 0))
|
|
|
|
break;
|
|
|
|
|
2011-01-04 03:35:48 +01:00
|
|
|
if (c != '0')
|
|
|
|
nozero = TRUE;
|
|
|
|
|
|
|
|
if (nozero)
|
|
|
|
n++;
|
|
|
|
|
2011-12-30 03:29:00 +01:00
|
|
|
if (n > MAX_FLOAT_DIGIT)
|
2010-06-02 23:12:23 +02:00
|
|
|
{
|
2011-12-30 03:29:00 +01:00
|
|
|
if (n == (MAX_FLOAT_DIGIT + 1) && (c >= '5'))
|
2010-06-02 23:12:23 +02:00
|
|
|
mantisse++;
|
|
|
|
if (!frac)
|
|
|
|
ndigit_frac--;
|
|
|
|
c = get_char();
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-28 04:31:48 +02:00
|
|
|
if (c == '0')
|
|
|
|
mantisse *= 10;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (frac)
|
|
|
|
frac_null = FALSE;
|
|
|
|
mantisse = mantisse * 10 + (c - '0');
|
|
|
|
}
|
2010-06-02 23:12:23 +02:00
|
|
|
|
|
|
|
if (frac)
|
|
|
|
ndigit_frac++;
|
2010-12-20 04:26:00 +01:00
|
|
|
else if (local)
|
2010-03-07 18:14:54 +01:00
|
|
|
ndigit_thsep++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = get_char();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c == 'e' || c == 'E')
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c < 0)
|
|
|
|
goto __END;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
/* Exponent */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c == 'e' || c == 'E')
|
2009-07-20 03:10:50 +02:00
|
|
|
{
|
|
|
|
c = get_char();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c == '+' || c == '-')
|
|
|
|
{
|
|
|
|
if (c == '-')
|
|
|
|
nexp_minus = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = get_char();
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (!isdigit(c) || (c < 0))
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
nexp = nexp * 10 + (c - '0');
|
|
|
|
if (nexp > DBL_MAX_10_EXP)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
c = get_char();
|
|
|
|
if (!isdigit(c) || (c < 0))
|
|
|
|
break;
|
|
|
|
}
|
2010-06-02 23:12:23 +02:00
|
|
|
|
|
|
|
if (nexp_minus)
|
|
|
|
nexp = (-nexp);
|
2010-12-20 04:26:00 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (c >= 0 && !isspace(c))
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
__END:
|
|
|
|
|
2010-03-07 18:14:54 +01:00
|
|
|
if (local && first_thsep && ndigit_thsep != 3)
|
|
|
|
return TRUE;
|
|
|
|
|
2012-04-28 04:53:51 +02:00
|
|
|
if (frac && frac_null)
|
2012-04-28 04:31:48 +02:00
|
|
|
mantisse = mantisse_int;
|
|
|
|
else
|
|
|
|
nexp -= ndigit_frac;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-28 17:08:24 +02:00
|
|
|
//fprintf(stderr, "%.24g %d\n", (double)mantisse, nexp);
|
|
|
|
//*result = mulpow10((double)mantisse, nexp);
|
|
|
|
*result = (double)mantisse * pow10(nexp);
|
2012-04-28 04:31:48 +02:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool NUMBER_from_string(int option, const char *str, int len, VALUE *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
int c;
|
|
|
|
int64_t val = 0;
|
|
|
|
double dval = 0.0;
|
|
|
|
TYPE type;
|
|
|
|
int base = 10;
|
|
|
|
bool minus = FALSE;
|
2009-07-20 03:10:50 +02:00
|
|
|
int pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
buffer_init(str, len);
|
|
|
|
|
|
|
|
jump_space();
|
|
|
|
|
|
|
|
c = get_char();
|
|
|
|
|
|
|
|
if (c == '+' || c == '-')
|
|
|
|
{
|
|
|
|
minus = (c == '-');
|
|
|
|
c = get_char();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option & NB_READ_INT_LONG)
|
|
|
|
{
|
|
|
|
if (option & NB_READ_HEX_BIN)
|
|
|
|
{
|
|
|
|
if (c == '&')
|
|
|
|
{
|
|
|
|
c = get_char();
|
|
|
|
|
|
|
|
if (c == 'H' || c == 'h')
|
|
|
|
{
|
|
|
|
base = 16;
|
|
|
|
c = get_char();
|
|
|
|
}
|
|
|
|
else if (c == 'O' || c == 'o')
|
|
|
|
{
|
|
|
|
base = 8;
|
|
|
|
c = get_char();
|
|
|
|
}
|
|
|
|
else if (c == 'X' || c == 'x')
|
|
|
|
{
|
|
|
|
base = 2;
|
|
|
|
c = get_char();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
else if (c == '%')
|
|
|
|
{
|
|
|
|
base = 2;
|
|
|
|
c = get_char();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (c == '-' || c == '+')
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
errno = 0;
|
2009-07-20 03:10:50 +02:00
|
|
|
pos = COMMON_pos - 1;
|
|
|
|
|
2011-11-02 00:24:07 +01:00
|
|
|
if (!read_integer(base, minus, &val, (option & NB_LOCAL) != 0))
|
2009-07-20 03:10:50 +02:00
|
|
|
{
|
2010-12-22 19:31:44 +01:00
|
|
|
if (minus) val = (-val);
|
|
|
|
|
2009-07-20 03:10:50 +02:00
|
|
|
if ((option & NB_READ_INTEGER) && IS_PURE_INTEGER(val))
|
|
|
|
{
|
|
|
|
type = T_INTEGER;
|
|
|
|
goto __END;
|
|
|
|
}
|
|
|
|
else if ((option & NB_READ_LONG))
|
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
type = T_LONG;
|
|
|
|
goto __END;
|
2009-07-20 03:10:50 +02:00
|
|
|
}
|
|
|
|
else if ((option & NB_READ_FLOAT) && base == 10)
|
|
|
|
{
|
|
|
|
type = T_FLOAT;
|
|
|
|
dval = (double)val;
|
|
|
|
goto __END;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if ((option & NB_READ_FLOAT) && base == 10)
|
|
|
|
{
|
2009-07-20 03:10:50 +02:00
|
|
|
COMMON_pos = pos;
|
|
|
|
get_char();
|
2010-12-20 04:26:00 +01:00
|
|
|
if (!read_float(&dval, (option & NB_LOCAL) != 0))
|
|
|
|
{
|
2010-12-29 16:25:29 +01:00
|
|
|
if (minus) dval = (-dval);
|
2010-12-20 04:26:00 +01:00
|
|
|
type = T_FLOAT;
|
|
|
|
goto __END;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__END:
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (last_char() >= 0) //(c >= 0 && !isspace(c))
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
value->type = type;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (type == T_INTEGER)
|
2010-12-22 19:31:44 +01:00
|
|
|
value->_integer.value = val;
|
2010-12-20 04:26:00 +01:00
|
|
|
else if (type == T_LONG)
|
2010-12-22 19:31:44 +01:00
|
|
|
value->_long.value = val;
|
2010-12-20 04:26:00 +01:00
|
|
|
else
|
2010-12-22 19:31:44 +01:00
|
|
|
value->_float.value = dval;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
//fprintf(stderr, "return FALSE\n");
|
2010-12-20 04:26:00 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void NUMBER_int_to_string(uint64_t nbr, int prec, int base, VALUE *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
char *ptr;
|
|
|
|
char *src;
|
|
|
|
int digit, len;
|
|
|
|
bool neg;
|
|
|
|
|
|
|
|
//if (prec < 0)
|
|
|
|
// ERROR_panic("NUMBER_int_to_string: prec < 0");
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
ptr = &COMMON_buffer[COMMON_BUF_MAX];
|
|
|
|
|
|
|
|
if (nbr == 0 && prec == 0)
|
|
|
|
{
|
|
|
|
STRING_char_value(value, '0');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
neg = (nbr & (1LL << 63)) != 0;
|
|
|
|
|
|
|
|
if (base == 10 && neg)
|
|
|
|
nbr = 1 + ~nbr;
|
|
|
|
|
|
|
|
while (nbr > 0)
|
|
|
|
{
|
|
|
|
digit = nbr % base;
|
|
|
|
nbr /= base;
|
|
|
|
|
|
|
|
ptr--;
|
|
|
|
len++;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*ptr = '0' + digit;
|
|
|
|
else
|
|
|
|
*ptr = 'A' + digit - 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (neg)
|
|
|
|
{
|
|
|
|
if (prec)
|
|
|
|
{
|
|
|
|
ptr += len - prec;
|
|
|
|
len = prec;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base == 10)
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
ptr--;
|
|
|
|
*ptr = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_new_temp_value(value, NULL, len);
|
|
|
|
src = value->_string.addr;
|
|
|
|
|
|
|
|
memcpy(src, ptr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(value, NULL, Max(len, prec));
|
|
|
|
src = value->_string.addr;
|
|
|
|
|
|
|
|
while (prec > len)
|
|
|
|
{
|
|
|
|
*src++ = '0';
|
|
|
|
prec--;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(src, ptr, len);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|