234
235
* suffix = ".bak" (style 1)
235
236
* foo.bar => foo.bak
236
237
* foo.bak => foo.$$$ (fallback)
238
* makefile => makefile.bak
239
* suffix = ".$$$" (style 1)
237
240
* foo.$$$ => foo.~~~ (fallback)
238
* makefile => makefile.bak
240
242
* suffix = "~" (style 2)
241
243
* foo.c => foo.c~
258
260
static const char suffix1[] = ".$$$";
259
261
static const char suffix2[] = ".~~~";
261
#define strEQ(s1,s2) (strcmp(s1,s2) == 0)
263
extern const char *ruby_find_basename(const char *, long *, long *);
264
extern const char *ruby_find_extname(const char *, long *);
263
#define strEQ(s1,s2) (strcmp((s1),(s2)) == 0)
267
266
ruby_add_suffix(VALUE str, const char *suffix)
270
int extlen = strlen(suffix);
269
long extlen = strlen(suffix);
274
272
const char *name;
278
276
name = StringValueCStr(str);
279
277
slen = strlen(name);
280
if (slen > sizeof(buf) - 1)
278
if (slen > (long)(sizeof(buf) - 1))
281
279
rb_fatal("Cannot do inplace edit on long filename (%ld characters)",
302
300
rb_str_cat(str, suffix, extlen);
305
304
strncpy(buf, name, slen);
307
p = buf + (ext - name);
311
310
if (suffix[1] == '\0') { /* Style 2 */
311
q = (char *)ruby_find_basename(buf, &baselen, 0);
313
if (len == 0 && baselen >= 8 && p + 3 <= buf + sizeof(buf)) p[len++] = '.'; /* DOSISH */
313
314
p[len] = *suffix;
316
else if ((q = (char *)ruby_find_basename(buf, &baselen, 0)) &&
317
else if (q && baselen < 8) {
371
371
#define D ((int*)d)
373
373
#define mmprepare(base, size) do {\
374
if (((long)base & (0x3)) == 0)\
375
if (size >= 16) mmkind = 1;\
378
high = (size & (~0xf));\
379
low = (size & 0x0c);\
374
if (((VALUE)(base) & (0x3)) == 0)\
375
if ((size) >= 16) mmkind = 1;\
378
high = ((size) & (~0xf));\
379
low = ((size) & 0x0c);\
382
382
#define mmarg mmkind, size, high, low
442
442
typedef struct { char *LL, *RR; } stack_node; /* Stack structure for L,l,R,r */
443
443
#define PUSH(ll,rr) do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */
444
#define POP(ll,rr) do { --top; ll = top->LL; rr = top->RR; } while (0) /* Pop L,l,R,r */
444
#define POP(ll,rr) do { --top; (ll) = top->LL; (rr) = top->RR; } while (0) /* Pop L,l,R,r */
446
#define med3(a,b,c) ((*cmp)(a,b,d)<0 ? \
447
((*cmp)(b,c,d)<0 ? b : ((*cmp)(a,c,d)<0 ? c : a)) : \
448
((*cmp)(b,c,d)>0 ? b : ((*cmp)(a,c,d)<0 ? a : c)))
446
#define med3(a,b,c) ((*cmp)((a),(b),d)<0 ? \
447
((*cmp)((b),(c),d)<0 ? (b) : ((*cmp)((a),(c),d)<0 ? (c) : (a))) : \
448
((*cmp)((b),(c),d)>0 ? (b) : ((*cmp)((a),(c),d)<0 ? (a) : (c))))
451
451
ruby_qsort(void* base, const size_t nel, const size_t size,
924
924
typedef double double_u;
926
926
# ifdef IEEE_LITTLE_ENDIAN
927
# define word0(x) (((ULong *)&x)[1])
928
# define word1(x) (((ULong *)&x)[0])
927
# define word0(x) (((ULong *)&(x))[1])
928
# define word1(x) (((ULong *)&(x))[0])
930
# define word0(x) (((ULong *)&x)[0])
931
# define word1(x) (((ULong *)&x)[1])
930
# define word0(x) (((ULong *)&(x))[0])
931
# define word1(x) (((ULong *)&(x))[1])
934
934
typedef U double_u;
935
935
# ifdef IEEE_LITTLE_ENDIAN
936
# define word0(x) (x.L[1])
937
# define word1(x) (x.L[0])
936
# define word0(x) ((x).L[1])
937
# define word1(x) ((x).L[0])
939
# define word0(x) (x.L[0])
940
# define word1(x) (x.L[1])
939
# define word0(x) ((x).L[0])
940
# define word1(x) ((x).L[1])
942
# define dval(x) (x.d)
942
# define dval(x) ((x).d)
945
945
/* The following definition of Storeinc is appropriate for MIPS processors.
947
947
* #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
949
949
#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
950
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
951
((unsigned short *)a)[0] = (unsigned short)c, a++)
950
#define Storeinc(a,b,c) (((unsigned short *)(a))[1] = (unsigned short)(b), \
951
((unsigned short *)(a))[0] = (unsigned short)(c), (a)++)
953
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
954
((unsigned short *)a)[1] = (unsigned short)c, a++)
953
#define Storeinc(a,b,c) (((unsigned short *)(a))[0] = (unsigned short)(b), \
954
((unsigned short *)(a))[1] = (unsigned short)(c), (a)++)
957
957
/* #define P DBL_MANT_DIG */
1076
1076
#ifdef RND_PRODQUOT
1077
#define rounded_product(a,b) a = rnd_prod(a, b)
1078
#define rounded_quotient(a,b) a = rnd_quot(a, b)
1077
#define rounded_product(a,b) ((a) = rnd_prod((a), (b)))
1078
#define rounded_quotient(a,b) ((a) = rnd_quot((a), (b)))
1079
1079
extern double rnd_prod(double, double), rnd_quot(double, double);
1081
#define rounded_product(a,b) a *= b
1082
#define rounded_quotient(a,b) a /= b
1081
#define rounded_product(a,b) ((a) *= (b))
1082
#define rounded_quotient(a,b) ((a) /= (b))
1085
1085
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
1181
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
1182
y->wds*sizeof(Long) + 2*sizeof(int))
1181
#define Bcopy(x,y) memcpy((char *)&(x)->sign, (char *)&(y)->sign, \
1182
(y)->wds*sizeof(Long) + 2*sizeof(int))
1184
1184
static Bigint *
1185
1185
multadd(Bigint *b, int m, int a) /* multiply by m and add a */
2120
2120
static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
2126
rb_warn("malformed value for Float(): %s. Ruby 1.9.3 for later will raise an ArgumentError for the value.", s00);
2128
while (*++s && (s1 = strchr(hexdigit, *s))) {
2130
adj += (s1 - hexdigit) & 15;
2126
if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
2127
while (*s == '0') s++;
2128
if ((s1 = strchr(hexdigit, *s)) != NULL) {
2130
adj += aadj * ((s1 - hexdigit) & 15);
2133
} while (*++s && (s1 = strchr(hexdigit, *s)));
2133
2136
if (*s == '.') {
2135
while (*++s && (s1 = strchr(hexdigit, *s))) {
2138
if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
2145
for (; *s && (s1 = strchr(hexdigit, *s)); ++s) {
2137
2146
adj += aadj * ((s1 - hexdigit) & 15);
2147
if ((aadj /= 16) == 0.0) {
2148
while (strchr(hexdigit, *++s));
2141
2157
if (*s == 'P' || *s == 'p') {
2142
2158
dsign = 0x2C - *++s; /* +: 2B, -: 2D */
2143
2159
if (abs(dsign) == 1) s++;
2144
2160
else dsign = 1;
2148
if (c < '0' || '9' < c) goto ret0;
2164
if (c < '0' || '9' < c) goto ret0;
2154
} while ('0' <= c && c <= '9');
2155
dval(rv) = ldexp(adj, nd * dsign);
2170
/* Float("0x0."+("0"*267)+"1fp2095") */
2171
if (nd + dsign * nd0 > 2095) {
2172
while ('0' <= c && c <= '9') c = *++s;
2175
} while ('0' <= c && c <= '9');
2159
rb_warn("malformed value for Float(): %s. Ruby 1.9.3 for later will raise an ArgumentError for the value.", s00);
2179
if (dsign) goto ret0;
2181
dval(rv) = ldexp(adj, nd0);
3141
#define rv_strdup(s, rve) nrv_alloc(s, rve, strlen(s)+1)
3160
#define rv_strdup(s, rve) nrv_alloc((s), (rve), strlen(s)+1)
3143
3162
#ifndef MULTIPLE_THREADS
3144
3163
/* freedtoa(s) must be used to free values s returned by dtoa
3176
static const char INFSTR[] = "Infinity";
3177
static const char NANSTR[] = "NaN";
3178
static const char ZEROSTR[] = "0";
3157
3180
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3159
3182
* Inspired by "How to Print Floating-Point Numbers Accurately" by
3273
3296
#ifdef IEEE_Arith
3274
3297
if (!word1(d) && !(word0(d) & 0xfffff))
3275
return rv_strdup("Infinity", rve);
3298
return rv_strdup(INFSTR, rve);
3277
return rv_strdup("NaN", rve);
3300
return rv_strdup(NANSTR, rve);
3907
3930
#define DBL_MANH_SIZE 20
3908
3931
#define DBL_MANL_SIZE 32
3909
#define INFSTR "Infinity"
3910
#define NANSTR "NaN"
3911
3932
#define DBL_ADJ (DBL_MAX_EXP - 2)
3912
3933
#define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1)
3913
3934
#define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
3914
#define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | (v << Exp_shift)))
3915
#define dmanh_get(u) ((int)(word0(u) & Frac_mask))
3916
#define dmanl_get(u) ((int)word1(u))
3935
#define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift)))
3936
#define dmanh_get(u) ((uint32_t)(word0(u) & Frac_mask))
3937
#define dmanl_get(u) ((uint32_t)word1(u))
3941
3962
* Outputs: decpt, sign, rve
3944
BSD__hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
3965
ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
3961
3982
if (isinf(d)) { /* FP_INFINITE */
3962
3983
*decpt = INT_MAX;
3963
return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
3984
return rv_strdup(INFSTR, rve);
3965
3986
else if (isnan(d)) { /* FP_NAN */
3966
3987
*decpt = INT_MAX;
3967
return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
3988
return rv_strdup(NANSTR, rve);
3969
3990
else if (d == 0.0) { /* FP_ZERO */
3971
return (nrv_alloc("0", rve, 1));
3992
return rv_strdup(ZEROSTR, rve);
3973
3994
else if (dexp_get(u)) { /* FP_NORMAL */
3974
3995
*decpt = dexp_get(u) - DBL_ADJ;
3986
4007
* enough space for all the digits.
3988
4009
bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
3989
s0 = rv_alloc(bufsize);
4010
s0 = rv_alloc(bufsize+1);
3991
4012
/* Round to the desired number of digits. */
3992
4013
if (SIGFIGS > ndigits && ndigits > 0) {
3993
4014
float redux = 1.0f;
3994
4016
int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
3995
4017
dexp_set(u, offset);
3998
4022
*decpt += dexp_get(u) - offset;