Home  >  Article  >  Backend Development  >  Nginx data structure-integer and string

Nginx data structure-integer and string

WBOY
WBOYOriginal
2016-07-29 09:08:35961browse

Nginx data structure – integer and string

Tags: Nginx data structure


Taking into account cross-platform, high efficiency, and unified specifications, Nginx encapsulates many data structures, most of which we use in other development projects Of course, there are some frequently used containers, and of course there are some complex containers. In each article, the author will analyze and practice one or two points.

Integer encapsulation

<code><span>typedef</span> intptr_t ngx_int_t;
<span>typedef</span> uintptr_t ngx_uint_t'</code>

String type

In Nginx, Ngx_str_t is used to represent a string, and its definition is as follows:

<code><span>typedef</span> struct {
    size_t      len;
    u_char     *<span><span>data</span>;</span>
} ngx_str_t;</code>

We can see that it is a simple structure with only two members, the data pointer Points to the starting address of the string, and len represents the length of the string.
You may be confused here. A string in C language only needs a pointer to represent it. Why is a length needed here? This is because the strings we often talk about in C language actually start with ' So what are the benefits of doing this? As a network server, Nginx certainly considers the need to facilitate development. In network requests, what we are most exposed to are URL addresses, request header information, request entities, etc. Take URL addresses, for example, user requests:

<code>GET /test/<span>string</span>?<span>a</span>=<span>1</span>&b=<span>2</span><span>http</span>/<span>1.1</span>\r\n</code>
Then if we use a Ngx_str_t structure to store this value, now we want to get the request type, is it GET, POST or PUT? We don't need to copy a memory. All we have to do is make a new ngx_str_t. The data pointer inside points to the same address as the original ngx_str_t, and then change len to 3.

Of course, this is just the simplest application. The string type is a basic type that is widely used in almost all business systems and network frameworks. A good design structure is an important guarantee for Nginx's low memory consumption.

ngx_str_t operations

With a simple structure like string, it is not particularly convenient. Modern high-level languages ​​such as Java and Python provide a wealth of operations on string types, and Nginx also provides There are many public functions for string manipulation, although some of them may not seem so easy to use, so let’s take a look at these functions one by one.

Go to bed, let’s write tomorrow

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); The above introduces the data structure of Nginx - integers and strings, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn