小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

nginx 源碼學(xué)習(xí)筆記(六)——nginx基本數(shù)據(jù)結(jié)構(gòu)

 waitingnothing 2017-06-28

簡單的數(shù)據(jù)類型

在core/ngx_confing.h中定義了基本的數(shù)據(jù)類型的映射,大部分都映射到c語言自身的數(shù)據(jù)類型:

typedef intptr_t        ngx_int_t;

typedef uintptr_t       ngx_uint_t;

typedef intptr_t        ngx_flag_t;

其中ngx_int_t,ngx_flag_t都映射為intptr_t;ngx_uint_t映射為uintptr_t。

這兩個(gè)類型在/usr/include/stdint.h的定義為:


  1. /* Types for `void *' pointers.  */  
  2. #if __WORDSIZE == 64  
  3. # ifndef __intptr_t_defined  
  4. typedef long int                intptr_t;  
  5. #  define __intptr_t_defined  
  6. # endif  
  7. typedef unsigned long int       uintptr_t;  
  8. #else  
  9. # ifndef __intptr_t_defined  
  10. typedef int                     intptr_t;  
  11. #  define __intptr_t_defined  
  12. # endif  
  13. typedef unsigned int            uintptr_t;  
  14. #endif  

看到這里大家應(yīng)該理解,基本操作應(yīng)該都是一樣的。

 

字符串的數(shù)據(jù)類型

nginx對c語言字符串類型進(jìn)行了簡單的封裝,core/ngx_string.h/c里面包含了這些封裝的內(nèi)容。其中定義了

ngx_str_t,

ngx_keyval_t,

ngx_variable_value_t

這幾個(gè)基礎(chǔ)類型:

  1. typedef struct {  
  2.     size_t      len;  
  3.     u_char     *data;  
  4. } ngx_str_t;  
  5.   
  6.   
  7. typedef struct {  
  8.     ngx_str_t   key;  
  9.     ngx_str_t   value;  
  10. } ngx_keyval_t;  
  11.   
  12.   
  13. typedef struct {  
  14.     unsigned    len:28;  
  15.   
  16.     unsigned    valid:1;  
  17.     unsigned    no_cacheable:1;  
  18.     unsigned    not_found:1;  
  19.     unsigned    escape:1;  
  20.   
  21.     u_char     *data;  
  22. } ngx_variable_value_t;  

ngx_str_t 包含兩個(gè)屬性:len即長度, data為數(shù)據(jù);初始化使用ngx_string宏進(jìn)行:

#define ngx_string(str)     {sizeof(str) -1,(u_chart*)str }

 

該模塊在ngx_string.h和ngx_string.c中。字符串的操作一般包括:初始化,復(fù)制,格式化輸出,大小寫轉(zhuǎn)換,查找子字符,查找子字符串,字符串轉(zhuǎn)換成數(shù)字,字符串編碼類型相關(guān)函數(shù),字符串比較,trim,split等函數(shù)。在這個(gè)類中間沒有調(diào)用其他模塊的函數(shù),作為一個(gè)http服務(wù)器,還需要實(shí)現(xiàn)URL轉(zhuǎn)換,簡單的html轉(zhuǎn)換等函數(shù)。字符串的結(jié)構(gòu)體非常簡單實(shí)用,是非常值得剛?cè)腴T的linux開發(fā)工程師學(xué)習(xí)的。

 

