Change arg of ctype functions from char to int

Consistent with functions defined in <ctype>; now all can be used
as a predicate function with the type int (*)(int).
This commit is contained in:
Andrew Bettison 2015-10-01 06:51:48 +09:30
parent 2fdd23e864
commit f73cd77185
3 changed files with 46 additions and 33 deletions

View File

@ -375,6 +375,21 @@ static inline int _skip_any(struct http_request *r)
return 1;
}
static inline int _skip_if(struct http_request *r, int (*predicate)(int))
{
if (_run_out(r) || !predicate(*r->cursor))
return 0;
++r->cursor;
return 1;
}
static inline int _skip_while(struct http_request *r, int (*predicate)(int))
{
while (!_run_out(r) && predicate(*r->cursor))
++r->cursor;
return 1;
}
static inline void _skip_all(struct http_request *r)
{
r->cursor = r->end;
@ -469,16 +484,14 @@ static int _skip_literal_nocase(struct http_request *r, const char *literal)
return *literal == '\0';
}
static int is_http_space(char c)
static int is_http_space(int c)
{
return c == ' ' || c == '\t';
}
static int _skip_optional_space(struct http_request *r)
{
while (!_run_out(r) && is_http_space(*r->cursor))
++r->cursor;
return 1;
return _skip_while(r, is_http_space);
}
static inline int _skip_space(struct http_request *r)

8
str.c
View File

@ -282,8 +282,8 @@ char *to_base64url_str(char *const dstBase64, const unsigned char *srcBinary, si
}
static size_t _base64_decode(unsigned char *dstBinary, size_t dstsiz, const char *const srcBase64, size_t srclen,
const char **afterp, int flags, int (*skip_pred)(char),
int (*isdigit_pred)(char), int (*ispad_pred)(char), uint8_t (*todigit)(char)
const char **afterp, int flags, int (*skip_pred)(int),
int (*isdigit_pred)(int), int (*ispad_pred)(int), uint8_t (*todigit)(char)
)
{
uint8_t buf = 0;
@ -358,14 +358,14 @@ static size_t _base64_decode(unsigned char *dstBinary, size_t dstsiz, const char
}
size_t base64_decode(unsigned char *dstBinary, size_t dstsiz, const char *const srcBase64, size_t srclen,
const char **afterp, int flags, int (*skip_pred)(char))
const char **afterp, int flags, int (*skip_pred)(int))
{
return _base64_decode(dstBinary, dstsiz, srcBase64, srclen, afterp, flags, skip_pred, is_base64_digit, is_base64_pad, base64_digit);
}
size_t base64url_decode(unsigned char *dstBinary, size_t dstsiz, const char *const srcBase64, size_t srclen,
const char **afterp, int flags, int (*skip_pred)(char))
const char **afterp, int flags, int (*skip_pred)(int))
{
return _base64_decode(dstBinary, dstsiz, srcBase64, srclen, afterp, flags, skip_pred, is_base64url_digit, is_base64url_pad, base64url_digit);
}

50
str.h
View File

