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.

361 lines
10 KiB

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
26 years ago
26 years ago
26 years ago
  1. /*
  2. Copyright (c) 2000, 2010, 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_INTERFACE
  16. #pragma interface /* gcc class implementation */
  17. #endif
  18. #ifndef NOT_FIXED_DEC
  19. #define NOT_FIXED_DEC 31
  20. #endif
  21. class String;
  22. int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
  23. String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  24. uint32 copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
  25. const char *from, uint32 from_length,
  26. CHARSET_INFO *from_cs, uint *errors);
  27. class String
  28. {
  29. char *Ptr;
  30. uint32 str_length,Alloced_length;
  31. bool alloced;
  32. CHARSET_INFO *str_charset;
  33. public:
  34. String()
  35. {
  36. Ptr=0; str_length=Alloced_length=0; alloced=0;
  37. str_charset= &my_charset_bin;
  38. }
  39. String(uint32 length_arg)
  40. {
  41. alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
  42. str_charset= &my_charset_bin;
  43. }
  44. String(const char *str, CHARSET_INFO *cs)
  45. {
  46. Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
  47. str_charset=cs;
  48. }
  49. String(const char *str,uint32 len, CHARSET_INFO *cs)
  50. {
  51. Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
  52. str_charset=cs;
  53. }
  54. String(char *str,uint32 len, CHARSET_INFO *cs)
  55. {
  56. Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
  57. str_charset=cs;
  58. }
  59. String(const String &str)
  60. {
  61. Ptr=str.Ptr ; str_length=str.str_length ;
  62. Alloced_length=str.Alloced_length; alloced=0;
  63. str_charset=str.str_charset;
  64. }
  65. static void *operator new(size_t size, MEM_ROOT *mem_root)
  66. { return (void*) alloc_root(mem_root, (uint) size); }
  67. static void operator delete(void *ptr_arg, size_t size)
  68. {
  69. (void) ptr_arg;
  70. (void) size;
  71. TRASH(ptr_arg, size);
  72. }
  73. static void operator delete(void *, MEM_ROOT *)
  74. { /* never called */ }
  75. ~String() { free(); }
  76. inline void set_charset(CHARSET_INFO *charset_arg)
  77. { str_charset= charset_arg; }
  78. inline CHARSET_INFO *charset() const { return str_charset; }
  79. inline uint32 length() const { return str_length;}
  80. inline uint32 alloced_length() const { return Alloced_length;}
  81. inline char& operator [] (uint32 i) const { return Ptr[i]; }
  82. inline void length(uint32 len) { str_length=len ; }
  83. inline bool is_empty() { return (str_length == 0); }
  84. inline void mark_as_const() { Alloced_length= 0;}
  85. inline const char *ptr() const { return Ptr; }
  86. inline char *c_ptr()
  87. {
  88. if (!Ptr || Ptr[str_length]) /* Should be safe */
  89. (void) realloc(str_length);
  90. return Ptr;
  91. }
  92. inline char *c_ptr_quick()
  93. {
  94. if (Ptr && str_length < Alloced_length)
  95. Ptr[str_length]=0;
  96. return Ptr;
  97. }
  98. inline char *c_ptr_safe()
  99. {
  100. if (Ptr && str_length < Alloced_length)
  101. Ptr[str_length]=0;
  102. else
  103. (void) realloc(str_length);
  104. return Ptr;
  105. }
  106. void set(String &str,uint32 offset,uint32 arg_length)
  107. {
  108. DBUG_ASSERT(&str != this);
  109. free();
  110. Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
  111. if (str.Alloced_length)
  112. Alloced_length=str.Alloced_length-offset;
  113. else
  114. Alloced_length=0;
  115. str_charset=str.str_charset;
  116. }
  117. inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
  118. {
  119. free();
  120. Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
  121. str_charset=cs;
  122. }
  123. inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
  124. {
  125. free();
  126. Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
  127. str_charset=cs;
  128. }
  129. bool set_ascii(const char *str, uint32 arg_length);
  130. inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
  131. {
  132. if (!alloced)
  133. {
  134. Ptr=(char*) str; str_length=Alloced_length=arg_length;
  135. }
  136. str_charset=cs;
  137. }
  138. bool set(longlong num, CHARSET_INFO *cs);
  139. bool set(ulonglong num, CHARSET_INFO *cs);
  140. bool set(double num,uint decimals, CHARSET_INFO *cs);
  141. /*
  142. PMG 2004.11.12
  143. This is a method that works the same as perl's "chop". It simply
  144. drops the last character of a string. This is useful in the case
  145. of the federated storage handler where I'm building a unknown
  146. number, list of values and fields to be used in a sql insert
  147. statement to be run on the remote server, and have a comma after each.
  148. When the list is complete, I "chop" off the trailing comma
  149. ex.
  150. String stringobj;
  151. stringobj.append("VALUES ('foo', 'fi', 'fo',");
  152. stringobj.chop();
  153. stringobj.append(")");
  154. In this case, the value of string was:
  155. VALUES ('foo', 'fi', 'fo',
  156. VALUES ('foo', 'fi', 'fo'
  157. VALUES ('foo', 'fi', 'fo')
  158. */
  159. inline void chop()
  160. {
  161. Ptr[str_length--]= '\0';
  162. }
  163. inline void free()
  164. {
  165. if (alloced)
  166. {
  167. alloced=0;
  168. Alloced_length=0;
  169. my_free(Ptr,MYF(0));
  170. Ptr=0;
  171. str_length=0; /* Safety */
  172. }
  173. }
  174. inline bool alloc(uint32 arg_length)
  175. {
  176. if (arg_length < Alloced_length)
  177. return 0;
  178. return real_alloc(arg_length);
  179. }
  180. bool real_alloc(uint32 arg_length); // Empties old string
  181. bool realloc(uint32 arg_length);
  182. inline void shrink(uint32 arg_length) // Shrink buffer
  183. {
  184. if (arg_length < Alloced_length)
  185. {
  186. char *new_ptr;
  187. if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
  188. {
  189. Alloced_length = 0;
  190. real_alloc(arg_length);
  191. }
  192. else
  193. {
  194. Ptr=new_ptr;
  195. Alloced_length=arg_length;
  196. }
  197. }
  198. }
  199. bool is_alloced() { return alloced; }
  200. inline String& operator = (const String &s)
  201. {
  202. if (&s != this)
  203. {
  204. /*
  205. It is forbidden to do assignments like
  206. some_string = substring_of_that_string
  207. */
  208. DBUG_ASSERT(!s.uses_buffer_owned_by(this));
  209. free();
  210. Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
  211. alloced=0;
  212. }
  213. return *this;
  214. }
  215. bool copy(); // Alloc string if not alloced
  216. bool copy(const String &s); // Allocate new string
  217. bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
  218. static bool needs_conversion(uint32 arg_length,
  219. CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
  220. uint32 *offset);
  221. bool copy_aligned(const char *s, uint32 arg_length, uint32 offset,
  222. CHARSET_INFO *cs);
  223. bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs);
  224. bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom,
  225. CHARSET_INFO *csto, uint *errors);
  226. bool append(const String &s);
  227. bool append(const char *s);
  228. bool append(const char *s,uint32 arg_length);
  229. bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
  230. bool append(IO_CACHE* file, uint32 arg_length);
  231. bool append_with_prefill(const char *s, uint32 arg_length,
  232. uint32 full_length, char fill_char);
  233. int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  234. int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  235. bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
  236. bool replace(uint32 offset,uint32 arg_length,const String &to);
  237. inline bool append(char chr)
  238. {
  239. if (str_length < Alloced_length)
  240. {
  241. Ptr[str_length++]=chr;
  242. }
  243. else
  244. {
  245. if (realloc(str_length+1))
  246. return 1;
  247. Ptr[str_length++]=chr;
  248. }
  249. return 0;
  250. }
  251. bool fill(uint32 max_length,char fill);
  252. void strip_sp();
  253. friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
  254. friend int stringcmp(const String *a,const String *b);
  255. friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  256. uint32 numchars();
  257. int charpos(int i,uint32 offset=0);
  258. int reserve(uint32 space_needed)
  259. {
  260. return realloc(str_length + space_needed);
  261. }
  262. int reserve(uint32 space_needed, uint32 grow_by);
  263. /*
  264. The following append operations do NOT check alloced memory
  265. q_*** methods writes values of parameters itself
  266. qs_*** methods writes string representation of value
  267. */
  268. void q_append(const char c)
  269. {
  270. Ptr[str_length++] = c;
  271. }
  272. void q_append(const uint32 n)
  273. {
  274. int4store(Ptr + str_length, n);
  275. str_length += 4;
  276. }
  277. void q_append(double d)
  278. {
  279. float8store(Ptr + str_length, d);
  280. str_length += 8;
  281. }
  282. void q_append(double *d)
  283. {
  284. float8store(Ptr + str_length, *d);
  285. str_length += 8;
  286. }
  287. void q_append(const char *data, uint32 data_len)
  288. {
  289. memcpy(Ptr + str_length, data, data_len);
  290. str_length += data_len;
  291. }
  292. void write_at_position(int position, uint32 value)
  293. {
  294. int4store(Ptr + position,value);
  295. }
  296. void qs_append(const char *str, uint32 len);
  297. void qs_append(double d);
  298. void qs_append(double *d);
  299. inline void qs_append(const char c)
  300. {
  301. Ptr[str_length]= c;
  302. str_length++;
  303. }
  304. void qs_append(int i);
  305. void qs_append(uint i);
  306. /* Inline (general) functions used by the protocol functions */
  307. inline char *prep_append(uint32 arg_length, uint32 step_alloc)
  308. {
  309. uint32 new_length= arg_length + str_length;
  310. if (new_length > Alloced_length)
  311. {
  312. if (realloc(new_length + step_alloc))
  313. return 0;
  314. }
  315. uint32 old_length= str_length;
  316. str_length+= arg_length;
  317. return Ptr+ old_length; /* Area to use */
  318. }
  319. inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
  320. {
  321. uint32 new_length= arg_length + str_length;
  322. if (new_length > Alloced_length && realloc(new_length + step_alloc))
  323. return TRUE;
  324. memcpy(Ptr+str_length, s, arg_length);
  325. str_length+= arg_length;
  326. return FALSE;
  327. }
  328. void print(String *print);
  329. /* Swap two string objects. Efficient way to exchange data without memcpy. */
  330. void swap(String &s);
  331. inline bool uses_buffer_owned_by(const String *s) const
  332. {
  333. return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
  334. }
  335. };