函數(shù)                                                                                       說明
ngx_string                                                                                        初始化函數(shù)
ngx_null_string                                                                                初始化空字符串函數(shù)
ngx_tolower                                                                                     字符轉(zhuǎn)小寫函數(shù)
ngx_toupper                                                                                     字符轉(zhuǎn)大寫函數(shù)
ngx_strncmp                                                                                    比較指定長度的字符串是否相同
ngx_strcmp                                                                                      比較字符串是否相同
ngx_strstr                                                                                         從字符串中找到需要的字符串
ngx_strlen                                                                                         字符串的長度
ngx_strchr                                                                                        在字符串中找到匹配的字符,返回 0為匹配
ngx_strlchr                                                                                      在字符串中找到匹配的字符,返回匹配的指針
ngx_memzero                                                                                  把一片內(nèi)存區(qū)設(shè)置為
0
ngx_memset                                                                                     
把一片內(nèi)存區(qū)設(shè)置為指定的數(shù)
ngx_memcpy                                                                                   復(fù)制內(nèi)存,沒有返回
ngx_cpymem                                                                                   復(fù)制內(nèi)存,返回復(fù)制完了dst的最后一個(gè)字符的下一個(gè)字符的指針
ngx_copy                                                                                         
ngx_cpymem
ngx_memcmp                                                                                 
比較內(nèi)存中的數(shù)據(jù)是否相同
ngx_strlow                                                                                       把字符串都轉(zhuǎn)換成小寫
ngx_cpystrn                                                                                     復(fù)制字符串,并且返回字符串的最后一個(gè)字符的下一個(gè)字符的指針
ngx_pstrdup                                                                                     復(fù)制字符串到pool,返回字符串的指針
ngx_sprintf                                                                                       把各種類型的數(shù)據(jù)格式化輸出到buf,最大的長度為
65536
ngx_snprintf                                                                                     
把各種類型的數(shù)據(jù)格式化輸出到指定長度的
buf
ngx_strcasecmp                                                                                
不分大小寫比較兩個(gè)字符串是否相同
ngx_strncasecmp                                                                             指定長短不分大小寫比較兩個(gè)字符串是否相同
ngx_strnstr                                                                                       在指定大小一個(gè)字符串中是否有子字符串
ngx_strstrn                                                                                       在一個(gè)字符串中是否有子指定大小的字符串
ngx_strcasestrn                                                                                在一個(gè)字符串中是否有子指定大小的字符串,不區(qū)分大小寫
ngx_rstrncmp                                                                                   從后往前比較兩個(gè)字符串是否相同,返回相同的位置
ngx_rstrncasecmp                                                                            從后往前比較兩個(gè)字符串是否相同,返回相同的位置,不區(qū)分大小寫
ngx_memn2cmp                                                                              比較兩個(gè)指定長度的內(nèi)存是否相同,也比較長的內(nèi)存是否包含短的內(nèi)存
ngx_atoi                                                                                           指定長度的字符串轉(zhuǎn)換成數(shù)字
ngx_atosz                                                                                         指定長度的字符串轉(zhuǎn)換成ssize_t類型數(shù)字
ngx_atoof                                                                                         指定長度的字符串轉(zhuǎn)換成off_t類型數(shù)字
ngx_atotm                                                                                        指定長度的字符串轉(zhuǎn)換成time_t類型數(shù)字

ngx_hextoi                                                                                        指定長度的字符串轉(zhuǎn)換成十六進(jìn)制數(shù)字
ngx_hex_dump                                                                                 把數(shù)字轉(zhuǎn)換成16進(jìn)制的字符串
ngx_encode_base64                                                                          base64編碼
ngx_decode_base64                                                                          base64解碼
ngx_utf8_decode                                                                              utf8字符解碼成雙字節(jié)的 unicode或是單字節(jié)字符,但是該函數(shù)會移動*p的值,

 

請注意

ngx_utf8_length                           得到utf8編碼的字符占幾個(gè)字節(jié)
ngx_utf8_cpystrn                         賦值utf8字符串,保證完整的復(fù)制
ngx_escape_uri                            uri進(jìn)行編碼
ngx_unescape_uri                        uri的進(jìn)行解碼
ngx_escape_html                         html進(jìn)行編碼
ngx_sort                                       排序,主要是用于數(shù)組排序
ngx_qsort                                     快速排序
ngx_value                                     把宏數(shù)字轉(zhuǎn)換成字符串

為了測試,我們可以用以下兩種方式打印出來
1 ngx_str_t str ;
2 printf (
%* s
, str . len , str . data );
3 prinrf (
%V, & str );

 


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多