@ -196,9 +196,9 @@ char *to_base64url_str(char *dstBase64url, const unsigned char *srcBinary, size_
* @author Andrew Bettison <andrew@servalproject.com>
*/
size_t base64_decode(unsigned char *dstBinary, size_t dstsiz, const char *const srcBase64, size_t srclen,
const char **afterp, int flags, int (*skip_pred)(char));
const char **afterp, int flags, int (*skip_pred)(int));
size_t base64url_decode(unsigned char *dstBinary, size_t dstsiz, const char *const srcBase64url, size_t srclen,
const char **afterp, int flags, int (*skip_pred)(char));
const char **afterp, int flags, int (*skip_pred)(int));
#define B64_CONSUME_ALL (1 << 0)
@ -220,40 +220,40 @@ extern uint8_t _serval_ctype_0[UINT8_MAX];
extern uint8_t _serval_ctype_1[UINT8_MAX];
extern uint8_t _serval_ctype_2[UINT8_MAX];
__SERVAL_DNA__STR_INLINE int is_http_char(char c) {
__SERVAL_DNA__STR_INLINE int is_http_char(int c) {
return isascii(c);
}
__SERVAL_DNA__STR_INLINE int is_http_ctl(char c) {
__SERVAL_DNA__STR_INLINE int is_http_ctl(int c) {
return iscntrl(c);
}
__SERVAL_DNA__STR_INLINE int is_base64_digit(char c) {
return (_serval_ctype_0[(unsigned char) c] & _SERVAL_CTYPE_0_BASE64) != 0;
__SERVAL_DNA__STR_INLINE int is_base64_digit(int c) {
return (_serval_ctype_0[(uint8_t) c] & _SERVAL_CTYPE_0_BASE64) != 0;
}
__SERVAL_DNA__STR_INLINE int is_base64url_digit(char c) {
return (_serval_ctype_0[(unsigned char) c] & _SERVAL_CTYPE_0_BASE64URL) != 0;
__SERVAL_DNA__STR_INLINE int is_base64url_digit(int c) {
return (_serval_ctype_0[(uint8_t) c] & _SERVAL_CTYPE_0_BASE64URL) != 0;
}
__SERVAL_DNA__STR_INLINE int is_base64_pad(char c) {
__SERVAL_DNA__STR_INLINE int is_base64_pad(int c) {
return c == '=';
}
__SERVAL_DNA__STR_INLINE int is_base64url_pad(char c) {
__SERVAL_DNA__STR_INLINE int is_base64url_pad(int c) {
return c == '=';
}
__SERVAL_DNA__STR_INLINE uint8_t base64_digit(char c) {
return _serval_ctype_0[(unsigned char) c] & _SERVAL_CTYPE_0_BASE64_MASK;
return _serval_ctype_0[(uint8_t) c] & _SERVAL_CTYPE_0_BASE64_MASK;
}
__SERVAL_DNA__STR_INLINE uint8_t base64url_digit(char c) {
return _serval_ctype_0[(unsigned char) c] & _SERVAL_CTYPE_0_BASE64_MASK;
return _serval_ctype_0[(uint8_t) c] & _SERVAL_CTYPE_0_BASE64_MASK;
}
__SERVAL_DNA__STR_INLINE int is_multipart_boundary(char c) {
return (_serval_ctype_2[(unsigned char) c] & _SERVAL_CTYPE_2_MULTIPART_BOUNDARY) != 0;
__SERVAL_DNA__STR_INLINE int is_multipart_boundary(int c) {
return (_serval_ctype_2[(uint8_t) c] & _SERVAL_CTYPE_2_MULTIPART_BOUNDARY) != 0;
}
__SERVAL_DNA__STR_INLINE int is_valid_multipart_boundary_string(const char *s)
@ -266,11 +266,11 @@ __SERVAL_DNA__STR_INLINE int is_valid_multipart_boundary_string(const char *s)
return s[-1] != ' ';
}
__SERVAL_DNA__STR_INLINE int is_http_separator(char c) {
return (_serval_ctype_1[(unsigned char) c] & _SERVAL_CTYPE_1_HTTP_SEPARATOR) != 0;
__SERVAL_DNA__STR_INLINE int is_http_separator(int c) {
return (_serval_ctype_1[(uint8_t) c] & _SERVAL_CTYPE_1_HTTP_SEPARATOR) != 0;
}
__SERVAL_DNA__STR_INLINE int is_http_token(char c) {
__SERVAL_DNA__STR_INLINE int is_http_token(int c) {
return is_http_char(c) && !is_http_ctl(c) && !is_http_separator(c);
}
@ -279,8 +279,8 @@ __SERVAL_DNA__STR_INLINE int is_http_token(char c) {
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
__SERVAL_DNA__STR_INLINE int hexvalue(char c) {
return isxdigit(c) ? _serval_ctype_1[(unsigned char) c] & _SERVAL_CTYPE_1_HEX_MASK : -1;
__SERVAL_DNA__STR_INLINE int hexvalue(int c) {
return isxdigit(c) ? _serval_ctype_1[(uint8_t) c] & _SERVAL_CTYPE_1_HEX_MASK : -1;
}
/* -------------------- In-line string formatting -------------------- */
@ -554,16 +554,16 @@ size_t www_form_uri_decode(char *const dst, ssize_t dstsiz, const char *srcUrien
*/
int str_is_uri(const char *uri);
__SERVAL_DNA__STR_INLINE int is_uri_char_scheme(char c) {
return (_serval_ctype_1[(unsigned char) c] & _SERVAL_CTYPE_1_URI_SCHEME) != 0;
__SERVAL_DNA__STR_INLINE int is_uri_char_scheme(int c) {
return (_serval_ctype_1[(uint8_t) c] & _SERVAL_CTYPE_1_URI_SCHEME) != 0;
}
__SERVAL_DNA__STR_INLINE int is_uri_char_unreserved(char c) {
return (_serval_ctype_1[(unsigned char) c] & _SERVAL_CTYPE_1_URI_UNRESERVED) != 0;
__SERVAL_DNA__STR_INLINE int is_uri_char_unreserved(int c) {
return (_serval_ctype_1[(uint8_t) c] & _SERVAL_CTYPE_1_URI_UNRESERVED) != 0;
}
__SERVAL_DNA__STR_INLINE int is_uri_char_reserved(char c) {
return (_serval_ctype_1[(unsigned char) c] & _SERVAL_CTYPE_1_URI_RESERVED) != 0;
__SERVAL_DNA__STR_INLINE int is_uri_char_reserved(int c) {
return (_serval_ctype_1[(uint8_t) c] & _SERVAL_CTYPE_1_URI_RESERVED) != 0;
}
/* Return true if the string resembles a URI scheme without the terminating colon.