Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/src/fmt_fp.c

    r331 r439  
     1#ifndef MRB_WITHOUT_FLOAT
     2#if defined(MRB_DISABLE_STDIO) || defined(_WIN32) || defined(_WIN64)
    13/*
    24
     
    2931#include <limits.h>
    3032#include <string.h>
    31 #include <stdint.h>
    3233#include <math.h>
    3334#include <float.h>
     
    6263#define PAD_SIZE 256
    6364static void
    64 pad(struct fmt_args *f, char c, int w, int l, int fl)
     65pad(struct fmt_args *f, char c, ptrdiff_t w, ptrdiff_t l, uint8_t fl)
    6566{
    6667  char pad[PAD_SIZE];
     
    9293
    9394static int
    94 fmt_fp(struct fmt_args *f, long double y, int w, int p, int fl, int t)
     95fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t)
    9596{
    9697  uint32_t big[(LDBL_MANT_DIG+28)/29 + 1          // mantissa expansion
     
    9899  uint32_t *a, *d, *r, *z;
    99100  uint32_t i;
    100   int e2=0, e, j, l;
     101  int e2=0, e, j;
     102  ptrdiff_t l;
    101103  char buf[9+LDBL_MANT_DIG/4], *s;
    102104  const char *prefix="-0X+0X 0X-0x+0x 0x";
    103   int pl;
     105  ptrdiff_t pl;
    104106  char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
    105107
     
    116118    const char *ss = (t&32)?"inf":"INF";
    117119    if (y!=y) ss=(t&32)?"nan":"NAN";
    118     pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
     120    pad(f, ' ', 0, 3+pl, fl&~ZERO_PAD);
    119121    out(f, prefix, pl);
    120122    out(f, ss, 3);
    121     pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
    122     return MAX(w, 3+pl);
     123    pad(f, ' ', 0, 3+pl, fl^LEFT_ADJ);
     124    return 3+(int)pl;
    123125  }
    124126
     
    128130  if ((t|32)=='a') {
    129131    long double round = 8.0;
    130     int re;
     132    ptrdiff_t re;
    131133
    132134    if (t&32) prefix += 9;
     
    168170      l = (s-buf) + (ebuf-estr);
    169171
    170     pad(f, ' ', w, pl+l, fl);
     172    pad(f, ' ', 0, pl+l, fl);
    171173    out(f, prefix, pl);
    172     pad(f, '0', w, pl+l, fl^ZERO_PAD);
     174    pad(f, '0', 0, pl+l, fl^ZERO_PAD);
    173175    out(f, buf, s-buf);
    174176    pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
    175177    out(f, estr, ebuf-estr);
    176     pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
    177     return MAX(w, pl+l);
     178    pad(f, ' ', 0, pl+l, fl^LEFT_ADJ);
     179    return (int)pl+(int)l;
    178180  }
    179181  if (p<0) p=6;
     
    203205  while (e2<0) {
    204206    uint32_t carry=0, *b;
    205     int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3+8)/9;
     207    int sh=MIN(9,-e2), need=1+((int)p+LDBL_MANT_DIG/3+8)/9;
    206208    for (d=a; d<z; d++) {
    207209      uint32_t rm = *d & ((1<<sh)-1);
     
    217219  }
    218220
    219   if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
     221  if (a<z) for (i=10, e=9*(int)(r-a); *a>=i; i*=10, e++);
    220222  else e=0;
    221223
    222224  /* Perform rounding: j is precision after the radix (possibly neg) */
    223   j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
     225  j = (int)p - ((t|32)!='f')*e - ((t|32)=='g' && p);
    224226  if (j < 9*(z-r-1)) {
    225227    uint32_t x;
     
    248250          (*d)++;
    249251        }
    250         for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
     252        for (i=10, e=9*(int)(r-a); *a>=i; i*=10, e++);
    251253      }
    252254    }
     
    287289  }
    288290
    289   pad(f, ' ', w, pl+l, fl);
     291  pad(f, ' ', 0, pl+l, fl);
    290292  out(f, prefix, pl);
    291   pad(f, '0', w, pl+l, fl^ZERO_PAD);
     293  pad(f, '0', 0, pl+l, fl^ZERO_PAD);
    292294
    293295  if ((t|32)=='f') {
     
    318320      }
    319321      out(f, ss, MIN(buf+9-ss, p));
    320       p -= buf+9-ss;
     322      p -= (int)(buf+9-ss);
    321323    }
    322324    pad(f, '0', p+18, 18, 0);
     
    324326  }
    325327
    326   pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
    327 
    328   return MAX(w, pl+l);
     328  pad(f, ' ', 0, pl+l, fl^LEFT_ADJ);
     329
     330  return (int)pl+(int)l;
    329331}
    330332
     
    332334fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo)
    333335{
    334   int p;
     336  ptrdiff_t p;
    335337
    336338  if (*fmt != '%') {
     
    352354  case 'e': case 'f': case 'g': case 'a':
    353355  case 'E': case 'F': case 'G': case 'A':
    354     return fmt_fp(f, flo, 0, p, 0, *fmt);
     356    return fmt_fp(f, flo, p, 0, *fmt);
    355357  default:
    356358    return -1;
     
    364366
    365367  f.mrb = mrb;
    366   f.str = mrb_str_buf_new(mrb, 24);
     368  f.str = mrb_str_new_capa(mrb, 24);
    367369  if (fmt_core(&f, fmt, mrb_float(flo)) < 0) {
    368370    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid format string");
     
    370372  return f.str;
    371373}
     374#else   /* MRB_DISABLE_STDIO || _WIN32 || _WIN64 */
     375#include <mruby.h>
     376#include <stdio.h>
     377
     378mrb_value
     379mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt)
     380{
     381  char buf[25];
     382
     383  snprintf(buf, sizeof(buf), fmt, mrb_float(flo));
     384  return mrb_str_new_cstr(mrb, buf);
     385}
     386#endif  /* MRB_DISABLE_STDIO || _WIN32 || _WIN64 */
     387#endif
Note: See TracChangeset for help on using the changeset viewer.