MySQL 8.0.37
Source Code Documentation
m_string.h File Reference
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include "decimal.h"
#include "lex_string.h"
#include "my_config.h"
#include "my_inttypes.h"
#include "my_macros.h"

Go to the source code of this file.

Macros

#define NullS   (char *)0
 Definition of the null string (a null pointer of type char *), used in some of our string handling code. More...
 
#define MAX_DECPT_FOR_F_FORMAT   DBL_DIG
 
#define MY_GCVT_MAX_FIELD_WIDTH    (DBL_DIG + 4 + std::max(5, MAX_DECPT_FOR_F_FORMAT))
 
#define STRING_WITH_LEN(X)   (X), ((sizeof(X) - 1))
 

Enumerations

enum  my_gcvt_arg_type { MY_GCVT_ARG_FLOAT , MY_GCVT_ARG_DOUBLE }
 

Functions

char * strmake (char *dst, const char *src, size_t length)
 
char * strcont (char *src, const char *set)
 
char * strxmov (char *dst, const char *src,...)
 
char * strxnmov (char *dst, size_t len, const char *src,...)
 
static void bchange (uchar *dst, size_t old_length, const uchar *src, size_t new_length, size_t tot_length)
 
static const char * strend (const char *s)
 
static char * strend (char *s)
 
static const char * strcend (const char *s, char c)
 
static char * strfill (char *s, size_t len, char fill)
 
static char * my_stpmov (char *dst, const char *src)
 
static char * my_stpnmov (char *dst, const char *src, size_t n)
 
static char * my_stpcpy (char *dst, const char *src)
 Copy a string from src to dst until (and including) terminating null byte. More...
 
static char * my_stpncpy (char *dst, const char *src, size_t n)
 Copy fixed-size string from src to dst. More...
 
static longlong my_strtoll (const char *nptr, char **endptr, int base)
 
static ulonglong my_strtoull (const char *nptr, char **endptr, int base)
 
static char * my_strtok_r (char *str, const char *delim, char **saveptr)
 
static int native_strcasecmp (const char *s1, const char *s2)
 
static int native_strncasecmp (const char *s1, const char *s2, size_t n)
 
static int is_prefix (const char *s, const char *t)
 
double my_strtod (const char *str, const char **end, int *error)
 Converts string to double (string does not have to be zero-terminated) More...
 
size_t my_fcvt (double x, int precision, char *to, bool *error)
 Converts a given floating point number to a zero-terminated string representation using the 'f' format. More...
 
size_t my_fcvt_compact (double x, char *to, bool *error)
 Converts a given floating point number to a zero-terminated string representation using the 'f' format. More...
 
size_t my_gcvt (double x, my_gcvt_arg_type type, int width, char *to, bool *error)
 Converts a given floating point number to a zero-terminated string representation with a given field width using the 'e' format (aka scientific notation) or the 'f' one. More...
 
const char * str2int (const char *src, int radix, long lower, long upper, long *val)
 
longlong my_strtoll10 (const char *nptr, const char **endptr, int *error)
 
char * ll2str (int64_t val, char *dst, int radix, bool upcase)
 Converts a 64-bit integer value to its character form and moves it to the destination buffer followed by a terminating NUL. More...
 
char * longlong10_to_str (int64_t val, char *dst, int radix)
 Converts a 64-bit integer to its string representation in decimal notation. More...
 
char * longlong2str (int64_t val, char *dst, int radix)
 
static char * llstr (longlong value, char *buff)
 
static char * ullstr (longlong value, char *buff)
 
static const ucharskip_trailing_space (const uchar *ptr, size_t len)
 Skip trailing space (ASCII spaces only). More...
 
static void human_readable_num_bytes (char *buf, int buf_len, double dbl_val)
 
static void lex_string_set (LEX_STRING *lex_str, char *c_str)
 
static void lex_cstring_set (LEX_CSTRING *lex_str, const char *c_str)
 

Variables

void *(* my_str_malloc )(size_t)
 
void *(* my_str_realloc )(void *, size_t)
 
void(* my_str_free )(void *)
 
const char _dig_vec_upper []
 
const char _dig_vec_lower []
 
static constexpr int FLOATING_POINT_BUFFER {311 + DECIMAL_NOT_SPECIFIED}
 

Macro Definition Documentation

◆ MAX_DECPT_FOR_F_FORMAT

#define MAX_DECPT_FOR_F_FORMAT   DBL_DIG

◆ MY_GCVT_MAX_FIELD_WIDTH

#define MY_GCVT_MAX_FIELD_WIDTH    (DBL_DIG + 4 + std::max(5, MAX_DECPT_FOR_F_FORMAT))

◆ NullS

#define NullS   (char *)0

Definition of the null string (a null pointer of type char *), used in some of our string handling code.

New code should use nullptr instead.

◆ STRING_WITH_LEN

#define STRING_WITH_LEN (   X)    (X), ((sizeof(X) - 1))

Enumeration Type Documentation

◆ my_gcvt_arg_type

Enumerator
MY_GCVT_ARG_FLOAT 
MY_GCVT_ARG_DOUBLE 

Function Documentation

