MySQL-based dynamic string processing-PHP Tutorial

Source: Internet
Author: User
MySQL-based dynamic string processing. MySQL uses dynamic strings to process MySQL. some dynamic strings are often processed, for example, DYNAMIC_STRING. To record the actual length of a dynamic string, the most MySQL dynamic string processing in the buffer zone
In MySQL, we often see some 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 new memory allocated and the length adjusted each time the string needs to be adjusted. MySQL uses DYNAMIC_STRING to save information related to dynamic strings:

 
 
  1. typedef struct st_dynamic_string
  2. {
  3. char *str;
  4. size_t length,max_length,alloc_increment;
  5. } DYNAMIC_STRING;
In this struct, str stores the first address of the actual string, length records the actual length of the string, and max_length records the maximum number of characters that can be stored in the string buffer. alloc_increment indicates that when the string needs to be allocated memory, memory size allocated each time.

Let's take a look at the initialization process of this struct:

 
 
  1. my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,size_t init_alloc, size_t alloc_increment)
  2. {
  3. size_t length;
  4. DBUG_ENTER("init_dynamic_string");

  5. if (!alloc_increment)
  6. alloc_increment=128;
  7. length=1;
  8. if (init_str && (length= strlen(init_str)+1) < init_alloc)
  9. init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment;
  10. if (!init_alloc)
  11. init_alloc=alloc_increment;

  12. if (!(str->str=(char*) my_malloc(init_alloc,MYF(MY_WME))))
  13. DBUG_RETURN(TRUE);
  14. str->length=length-1;
  15. if (init_str)
  16. memcpy(str->str,init_str,length);
  17. str->max_length=init_alloc;
  18. str->alloc_increment=alloc_increment;
  19. DBUG_RETURN(FALSE);
  20. }
From the above function, we can see that during initialization, the size of the initially allocated string buffer init_alloc will be determined based on the required initial string. After the DYNAMIC_STRING space is allocated, we will initialize the buffer based on the buffer size, the actual length of the string, and alloc_increment: length: the actual length of the string max_length: the maximum length of the buffer alloc_increment: the size of the memory unit allocated next time when the space is insufficient.

After initializing the content, if you need to add more characters to the buffer next time, you can determine whether to resize the buffer based on these values:

 
 
  1. My_bool dynstr_append_mem (DYNAMIC_STRING * str, const char * append,
  2. Size_t length)
  3. {
  4. Char * new_ptr;
  5. If (str-> length + length> = str-> max_length) // if a new string is added, the total length exceeds the buffer size.
  6. {
  7. // How many memories of alloc_increment need to be allocated to save the new string
  8. Size_t new_length = (str-> length + str-> alloc_increment )/
  9. Str-> alloc_increment;
  10. New_length * = str-> alloc_increment;

  11. If (! (New_ptr = (char *) my_realloc (str-> str, new_length, MYF (MY_WME ))))
  12. Return TRUE;
  13. Str-> str = new_ptr;
  14. Str-> max_length = new_length;
  15. }
  16. // Append the newly allocated content to str
  17. Memcpy (str-> str + str-> length, append, length );
  18. Str-> length + = length; // the new length of str after resizing
  19. Str-> str [str-> length] = 0;/* Safety for C programs * // The last character of the string is '\ 0'
  20. Return FALSE;
  21. }
From the code above, we can see that after the string is initialized, if you need to add new content to the string, you just need to dynamically realloc it based on the information stored previously. This struct records the complete string-related content, so dynamic resizing is very convenient.
Of course, in addition to this, there are also such as string truncation, initial string setting, and quotation marks for escape OS: truncation after the string offset is greater than N.

 
 
  1. my_bool dynstr_trunc(DYNAMIC_STRING *str, size_t n)
  2. {
  3. str->length-=n;
  4. str->str[str->length]= '\0';
  5. return FALSE;
  6. }
Returns the first occurrence of a character in a string. If no, the end address of the string is returned (pointing to '\ 0 ')

 
 
  1. char *strcend(register const char *s, register pchar c)
  2. {
  3. for (;;)
  4. {
  5. if (*s == (char) c) return (char*) s;
  6. if (!*s++) return (char*) s-1;
  7. }
  8. }
String content expansion:

 
 
  1. My_bool dynstr_realloc (DYNAMIC_STRING * str, size_t additional_size)
  2. {
  3. DBUG_ENTER ("dynstr_realloc ");

  4. If (! Additional_size) DBUG_RETURN (FALSE );
  5. If (str-> length + additional_size> str-> max_length) // if the content of the new string exceeds the maximum length of the buffer
  6. {
  7. Str-> max_length = (str-> length + additional_size + str-> alloc_increment-1 )/
  8. Str-> alloc_increment) * str-> alloc_increment;
  9. If (! (Str-> str = (char *) my_realloc (str-> str, str-> max_length, MYF (MY_WME ))))
  10. DBUG_RETURN (TRUE );
  11. }
  12. DBUG_RETURN (FALSE );
  13. }
Enclose strings in quotation marks and escape the single quotation marks. it is mainly used to execute some system commands (system (cmd )). For example, ls-al will be changed to \ 'ls-al \ '. for example, ls-a' l will be changed to \ 'ls-a \ 'L \'

 
 
  1. /*
  2. Concatenates any number of strings, escapes any OS quote in the result then
  3. surround the whole affair in another set of quotes which is finally appended
  4. to specified DYNAMIC_STRING. This function is especially useful when
  5. building strings to be executed with the system() function.

  6. @param str Dynamic String which will have addtional strings appended.
  7. @param append String to be appended.
  8. @param ... Optional. Additional string(s) to be appended.

  9. @note The final argument in the list must be NullS even if no additional
  10. options are passed.

  11. @return True = Success.
  12. */

  13. my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
  14. {

  15. const char *quote_str= "\'";
  16. const uint quote_len= 1;
  17. my_bool ret= TRUE;
  18. va_list dirty_text;

  19. ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
  20. va_start(dirty_text, append);
  21. while (append != NullS)
  22. {
  23. const char *cur_pos= append;
  24. const char *next_pos= cur_pos;

  25. /* Search for quote in each string and replace with escaped quote */
  26. while(*(next_pos= strcend(cur_pos, quote_str[0])) != '\0')
  27. {
  28. ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
  29. ret&= dynstr_append_mem(str ,"\\", 1);
  30. ret&= dynstr_append_mem(str, quote_str, quote_len);
  31. cur_pos= next_pos + 1;
  32. }
  33. ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
  34. append= va_arg(dirty_text, char *);
  35. }
  36. va_end(dirty_text);
  37. ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */

  38. return ret;
  39. }
By defining the structure information of a dynamic string, each time you add more characters to the string in multiple times, the string is dynamically resized based on the current length. And after each expansion, the structure records 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 unit ). In this way, it is very convenient to process dynamic strings.



Http://www.bkjia.com/PHPjc/1079090.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1079090.htmlTechArticleMySQL of dynamic string processing MySQL, often see some of the dynamic string processing, such as: DYNAMIC_STRING. To record the actual length of a dynamic string, the buffer is the most...

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.