static unsigned long __cdecl strtoxl (
_locale_t plocinfo,
const char *nptr,
const char **endptr,
int ibase,
int flags
)
{
const char *p;
char c;
unsigned long number;
unsigned digval;
unsigned long maxval;
_LocaleUpdate _loc_update(plocinfo);
/* validation section */
if (endptr != NULL)
{
/* store beginning of string in endptr */
*endptr = ( char *)nptr;
}
_VALIDATE_RETURN(nptr != NULL, EINVAL, 0L);
_VALIDATE_RETURN(ibase == 0 || (2 <= ibase && ibase <= 36), EINVAL, 0L);
p = nptr; /* p is our scanning pointer */
number = 0; /* start with zero */
//1. 这里关注到,函数没有检查传入的原字符指针是否为空, 如果传递了一个空的就崩了....
c = *p++; /* read char */
while ( _isspace_l(( int )(unsigned char )c, _loc_update.GetLocaleT()) )
c = *p++; /* skip whitespace */
//2. 不要期望能够 转换负负得正的字符串, 注意 "--100" 得到 0 , "-100" 得到 -100
if (c == ‘-‘ ) {
flags |= FL_NEG; /* remember minus sign */
c = *p++;
}
else if (c == ‘+‘ )
c = *p++; /* skip sign */
//3. 基数是 2 到 36 的闭区间 , [2, 36]
if (ibase < 0 || ibase == 1 || ibase > 36) {
/* bad base! */
if (endptr)
/* store beginning of string in endptr */
*endptr = nptr;
return 0L; /* return 0 */
}
//4. 如果转换的时候基数输入是0, 则基数取决于原字符的前面两个字符,
// 以非0开头的是 10进制字符串,
// 以0x或者0X开头的是 16进制字符串,
// 而仅仅以 0开头的是 8进制
else if (ibase == 0) {
/* determine base free-lance, based on first two chars of
string */
if (c != ‘0‘ )
ibase = 10;
else if (*p == ‘x‘ || *p == ‘X‘ )
ibase = 16;
else
ibase = 8;
}
// {{{ 源码里面,这个地方 有这么一段 暂时不知道是干嘛的, 在我看来貌似是多余的
if (ibase == 0) {
/* determine base free-lance, based on first two chars of
string */
if (c != ‘0‘ )
ibase = 10;
else if (*p == ‘x‘ || *p == ‘X‘ )
ibase = 16;
else
ibase = 8;
}
//}}}
// 5. 如果是 16 进制,则跳过0x 或者 0X 的前缀
if (ibase == 16) {
/* we might have 0x in front of number; remove if there */
if (c == ‘0‘ && (*p == ‘x‘ || *p == ‘X‘ )) {
++p;
c = *p++; /* advance past prefix */
}
}
// 6. 下面就是读取字符串,然后按照 local 解析应用的数值, 如果在转换过程中出现各种情况都会对标志位flags 进行标记
/* if our number exceeds this, we will overflow on multiply */
maxval = ULONG_MAX / ibase;
for (;;) { /* exit in middle of loop */
/* convert c to value */
if ( __ascii_isdigit_l(( int )(unsigned char )c, _loc_update.GetLocaleT()) )
digval = c - ‘0‘ ;
else if ( __ascii_isalpha_l(( int )(unsigned char )c, _loc_update.GetLocaleT()) )
digval = __ascii_toupper(c) - ‘A‘ + 10;
else
break ;
if (digval >= (unsigned)ibase)
break ; /* exit loop if bad digit found */
/* record the fact we have read one digit */
flags |= FL_READDIGIT;
/* we now need to compute number = number * base + digval,
but we need to know if overflow occured. This requires
a tricky pre-check. */
if (number < maxval || (number == maxval &&
(unsigned long )digval <= ULONG_MAX % ibase)) {
/* we won‘t overflow, go ahead and multiply */
number = number * ibase + digval;
}
else {
/* we would have overflowed -- set the overflow flag */
flags |= FL_OVERFLOW;
if (endptr == NULL) {
/* no need to keep on parsing if we
don‘t have to return the endptr. */
break ;
}
}
c = *p++; /* read next digit */
}
--p; /* point to place that stopped scan */
if (!(flags & FL_READDIGIT)) {
/* no number there; return 0 and point to beginning of
string */
if (endptr)
/* store beginning of string in endptr later on */
p = nptr;
number = 0L; /* return 0 */
}
else if ( (flags & FL_OVERFLOW) ||
( !(flags & FL_UNSIGNED) &&
( ( (flags & FL_NEG) && (number > -LONG_MIN) ) ||
( !(flags & FL_NEG) && (number > LONG_MAX) ) ) ) )
{
/* overflow or signed overflow occurred */
errno = ERANGE; //(老的实现方式和新的实现方式区别主要在这里, 新版友记录转换过程)
if ( flags & FL_UNSIGNED )
number = ULONG_MAX;
else if ( flags & FL_NEG )
number = (unsigned long )(-LONG_MIN);
else
number = LONG_MAX;
}
if (endptr != NULL)
/* store pointer to char that stopped the scan */
*endptr = p;
if (flags & FL_NEG)
/* negate result if there was a neg sign */
number = (unsigned long )(-( long )number);
return number; /* done. */
}
|