Commit d30a76ff authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(char2digit): Removed the base argument. Simplified the code. This

	should be faster now.
(s_strtol): Removed the base argument.  Don't allow leading '+' or '-'
	signs.  This simplifies the code.
parent 3ff9d212
/*
* $Id: s-string.c,v 1.27 2002/08/02 19:09:47 ceder Exp $
* $Id: s-string.c,v 1.28 2002/08/10 19:57:47 ceder Exp $
* Copyright (C) 1990-1996, 1998-1999, 2001-2002 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -840,58 +840,36 @@ s_strtok (const String source,
* Convert a char to a number in base BASE.
*/
static int
char2digit (const char ch,
const int base)
char2digit (const char ch)
{
int index;
static const char * translate_table =
"0123456789abcdefghijklmnopqrstuvwxyz";
int c = (unsigned char)ch;
int c = (unsigned char)ch;
index = 0;
while ( (index < base)
&& ( translate_table[index]
!= (isalpha(c) ? tolower(c) : ch)))
index++;
if (index >= base)
if (c < '0' || c > '9')
return -1;
else
return index;
return c - '0';
}
/*
* Convert the String STR to a long, using the base BASE.
* Convert the String STR to a long, using base ten.
* Leading blanks are skipped according to isblank() in <ctype.h>.
* The index of the first character that couldn't be used to form
* the number is returned in *FIRST_ILL_CHAR. Returns -1 in
* *first_ill_char if there was an error in the parameters.
* BASE may be in the range 2..36
* Leading signs are not allowed.
*/
#define MAXBASE 36
EXPORT long
s_strtol (const String str,
String_size * first_ill_char,
const int base )
String_size * first_ill_char)
{
long number = 0; /* The result */
int sign = 0; /* Sign o' the times :-) */
String_size char_no;
int digit;
assert (base >= 2 && base <= MAXBASE);
if (s_empty(str))
{
*first_ill_char = -1;
return 0;
}
/* Skip all blanks */
char_no = 0;
while ( (char_no < s_strlen(str)) && isspace(str.string[char_no]) )
......@@ -903,30 +881,15 @@ s_strtol (const String str,
return 0;
}
/* Find any sign character (+ or -) */
if (str.string[char_no] == '+')
{
sign = 1;
char_no++;
}
else if (str.string[char_no] == '-')
{
sign = -1;
char_no++;
}
while ( (char_no < s_strlen(str))
&& ((digit = char2digit(str.string[char_no], base)) != -1) )
&& ((digit = char2digit(str.string[char_no])) != -1) )
{
number = number * base + digit;
number = 10 * number + digit;
char_no++;
}
*first_ill_char = char_no;
if (sign != -1)
return number;
else
return -number;
return number;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment