nginx 源码学习笔记(六)——nginx基本数据结构

简单的数据类型

在core/ngx_confing.h中定义了基本的数据类型的映射,大部分都映射到c语言自身的数据类型:

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。

这两个类型在/usr/include/stdint.h的定义为:



看到这里大家应该理解,基本操作应该都是一样的。

 

字符串的数据类型

nginx对c语言字符串类型进行了简单的封装,core/ngx_string.h/c里面包含了这些封装的内容。其中定义了

ngx_str_t,

ngx_keyval_t,

ngx_variable_value_t

这几个基础类型:


ngx_str_t 包含两个属性:len即长度, data为数据;初始化使用ngx_string宏进行:

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

 

该模块在ngx_string.h和ngx_string.c中。字符串的操作一般包括:初始化,复制,格式化输出,大小写转换,查找子字符,查找子字符串,字符串转换成数字,字符串编码类型相关函数,字符串比较,trim,split等函数。在这个类中间没有调用其他模块的函数,作为一个http服务器,还需要实现URL转换,简单的html转换等函数。字符串的结构体非常简单实用,是非常值得刚入门的linux开发工程师学习的。

 

函数                                                                                       说明
ngx_string                                                                                        初始化函数
ngx_null_string                                                                                初始化空字符串函数
ngx_tolower                                                                                     字符转小写函数
ngx_toupper                                                                                     字符转大写函数
ngx_strncmp                                                                                    比较指定长度的字符串是否相同
ngx_strcmp                                                                                      比较字符串是否相同
ngx_strstr                                                                                         从字符串中找到需要的字符串
ngx_strlen                                                                                         字符串的长度
ngx_strchr                                                                                        在字符串中找到匹配的字符,返回 0为匹配
ngx_strlchr                                                                                      在字符串中找到匹配的字符,返回匹配的指针
ngx_memzero                                                                                  把一片内存区设置为
0
ngx_memset                                                                                     
把一片内存区设置为指定的数
ngx_memcpy                                                                                   复制内存,没有返回
ngx_cpymem                                                                                   复制内存,返回复制完了dst的最后一个字符的下一个字符的指针
ngx_copy                                                                                         
ngx_cpymem
ngx_memcmp                                                                                 
比较内存中的数据是否相同
ngx_strlow                                                                                       把字符串都转换成小写
ngx_cpystrn                                                                                     复制字符串,并且返回字符串的最后一个字符的下一个字符的指针
ngx_pstrdup                                                                                     复制字符串到pool,返回字符串的指针
ngx_sprintf                                                                                       把各种类型的数据格式化输出到buf,最大的长度为
65536
ngx_snprintf                                                                                     
把各种类型的数据格式化输出到指定长度的
buf
ngx_strcasecmp                                                                                
不分大小写比较两个字符串是否相同
ngx_strncasecmp                                                                             指定长短不分大小写比较两个字符串是否相同
ngx_strnstr                                                                                       在指定大小一个字符串中是否有子字符串
ngx_strstrn                                                                                       在一个字符串中是否有子指定大小的字符串
ngx_strcasestrn                                                                                在一个字符串中是否有子指定大小的字符串,不区分大小写
ngx_rstrncmp                                                                                   从后往前比较两个字符串是否相同,返回相同的位置
ngx_rstrncasecmp                                                                            从后往前比较两个字符串是否相同,返回相同的位置,不区分大小写
ngx_memn2cmp                                                                              比较两个指定长度的内存是否相同,也比较长的内存是否包含短的内存
ngx_atoi                                                                                           指定长度的字符串转换成数字
ngx_atosz                                                                                         指定长度的字符串转换成ssize_t类型数字
ngx_atoof                                                                                         指定长度的字符串转换成off_t类型数字
ngx_atotm                                                                                        指定长度的字符串转换成time_t类型数字

ngx_hextoi                                                                                        指定长度的字符串转换成十六进制数字
ngx_hex_dump                                                                                 把数字转换成16进制的字符串
ngx_encode_base64                                                                          base64编码
ngx_decode_base64                                                                          base64解码
ngx_utf8_decode                                                                              utf8字符解码成双字节的 unicode或是单字节字符,但是该函数会移动*p的值,

 

请注意

ngx_utf8_length                           得到utf8编码的字符占几个字节
ngx_utf8_cpystrn                         赋值utf8字符串,保证完整的复制
ngx_escape_uri                            uri进行编码
ngx_unescape_uri                        uri的进行解码
ngx_escape_html                         html进行编码
ngx_sort                                       排序,主要是用于数组排序
ngx_qsort                                     快速排序
ngx_value                                     把宏数字转换成字符串

为了测试,我们可以用以下两种方式打印出来
1 ngx_str_t str ;
2 printf (
%* s
, str . len , str . data );
3 prinrf (
%V, & str );

 


posted on 2012-03-15 14:00  h2内存数据库  阅读(4406)  评论(0编辑  收藏  举报

导航