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.

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