美文网首页
4.字符串 ngx_strlow

4.字符串 ngx_strlow

作者: IN4 | 来源:发表于2017-11-24 15:37 被阅读0次

源码文件地址
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 :
代码中的 pdst 要求可写入, 不然可能出现 Bus error

相关文章

网友评论

      本文标题:4.字符串 ngx_strlow

      本文链接:https://www.haomeiwen.com/subject/rykfbxtx.html