Mysql Dynamic String Processing Dynamic_string_mysql

Source: Internet
Author: User

MySQL, you will often see some of the processing of dynamic strings, such as: Dynamic_string.

In order to record the actual length of the dynamic string, the maximum length of the buffer, and the need to adjust each string, the new memory is allocated in a timely manner, and the length is adjusted. MySQL uses dynamic_string to hold dynamic string-related information:

typedef struct ST_DYNAMIC_STRING
{
 char *str;
 size_t length, max_length, alloc_increment;
} dynamic_string;

In this structure, STR stores the first address of the actual string, the length of the string, and the maximum number of characters the Max_length record string buffer can hold, alloc_increment indicates how much memory is allocated each time the string needs to allocate memory.

Here's a look at the initialization process for this structure:

My_bool init_dynamic_string (dynamic_string *str, const char *INIT_STR, size_t init_alloc, size_t alloc_increment)
{
 size_t length;
 Dbug_enter ("init_dynamic_string");
 
 if (!alloc_increment)
 alloc_increment = 128;
 length = 1;
 if (init_str && (length = strlen (INIT_STR) + 1) < Init_alloc)
 Init_alloc = (length + alloc_increment -1)/alloc_increment) * alloc_increment;
 if (!init_alloc)
 init_alloc = alloc_increment;
 
 if (!) ( STR->STR = (char *) my_malloc (Init_alloc, MYF (MY_WME)))
 Dbug_return (TRUE);
 Str->length = length-1;
 if (INIT_STR)
 memcpy (str->str, init_str, length);
 Str->max_length = Init_alloc;
 Str->alloc_increment = alloc_increment;
 Dbug_return (FALSE);

As you can see from the above function, the initial allocated string buffer size Init_alloc will be judged based on the string you want to initialize. After allocating the dynamic_string space, we initialize it based on the size of the buffer, the actual length of the string, and the alloc_increment:

Length: The actual length of the string

Max_length: Maximum length of buffer

Alloc_increment: When there is not enough space, the next allocated memory unit size.

Once this is initialized, the next time you need to add more characters to the buffer, you can determine whether you need to expand the buffer by these values:

My_bool Dynstr_append_mem (dynamic_string *str, const char *append, size_t length)
{
 char *new_ptr;
 if (str->length + length >= str->max_length)/* If the new string is added, the total length exceeds the buffer size * *
* The number of alloc_increment sizes that need to be allocated Save to save the new string * *
 size_t new_length = (str->length + length + str->alloc_increment)/
    STR->ALLOC_ increment;
 New_length *= str->alloc_increment;
 
 if (!) ( New_ptr = (char *) my_realloc (str->str, New_length, Myf (MY_WME))) return
  (TRUE);
 Str->str = new_ptr;
 Str->max_length = new_length;
 }
/* will be the new assigned content, append after str * * * *
 memcpy (str->str + str->length, append, length);
 str->length = length;               * * Capacity after the new length of STR
 /str->str[str->length] = 0;/* Safety for C programs    /* The last character of the string is ' * '
 /return ( FALSE);

As you can see from the above code, after the string initialization is good, then if you need to add new content to the string, only need to be based on the information stored before the dynamic realloc. Because the structure records the full string-related content, dynamic expansion can be very convenient to handle.

Of course, in addition to these, there are examples of string truncation, string initialization, Escape OS quotes, and so on:

The truncation after the string is offset greater than N.

My_bool Dynstr_trunc (dynamic_string *str, size_t N)
{
 str->length = n;
 Str->str[str->length] = ' the ';
 return (FALSE);

Returns the address of a character that appears for the first time in a string. If not, return the address at the end of the string (point to ')

Char *strcend (Register const char *s, register Pchar c)
{for
 (;;)
 {
 if (*s = = (char) c) return
  ((char *) s);
 if (!*s++) return
  ((char *) s-1);
 }

String Content Expansion:

My_bool Dynstr_realloc (dynamic_string *str, size_t additional_size)
{
 dbug_enter ("Dynstr_realloc");
 
 if (!additional_size)
 Dbug_return (FALSE);
 if (str->length + additional_size > Str->max_length) * * If the new string content exceeds the maximum length of the buffer
 /{str->max_length = ( (str->length + additional_size + str->alloc_increment-1)/
    str->alloc_increment) * STR->ALLOC_ increment;
 if (!) ( STR->STR = (char *) my_realloc (str->str, Str->max_length, Myf (MY_WME)))
  Dbug_return (TRUE);
 }
 Dbug_return (FALSE);

The string is enclosed in quotation marks, and the single quotation marks are escaped, primarily for the execution of some system commands (systems (CMD)).

For example: Ls-al will become ' Ls-al '

For example: Ls-a ' L will become ' ls-a\ ' l '

* * concatenates any number of strings, escapes any OS quote in the result then * surround the whole affair in another Set of quotes which is finally appended * to specified dynamic_string.
 This function is especially useful when * building strings to be executed with the system () function.
 * * @param str Dynamic String which would have addtional strings appended.
 * @param append String to be appended. * @param ... Optional.
 Additional string (s) to is appended.
 * * * The final argument in the ' list must be NullS even if no additional * options are passed.
 * @return True = Success.
* * My_bool dynstr_append_os_quoted (dynamic_string *str, const char *append, ...)
 {const char *quote_str = "\";
 const UINT Quote_len = 1;
 My_bool ret = TRUE;
 
 Va_list Dirty_text; RET &= dynstr_append_mem (str, QUOTE_STR, Quote_len);
 /* Leading Quote * * Va_start (dirty_text, append);
 while (append!= NullS) {const char *cur_pos = append; const char *next_pos = Cur_Pos /* Search for quote in each string and replace with escaped quote/while (* Next_pos = Strcend (Cur_pos, quote_str[0)
  ) (!=) {ret &= dynstr_append_mem (str, cur_pos, (UINT) (Next_pos-cur_pos));
  RET &= dynstr_append_mem (str, "\ \", 1);
  RET &= dynstr_append_mem (str, QUOTE_STR, Quote_len);
 Cur_pos = Next_pos + 1;
 RET &= dynstr_append_mem (str, cur_pos, (UINT) (Next_pos-cur_pos));
 Append = Va_arg (Dirty_text, char *);
 } va_end (Dirty_text); RET &= dynstr_append_mem (str, QUOTE_STR, Quote_len);
/* Trailing Quote * * (ret);
 }

By defining the structure information of a dynamic string, each time a string is added more characters, the dynamic expansion is based on the current length of the string. And after each expansion, the structure is recorded the actual information of the current string (the length of the current string, the buffer can accommodate the length of the string, the length of the expansion of the unit). In this way, the processing of dynamic strings becomes very convenient.

Related Article

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.