Ruby  2.5.0dev(2017-10-22revision60238)
Data Structures | Macros | Typedefs | Functions | Variables
util.c File Reference
#include "internal.h"
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <float.h>
#include "ruby/util.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "stdlib.h"
#include "string.h"

Go to the source code of this file.

Data Structures

struct  stack_node
 
union  U
 
struct  Bigint
 

Macros

#define hexdigit   ruby_hexdigits
 
#define S_ISDIR(m)   (((m) & S_IFMT) == S_IFDIR)
 
#define mmtype   long
 
#define mmcount   (16 / SIZEOF_LONG)
 
#define A   ((mmtype*)a)
 
#define B   ((mmtype*)b)
 
#define C   ((mmtype*)c)
 
#define D   ((mmtype*)d)
 
#define mmstep   (sizeof(mmtype) * mmcount)
 
#define mmprepare(base, size)
 
#define mmarg   mmkind, size, high, low
 
#define mmargdecl   int mmkind, size_t size, size_t high, size_t low
 
#define mmswap(a, b)   mmswap_((a),(b),mmarg)
 
#define mmrot3(a, b, c)   mmrot3_((a),(b),(c),mmarg)
 
#define PUSH(ll, rr)   do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */
 
#define POP(ll, rr)   do { --top; (ll) = top->LL; (rr) = top->RR; } while (0) /* Pop L,l,R,r */
 
#define med3(a, b, c)
 
#define PATH_MAX   8192
 
#define IEEE_LITTLE_ENDIAN
 
#define MALLOC   xmalloc
 
#define FREE   xfree
 
#define PRIVATE_MEM   2304
 
#define PRIVATE_mem   ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
 
#define IEEE_Arith
 
#define word0(x)   ((x).L[1])
 
#define word1(x)   ((x).L[0])
 
#define dval(x)   ((x).d)
 
#define Storeinc(a, b, c)
 
#define Exp_shift   20
 
#define Exp_shift1   20
 
#define Exp_msk1   0x100000
 
#define Exp_msk11   0x100000
 
#define Exp_mask   0x7ff00000
 
#define P   53
 
#define Bias   1023
 
#define Emin   (-1022)
 
#define Exp_1   0x3ff00000
 
#define Exp_11   0x3ff00000
 
#define Ebits   11
 
#define Frac_mask   0xfffff
 
#define Frac_mask1   0xfffff
 
#define Ten_pmax   22
 
#define Bletch   0x10
 
#define Bndry_mask   0xfffff
 
#define Bndry_mask1   0xfffff
 
#define LSB   1
 
#define Sign_bit   0x80000000
 
#define Log2P   1
 
#define Tiny0   0
 
#define Tiny1   1
 
#define Quick_max   14
 
#define Int_max   14
 
#define Avoid_Underflow
 
#define Flt_Rounds   1
 
#define Rounding   Flt_Rounds
 
#define rounded_product(a, b)   ((a) *= (b))
 
#define rounded_quotient(a, b)   ((a) /= (b))
 
#define Big0   (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
 
#define Big1   0xffffffff
 
#define Pack_32
 
#define FFFFFFFF   0xffffffffUL
 
#define Llong   long long
 
#define ULLong   unsigned Llong
 
#define MULTIPLE_THREADS   1
 
#define ACQUIRE_DTOA_LOCK(n)   /*unused right now*/
 
#define FREE_DTOA_LOCK(n)   /*unused right now*/
 
#define Kmax   15
 
#define Bcopy(x, y)
 
#define d0   word0(d)
 
#define d1   word1(d)
 
#define d0   word0(d)
 
#define d1   word1(d)
 
#define Scale_Bit   0x10
 
#define n_bigtens   5
 
#define rv_alloc(i)   xmalloc(i)
 
#define rv_strdup(s, rve)   nrv_alloc((s), (rve), strlen(s)+1)
 
#define DBL_MANH_SIZE   20
 
#define DBL_MANL_SIZE   32
 
