nginx之ngx_strlow函数参数思考

简约不简单的ngx_strlow() 函数,不错的形参设计方式。

1.若是本人设计ngx_strlow()函数会怎么写?
2.ngx_strlow函数形参相比本人的 写法有什么优点?

A1.本人写法以下
nginx

#define my_tolower(c) (char)(((c) >= 'A' && (c) <= 'Z') ? ((c) | 0x20) : (c))
char *my_strlow(char *s) {
    char *str = s;
    while (*s) {
        *s = my_tolower(*s);
        s++;
    }   
    return str;
}

A2.操做不一样buf时,写法和效率都有优点。函数

究竟ngx_strlow比本人的写法有什么优点?会从易用性,效率两方面比较。
比较易用性:
1.操做同一块buf
  1.me的用法 my_strlow(buf);                               + 1分
  2.nginx的用法 ngx_strlow(buf, buf, strlen(buf) + 1);
2.操做不一样的buf
  1.me的用法设计

    char buf2[MAX] = "";
    strncpy(buf2, buf, sizeof(buf) - 1);
    buf2[sizeof(buf) - 1] = '\0';
    my_tolower(buf2);

  2.ngx_strlow(buf2, buf, strlen(buf) + 1);                + 1分

比较效率:
2.操做不一样的buf
  1.me的用法
    两次值拷贝
  2.ngx_strlow                                             + 1分
    一次值拷贝

从得分上看ngx_strlow写法更具优点。


code

相关文章
相关标签/搜索