You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1233 lines
29 KiB

26 years ago
26 years ago
26 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
  1. /*
  2. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. /* This file is originally from the mysql distribution. Coded by monty */
  15. #ifdef USE_PRAGMA_IMPLEMENTATION
  16. #pragma implementation // gcc: Class implementation
  17. #endif
  18. #include <my_global.h>
  19. #include <my_sys.h>
  20. #include <m_string.h>
  21. #include <m_ctype.h>
  22. #ifdef HAVE_FCONVERT
  23. #include <floatingpoint.h>
  24. #endif
  25. /*
  26. The following extern declarations are ok as these are interface functions
  27. required by the string function
  28. */
  29. extern uchar* sql_alloc(unsigned size);
  30. extern void sql_element_free(void *ptr);
  31. #include "sql_string.h"
  32. /*****************************************************************************
  33. ** String functions
  34. *****************************************************************************/
  35. bool String::real_alloc(uint32 arg_length)
  36. {
  37. arg_length=ALIGN_SIZE(arg_length+1);
  38. str_length=0;
  39. if (Alloced_length < arg_length)
  40. {
  41. free();
  42. if (!(Ptr=(char*) my_malloc(arg_length,MYF(MY_WME))))
  43. return TRUE;
  44. Alloced_length=arg_length;
  45. alloced=1;
  46. }
  47. Ptr[0]=0;
  48. return FALSE;
  49. }
  50. /**
  51. Allocates a new buffer on the heap for this String.
  52. - If the String's internal buffer is privately owned and heap allocated,
  53. one of the following is performed.
  54. - If the requested length is greater than what fits in the buffer, a new
  55. buffer is allocated, data moved and the old buffer freed.
  56. - If the requested length is less or equal to what fits in the buffer, a
  57. null character is inserted at the appropriate position.
  58. - If the String does not keep a private buffer on the heap, such a buffer
  59. will be allocated and the string copied accoring to its length, as found
  60. in String::length().
  61. For C compatibility, the new string buffer is null terminated.
  62. @param alloc_length The requested string size in characters, excluding any
  63. null terminator.
  64. @retval false Either the copy operation is complete or, if the size of the
  65. new buffer is smaller than the currently allocated buffer (if one exists),
  66. no allocation occured.
  67. @retval true An error occured when attempting to allocate memory.
  68. */
  69. bool String::realloc(uint32 alloc_length)
  70. {
  71. uint32 len=ALIGN_SIZE(alloc_length+1);
  72. if (Alloced_length < len)
  73. {
  74. char *new_ptr;
  75. if (alloced)
  76. {
  77. if (!(new_ptr= (char*) my_realloc(Ptr,len,MYF(MY_WME))))
  78. return TRUE; // Signal error
  79. }
  80. else if ((new_ptr= (char*) my_malloc(len,MYF(MY_WME))))
  81. {
  82. if (str_length > len - 1)
  83. str_length= 0;
  84. if (str_length) // Avoid bugs in memcpy on AIX
  85. memcpy(new_ptr,Ptr,str_length);
  86. new_ptr[str_length]=0;
  87. alloced=1;
  88. }
  89. else
  90. return TRUE; // Signal error
  91. Ptr= new_ptr;
  92. Alloced_length= len;
  93. }
  94. Ptr[alloc_length]=0; // This make other funcs shorter
  95. return FALSE;
  96. }
  97. bool String::set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs)
  98. {
  99. uint l=20*cs->mbmaxlen+1;
  100. int base= unsigned_flag ? 10 : -10;
  101. if (alloc(l))
  102. return TRUE;
  103. str_length=(uint32) (cs->cset->longlong10_to_str)(cs,Ptr,l,base,num);
  104. str_charset=cs;
  105. return FALSE;
  106. }
  107. bool String::set_real(double num,uint decimals, CHARSET_INFO *cs)
  108. {
  109. char buff[FLOATING_POINT_BUFFER];
  110. uint dummy_errors;
  111. str_charset=cs;
  112. if (decimals >= NOT_FIXED_DEC)
  113. {
  114. // Enough for a DATETIME
  115. uint32 len= sprintf(buff, "%.15g", num);
  116. return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
  117. }
  118. #ifdef HAVE_FCONVERT
  119. int decpt,sign;
  120. char *pos,*to;
  121. VOID(fconvert(num,(int) decimals,&decpt,&sign,buff+1));
  122. if (!my_isdigit(&my_charset_latin1, buff[1]))
  123. { // Nan or Inf
  124. pos=buff+1;
  125. if (sign)
  126. {
  127. buff[0]='-';
  128. pos=buff;
  129. }
  130. uint dummy_errors;
  131. return copy(pos,(uint32) strlen(pos), &my_charset_latin1, cs, &dummy_errors);
  132. }
  133. if (alloc((uint32) ((uint32) decpt+3+decimals)))
  134. return TRUE;
  135. to=Ptr;
  136. if (sign)
  137. *to++='-';
  138. pos=buff+1;
  139. if (decpt < 0)
  140. { /* value is < 0 */
  141. *to++='0';
  142. if (!decimals)
  143. goto end;
  144. *to++='.';
  145. if ((uint32) -decpt > decimals)
  146. decpt= - (int) decimals;
  147. decimals=(uint32) ((int) decimals+decpt);
  148. while (decpt++ < 0)
  149. *to++='0';
  150. }
  151. else if (decpt == 0)
  152. {
  153. *to++= '0';
  154. if (!decimals)
  155. goto end;
  156. *to++='.';
  157. }
  158. else
  159. {
  160. while (decpt-- > 0)
  161. *to++= *pos++;
  162. if (!decimals)
  163. goto end;
  164. *to++='.';
  165. }
  166. while (decimals--)
  167. *to++= *pos++;
  168. end:
  169. *to=0;
  170. str_length=(uint32) (to-Ptr);
  171. return FALSE;
  172. #else
  173. #ifdef HAVE_SNPRINTF
  174. buff[sizeof(buff)-1]=0; // Safety
  175. IF_DBUG(int num_chars= )
  176. snprintf(buff, sizeof(buff)-1, "%.*f",(int) decimals, num);
  177. DBUG_ASSERT(num_chars > 0);
  178. DBUG_ASSERT(num_chars < (int) sizeof(buff));
  179. #else
  180. sprintf(buff,"%.*f",(int) decimals,num);
  181. #endif
  182. return copy(buff,(uint32) strlen(buff), &my_charset_latin1, cs,
  183. &dummy_errors);
  184. #endif
  185. }
  186. bool String::copy()
  187. {
  188. if (!alloced)
  189. {
  190. Alloced_length=0; // Force realloc
  191. return realloc(str_length);
  192. }
  193. return FALSE;
  194. }
  195. /**
  196. Copies the internal buffer from str. If this String has a private heap
  197. allocated buffer where new data does not fit, a new buffer is allocated
  198. before copying and the old buffer freed. Character set information is also
  199. copied.
  200. @param str The string whose internal buffer is to be copied.
  201. @retval false Success.
  202. @retval true Memory allocation failed.
  203. */
  204. bool String::copy(const String &str)
  205. {
  206. if (alloc(str.str_length))
  207. return TRUE;
  208. str_length=str.str_length;
  209. bmove(Ptr,str.Ptr,str_length); // May be overlapping
  210. Ptr[str_length]=0;
  211. str_charset=str.str_charset;
  212. return FALSE;
  213. }
  214. bool String::copy(const char *str,uint32 arg_length, CHARSET_INFO *cs)
  215. {
  216. if (alloc(arg_length))
  217. return TRUE;
  218. if ((str_length=arg_length))
  219. memcpy(Ptr,str,arg_length);
  220. Ptr[arg_length]=0;
  221. str_charset=cs;
  222. return FALSE;
  223. }
  224. /*
  225. Checks that the source string can be just copied to the destination string
  226. without conversion.
  227. SYNPOSIS
  228. needs_conversion()
  229. arg_length Length of string to copy.
  230. from_cs Character set to copy from
  231. to_cs Character set to copy to
  232. uint32 *offset Returns number of unaligned characters.
  233. RETURN
  234. 0 No conversion needed
  235. 1 Either character set conversion or adding leading zeros
  236. (e.g. for UCS-2) must be done
  237. NOTE
  238. to_cs may be NULL for "no conversion" if the system variable
  239. character_set_results is NULL.
  240. */
  241. bool String::needs_conversion(uint32 arg_length,
  242. CHARSET_INFO *from_cs,
  243. CHARSET_INFO *to_cs,
  244. uint32 *offset)
  245. {
  246. *offset= 0;
  247. if (!to_cs ||
  248. (to_cs == &my_charset_bin) ||
  249. (to_cs == from_cs) ||
  250. my_charset_same(from_cs, to_cs) ||
  251. ((from_cs == &my_charset_bin) &&
  252. (!(*offset=(arg_length % to_cs->mbminlen)))))
  253. return FALSE;
  254. return TRUE;
  255. }
  256. /*
  257. Copy a multi-byte character sets with adding leading zeros.
  258. SYNOPSIS
  259. copy_aligned()
  260. str String to copy
  261. arg_length Length of string. This should NOT be dividable with
  262. cs->mbminlen.
  263. offset arg_length % cs->mb_minlength
  264. cs Character set for 'str'
  265. NOTES
  266. For real multi-byte, ascii incompatible charactser sets,
  267. like UCS-2, add leading zeros if we have an incomplete character.
  268. Thus,
  269. SELECT _ucs2 0xAA
  270. will automatically be converted into
  271. SELECT _ucs2 0x00AA
  272. RETURN
  273. 0 ok
  274. 1 error
  275. */
  276. bool String::copy_aligned(const char *str,uint32 arg_length, uint32 offset,
  277. CHARSET_INFO *cs)
  278. {
  279. /* How many bytes are in incomplete character */
  280. offset= cs->mbmaxlen - offset; /* How many zeros we should prepend */
  281. DBUG_ASSERT(offset && offset != cs->mbmaxlen);
  282. uint32 aligned_length= arg_length + offset;
  283. if (alloc(aligned_length))
  284. return TRUE;
  285. /*
  286. Note, this is only safe for big-endian UCS-2.
  287. If we add little-endian UCS-2 sometimes, this code
  288. will be more complicated. But it's OK for now.
  289. */
  290. bzero((char*) Ptr, offset);
  291. memcpy(Ptr + offset, str, arg_length);
  292. Ptr[aligned_length]=0;
  293. /* str_length is always >= 0 as arg_length is != 0 */
  294. str_length= aligned_length;
  295. str_charset= cs;
  296. return FALSE;
  297. }
  298. bool String::set_or_copy_aligned(const char *str,uint32 arg_length,
  299. CHARSET_INFO *cs)
  300. {
  301. /* How many bytes are in incomplete character */
  302. uint32 offset= (arg_length % cs->mbminlen);
  303. if (!offset) /* All characters are complete, just copy */
  304. {
  305. set(str, arg_length, cs);
  306. return FALSE;
  307. }
  308. return copy_aligned(str, arg_length, offset, cs);
  309. }
  310. /**
  311. Copies the character data into this String, with optional character set
  312. conversion.
  313. @return
  314. FALSE ok
  315. TRUE Could not allocate result buffer
  316. */
  317. bool String::copy(const char *str, uint32 arg_length,
  318. CHARSET_INFO *from_cs, CHARSET_INFO *to_cs, uint *errors)
  319. {
  320. uint32 offset;
  321. DBUG_ASSERT(!str || str != Ptr);
  322. if (!needs_conversion(arg_length, from_cs, to_cs, &offset))
  323. {
  324. *errors= 0;
  325. return copy(str, arg_length, to_cs);
  326. }
  327. if ((from_cs == &my_charset_bin) && offset)
  328. {
  329. *errors= 0;
  330. return copy_aligned(str, arg_length, offset, to_cs);
  331. }
  332. uint32 new_length= to_cs->mbmaxlen*arg_length;
  333. if (alloc(new_length))
  334. return TRUE;
  335. str_length=copy_and_convert((char*) Ptr, new_length, to_cs,
  336. str, arg_length, from_cs, errors);
  337. str_charset=to_cs;
  338. return FALSE;
  339. }
  340. /*
  341. Set a string to the value of a latin1-string, keeping the original charset
  342. SYNOPSIS
  343. copy_or_set()
  344. str String of a simple charset (latin1)
  345. arg_length Length of string
  346. IMPLEMENTATION
  347. If string object is of a simple character set, set it to point to the
  348. given string.
  349. If not, make a copy and convert it to the new character set.
  350. RETURN
  351. 0 ok
  352. 1 Could not allocate result buffer
  353. */
  354. bool String::set_ascii(const char *str, uint32 arg_length)
  355. {
  356. if (str_charset->mbminlen == 1)
  357. {
  358. set(str, arg_length, str_charset);
  359. return 0;
  360. }
  361. uint dummy_errors;
  362. return copy(str, arg_length, &my_charset_latin1, str_charset, &dummy_errors);
  363. }
  364. /* This is used by mysql.cc */
  365. bool String::fill(uint32 max_length,char fill_char)
  366. {
  367. if (str_length > max_length)
  368. Ptr[str_length=max_length]=0;
  369. else
  370. {
  371. if (realloc(max_length))
  372. return TRUE;
  373. bfill(Ptr+str_length,max_length-str_length,fill_char);
  374. str_length=max_length;
  375. }
  376. return FALSE;
  377. }
  378. void String::strip_sp()
  379. {
  380. while (str_length && my_isspace(str_charset,Ptr[str_length-1]))
  381. str_length--;
  382. }
  383. bool String::append(const String &s)
  384. {
  385. if (s.length())
  386. {
  387. if (realloc(str_length+s.length()))
  388. return TRUE;
  389. memcpy(Ptr+str_length,s.ptr(),s.length());
  390. str_length+=s.length();
  391. }
  392. return FALSE;
  393. }
  394. /*
  395. Append an ASCII string to the a string of the current character set
  396. */
  397. bool String::append(const char *s,uint32 arg_length)
  398. {
  399. if (!arg_length)
  400. return FALSE;
  401. /*
  402. For an ASCII incompatible string, e.g. UCS-2, we need to convert
  403. */
  404. if (str_charset->mbminlen > 1)
  405. {
  406. uint32 add_length=arg_length * str_charset->mbmaxlen;
  407. uint dummy_errors;
  408. if (realloc(str_length+ add_length))
  409. return TRUE;
  410. str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset,
  411. s, arg_length, &my_charset_latin1,
  412. &dummy_errors);
  413. return FALSE;
  414. }
  415. /*
  416. For an ASCII compatinble string we can just append.
  417. */
  418. if (realloc(str_length+arg_length))
  419. return TRUE;
  420. memcpy(Ptr+str_length,s,arg_length);
  421. str_length+=arg_length;
  422. return FALSE;
  423. }
  424. /*
  425. Append a 0-terminated ASCII string
  426. */
  427. bool String::append(const char *s)
  428. {
  429. return append(s, (uint) strlen(s));
  430. }
  431. /*
  432. Append a string in the given charset to the string
  433. with character set recoding
  434. */
  435. bool String::append(const char *s,uint32 arg_length, CHARSET_INFO *cs)
  436. {
  437. uint32 dummy_offset;
  438. if (needs_conversion(arg_length, cs, str_charset, &dummy_offset))
  439. {
  440. uint32 add_length= arg_length / cs->mbminlen * str_charset->mbmaxlen;
  441. uint dummy_errors;
  442. if (realloc(str_length + add_length))
  443. return TRUE;
  444. str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset,
  445. s, arg_length, cs, &dummy_errors);
  446. }
  447. else
  448. {
  449. if (realloc(str_length + arg_length))
  450. return TRUE;
  451. memcpy(Ptr + str_length, s, arg_length);
  452. str_length+= arg_length;
  453. }
  454. return FALSE;
  455. }
  456. #ifdef TO_BE_REMOVED
  457. bool String::append(FILE* file, uint32 arg_length, myf my_flags)
  458. {
  459. if (realloc(str_length+arg_length))
  460. return TRUE;
  461. if (my_fread(file, (uchar*) Ptr + str_length, arg_length, my_flags))
  462. {
  463. shrink(str_length);
  464. return TRUE;
  465. }
  466. str_length+=arg_length;
  467. return FALSE;
  468. }
  469. #endif
  470. bool String::append(IO_CACHE* file, uint32 arg_length)
  471. {
  472. if (realloc(str_length+arg_length))
  473. return TRUE;
  474. if (my_b_read(file, (uchar*) Ptr + str_length, arg_length))
  475. {
  476. shrink(str_length);
  477. return TRUE;
  478. }
  479. str_length+=arg_length;
  480. return FALSE;
  481. }
  482. bool String::append_with_prefill(const char *s,uint32 arg_length,
  483. uint32 full_length, char fill_char)
  484. {
  485. int t_length= arg_length > full_length ? arg_length : full_length;
  486. if (realloc(str_length + t_length))
  487. return TRUE;
  488. t_length= full_length - arg_length;
  489. if (t_length > 0)
  490. {
  491. bfill(Ptr+str_length, t_length, fill_char);
  492. str_length=str_length + t_length;
  493. }
  494. append(s, arg_length);
  495. return FALSE;
  496. }
  497. uint32 String::numchars()
  498. {
  499. return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
  500. }
  501. int String::charpos(int i,uint32 offset)
  502. {
  503. if (i <= 0)
  504. return i;
  505. return str_charset->cset->charpos(str_charset,Ptr+offset,Ptr+str_length,i);
  506. }
  507. int String::strstr(const String &s,uint32 offset)
  508. {
  509. if (s.length()+offset <= str_length)
  510. {
  511. if (!s.length())
  512. return ((int) offset); // Empty string is always found
  513. register const char *str = Ptr+offset;
  514. register const char *search=s.ptr();
  515. const char *end=Ptr+str_length-s.length()+1;
  516. const char *search_end=s.ptr()+s.length();
  517. skip:
  518. while (str != end)
  519. {
  520. if (*str++ == *search)
  521. {
  522. register char *i,*j;
  523. i=(char*) str; j=(char*) search+1;
  524. while (j != search_end)
  525. if (*i++ != *j++) goto skip;
  526. return (int) (str-Ptr) -1;
  527. }
  528. }
  529. }
  530. return -1;
  531. }
  532. /*
  533. ** Search string from end. Offset is offset to the end of string
  534. */
  535. int String::strrstr(const String &s,uint32 offset)
  536. {
  537. if (s.length() <= offset && offset <= str_length)
  538. {
  539. if (!s.length())
  540. return offset; // Empty string is always found
  541. register const char *str = Ptr+offset-1;
  542. register const char *search=s.ptr()+s.length()-1;
  543. const char *end=Ptr+s.length()-2;
  544. const char *search_end=s.ptr()-1;
  545. skip:
  546. while (str != end)
  547. {
  548. if (*str-- == *search)
  549. {
  550. register char *i,*j;
  551. i=(char*) str; j=(char*) search-1;
  552. while (j != search_end)
  553. if (*i-- != *j--) goto skip;
  554. return (int) (i-Ptr) +1;
  555. }
  556. }
  557. }
  558. return -1;
  559. }
  560. /*
  561. Replace substring with string
  562. If wrong parameter or not enough memory, do nothing
  563. */
  564. bool String::replace(uint32 offset,uint32 arg_length,const String &to)
  565. {
  566. return replace(offset,arg_length,to.ptr(),to.length());
  567. }
  568. bool String::replace(uint32 offset,uint32 arg_length,
  569. const char *to, uint32 to_length)
  570. {
  571. long diff = (long) to_length-(long) arg_length;
  572. if (offset+arg_length <= str_length)
  573. {
  574. if (diff < 0)
  575. {
  576. if (to_length)
  577. memcpy(Ptr+offset,to,to_length);
  578. bmove(Ptr+offset+to_length,Ptr+offset+arg_length,
  579. str_length-offset-arg_length);
  580. }
  581. else
  582. {
  583. if (diff)
  584. {
  585. if (realloc(str_length+(uint32) diff))
  586. return TRUE;
  587. bmove_upp((uchar*) Ptr+str_length+diff, (uchar*) Ptr+str_length,
  588. str_length-offset-arg_length);
  589. }
  590. if (to_length)
  591. memcpy(Ptr+offset,to,to_length);
  592. }
  593. str_length+=(uint32) diff;
  594. }
  595. return FALSE;
  596. }
  597. // added by Holyfoot for "geometry" needs
  598. int String::reserve(uint32 space_needed, uint32 grow_by)
  599. {
  600. if (Alloced_length < str_length + space_needed)
  601. {
  602. if (realloc(Alloced_length + max(space_needed, grow_by) - 1))
  603. return TRUE;
  604. }
  605. return FALSE;
  606. }
  607. void String::qs_append(const char *str, uint32 len)
  608. {
  609. memcpy(Ptr + str_length, str, len + 1);
  610. str_length += len;
  611. }
  612. void String::qs_append(double d)
  613. {
  614. char *buff = Ptr + str_length;
  615. str_length+= sprintf(buff, "%.15g", d);
  616. }
  617. void String::qs_append(double *d)
  618. {
  619. double ld;
  620. float8get(ld, (char*) d);
  621. qs_append(ld);
  622. }
  623. void String::qs_append(int i)
  624. {
  625. char *buff= Ptr + str_length;
  626. char *end= int10_to_str(i, buff, -10);
  627. str_length+= (int) (end-buff);
  628. }
  629. void String::qs_append(uint i)
  630. {
  631. char *buff= Ptr + str_length;
  632. char *end= int10_to_str(i, buff, 10);
  633. str_length+= (int) (end-buff);
  634. }
  635. /*
  636. Compare strings according to collation, without end space.
  637. SYNOPSIS
  638. sortcmp()
  639. s First string
  640. t Second string
  641. cs Collation
  642. NOTE:
  643. Normally this is case sensitive comparison
  644. RETURN
  645. < 0 s < t
  646. 0 s == t
  647. > 0 s > t
  648. */
  649. int sortcmp(const String *s,const String *t, CHARSET_INFO *cs)
  650. {
  651. return cs->coll->strnncollsp(cs,
  652. (uchar *) s->ptr(),s->length(),
  653. (uchar *) t->ptr(),t->length(), 0);
  654. }
  655. /*
  656. Compare strings byte by byte. End spaces are also compared.
  657. SYNOPSIS
  658. stringcmp()
  659. s First string
  660. t Second string
  661. NOTE:
  662. Strings are compared as a stream of uchars
  663. RETURN
  664. < 0 s < t
  665. 0 s == t
  666. > 0 s > t
  667. */
  668. int stringcmp(const String *s,const String *t)
  669. {
  670. uint32 s_len=s->length(),t_len=t->length(),len=min(s_len,t_len);
  671. int cmp= memcmp(s->ptr(), t->ptr(), len);
  672. return (cmp) ? cmp : (int) (s_len - t_len);
  673. }
  674. String *copy_if_not_alloced(String *to,String *from,uint32 from_length)
  675. {
  676. if (from->Alloced_length >= from_length)
  677. return from;
  678. if (from->alloced || !to || from == to)
  679. {
  680. (void) from->realloc(from_length);
  681. return from;
  682. }
  683. if (to->realloc(from_length))
  684. return from; // Actually an error
  685. if ((to->str_length=min(from->str_length,from_length)))
  686. memcpy(to->Ptr,from->Ptr,to->str_length);
  687. to->str_charset=from->str_charset;
  688. return to;
  689. }
  690. /****************************************************************************
  691. Help functions
  692. ****************************************************************************/
  693. /*
  694. copy a string from one character set to another
  695. SYNOPSIS
  696. copy_and_convert()
  697. to Store result here
  698. to_cs Character set of result string
  699. from Copy from here
  700. from_length Length of from string
  701. from_cs From character set
  702. NOTES
  703. 'to' must be big enough as form_length * to_cs->mbmaxlen
  704. RETURN
  705. length of bytes copied to 'to'
  706. */
  707. uint32
  708. copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
  709. const char *from, uint32 from_length, CHARSET_INFO *from_cs,
  710. uint *errors)
  711. {
  712. int cnvres;
  713. my_wc_t wc;
  714. const uchar *from_end= (const uchar*) from+from_length;
  715. char *to_start= to;
  716. uchar *to_end= (uchar*) to+to_length;
  717. my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc;
  718. my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
  719. uint error_count= 0;
  720. while (1)
  721. {
  722. if ((cnvres= (*mb_wc)(from_cs, &wc, (uchar*) from,
  723. from_end)) > 0)
  724. from+= cnvres;
  725. else if (cnvres == MY_CS_ILSEQ)
  726. {
  727. error_count++;
  728. from++;
  729. wc= '?';
  730. }
  731. else if (cnvres > MY_CS_TOOSMALL)
  732. {
  733. /*
  734. A correct multibyte sequence detected
  735. But it doesn't have Unicode mapping.
  736. */
  737. error_count++;
  738. from+= (-cnvres);
  739. wc= '?';
  740. }
  741. else
  742. break; // Not enough characters
  743. outp:
  744. if ((cnvres= (*wc_mb)(to_cs, wc, (uchar*) to, to_end)) > 0)
  745. to+= cnvres;
  746. else if (cnvres == MY_CS_ILUNI && wc != '?')
  747. {
  748. error_count++;
  749. wc= '?';
  750. goto outp;
  751. }
  752. else
  753. break;
  754. }
  755. *errors= error_count;
  756. return (uint32) (to - to_start);
  757. }
  758. /**
  759. Copy string with HEX-encoding of "bad" characters.
  760. @details This functions copies the string pointed by "src"
  761. to the string pointed by "dst". Not more than "srclen" bytes
  762. are read from "src". Any sequences of bytes representing
  763. a not-well-formed substring (according to cs) are hex-encoded,
  764. and all well-formed substrings (according to cs) are copied as is.
  765. Not more than "dstlen" bytes are written to "dst". The number
  766. of bytes written to "dst" is returned.
  767. @param cs character set pointer of the destination string
  768. @param[out] dst destination string
  769. @param dstlen size of dst
  770. @param src source string
  771. @param srclen length of src
  772. @retval result length
  773. */
  774. size_t
  775. my_copy_with_hex_escaping(CHARSET_INFO *cs,
  776. char *dst, size_t dstlen,
  777. const char *src, size_t srclen)
  778. {
  779. const char *srcend= src + srclen;
  780. char *dst0= dst;
  781. for ( ; src < srcend ; )
  782. {
  783. size_t chlen;
  784. if ((chlen= my_ismbchar(cs, src, srcend)))
  785. {
  786. if (dstlen < chlen)
  787. break; /* purecov: inspected */
  788. memcpy(dst, src, chlen);
  789. src+= chlen;
  790. dst+= chlen;
  791. dstlen-= chlen;
  792. }
  793. else if (*src & 0x80)
  794. {
  795. if (dstlen < 4)
  796. break; /* purecov: inspected */
  797. *dst++= '\\';
  798. *dst++= 'x';
  799. *dst++= _dig_vec_upper[((unsigned char) *src) >> 4];
  800. *dst++= _dig_vec_upper[((unsigned char) *src) & 15];
  801. src++;
  802. dstlen-= 4;
  803. }
  804. else
  805. {
  806. if (dstlen < 1)
  807. break; /* purecov: inspected */
  808. *dst++= *src++;
  809. dstlen--;
  810. }
  811. }
  812. return dst - dst0;
  813. }
  814. /*
  815. copy a string,
  816. with optional character set conversion,
  817. with optional left padding (for binary -> UCS2 conversion)
  818. SYNOPSIS
  819. well_formed_copy_nchars()
  820. to Store result here
  821. to_length Maxinum length of "to" string
  822. to_cs Character set of "to" string
  823. from Copy from here
  824. from_length Length of from string
  825. from_cs From character set
  826. nchars Copy not more that nchars characters
  827. well_formed_error_pos Return position when "from" is not well formed
  828. or NULL otherwise.
  829. cannot_convert_error_pos Return position where a not convertable
  830. character met, or NULL otherwise.
  831. from_end_pos Return position where scanning of "from"
  832. string stopped.
  833. NOTES
  834. RETURN
  835. length of bytes copied to 'to'
  836. */
  837. uint32
  838. well_formed_copy_nchars(CHARSET_INFO *to_cs,
  839. char *to, uint to_length,
  840. CHARSET_INFO *from_cs,
  841. const char *from, uint from_length,
  842. uint nchars,
  843. const char **well_formed_error_pos,
  844. const char **cannot_convert_error_pos,
  845. const char **from_end_pos)
  846. {
  847. uint res;
  848. if ((to_cs == &my_charset_bin) ||
  849. (from_cs == &my_charset_bin) ||
  850. (to_cs == from_cs) ||
  851. my_charset_same(from_cs, to_cs))
  852. {
  853. if (to_length < to_cs->mbminlen || !nchars)
  854. {
  855. *from_end_pos= from;
  856. *cannot_convert_error_pos= NULL;
  857. *well_formed_error_pos= NULL;
  858. return 0;
  859. }
  860. if (to_cs == &my_charset_bin)
  861. {
  862. res= min(min(nchars, to_length), from_length);
  863. memmove(to, from, res);
  864. *from_end_pos= from + res;
  865. *well_formed_error_pos= NULL;
  866. *cannot_convert_error_pos= NULL;
  867. }
  868. else
  869. {
  870. int well_formed_error;
  871. uint from_offset;
  872. if ((from_offset= (from_length % to_cs->mbminlen)) &&
  873. (from_cs == &my_charset_bin))
  874. {
  875. /*
  876. Copying from BINARY to UCS2 needs to prepend zeros sometimes:
  877. INSERT INTO t1 (ucs2_column) VALUES (0x01);
  878. 0x01 -> 0x0001
  879. */
  880. uint pad_length= to_cs->mbminlen - from_offset;
  881. bzero(to, pad_length);
  882. memmove(to + pad_length, from, from_offset);
  883. nchars--;
  884. from+= from_offset;
  885. from_length-= from_offset;
  886. to+= to_cs->mbminlen;
  887. to_length-= to_cs->mbminlen;
  888. }
  889. set_if_smaller(from_length, to_length);
  890. res= to_cs->cset->well_formed_len(to_cs, from, from + from_length,
  891. nchars, &well_formed_error);
  892. memmove(to, from, res);
  893. *from_end_pos= from + res;
  894. *well_formed_error_pos= well_formed_error ? from + res : NULL;
  895. *cannot_convert_error_pos= NULL;
  896. if (from_offset)
  897. res+= to_cs->mbminlen;
  898. }
  899. }
  900. else
  901. {
  902. int cnvres;
  903. my_wc_t wc;
  904. my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc;
  905. my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
  906. const uchar *from_end= (const uchar*) from + from_length;
  907. uchar *to_end= (uchar*) to + to_length;
  908. char *to_start= to;
  909. *well_formed_error_pos= NULL;
  910. *cannot_convert_error_pos= NULL;
  911. for ( ; nchars; nchars--)
  912. {
  913. const char *from_prev= from;
  914. if ((cnvres= (*mb_wc)(from_cs, &wc, (uchar*) from, from_end)) > 0)
  915. from+= cnvres;
  916. else if (cnvres == MY_CS_ILSEQ)
  917. {
  918. if (!*well_formed_error_pos)
  919. *well_formed_error_pos= from;
  920. from++;
  921. wc= '?';
  922. }
  923. else if (cnvres > MY_CS_TOOSMALL)
  924. {
  925. /*
  926. A correct multibyte sequence detected
  927. But it doesn't have Unicode mapping.
  928. */
  929. if (!*cannot_convert_error_pos)
  930. *cannot_convert_error_pos= from;
  931. from+= (-cnvres);
  932. wc= '?';
  933. }
  934. else
  935. break; // Not enough characters
  936. outp:
  937. if ((cnvres= (*wc_mb)(to_cs, wc, (uchar*) to, to_end)) > 0)
  938. to+= cnvres;
  939. else if (cnvres == MY_CS_ILUNI && wc != '?')
  940. {
  941. if (!*cannot_convert_error_pos)
  942. *cannot_convert_error_pos= from_prev;
  943. wc= '?';
  944. goto outp;
  945. }
  946. else
  947. {
  948. from= from_prev;
  949. break;
  950. }
  951. }
  952. *from_end_pos= from;
  953. res= (uint) (to - to_start);
  954. }
  955. return (uint32) res;
  956. }
  957. void String::print(String *str)
  958. {
  959. char *st= (char*)Ptr, *end= st+str_length;
  960. for (; st < end; st++)
  961. {
  962. uchar c= *st;
  963. switch (c)
  964. {
  965. case '\\':
  966. str->append(STRING_WITH_LEN("\\\\"));
  967. break;
  968. case '\0':
  969. str->append(STRING_WITH_LEN("\\0"));
  970. break;
  971. case '\'':
  972. str->append(STRING_WITH_LEN("\\'"));
  973. break;
  974. case '\n':
  975. str->append(STRING_WITH_LEN("\\n"));
  976. break;
  977. case '\r':
  978. str->append(STRING_WITH_LEN("\\r"));
  979. break;
  980. case '\032': // Ctrl-Z
  981. str->append(STRING_WITH_LEN("\\Z"));
  982. break;
  983. default:
  984. str->append(c);
  985. }
  986. }
  987. }
  988. /*
  989. Exchange state of this object and argument.
  990. SYNOPSIS
  991. String::swap()
  992. RETURN
  993. Target string will contain state of this object and vice versa.
  994. */
  995. void String::swap(String &s)
  996. {
  997. swap_variables(char *, Ptr, s.Ptr);
  998. swap_variables(uint32, str_length, s.str_length);
  999. swap_variables(uint32, Alloced_length, s.Alloced_length);
  1000. swap_variables(bool, alloced, s.alloced);
  1001. swap_variables(CHARSET_INFO*, str_charset, s.str_charset);
  1002. }
  1003. /**
  1004. Convert string to printable ASCII string
  1005. @details This function converts input string "from" replacing non-ASCII bytes
  1006. with hexadecimal sequences ("\xXX") optionally appending "..." to the end of
  1007. the resulting string.
  1008. This function used in the ER_TRUNCATED_WRONG_VALUE_FOR_FIELD error messages,
  1009. e.g. when a string cannot be converted to a result charset.
  1010. @param to output buffer
  1011. @param to_len size of the output buffer (8 bytes or greater)
  1012. @param from input string
  1013. @param from_len size of the input string
  1014. @param from_cs input charset
  1015. @param nbytes maximal number of bytes to convert (from_len if 0)
  1016. @return number of bytes in the output string
  1017. */
  1018. uint convert_to_printable(char *to, size_t to_len,
  1019. const char *from, size_t from_len,
  1020. CHARSET_INFO *from_cs, size_t nbytes /*= 0*/)
  1021. {
  1022. /* needs at least 8 bytes for '\xXX...' and zero byte */
  1023. DBUG_ASSERT(to_len >= 8);
  1024. char *t= to;
  1025. char *t_end= to + to_len - 1; // '- 1' is for the '\0' at the end
  1026. const char *f= from;
  1027. const char *f_end= from + (nbytes ? min(from_len, nbytes) : from_len);
  1028. char *dots= to; // last safe place to append '...'
  1029. if (!f || t == t_end)
  1030. return 0;
  1031. for (; t < t_end && f < f_end; f++)
  1032. {
  1033. /*
  1034. If the source string is ASCII compatible (mbminlen==1)
  1035. and the source character is in ASCII printable range (0x20..0x7F),
  1036. then display the character as is.
  1037. Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
  1038. or the source character is not in the printable range,
  1039. then print the character using HEX notation.
  1040. */
  1041. if (((unsigned char) *f) >= 0x20 &&
  1042. ((unsigned char) *f) <= 0x7F &&
  1043. from_cs->mbminlen == 1)
  1044. {
  1045. *t++= *f;
  1046. }
  1047. else
  1048. {
  1049. if (t_end - t < 4) // \xXX
  1050. break;
  1051. *t++= '\\';
  1052. *t++= 'x';
  1053. *t++= _dig_vec_upper[((unsigned char) *f) >> 4];
  1054. *t++= _dig_vec_upper[((unsigned char) *f) & 0x0F];
  1055. }
  1056. if (t_end - t >= 3) // '...'
  1057. dots= t;
  1058. }
  1059. if (f < from + from_len)
  1060. memcpy(dots, STRING_WITH_LEN("...\0"));
  1061. else
  1062. *t= '\0';
  1063. return t - to;
  1064. }