#define DBL_ADJ   (DBL_MAX_EXP - 2)
 
#define SIGFIGS   ((DBL_MANT_DIG + 3) / 4 + 1)
 
#define dexp_get(u)   ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
 
#define dexp_set(u, v)   (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift)))
 
#define dmanh_get(u)   ((uint32_t)(word0(u) & Frac_mask))
 
#define dmanl_get(u)   ((uint32_t)word1(u))
 

Typedefs

typedef int() cmpfunc_t(const void *, const void *, void *)
 
typedef U double_u
 
typedef struct Bigint Bigint
 

Functions

unsigned long ruby_scan_oct (const char *start, size_t len, size_t *retlen)
 
unsigned long ruby_scan_hex (const char *start, size_t len, size_t *retlen)
 
unsigned long ruby_scan_digits (const char *str, ssize_t len, int base, size_t *retlen, int *overflow)
 
unsigned long ruby_strtoul (const char *str, char **endptr, int base)
 
void ruby_qsort (void *base, const size_t nel, const size_t size, cmpfunc_t *cmp, void *d)
 
char * ruby_strdup (const char *str)
 
char * ruby_getcwd (void)
 
double ruby_strtod (const char *s00, char **se)
 
char * ruby_dtoa (double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
 
void ruby_each_words (const char *str, void(*func)(const char *, int, void *), void *arg)
 
char * ruby_hdtoa (double d, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve)
 

Variables

const char ruby_hexdigits [] = "0123456789abcdef0123456789ABCDEF"
 
const signed char ruby_digit36_to_number_table []
 

Macro Definition Documentation

◆ A

#define A   ((mmtype*)a)

Definition at line 231 of file util.c.

◆ ACQUIRE_DTOA_LOCK

#define ACQUIRE_DTOA_LOCK (   n)    /*unused right now*/

Definition at line 1039 of file util.c.

◆ Avoid_Underflow

#define Avoid_Underflow

Definition at line 909 of file util.c.

◆ B

#define B   ((mmtype*)b)

Definition at line 232 of file util.c.

◆ Bcopy

#define Bcopy (   x,
 
)
Value:
memcpy((char *)&(x)->sign, (char *)&(y)->sign, \
(y)->wds*sizeof(Long) + 2*sizeof(int))

Definition at line 1105 of file util.c.

◆ Bias

#define Bias   1023

Definition at line 890 of file util.c.

◆ Big0

#define Big0   (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))

Definition at line 1005 of file util.c.

◆ Big1

#define Big1   0xffffffff

Definition at line 1006 of file util.c.

◆ Bletch

#define Bletch   0x10

Definition at line 898 of file util.c.

◆ Bndry_mask

#define Bndry_mask   0xfffff

Definition at line 899 of file util.c.

◆ Bndry_mask1

#define Bndry_mask1   0xfffff

Definition at line 900 of file util.c.

◆ C

#define C   ((mmtype*)c)

Definition at line 233 of file util.c.

◆ D

#define D   ((mmtype*)d)

Definition at line 234 of file util.c.

Referenced by permute().

◆ d0 [1/2]

#define d0   word0(d)

◆ d0 [2/2]

#define d0   word0(d)

◆ d1 [1/2]

#define d1   word1(d)

◆ d1 [2/2]

#define d1   word1(d)

◆ DBL_ADJ

#define DBL_ADJ   (DBL_MAX_EXP - 2)

Definition at line 3866 of file util.c.

Referenced by ruby_hdtoa().

◆ DBL_MANH_SIZE

#define DBL_MANH_SIZE   20

Definition at line 3864 of file util.c.

Referenced by ruby_hdtoa().

◆ DBL_MANL_SIZE

#define DBL_MANL_SIZE   32

Definition at line 3865 of file util.c.

Referenced by ruby_hdtoa().

◆ dexp_get

#define dexp_get (   u)    ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)

Definition at line 3868 of file util.c.

