nginx开发_字符串操作函数

由于ngx_str_t为非NULL结尾的函数,且网络请求中有大量忽略大小写的需求,所以nginx内部封装了许多字符串操作相关的函数,函数名称极其相识,且使用时有有些约定,特此整理。

赋值&拷贝

#define ngx_string(str)        { sizeof(str) - 1, (u_char *) str }
#define ngx_null_string        { 0, NULL }
#define ngx_str_set(str, text) (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text
#define ngx_str_null(str)      (str)->len = 0; (str)->data = NULL
//ngx_string与ngx_null_string常用于变量初始化
//ngx_str_set与ngx_str_null常用于变量赋值

#define ngx_memzero(buf, n)       (void) memset(buf, 0, n)
#define ngx_memset(buf, c, n)     (void) memset(buf, c, n)

#define ngx_memcpy(dst, src, n)   (void) memcpy(dst, src, n)
#define ngx_cpymem(dst, src, n)   (((u_char *) memcpy(dst, src, n)) + (n))
#define ngx_copy                  ngx_cpymem
//ngx_cpymem返回dst的结尾位置

#define ngx_memmove(dst, src, n)   (void) memmove(dst, src, n)
#define ngx_movemem(dst, src, n)   (((u_char *) memmove(dst, src, n)) + (n))
//ngx_movemem返回dst的结尾位置

u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n);
//字符串赋值,且dst为NULL结尾
u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src);
//从pool内申请空间,并使用src赋值,不保证NULL结尾

字符串长度

#define ngx_strlen(s)       strlen((const char *) s)
size_t ngx_strnlen(u_char *p, size_t n);
//返回第一个'\0'或者n

大小写转换

#define ngx_tolower(c)      (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define ngx_toupper(c)      (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)

void ngx_strlow(u_char *dst, u_char *src, size_t n);

字符串比对

#define ngx_strncmp(s1, s2, n)  strncmp((const char *) s1, (const char *) s2, n)
//调用前需要保证s1与s2长度一致
#define ngx_strcmp(s1, s2)  strcmp((const char *) s1, (const char *) s2)
//s1与s2都需要NULL结尾
ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
//调用前需要保证s1与s2长度一致,使用较少,常用ngx_strncmp

#define ngx_memcmp(s1, s2, n)  memcmp((const char *) s1, (const char *) s2, n)

ngx_int_t ngx_strcasecmp(u_char *s1, u_char *s2);
//忽略大小写比对,s1需要NULL结尾,调用前需要保证s1与s2长度一致
ngx_int_t ngx_strncasecmp(u_char *s1, u_char *s2, size_t n);
//忽略大小写比对,调用前需要保证s1与s2长度一致
ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
//忽略大小写比对,调用前需要保证s1与s2长度一致
ngx_int_t ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2);
//内部判断长度相等
ngx_int_t ngx_dns_strcmp(u_char *s1, u_char *s2);
//忽略大小写比对,且忽略最后的'.'字符
ngx_int_t ngx_filename_cmp(u_char *s1, u_char *s2, size_t n);
//根据操作系统,选择是否忽略大小写,且忽略最后的'/'字符

字符串搜索

#define ngx_strchr(s1, c)   strchr((const char *) s1, (int) c)
//字符搜索s1默认NULL结尾
static ngx_inline u_char *  ngx_strlchr(u_char *p, u_char *last, u_char c);
//字符搜索
u_char *ngx_strnstr(u_char *s1, char *s2, size_t n);
//在s1中搜索子串s2,s2需要NULL结尾,n为s1长度
u_char *ngx_strstrn(u_char *s1, char *s2, size_t n);
//在s1中搜索子串s2,s1需要NULL结尾,n为s2长度-1
u_char *ngx_strcasestrn(u_char *s1, char *s2, size_t n);
//忽略大小写,在s1中搜索子串s2,s1需要NULL结尾,n为s2长度-1
u_char *ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n);
//忽略大小写,在[s1,last]中搜索子串s2,n为s2长度-1

字符串格式化

u_char * ngx_cdecl ngx_sprintf(u_char *buf, const char *fmt, ...);
//字符串格式化,外部保证buf大小
u_char * ngx_cdecl ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...);
u_char * ngx_cdecl ngx_slprintf(u_char *buf, u_char *last, const char *fmt, ...);
u_char *ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args);
//ngx_sprintf、ngx_snprintf、ngx_slprintf的内部函数,用于不定长参数的函数使用

字符串/数字转换

ngx_int_t ngx_atoi(u_char *line, size_t n);
ngx_int_t ngx_atofp(u_char *line, size_t n, size_t point);
ssize_t ngx_atosz(u_char *line, size_t n);
off_t ngx_atoof(u_char *line, size_t n);
time_t ngx_atotm(u_char *line, size_t n);
ngx_int_t ngx_hextoi(u_char *line, size_t n);
u_char *ngx_hex_dump(u_char *dst, u_char *src, size_t len);

字符串编解码

#define ngx_base64_encoded_length(len)  (((len + 2) / 3) * 4)
#define ngx_base64_decoded_length(len)  (((len + 3) / 4) * 3)
void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src);
void ngx_encode_base64url(ngx_str_t *dst, ngx_str_t *src);
ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src);
ngx_int_t ngx_decode_base64url(ngx_str_t *dst, ngx_str_t *src);

uint32_t ngx_utf8_decode(u_char **p, size_t n);
size_t ngx_utf8_length(u_char *p, size_t n);
u_char *ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len);

uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type);
void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type);
uintptr_t ngx_escape_html(u_char *dst, u_char *src, size_t size);
uintptr_t ngx_escape_json(u_char *dst, u_char *src, size_t size);

转载于:https://www.cnblogs.com/atskyline/p/8124247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值