源码文件地址
src/core/ngx_string.h
src/core/ngx_string.c
源码
#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);
void
ngx_strlow(u_char *dst, u_char *src, size_t n)
{
while (n) {
*dst = ngx_tolower(*src);
dst++;
src++;
n--;
}
}
实例
#include <stdio.h>
#include <nginx.h>
#include <ngx_core.h>
#include <ngx_config.h>
#include <ngx_string.h>
volatile ngx_cycle_t *ngx_cycle;
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)
{
}
void
t_strup(u_char *dst, u_char *src, size_t u);
int
main(int argc, char *argv[])
{
ngx_str_t st = ngx_string("SHANG HAI");
ngx_str_t *stp = &st;
ngx_log_t *log = NULL;
u_char *p = ngx_alloc(stp->len, log);
t_strup(p, stp->data, stp->len);
printf("p.len = %lu\n", sizeof(p));
printf("p.data = %s\n\n", p);
ngx_strlow(p, stp->data, 3);
printf("p.len = %lu\n", sizeof(p));
printf("p.data = %s\n", p);
return 0;
}
void
t_strup(u_char *dst, u_char *src, size_t n)
{
while(n){
*dst = ngx_toupper(*src);
dst++;
src++;
n--;
}
}
执行
gcc -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g
-I ../../objs/
-I ../os/unix/
-I../core/
-I /usr/local/opt/pcre/include/
-I../event/
-I../os/
./j_str.c -o ./j_str.o
gcc -o ./j_str ./j_str.o
../../objs/src/core/ngx_string.o
../../objs/src/os/unix/ngx_alloc.o
../../objs/src/core/ngx_palloc.o
结果
p.len = 8
p.data = SHANG HAI
p.len = 8
p.data = shaNG HAI
ps :
代码中的 p
和 dst
要求可写入, 不然可能出现 Bus error
网友评论