◆ bchange()

static void bchange ( uchar dst,
size_t  old_length,
const uchar src,
size_t  new_length,
size_t  tot_length 
)
inlinestatic

◆ human_readable_num_bytes()

static void human_readable_num_bytes ( char *  buf,
int  buf_len,
double  dbl_val 
)
inlinestatic

◆ is_prefix()

static int is_prefix ( const char *  s,
const char *  t 
)
inlinestatic

◆ lex_cstring_set()

static void lex_cstring_set ( LEX_CSTRING lex_str,
const char *  c_str 
)
inlinestatic

◆ lex_string_set()

static void lex_string_set ( LEX_STRING lex_str,
char *  c_str 
)
inlinestatic

◆ ll2str()

char * ll2str ( int64_t  val,
char *  dst,
int  radix,
bool  upcase 
)

Converts a 64-bit integer value to its character form and moves it to the destination buffer followed by a terminating NUL.

If radix is -2..-36, val is taken to be SIGNED, if radix is 2..36, val is taken to be UNSIGNED. That is, val is signed if and only if radix is. All other radixes are treated as bad and nothing will be changed in this case.

For conversion to decimal representation (radix is -10 or 10) one should use the optimized longlong10_to_str() function instead.

Parameters
valthe value to convert
dstthe buffer where the string representation should be stored
radixradix of scale of notation
upcasetrue if we should use upper-case digits
Returns
pointer to the ending NUL character, or nullptr if radix is bad

◆ llstr()

static char * llstr ( longlong  value,
char *  buff 
)
inlinestatic

◆ longlong10_to_str()

char * longlong10_to_str ( int64_t  val,
char *  dst,
int  radix 
)

Converts a 64-bit integer to its string representation in decimal notation.

It is optimized for the normal case of radix 10/-10. It takes only the sign of radix parameter into account and not its absolute value.

Parameters
valthe value to convert
dstthe buffer where the string representation should be stored
radix10 if val is unsigned, -10 if val is signed
Returns
pointer to the ending NUL character

◆ longlong2str()

char * longlong2str ( int64_t  val,
char *  dst,
int  radix 
)
inline

◆ my_fcvt()

size_t my_fcvt ( double  x,
int  precision,
char *  to,
bool *  error 
)

Converts a given floating point number to a zero-terminated string representation using the 'f' format.

This function is a wrapper around dtoa() to do the same as sprintf(to, "%-.*f", precision, x), though the conversion is usually more precise. The only difference is in handling [-,+]infinity and nan values, in which case we print '0\0' to the output string and indicate an overflow.

Parameters
xthe input floating point number.
precisionthe number of digits after the decimal point. All properties of sprintf() apply:
  • if the number of significant digits after the decimal point is less than precision, the resulting string is right-padded with zeros
  • if the precision is 0, no decimal point appears
  • if a decimal point appears, at least one digit appears before it
topointer to the output buffer. The longest string which my_fcvt() can return is FLOATING_POINT_BUFFER bytes (including the terminating '\0').
errorif not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'.
Returns
number of written characters (excluding terminating '\0')

◆ my_fcvt_compact()

size_t my_fcvt_compact ( double  x,
char *  to,
bool *  error 
)

Converts a given floating point number to a zero-terminated string representation using the 'f' format.

This function is a wrapper around dtoa() to do almost the same as sprintf(to, "%-.*f", precision, x), though the conversion is usually more precise. The only difference is in handling [-,+]infinity and nan values, in which case we print '0\0' to the output string and indicate an overflow.

The string always contains the minimum number of digits necessary to reproduce the same binary double value if the string is parsed back to a double value.

Parameters
xthe input floating point number.
topointer to the output buffer. The longest string which my_fcvt() can return is FLOATING_POINT_BUFFER bytes (including the terminating '\0').
errorif not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'.
Returns
number of written characters (excluding terminating '\0')

◆ my_gcvt()

size_t my_gcvt ( double  x,
my_gcvt_arg_type  type,
int  width,
char *  to,
bool *  error 
)

Converts a given floating point number to a zero-terminated string representation with a given field width using the 'e' format (aka scientific notation) or the 'f' one.

The format is chosen automatically to provide the most number of significant digits (and thus, precision) with a given field width. In many cases, the result is similar to that of sprintf(to, "%g", x) with a few notable differences:

  • the conversion is usually more precise than C library functions.
  • there is no 'precision' argument. instead, we specify the number of characters available for conversion (i.e. a field width).
  • the result never exceeds the specified field width. If the field is too short to contain even a rounded decimal representation, my_gcvt() indicates overflow and truncates the output string to the specified width.
  • float-type arguments are handled differently than double ones. For a float input number (i.e. when the 'type' argument is MY_GCVT_ARG_FLOAT) we deliberately limit the precision of conversion by FLT_DIG digits to avoid garbage past the significant digits.
  • unlike sprintf(), in cases where the 'e' format is preferred, we don't zero-pad the exponent to save space for significant digits. The '+' sign for a positive exponent does not appear for the same reason.
