示例#1
0
/*
 * Parse the whole number portion of a number.
 *
 * 0
 * [1-9][0-9]*
 */
static bool
_parseWholeNumber(PARCJSONParser *parser, int64_t *value)
{
    bool result = false;
    int sign = 1;

    char nextCharacter;

    if (parcJSONParser_Next(parser, &nextCharacter)) {
        if (nextCharacter == '0') {
            *value = 0;
            result = true;
        } else if (isdigit(nextCharacter)) {
            *value = _digittoint(nextCharacter);
            while (parcJSONParser_Next(parser, &nextCharacter)) {
                if (!isdigit(nextCharacter)) {
                    parcJSONParser_Advance(parser, -1);
                    break;
                }
                *value = *value * 10 + _digittoint(nextCharacter);
            }
            *value = *value * sign;
            result = true;
        }
    }

    return result;
}
示例#2
0
文件: s_nan.c 项目: bingos/bitrig
/*
 * Scan a string of hexadecimal digits (the format nan(3) expects) and
 * make a bit array (using the local endianness). We stop when we
 * encounter an invalid character, NUL, etc.  If we overflow, we do
 * the same as gcc's __builtin_nan(), namely, discard the high order bits.
 *
 * The format this routine accepts needs to be compatible with what is used
 * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
 * __builtin_nan(). In fact, we're only 100% compatible for strings we
 * consider valid, so we might be violating the C standard. But it's
 * impossible to use nan(3) portably anyway, so this seems good enough.
 */
void
_scan_nan(uint32_t *words, int num_words, const char *s)
{
	int si;		/* index into s */
	int bitpos;	/* index into words (in bits) */

	bzero(words, num_words * sizeof(uint32_t));

	/* Allow a leading '0x'. (It's expected, but redundant.) */
	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
		s += 2;

	/* Scan forwards in the string, looking for the end of the sequence. */
	for (si = 0; isxdigit(s[si]); si++)
		;

	/* Scan backwards, filling in the bits in words[] as we go. */
#if _BYTE_ORDER == _LITTLE_ENDIAN
	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
#else
	for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
#endif
		if (--si < 0)
			break;
		words[bitpos / 32] |= _digittoint(s[si]) << (bitpos % 32);
	}
}

double
nan(const char *s)
{
	union {
		double d;
		uint32_t bits[2];
	} u;

	_scan_nan(u.bits, 2, s);
#if _BYTE_ORDER == _LITTLE_ENDIAN
	u.bits[1] |= 0x7ff80000;
#else
	u.bits[0] |= 0x7ff80000;
#endif
	return (u.d);
}

float
nanf(const char *s)
{
	union {
		float f;
		uint32_t bits[1];
	} u;

	_scan_nan(u.bits, 1, s);
	u.bits[0] |= 0x7fc00000;
	return (u.f);
}

#if	LDBL_MANT_DIG == DBL_MANT_DIG
__strong_alias(nanl, nan);
示例#3
0
/**
 * Parse and compute the base 10 value of a a sequence of digits from 0 to 9, inclusive.
 *
 * @param [in] parser A pointer to a PARCJSONParser instance.
 * @param [out] value A pointer to a value that will receive the base 10 value.
 *
 * @return true If there were parsable digits.
 */
static bool
_parseDigits09(PARCJSONParser *parser, int64_t *value)
{
    bool result = false;

    *value = 0;
    char nextDigit;
    while (parcJSONParser_Next(parser, &nextDigit)) {
        *value = *value * 10 + _digittoint(nextDigit);
        result = true;
    }

    return result;
}
示例#4
0
uint64_t
parcBuffer_ParseDecimalNumber(PARCBuffer *buffer)
{
    char *bytes = parcBuffer_Overlay(buffer, 0);

    int start = 0;

    unsigned count = 0;
    uint64_t result = 0;
    for (int i = start; i < parcBuffer_Remaining(buffer) && isdigit(bytes[i]); i++) {
        result = (result * 10) + _digittoint(bytes[i]);
        count++;
    }

    parcBuffer_SetPosition(buffer, parcBuffer_Position(buffer) + count);

    return result;
}
示例#5
0
uint64_t
parcBuffer_ParseHexNumber(PARCBuffer *buffer)
{
    char *bytes = parcBuffer_Overlay(buffer, 0);

    int start = 0;
    if (parcBuffer_Remaining(buffer) > 2) {
        if (bytes[0] == '0' && bytes[1] == 'x') {
            start = 2;
        }
    }

    unsigned count = 0;
    uint64_t result = 0;
    for (int i = start; i < parcBuffer_Remaining(buffer) && isxdigit(bytes[i]); i++) {
        result = (result * 16) + _digittoint(bytes[i]);
        count++;
    }

    parcBuffer_SetPosition(buffer, parcBuffer_Position(buffer) + start + count);

    return result;
}
示例#6
0
static bool
_parseFractionNumber(PARCJSONParser *parser, int64_t *value, int *log10)
{
    bool result = false;

    if (parcJSONParser_Remaining(parser) > 0) {
        *value = 0;
        *log10 = 0;
        char nextCharacter;
        while (parcJSONParser_Next(parser, &nextCharacter)) {
            if (!isdigit(nextCharacter)) {
                parcJSONParser_Advance(parser, -1);
                break;
            }
            *value = *value * 10 + _digittoint(nextCharacter);
            *log10 = *log10 + 1;
        }

        result = true;
    }

    return result;
}