Replace str_to_uint() with str_to_uint32()

Also str_to_int() with str_to_int32()
This commit is contained in:
Andrew Bettison 2013-12-30 14:35:55 +10:30
parent 72040517e1
commit 0c88f8bf89
3 changed files with 47 additions and 11 deletions

View File

@ -541,9 +541,9 @@ static inline int _parse_http_size_t(struct http_request *r, http_size_t *szp)
return !_run_out(r) && isdigit(*r->cursor) && str_to_uint64(r->cursor, 10, szp, &r->cursor);
}
static inline int _parse_uint(struct http_request *r, unsigned int *uintp)
static inline int _parse_uint32(struct http_request *r, uint32_t *uint32p)
{
return !_run_out(r) && isdigit(*r->cursor) && str_to_uint(r->cursor, 10, uintp, &r->cursor);
return !_run_out(r) && isdigit(*r->cursor) && str_to_uint32(r->cursor, 10, uint32p, &r->cursor);
}
static unsigned _parse_ranges(struct http_request *r, struct http_range *range, unsigned nrange)
@ -793,12 +793,12 @@ static int http_request_parse_http_version(struct http_request *r)
// Parse HTTP version: HTTP/m.n followed by CRLF.
assert(r->version_major == 0);
assert(r->version_minor == 0);
unsigned major, minor;
uint32_t major, minor;
if (!( _skip_literal(r, "HTTP/")
&& _parse_uint(r, &major)
&& _parse_uint32(r, &major)
&& major > 0 && major < UINT8_MAX
&& _skip_literal(r, ".")
&& _parse_uint(r, &minor)
&& _parse_uint32(r, &minor)
&& minor < UINT8_MAX
&& _skip_eol(r)
)

44
str.c
View File

@ -645,7 +645,7 @@ char *str_str(char *haystack, const char *needle, size_t haystack_len)
return NULL;
}
int str_to_int(const char *str, int base, int *result, const char **afterp)
int str_to_int32(const char *str, int base, int32_t *result, const char **afterp)
{
if (isspace(*str))
return 0;
@ -654,14 +654,14 @@ int str_to_int(const char *str, int base, int *result, const char **afterp)
long value = strtol(str, (char**)&end, base);
if (afterp)
*afterp = end;
if (errno == ERANGE || end == str || value > INT_MAX || value < INT_MIN || isdigit(*end) || (!afterp && *end))
if (errno == ERANGE || end == str || value > INT32_MAX || value < INT32_MIN || isdigit(*end) || (!afterp && *end))
return 0;
if (result)
*result = value;
return 1;
}
int str_to_uint(const char *str, int base, unsigned *result, const char **afterp)
int str_to_uint32(const char *str, int base, uint32_t *result, const char **afterp)
{
if (isspace(*str))
return 0;
@ -670,7 +670,7 @@ int str_to_uint(const char *str, int base, unsigned *result, const char **afterp
unsigned long value = strtoul(str, (char**)&end, base);
if (afterp)
*afterp = end;
if (errno == ERANGE || end == str || value > UINT_MAX || isdigit(*end) || (!afterp && *end))
if (errno == ERANGE || end == str || value > UINT32_MAX || isdigit(*end) || (!afterp && *end))
return 0;
if (result)
*result = value;
@ -758,6 +758,42 @@ int str_to_int64_scaled(const char *str, int base, int64_t *result, const char *
return 1;
}
int str_to_uint32_scaled(const char *str, int base, uint32_t *result, const char **afterp)
{
uint32_t value;
const char *end = str;
if (!str_to_uint32(str, base, &value, &end)) {
if (afterp)
*afterp = end;
return 0;
}
value *= scale_factor(end, &end);
if (afterp)
*afterp = end;
else if (*end)
return 0;
if (result)
*result = value;
return 1;
}
int uint32_scaled_to_str(char *str, size_t len, uint32_t value)
{
char symbol = '\0';
int i;
for (i = 0; i != NELS(scale_factors); ++i)
if (value % scale_factors[i].factor == 0) {
value /= scale_factors[i].factor;
symbol = scale_factors[i].symbol;
break;
}
strbuf b = strbuf_local(str, len);
strbuf_sprintf(b, "%lu", (unsigned long) value);
if (symbol)
strbuf_putc(b, symbol);
return strbuf_overrun(b) ? 0 : 1;
}
int str_to_uint64_scaled(const char *str, int base, uint64_t *result, const char **afterp)
{
uint64_t value;

4
str.h
View File

@ -391,8 +391,8 @@ char *str_str(char *haystack, const char *needle, size_t haystack_len);
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int str_to_int(const char *str, int base, int *result, const char **afterp);
int str_to_uint(const char *str, int base, unsigned *result, const char **afterp);
int str_to_int32(const char *str, int base, int32_t *result, const char **afterp);
int str_to_uint32(const char *str, int base, uint32_t *result, const char **afterp);
int str_to_int64(const char *str, int base, int64_t *result, const char **afterp);
int str_to_uint64(const char *str, int base, uint64_t *result, const char **afterp);