Parameters
xthe input floating point number.
typeis either MY_GCVT_ARG_FLOAT or MY_GCVT_ARG_DOUBLE. Specifies the type of the input number (see notes above).
widthfield width in characters. The minimal field width to hold any number representation (albeit rounded) is 7 characters ("-Ne-NNN").
topointer to the output buffer. The result is always zero-terminated, and the longest returned string is thus 'width + 1' bytes.
errorif not NULL, points to a location where the status of conversion is stored upon return. false successful conversion true the input number is [-,+]infinity or nan. The output string in this case is always '0'.
Returns
number of written characters (excluding terminating '\0')

my_gcvt(-9e-3, ..., 4, ...); my_gcvt(-9e-3, ..., 2, ...); my_gcvt(1.87e-3, ..., 4, ...); my_gcvt(55, ..., 1, ...);

We do our best to minimize such cases by:

  • passing to dtoa() the field width as the number of significant digits
  • removing the sign of the number early (and decreasing the width before passing it to dtoa())
  • choosing the proper format to preserve the most number of significant digits.

◆ my_stpcpy()

static char * my_stpcpy ( char *  dst,
const char *  src 
)
inlinestatic

Copy a string from src to dst until (and including) terminating null byte.

Parameters
dstDestination
srcSource
Note
src and dst cannot overlap. Use my_stpmov() if src and dst overlaps.
Unsafe, consider using my_stpnpy() instead.
Returns
pointer to terminating null byte.

◆ my_stpmov()

static char * my_stpmov ( char *  dst,
const char *  src 
)
inlinestatic

◆ my_stpncpy()

static char * my_stpncpy ( char *  dst,
const char *  src,
size_t  n 
)
inlinestatic

Copy fixed-size string from src to dst.

Parameters
dstDestination
srcSource
nMaximum number of characters to copy.
Note
src and dst cannot overlap Use my_stpnmov() if src and dst overlaps.
Returns
pointer to terminating null byte.

◆ my_stpnmov()

static char * my_stpnmov ( char *  dst,
const char *  src,
size_t  n 
)
inlinestatic

◆ my_strtod()

double my_strtod ( const char *  str,
const char **  end,
int *  error 
)

Converts string to double (string does not have to be zero-terminated)

This is a wrapper around dtoa's version of strtod().

Parameters
strinput string
endaddress of a pointer to the first character after the input string. Upon return the pointer is set to point to the first rejected character.
errorUpon return is set to EOVERFLOW in case of underflow or overflow.
Returns
The resulting double value. In case of underflow, 0.0 is returned. In case overflow, signed DBL_MAX is returned.

◆ my_strtok_r()

static char * my_strtok_r ( char *  str,
const char *  delim,
char **  saveptr 
)
inlinestatic

◆ my_strtoll()

static longlong my_strtoll ( const char *  nptr,
char **  endptr,
int  base 
)
inlinestatic

◆ my_strtoll10()

longlong my_strtoll10 ( const char *  nptr,
const char **  endptr,
int *  error 
)

◆ my_strtoull()

static ulonglong my_strtoull ( const char *  nptr,
char **  endptr,
int  base 
)
inlinestatic

◆ native_strcasecmp()

static int native_strcasecmp ( const char *  s1,
const char *  s2 
)
inlinestatic

◆ native_strncasecmp()

static int native_strncasecmp ( const char *  s1,
const char *  s2,
size_t  n 
)
inlinestatic

◆ skip_trailing_space()

static const uchar * skip_trailing_space ( const uchar ptr,
size_t  len 
)
inlinestatic

Skip trailing space (ASCII spaces only).

Returns
New end of the string.

◆ str2int()

const char * str2int ( const char *  src,
int  radix,
long  lower,
long  upper,
long *  val 
)

◆ strcend()

static const char * strcend ( const char *  s,
char  c 
)
inlinestatic

◆ strcont()

char * strcont ( char *  src,
const char *  set 
)

◆ strend() [1/2]

static char * strend ( char *  s)
inlinestatic

◆ strend() [2/2]

static const char * strend ( const char *  s)
inlinestatic

◆ strfill()

static char * strfill ( char *  s,
size_t  len,
char  fill 
)
inlinestatic

◆ strmake()

char * strmake ( char *  dst,
const char *  src,
size_t  length 
)

◆ strxmov()

char * strxmov ( char *  dst,
const char *  src,
  ... 
)

◆ strxnmov()

char * strxnmov ( char *  dst,
size_t  len,
const char *  src,
  ... 
)

◆ ullstr()

static char * ullstr ( longlong  value,
char *  buff 
)
inlinestatic

Variable Documentation

◆ _dig_vec_lower

const char _dig_vec_lower[]
extern

◆ _dig_vec_upper

const char _dig_vec_upper[]
extern

◆ FLOATING_POINT_BUFFER

constexpr int FLOATING_POINT_BUFFER {311 + DECIMAL_NOT_SPECIFIED}
staticconstexpr

◆ my_str_free

void(* my_str_free) (void *) ( void *  )
extern

◆ my_str_malloc

void *(* my_str_malloc) (size_t) ( size_t  )
extern

◆ my_str_realloc

void *(* my_str_realloc) (void *, size_t) ( void *  ,
size_t   
)
extern