Referenced by ruby_hdtoa().

◆ dexp_set

#define dexp_set (   u,
 
)    (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift)))

Definition at line 3869 of file util.c.

Referenced by ruby_hdtoa().

◆ dmanh_get

#define dmanh_get (   u)    ((uint32_t)(word0(u) & Frac_mask))

Definition at line 3870 of file util.c.

Referenced by ruby_hdtoa().

◆ dmanl_get

#define dmanl_get (   u)    ((uint32_t)word1(u))

Definition at line 3871 of file util.c.

Referenced by ruby_hdtoa().

◆ dval

#define dval (   x)    ((x).d)

Definition at line 862 of file util.c.

Referenced by ruby_dtoa(), and ruby_strtod().

◆ Ebits

#define Ebits   11

Definition at line 894 of file util.c.

◆ Emin

#define Emin   (-1022)

Definition at line 891 of file util.c.

◆ Exp_1

#define Exp_1   0x3ff00000

Definition at line 892 of file util.c.

◆ Exp_11

#define Exp_11   0x3ff00000

Definition at line 893 of file util.c.

◆ Exp_mask

#define Exp_mask   0x7ff00000

Definition at line 888 of file util.c.

Referenced by ruby_dtoa().

◆ Exp_msk1

#define Exp_msk1   0x100000

Definition at line 886 of file util.c.

◆ Exp_msk11

#define Exp_msk11   0x100000

Definition at line 887 of file util.c.

◆ Exp_shift

#define Exp_shift   20

Definition at line 884 of file util.c.

◆ Exp_shift1

#define Exp_shift1   20

Definition at line 885 of file util.c.

◆ FFFFFFFF

#define FFFFFFFF   0xffffffffUL

Definition at line 1012 of file util.c.

◆ Flt_Rounds

#define Flt_Rounds   1

Definition at line 919 of file util.c.

Referenced by ruby_dtoa().

◆ Frac_mask

#define Frac_mask   0xfffff

Definition at line 895 of file util.c.

◆ Frac_mask1

#define Frac_mask1   0xfffff

Definition at line 896 of file util.c.

◆ FREE

#define FREE   xfree

Definition at line 773 of file util.c.

◆ FREE_DTOA_LOCK

#define FREE_DTOA_LOCK (   n)    /*unused right now*/

Definition at line 1040 of file util.c.

◆ hexdigit

#define hexdigit   ruby_hexdigits

Definition at line 31 of file util.c.

Referenced by ruby_scan_hex(), and ruby_strtod().

◆ IEEE_Arith

#define IEEE_Arith

Definition at line 790 of file util.c.

◆ IEEE_LITTLE_ENDIAN

#define IEEE_LITTLE_ENDIAN

Definition at line 724 of file util.c.

◆ Int_max

#define Int_max   14

Definition at line 907 of file util.c.

◆ Kmax

#define Kmax   15

Definition at line 1043 of file util.c.

◆ Llong

#define Llong   long long

Definition at line 1026 of file util.c.

◆ Log2P

#define Log2P   1

Definition at line 903 of file util.c.

◆ LSB

#define LSB   1

Definition at line 901 of file util.c.

◆ MALLOC

#define MALLOC   xmalloc

Definition at line 768 of file util.c.

◆ med3

#define med3 (   a,
  b,
 
)
Value:
((*cmp)((a),(b),d)<0 ? \
((*cmp)((b),(c),d)<0 ? (b) : ((*cmp)((a),(c),d)<0 ? (c) : (a))) : \
((*cmp)((b),(c),d)>0 ? (b) : ((*cmp)((a),(c),d)<0 ? (a) : (c))))

Definition at line 338 of file util.c.

◆ mmarg

#define mmarg   mmkind, size, high, low

Definition at line 246 of file util.c.

◆ mmargdecl

#define mmargdecl   int mmkind, size_t size, size_t high, size_t low

Definition at line 247 of file util.c.

◆ mmcount

