Nginx data structure-integer and string

Source: Internet
Author: User
: This article mainly introduces the data structure of Nginx-integer and string. if you are interested in the PHP Tutorial, please refer to it. Nginx data structure-integer and string

Considering cross-platform, efficient, and standardized, Nginx encapsulates many data structures, most of which are frequently used in other development projects. of course, there are some complicated containers, each article will analyze and explain one or two points.

Integer encapsulation

typedef intptr_t ngx_int_t;typedef uintptr_t ngx_uint_t'

String type

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

typedef struct {    size_t      len;    u_char     *data;} ngx_str_t;

We can see that it is a simple struct with only two members. The data pointer points to the starting address of the string, and len indicates the length of the string.
Here you may be wondering why a string in C language only needs a pointer to indicate it. why do we still need a length? This is because the string we often say in the C language is actually a string ending with '\ 0', which is commonly known as the convention. once this mark is read, it indicates that the string has ended, when a string is created in C ++, the compiler automatically adds '\ 0' to the end. However, the data pointer in Ngx_str_t does not point to a string in C language, but is only the starting address of a string of common characters. this string does not use '\ 0' as its end, so we need len to tell the user the length of the string.
So what are the benefits of doing so? As a network server, Nginx is more concerned with this convenient development requirement. in network requests, we are most exposed to URL addresses, request header information, request entities, and so on, take the URL address for example, user request:

GET /test/string?a=1&b=2http/1.1\r\n

If we use an Ngx_str_t struct to store this value, now we want to GET the request type, whether it is GET or POST or PUT? We don't need to copy a copy of the memory. we just need to create a new ngx_str_t. The data pointer in it is directed to an address with the original ngx_str_t, and then change len to 3.
Of course, this is just the simplest application. the string type is almost a basic type of business system or network framework, A good design structure is an important guarantee for Nginx's low memory consumption.

Ngx_str_t operation

With the string, a simple struct is not very convenient. in modern advanced languages such as Java and Python, it provides a wide range of string-type operations, nginx also provides a lot of string operations for public functions. although some functions do not seem so easy to use, let's take a look at them one by one.

Go to bed. write it tomorrow.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces the Nginx data structure-integer and string, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.