blob: 3859e0acd6f2aa1ea6a595b69f0470e76d392a9e [file] [log] [blame]
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#ifndef _NXT_STRING_H_INCLUDED_
#define _NXT_STRING_H_INCLUDED_
typedef struct {
size_t length;
u_char *start;
} nxt_str_t;
/*
* C99 allows to assign struct as compound literal with struct name cast only.
* SunC however issues error on the cast in struct static initialization:
* non-constant initializer: op "NAME"
* So a separate nxt_string_value() macro is intended to use in assignment.
*/
#define nxt_length(s) (sizeof(s) - 1)
#define nxt_string(s) { nxt_length(s), (u_char *) s }
#define nxt_null_string { 0, NULL }
#define nxt_string_value(s) (nxt_str_t) nxt_string(s)
nxt_inline u_char
nxt_lower_case(u_char c)
{
return (u_char) ((c >= 'A' && c <= 'Z') ? c | 0x20 : c);
}
nxt_inline u_char
nxt_upper_case(u_char c)
{
return (u_char) ((c >= 'a' && c <= 'z') ? c & 0xDF : c);
}
nxt_inline u_char *
nxt_strlchr(u_char *p, u_char *last, u_char c)
{
while (p < last) {
if (*p == c) {
return p;
}
p++;
}
return NULL;
}
#define \
nxt_strlen(s) \
strlen((char *) s)
#define \
nxt_cpymem(dst, src, n) \
(((u_char *) memcpy(dst, src, n)) + (n))
#define \
nxt_strncmp(s1, s2, n) \
strncmp((char *) s1, (char *) s2, n)
#define \
nxt_strchr(s1, c) \
(u_char *) strchr((const char *) s1, (int) c)
#define \
nxt_memset(buf, c, length) \
(void) memset(buf, c, length)
#define \
nxt_memzero(buf, length) \
(void) memset(buf, 0, length)
#if (NXT_HAVE_EXPLICIT_BZERO)
#define \
nxt_explicit_memzero(buf, length) \
explicit_bzero(buf, length)
#elif (NXT_HAVE_EXPLICIT_MEMSET)
#define \
nxt_explicit_memzero(buf, length) \
(void) explicit_memset(buf, 0, length)
#else
nxt_inline void
nxt_explicit_memzero(u_char *buf, size_t length)
{
volatile u_char *p = (volatile u_char *) buf;
while (length != 0) {
*p++ = 0;
length--;
}
}
#endif
#define \
nxt_strstr_eq(s1, s2) \
(((s1)->length == (s2)->length) \
&& (memcmp((s1)->start, (s2)->start, (s1)->length) == 0))
#endif /* _NXT_STRING_H_INCLUDED_ */