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

相關文章
相關標籤/搜索