#define mmcount   (16 / SIZEOF_LONG)

Definition at line 230 of file util.c.

◆ mmprepare

#define mmprepare (   base,
  size 
)
Value:
do {\
if (((VALUE)(base) % sizeof(mmtype)) == 0 && ((size) % sizeof(mmtype)) == 0) \
if ((size) >= mmstep) mmkind = 1;\
else mmkind = 0;\
else mmkind = -1;\
high = ((size) / mmstep) * mmstep;\
low = ((size) % mmstep);\
} while (0)\
#define mmstep
Definition: util.c:236
unsigned long VALUE
Definition: ruby.h:85
#define mmtype
Definition: util.c:229
int size
Definition: encoding.c:57

Definition at line 237 of file util.c.

◆ mmrot3

#define mmrot3 (   a,
  b,
 
)    mmrot3_((a),(b),(c),mmarg)

Definition at line 323 of file util.c.

◆ mmstep

#define mmstep   (sizeof(mmtype) * mmcount)

Definition at line 236 of file util.c.

◆ mmswap

#define mmswap (   a,
 
)    mmswap_((a),(b),mmarg)

Definition at line 285 of file util.c.

◆ mmtype

#define mmtype   long

Definition at line 229 of file util.c.

◆ MULTIPLE_THREADS

#define MULTIPLE_THREADS   1

Definition at line 1033 of file util.c.

◆ n_bigtens

#define n_bigtens   5

Definition at line 1900 of file util.c.

◆ P

#define P   53

Definition at line 889 of file util.c.

◆ Pack_32

#define Pack_32

Definition at line 1009 of file util.c.

◆ PATH_MAX

#define PATH_MAX   8192

◆ POP

#define POP (   ll,
  rr 
)    do { --top; (ll) = top->LL; (rr) = top->RR; } while (0) /* Pop L,l,R,r */

Definition at line 336 of file util.c.

◆ PRIVATE_MEM

#define PRIVATE_MEM   2304

Definition at line 778 of file util.c.

◆ PRIVATE_mem

#define PRIVATE_mem   ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))

Definition at line 780 of file util.c.

◆ PUSH

#define PUSH (   ll,
  rr 
)    do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */

Definition at line 335 of file util.c.

◆ Quick_max

#define Quick_max   14

Definition at line 906 of file util.c.

◆ rounded_product

#define rounded_product (   a,
 
)    ((a) *= (b))

Definition at line 1001 of file util.c.

◆ rounded_quotient

#define rounded_quotient (   a,
 
)    ((a) /= (b))

Definition at line 1002 of file util.c.

◆ Rounding

#define Rounding   Flt_Rounds

Definition at line 928 of file util.c.

◆ rv_alloc

#define rv_alloc (   i)    xmalloc(i)

Definition at line 3074 of file util.c.

Referenced by ruby_hdtoa().

◆ rv_strdup

#define rv_strdup (   s,
  rve 
)    nrv_alloc((s), (rve), strlen(s)+1)

Definition at line 3089 of file util.c.

Referenced by ruby_dtoa(), and ruby_hdtoa().

◆ S_ISDIR

#define S_ISDIR (   m)    (((m) & S_IFMT) == S_IFDIR)

Definition at line 195 of file util.c.

◆ Scale_Bit

#define Scale_Bit   0x10

Definition at line 1899 of file util.c.

◆ SIGFIGS

#define SIGFIGS   ((DBL_MANT_DIG + 3) / 4 + 1)

Definition at line 3867 of file util.c.

Referenced by ruby_hdtoa().

◆ Sign_bit

#define Sign_bit   0x80000000

Definition at line 902 of file util.c.

Referenced by ruby_dtoa(), and ruby_hdtoa().

◆ Storeinc

#define Storeinc (   a,
  b,
 
)
Value:
(((unsigned short *)(a))[1] = (unsigned short)(b), \
((unsigned short *)(a))[0] = (unsigned short)(c), (a)++)

