MySQL Dynamic string Processing _php tutorial

Source: Internet
Author: User

MySQL Dynamic String processing


In MySQL, you will 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 each time the string needs to be adjusted, the new memory is allocated in time, and the length is adjusted. MySQL uses dynamic_string to save dynamic string-related information:

 
  
  
  1. typedef struct ST_DYNAMIC_STRING
  2. {
  3. Char *str;
  4. size_t length,max_length,alloc_increment;
In this struct, STR stores the first address of the actual string, length records the actual length of the string, how many characters the Max_length record string buffer can hold, and 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 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. }
As you can see from the above function, when initializing, the initial allocated string buffer size Init_alloc will be judged based on the string that needs to be initialized. After allocating the dynamic_string space, we initialize it based on the size of the buffer, the actual length of the string, and alloc_increment: length: The actual length of the string max_length: Maximum length of the buffer alloc_ Increment: When space is not enough, the next time you allocate memory for the cell size.

After initializing the content, if you need to add more characters to the buffer next time, you can determine whether the buffer needs to be expanded by 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 alloc_increment-sized memory needs to be allocated in order to save the new string
  8. size_t new_length= (str->length+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; New length of Str after expansion
  19. str->str[str->length]=0; /* Safety for C programs *///The last character of the string is ' \ s '
  20. return FALSE;
  21. }
As can be seen from the above code, after the initialization of the string, and then if you need to add new content to the string, just based on the previously stored information to the dynamic realloc. Because the structure records the complete string-related content, dynamic expansion is very easy to handle.
Of course, in addition to these, there are examples such as string truncation, string initialization, escaping OS quotes, and so on: the 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]= ' + ';
  5. return FALSE;
Returns the address of the first occurrence of a character in a string. If not, returns the address at the end of the string (point to '% ')

 
  
  
  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. }
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 new string content 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);
Enclose the string in quotation marks, escaping the single quotation mark, which is used primarily to execute some system commands (systems (CMD)). For example: Ls-al will become \ ' ls-al\ ' such as: Ls-a ' l will become ' 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 was especially useful when
  5. Building strings to is executed with the system () function.

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

  9. @note The final argument in the list must is 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]))! = ' + ')
  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 a string is added more characters, it is dynamically expanded according to the current length of the string. And after each expansion, the struct records the actual information of the current string (the length of the current string, the buffer can hold the length of the string, the unit length for expansion). This makes it very convenient to manipulate dynamic strings.



http://www.bkjia.com/PHPjc/1079090.html www.bkjia.com true http://www.bkjia.com/PHPjc/1079090.html techarticle mysql dynamic string processing in MySQL, often see some of the dynamic string processing, such as: Dynamic_string. In order to record the actual length of the 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.