Skip to content

Commit f813ac2

Browse files
cvicentiuvuvova
authored andcommitted
Introduce String::append_float
This method will write out a float to a String object, keeping the charset of the original string. Also have Float::to_string make use of String::append_float
1 parent 26e599c commit f813ac2

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

sql/sql_string.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ bool Binary_string::realloc_raw(size_t alloc_length)
118118
}
119119

120120

121+
static uint32 write_float_str_to_buff(char *buff, int buff_len,
122+
float num, uint decimals)
123+
{
124+
if (decimals >= FLOATING_POINT_DECIMALS)
125+
return (uint32)my_gcvt(num, MY_GCVT_ARG_FLOAT, buff_len - 1, buff, NULL);
126+
else
127+
return (uint32)my_fcvt(num, decimals, buff, NULL);
128+
}
129+
130+
bool String::append_float(float num, uint decimals)
131+
{
132+
uint dummy_errors;
133+
size_t len;
134+
DBUG_ASSERT(!std::isnan(num));
135+
DBUG_ASSERT(!std::isinf(num));
136+
if (realloc_with_extra_if_needed(str_length + FLOATING_POINT_BUFFER))
137+
return true;
138+
139+
if (charset()->mbminlen > 1)
140+
{
141+
char buff[FLOATING_POINT_BUFFER];
142+
len= write_float_str_to_buff(buff, sizeof(buff), num, decimals);
143+
str_length+= copy_and_convert(Ptr + str_length, len, charset(), buff, len,
144+
&my_charset_latin1, &dummy_errors);
145+
}
146+
else
147+
str_length+= write_float_str_to_buff(Ptr + str_length,
148+
FLOATING_POINT_BUFFER, num, decimals);
149+
return false;
150+
}
151+
152+
121153
bool String::set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs)
122154
{
123155
/*

sql/sql_string.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020

2121
/* This file is originally from the mysql distribution. Coded by monty */
2222

23+
#include <my_global.h>
24+
#include <cmath>
25+
2326
#include "m_ctype.h" /* my_charset_bin */
2427
#include <my_sys.h> /* alloc_root, my_free, my_realloc */
2528
#include "m_string.h" /* TRASH */
29+
#include "sql_const.h"
2630
#include "sql_list.h"
2731

2832
class String;
@@ -1007,6 +1011,12 @@ class String: public Charset, public Binary_string
10071011
{
10081012
return Binary_string::append(s);
10091013
}
1014+
1015+
// Append a float value to the string keeping the string's charset.
1016+
//
1017+
// The float value must not be NaN or Inf, it will be represented as 0 in
1018+
// that case.
1019+
bool append_float(float num, uint decimals);
10101020
inline bool append(char chr)
10111021
{
10121022
return Binary_string::append_char(chr);

sql/sql_type.cc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,9 @@ Type_handler_data *type_handler_data= NULL;
328328

329329
bool Float::to_string(String *val_buffer, uint dec) const
330330
{
331-
uint to_length= 70;
332-
if (val_buffer->alloc(to_length))
333-
return true;
334-
335-
char *to=(char*) val_buffer->ptr();
336-
size_t len;
337-
338-
if (dec >= FLOATING_POINT_DECIMALS)
339-
len= my_gcvt(m_value, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
340-
else
341-
{
342-
/*
343-
We are safe here because the buffer length is 70, and
344-
fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
345-
will be not longer than 69 chars + terminating '\0'.
346-
*/
347-
len= my_fcvt(m_value, (int) dec, to, NULL);
348-
}
349-
val_buffer->length((uint) len);
350331
val_buffer->set_charset(&my_charset_numeric);
351-
return false;
332+
val_buffer->length(0);
333+
return val_buffer->append_float(m_value, dec);
352334
}
353335

354336

0 commit comments

Comments
 (0)