Definition at line 870 of file util.c.

◆ Ten_pmax

#define Ten_pmax   22

Definition at line 897 of file util.c.

◆ Tiny0

#define Tiny0   0

Definition at line 904 of file util.c.

◆ Tiny1

#define Tiny1   1

Definition at line 905 of file util.c.

◆ ULLong

#define ULLong   unsigned Llong

Definition at line 1029 of file util.c.

◆ word0

#define word0 (   x)    ((x).L[1])

Definition at line 856 of file util.c.

Referenced by ruby_dtoa(), and ruby_hdtoa().

◆ word1

#define word1 (   x)    ((x).L[0])

Definition at line 857 of file util.c.

Referenced by ruby_dtoa().

Typedef Documentation

◆ Bigint

typedef struct Bigint Bigint

Definition at line 1051 of file util.c.

◆ cmpfunc_t

typedef int() cmpfunc_t(const void *, const void *, void *)

Definition at line 342 of file util.c.

◆ double_u

typedef U double_u

Definition at line 854 of file util.c.

Function Documentation

◆ ruby_dtoa()

char* ruby_dtoa ( double  d_,
int  mode,
int  ndigits,
int *  decpt,
int *  sign,
char **  rve 
)

Definition at line 3144 of file util.c.

References dval, Exp_mask, Flt_Rounds, Bigint::k, rv_strdup, S, Sign_bit, word0, word1, and Bigint::x.

◆ ruby_each_words()

void ruby_each_words ( const char *  str,
void(*)(const char *, int, void *)  func,
void *  arg 
)

Definition at line 3822 of file util.c.

References ISSPACE, and len.

Referenced by ruby_set_debug_option().

◆ ruby_getcwd()

char* ruby_getcwd ( void  )

◆ ruby_hdtoa()

char* ruby_hdtoa ( double  d,
const char *  xdigs,
int  ndigits,
int *  decpt,
int *  sign,
char **  rve 
)

◆ ruby_qsort()

void ruby_qsort ( void *  base,
const size_t  nel,
const size_t  size,
cmpfunc_t cmp,
void *  d 
)

Definition at line 344 of file util.c.

◆ ruby_scan_digits()

unsigned long ruby_scan_digits ( const char *  str,
ssize_t  len,
int  base,
size_t *  retlen,
int *  overflow 
)

Definition at line 84 of file util.c.

References ruby_digit36_to_number_table.

Referenced by ruby_strtoul().

◆ ruby_scan_hex()

unsigned long ruby_scan_hex ( const char *  start,
size_t  len,
size_t *  retlen 
)

Definition at line 48 of file util.c.

References hexdigit, and strchr().

◆ ruby_scan_oct()

unsigned long ruby_scan_oct ( const char *  start,
size_t  len,
size_t *  retlen 
)

Definition at line 34 of file util.c.

◆ ruby_strdup()

char* ruby_strdup ( const char *  str)

Definition at line 496 of file util.c.

References len, strlen(), and xmalloc.

Referenced by compat_init_setproctitle(), and ruby_getcwd().

◆ ruby_strtod()

double ruby_strtod ( const char *  s00,
char **  se 
)

Definition at line 1994 of file util.c.

References DBL_DIG, dval, errno, hexdigit, ISDIGIT, Bigint::k, NULL, Bigint::sign, and strchr().

◆ ruby_strtoul()

unsigned long ruby_strtoul ( const char *  str,
char **  endptr,
int  base 
)

Definition at line 117 of file util.c.

References errno, ISSPACE, len, and ruby_scan_digits().

Referenced by rb_pipe().

Variable Documentation

◆ ruby_digit36_to_number_table

const signed char ruby_digit36_to_number_table[]
Initial value:
= {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
}

Definition at line 63 of file util.c.

Referenced by ruby_scan_digits().

◆ ruby_hexdigits

const char ruby_hexdigits[] = "0123456789abcdef0123456789ABCDEF"

Definition at line 30 of file util.c.