2000-05-09 00:53:16 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
sprintf.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri Oct 15 10:39:26 JST 1993
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2013-04-09 17:37:04 -04:00
|
|
|
#include "internal.h"
|
2014-11-15 06:49:06 -05:00
|
|
|
#include "ruby/re.h"
|
2014-08-18 04:06:48 -04:00
|
|
|
#include "id.h"
|
1999-08-13 01:45:20 -04:00
|
|
|
#include <math.h>
|
2005-07-22 21:02:18 -04:00
|
|
|
#include <stdarg.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-07-02 12:24:11 -04:00
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
|
|
|
|
|
2013-06-11 08:06:40 -04:00
|
|
|
static void fmt_setup(char*,size_t,int,int,int,int);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-03-20 01:27:22 -05:00
|
|
|
static char
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
sign_bits(int base, const char *p)
|
2003-03-20 01:27:22 -05:00
|
|
|
{
|
|
|
|
char c = '.';
|
|
|
|
|
|
|
|
switch (base) {
|
|
|
|
case 16:
|
|
|
|
if (*p == 'X') c = 'F';
|
|
|
|
else c = 'f';
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
c = '7'; break;
|
|
|
|
case 2:
|
|
|
|
c = '1'; break;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#define FNONE 0
|
|
|
|
#define FSHARP 1
|
|
|
|
#define FMINUS 2
|
|
|
|
#define FPLUS 4
|
|
|
|
#define FZERO 8
|
1999-01-19 23:59:39 -05:00
|
|
|
#define FSPACE 16
|
|
|
|
#define FWIDTH 32
|
|
|
|
#define FPREC 64
|
2007-07-15 16:45:55 -04:00
|
|
|
#define FPREC0 128
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-16 09:44:19 -04:00
|
|
|
#define CHECK(l) do {\
|
2009-04-13 04:21:41 -04:00
|
|
|
int cr = ENC_CODERANGE(result);\
|
1998-01-16 07:13:05 -05:00
|
|
|
while (blen + (l) >= bsiz) {\
|
|
|
|
bsiz*=2;\
|
2002-10-16 09:44:19 -04:00
|
|
|
}\
|
|
|
|
rb_str_resize(result, bsiz);\
|
2009-04-13 04:21:41 -04:00
|
|
|
ENC_CODERANGE_SET(result, cr);\
|
2006-08-31 06:47:44 -04:00
|
|
|
buf = RSTRING_PTR(result);\
|
2002-10-16 09:44:19 -04:00
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
#define PUSH(s, l) do { \
|
1998-01-16 07:13:05 -05:00
|
|
|
CHECK(l);\
|
2010-12-29 21:50:34 -05:00
|
|
|
memcpy(&buf[blen], (s), (l));\
|
1998-01-16 07:13:05 -05:00
|
|
|
blen += (l);\
|
2002-04-18 04:46:18 -04:00
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2005-07-22 21:02:18 -04:00
|
|
|
#define FILL(c, l) do { \
|
|
|
|
CHECK(l);\
|
2010-12-29 21:50:34 -05:00
|
|
|
memset(&buf[blen], (c), (l));\
|
2005-07-22 21:02:18 -04:00
|
|
|
blen += (l);\
|
|
|
|
} while (0)
|
|
|
|
|
2002-10-16 09:44:19 -04:00
|
|
|
#define GETARG() (nextvalue != Qundef ? nextvalue : \
|
2014-06-26 14:41:57 -04:00
|
|
|
GETNEXTARG())
|
|
|
|
|
|
|
|
#define GETNEXTARG() ( \
|
2014-07-02 04:03:51 -04:00
|
|
|
check_next_arg(posarg, nextarg), \
|
2002-10-16 09:44:19 -04:00
|
|
|
(posarg = nextarg++, GETNTHARG(posarg)))
|
|
|
|
|
2014-07-02 04:03:53 -04:00
|
|
|
#define GETPOSARG(n) ( \
|
|
|
|
check_pos_arg(posarg, (n)), \
|
|
|
|
(posarg = -1, GETNTHARG(n)))
|
2002-10-16 09:44:19 -04:00
|
|
|
|
|
|
|
#define GETNTHARG(nth) \
|
2010-12-29 21:50:34 -05:00
|
|
|
(((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
|
1999-11-10 23:08:26 -05:00
|
|
|
|
2014-07-08 04:04:12 -04:00
|
|
|
#define CHECKNAMEARG(name, len, enc) ( \
|
2014-07-02 04:03:57 -04:00
|
|
|
check_name_arg(posarg, name, len, enc), \
|
2014-07-08 04:04:12 -04:00
|
|
|
posarg = -2)
|
2008-09-30 04:01:11 -04:00
|
|
|
|
2007-07-15 16:45:55 -04:00
|
|
|
#define GETNUM(n, val) \
|
2014-07-02 04:03:48 -04:00
|
|
|
(!(p = get_num(p, end, enc, &(n))) ? \
|
|
|
|
rb_raise(rb_eArgError, #val " too big") : (void)0)
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
#define GETASTER(val) do { \
|
|
|
|
t = p++; \
|
|
|
|
n = 0; \
|
2014-06-26 09:30:08 -04:00
|
|
|
GETNUM(n, val); \
|
1999-11-10 23:08:26 -05:00
|
|
|
if (*p == '$') { \
|
2002-10-16 09:44:19 -04:00
|
|
|
tmp = GETPOSARG(n); \
|
1999-11-10 23:08:26 -05:00
|
|
|
} \
|
|
|
|
else { \
|
2014-06-26 14:41:57 -04:00
|
|
|
tmp = GETNEXTARG(); \
|
1999-11-10 23:08:26 -05:00
|
|
|
p = t; \
|
|
|
|
} \
|
2010-12-29 21:50:34 -05:00
|
|
|
(val) = NUM2INT(tmp); \
|
2002-04-18 04:46:18 -04:00
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2014-07-02 04:03:48 -04:00
|
|
|
static const char *
|
|
|
|
get_num(const char *p, const char *end, rb_encoding *enc, int *valp)
|
|
|
|
{
|
|
|
|
int next_n = *valp;
|
|
|
|
for (; p < end && rb_enc_isdigit(*p, enc); p++) {
|
|
|
|
if (MUL_OVERFLOW_INT_P(10, next_n))
|
|
|
|
return NULL;
|
|
|
|
next_n *= 10;
|
|
|
|
if (INT_MAX - (*p - '0') < next_n)
|
|
|
|
return NULL;
|
|
|
|
next_n += *p - '0';
|
|
|
|
}
|
|
|
|
if (p >= end) {
|
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%*[0-9]");
|
|
|
|
}
|
|
|
|
*valp = next_n;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-07-02 04:03:51 -04:00
|
|
|
static void
|
|
|
|
check_next_arg(int posarg, int nextarg)
|
|
|
|
{
|
|
|
|
switch (posarg) {
|
|
|
|
case -1:
|
|
|
|
rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg);
|
|
|
|
case -2:
|
|
|
|
rb_raise(rb_eArgError, "unnumbered(%d) mixed with named", nextarg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 04:03:53 -04:00
|
|
|
static void
|
|
|
|
check_pos_arg(int posarg, int n)
|
|
|
|
{
|
|
|
|
if (posarg > 0) {
|
|
|
|
rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", n, posarg);
|
|
|
|
}
|
|
|
|
if (posarg == -2) {
|
|
|
|
rb_raise(rb_eArgError, "numbered(%d) after named", n);
|
|
|
|
}
|
|
|
|
if (n < 1) {
|
|
|
|
rb_raise(rb_eArgError, "invalid index - %d$", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 04:03:57 -04:00
|
|
|
static void
|
|
|
|
check_name_arg(int posarg, const char *name, int len, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
if (posarg > 0) {
|
|
|
|
rb_enc_raise(enc, rb_eArgError, "named%.*s after unnumbered(%d)", len, name, posarg);
|
|
|
|
}
|
|
|
|
if (posarg == -1) {
|
|
|
|
rb_enc_raise(enc, rb_eArgError, "named%.*s after numbered", len, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 04:01:11 -04:00
|
|
|
static VALUE
|
|
|
|
get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
if (*hash != Qundef) return *hash;
|
|
|
|
if (argc != 2) {
|
|
|
|
rb_raise(rb_eArgError, "one hash required");
|
|
|
|
}
|
2012-06-26 03:46:24 -04:00
|
|
|
tmp = rb_check_hash_type(argv[1]);
|
2008-09-30 04:01:11 -04:00
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eArgError, "one hash required");
|
|
|
|
}
|
|
|
|
return (*hash = tmp);
|
|
|
|
}
|
2003-12-28 01:33:07 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* format(format_string [, arguments...] ) -> string
|
|
|
|
* sprintf(format_string [, arguments...] ) -> string
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns the string resulting from applying <i>format_string</i> to
|
2008-03-07 03:31:41 -05:00
|
|
|
* any additional arguments. Within the format string, any characters
|
2009-02-22 09:23:33 -05:00
|
|
|
* other than format sequences are copied to the result.
|
2008-03-07 03:31:41 -05:00
|
|
|
*
|
|
|
|
* The syntax of a format sequence is follows.
|
|
|
|
*
|
|
|
|
* %[flags][width][.precision]type
|
|
|
|
*
|
|
|
|
* A format
|
2003-12-28 01:33:07 -05:00
|
|
|
* sequence consists of a percent sign, followed by optional flags,
|
|
|
|
* width, and precision indicators, then terminated with a field type
|
2008-03-07 03:31:41 -05:00
|
|
|
* character. The field type controls how the corresponding
|
2003-12-28 01:33:07 -05:00
|
|
|
* <code>sprintf</code> argument is to be interpreted, while the flags
|
2008-04-20 02:44:41 -04:00
|
|
|
* modify that interpretation.
|
|
|
|
*
|
|
|
|
* The field type characters are:
|
|
|
|
*
|
|
|
|
* Field | Integer Format
|
|
|
|
* ------+--------------------------------------------------------------
|
|
|
|
* b | Convert argument as a binary number.
|
|
|
|
* | Negative numbers will be displayed as a two's complement
|
|
|
|
* | prefixed with `..1'.
|
|
|
|
* B | Equivalent to `b', but uses an uppercase 0B for prefix
|
|
|
|
* | in the alternative format by #.
|
|
|
|
* d | Convert argument as a decimal number.
|
|
|
|
* i | Identical to `d'.
|
|
|
|
* o | Convert argument as an octal number.
|
|
|
|
* | Negative numbers will be displayed as a two's complement
|
|
|
|
* | prefixed with `..7'.
|
|
|
|
* u | Identical to `d'.
|
|
|
|
* x | Convert argument as a hexadecimal number.
|
|
|
|
* | Negative numbers will be displayed as a two's complement
|
|
|
|
* | prefixed with `..f' (representing an infinite string of
|
|
|
|
* | leading 'ff's).
|
|
|
|
* X | Equivalent to `x', but uses uppercase letters.
|
|
|
|
*
|
|
|
|
* Field | Float Format
|
|
|
|
* ------+--------------------------------------------------------------
|
2009-02-22 09:23:33 -05:00
|
|
|
* e | Convert floating point argument into exponential notation
|
2008-04-20 02:44:41 -04:00
|
|
|
* | with one digit before the decimal point as [-]d.dddddde[+-]dd.
|
|
|
|
* | The precision specifies the number of digits after the decimal
|
|
|
|
* | point (defaulting to six).
|
|
|
|
* E | Equivalent to `e', but uses an uppercase E to indicate
|
|
|
|
* | the exponent.
|
2009-02-22 09:23:33 -05:00
|
|
|
* f | Convert floating point argument as [-]ddd.dddddd,
|
2008-04-20 02:44:41 -04:00
|
|
|
* | where the precision specifies the number of digits after
|
|
|
|
* | the decimal point.
|
|
|
|
* g | Convert a floating point number using exponential form
|
|
|
|
* | if the exponent is less than -4 or greater than or
|
|
|
|
* | equal to the precision, or in dd.dddd form otherwise.
|
|
|
|
* | The precision specifies the number of significant digits.
|
|
|
|
* G | Equivalent to `g', but use an uppercase `E' in exponent form.
|
2010-04-01 00:32:57 -04:00
|
|
|
* a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
|
|
|
|
* | which is consisted from optional sign, "0x", fraction part
|
|
|
|
* | as hexadecimal, "p", and exponential part as decimal.
|
|
|
|
* A | Equivalent to `a', but use uppercase `X' and `P'.
|
2008-04-20 02:44:41 -04:00
|
|
|
*
|
|
|
|
* Field | Other Format
|
|
|
|
* ------+--------------------------------------------------------------
|
|
|
|
* c | Argument is the numeric code for a single character or
|
|
|
|
* | a single character string itself.
|
|
|
|
* p | The valuing of argument.inspect.
|
|
|
|
* s | Argument is a string to be substituted. If the format
|
|
|
|
* | sequence contains a precision, at most that many characters
|
|
|
|
* | will be copied.
|
2008-06-10 19:21:26 -04:00
|
|
|
* % | A percent sign itself will be displayed. No argument taken.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-04-20 02:44:41 -04:00
|
|
|
* The flags modifies the behavior of the formats.
|
|
|
|
* The flag characters are:
|
2003-12-28 01:33:07 -05:00
|
|
|
*
|
2008-03-06 10:46:18 -05:00
|
|
|
* Flag | Applies to | Meaning
|
|
|
|
* ---------+---------------+-----------------------------------------
|
2009-02-22 09:23:33 -05:00
|
|
|
* space | bBdiouxX | Leave a space at the start of
|
2010-04-01 00:32:57 -04:00
|
|
|
* | aAeEfgG | non-negative numbers.
|
2008-04-20 02:44:41 -04:00
|
|
|
* | (numeric fmt) | For `o', `x', `X', `b' and `B', use
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | a minus sign with absolute value for
|
|
|
|
* | | negative values.
|
2008-03-06 10:46:18 -05:00
|
|
|
* ---------+---------------+-----------------------------------------
|
|
|
|
* (digit)$ | all | Specifies the absolute argument number
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | for this field. Absolute and relative
|
2008-03-06 10:46:18 -05:00
|
|
|
* | | argument numbers cannot be mixed in a
|
|
|
|
* | | sprintf string.
|
|
|
|
* ---------+---------------+-----------------------------------------
|
2008-04-20 02:44:41 -04:00
|
|
|
* # | bBoxX | Use an alternative format.
|
2010-04-01 00:32:57 -04:00
|
|
|
* | aAeEfgG | For the conversions `o', increase the precision
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | until the first digit will be `0' if
|
|
|
|
* | | it is not formatted as complements.
|
|
|
|
* | | For the conversions `x', `X', `b' and `B'
|
|
|
|
* | | on non-zero, prefix the result with ``0x'',
|
|
|
|
* | | ``0X'', ``0b'' and ``0B'', respectively.
|
2010-04-01 00:32:57 -04:00
|
|
|
* | | For `a', `A', `e', `E', `f', `g', and 'G',
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | force a decimal point to be added,
|
|
|
|
* | | even if no digits follow.
|
2008-03-06 10:46:18 -05:00
|
|
|
* | | For `g' and 'G', do not remove trailing zeros.
|
|
|
|
* ---------+---------------+-----------------------------------------
|
2008-04-20 02:44:41 -04:00
|
|
|
* + | bBdiouxX | Add a leading plus sign to non-negative
|
2010-04-01 00:32:57 -04:00
|
|
|
* | aAeEfgG | numbers.
|
2008-04-20 02:44:41 -04:00
|
|
|
* | (numeric fmt) | For `o', `x', `X', `b' and `B', use
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | a minus sign with absolute value for
|
|
|
|
* | | negative values.
|
2008-03-06 10:46:18 -05:00
|
|
|
* ---------+---------------+-----------------------------------------
|
|
|
|
* - | all | Left-justify the result of this conversion.
|
|
|
|
* ---------+---------------+-----------------------------------------
|
2008-04-20 02:44:41 -04:00
|
|
|
* 0 (zero) | bBdiouxX | Pad with zeros, not spaces.
|
2010-04-01 00:32:57 -04:00
|
|
|
* | aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
|
2008-04-20 02:44:41 -04:00
|
|
|
* | (numeric fmt) | is used for negative numbers formatted as
|
2008-03-07 03:31:41 -05:00
|
|
|
* | | complements.
|
2008-03-06 10:46:18 -05:00
|
|
|
* ---------+---------------+-----------------------------------------
|
2009-02-22 09:23:33 -05:00
|
|
|
* * | all | Use the next argument as the field width.
|
2008-03-06 10:46:18 -05:00
|
|
|
* | | If negative, left-justify the result. If the
|
2009-02-22 09:23:33 -05:00
|
|
|
* | | asterisk is followed by a number and a dollar
|
2008-03-06 10:46:18 -05:00
|
|
|
* | | sign, use the indicated argument as the width.
|
2003-12-28 01:33:07 -05:00
|
|
|
*
|
2008-03-07 03:31:41 -05:00
|
|
|
* Examples of flags:
|
|
|
|
*
|
|
|
|
* # `+' and space flag specifies the sign of non-negative numbers.
|
|
|
|
* sprintf("%d", 123) #=> "123"
|
|
|
|
* sprintf("%+d", 123) #=> "+123"
|
|
|
|
* sprintf("% d", 123) #=> " 123"
|
|
|
|
*
|
|
|
|
* # `#' flag for `o' increases number of digits to show `0'.
|
|
|
|
* # `+' and space flag changes format of negative numbers.
|
|
|
|
* sprintf("%o", 123) #=> "173"
|
|
|
|
* sprintf("%#o", 123) #=> "0173"
|
|
|
|
* sprintf("%+o", -123) #=> "-173"
|
|
|
|
* sprintf("%o", -123) #=> "..7605"
|
|
|
|
* sprintf("%#o", -123) #=> "..7605"
|
|
|
|
*
|
|
|
|
* # `#' flag for `x' add a prefix `0x' for non-zero numbers.
|
|
|
|
* # `+' and space flag disables complements for negative numbers.
|
|
|
|
* sprintf("%x", 123) #=> "7b"
|
|
|
|
* sprintf("%#x", 123) #=> "0x7b"
|
|
|
|
* sprintf("%+x", -123) #=> "-7b"
|
|
|
|
* sprintf("%x", -123) #=> "..f85"
|
|
|
|
* sprintf("%#x", -123) #=> "0x..f85"
|
|
|
|
* sprintf("%#x", 0) #=> "0"
|
|
|
|
*
|
|
|
|
* # `#' for `X' uses the prefix `0X'.
|
|
|
|
* sprintf("%X", 123) #=> "7B"
|
|
|
|
* sprintf("%#X", 123) #=> "0X7B"
|
|
|
|
*
|
|
|
|
* # `#' flag for `b' add a prefix `0b' for non-zero numbers.
|
|
|
|
* # `+' and space flag disables complements for negative numbers.
|
|
|
|
* sprintf("%b", 123) #=> "1111011"
|
|
|
|
* sprintf("%#b", 123) #=> "0b1111011"
|
|
|
|
* sprintf("%+b", -123) #=> "-1111011"
|
|
|
|
* sprintf("%b", -123) #=> "..10000101"
|
|
|
|
* sprintf("%#b", -123) #=> "0b..10000101"
|
|
|
|
* sprintf("%#b", 0) #=> "0"
|
|
|
|
*
|
|
|
|
* # `#' for `B' uses the prefix `0B'.
|
|
|
|
* sprintf("%B", 123) #=> "1111011"
|
|
|
|
* sprintf("%#B", 123) #=> "0B1111011"
|
|
|
|
*
|
|
|
|
* # `#' for `e' forces to show the decimal point.
|
|
|
|
* sprintf("%.0e", 1) #=> "1e+00"
|
|
|
|
* sprintf("%#.0e", 1) #=> "1.e+00"
|
|
|
|
*
|
|
|
|
* # `#' for `f' forces to show the decimal point.
|
|
|
|
* sprintf("%.0f", 1234) #=> "1234"
|
|
|
|
* sprintf("%#.0f", 1234) #=> "1234."
|
|
|
|
*
|
|
|
|
* # `#' for `g' forces to show the decimal point.
|
|
|
|
* # It also disables stripping lowest zeros.
|
|
|
|
* sprintf("%g", 123.4) #=> "123.4"
|
|
|
|
* sprintf("%#g", 123.4) #=> "123.400"
|
|
|
|
* sprintf("%g", 123456) #=> "123456"
|
|
|
|
* sprintf("%#g", 123456) #=> "123456."
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* The field width is an optional integer, followed optionally by a
|
2008-03-07 03:31:41 -05:00
|
|
|
* period and a precision. The width specifies the minimum number of
|
|
|
|
* characters that will be written to the result for this field.
|
|
|
|
*
|
|
|
|
* Examples of width:
|
|
|
|
*
|
|
|
|
* # padding is done by spaces, width=20
|
|
|
|
* # 0 or radix-1. <------------------>
|
|
|
|
* sprintf("%20d", 123) #=> " 123"
|
|
|
|
* sprintf("%+20d", 123) #=> " +123"
|
|
|
|
* sprintf("%020d", 123) #=> "00000000000000000123"
|
|
|
|
* sprintf("%+020d", 123) #=> "+0000000000000000123"
|
|
|
|
* sprintf("% 020d", 123) #=> " 0000000000000000123"
|
|
|
|
* sprintf("%-20d", 123) #=> "123 "
|
|
|
|
* sprintf("%-+20d", 123) #=> "+123 "
|
|
|
|
* sprintf("%- 20d", 123) #=> " 123 "
|
|
|
|
* sprintf("%020x", -123) #=> "..ffffffffffffffff85"
|
|
|
|
*
|
|
|
|
* For
|
2003-12-28 01:33:07 -05:00
|
|
|
* numeric fields, the precision controls the number of decimal places
|
2008-03-07 03:31:41 -05:00
|
|
|
* displayed. For string fields, the precision determines the maximum
|
|
|
|
* number of characters to be copied from the string. (Thus, the format
|
2003-12-28 01:33:07 -05:00
|
|
|
* sequence <code>%10.10s</code> will always contribute exactly ten
|
|
|
|
* characters to the result.)
|
|
|
|
*
|
2008-03-07 03:31:41 -05:00
|
|
|
* Examples of precisions:
|
|
|
|
*
|
|
|
|
* # precision for `d', 'o', 'x' and 'b' is
|
|
|
|
* # minimum number of digits <------>
|
|
|
|
* sprintf("%20.8d", 123) #=> " 00000123"
|
|
|
|
* sprintf("%20.8o", 123) #=> " 00000173"
|
|
|
|
* sprintf("%20.8x", 123) #=> " 0000007b"
|
|
|
|
* sprintf("%20.8b", 123) #=> " 01111011"
|
|
|
|
* sprintf("%20.8d", -123) #=> " -00000123"
|
|
|
|
* sprintf("%20.8o", -123) #=> " ..777605"
|
|
|
|
* sprintf("%20.8x", -123) #=> " ..ffff85"
|
|
|
|
* sprintf("%20.8b", -11) #=> " ..110101"
|
|
|
|
*
|
|
|
|
* # "0x" and "0b" for `#x' and `#b' is not counted for
|
|
|
|
* # precision but "0" for `#o' is counted. <------>
|
|
|
|
* sprintf("%#20.8d", 123) #=> " 00000123"
|
|
|
|
* sprintf("%#20.8o", 123) #=> " 00000173"
|
|
|
|
* sprintf("%#20.8x", 123) #=> " 0x0000007b"
|
|
|
|
* sprintf("%#20.8b", 123) #=> " 0b01111011"
|
|
|
|
* sprintf("%#20.8d", -123) #=> " -00000123"
|
|
|
|
* sprintf("%#20.8o", -123) #=> " ..777605"
|
|
|
|
* sprintf("%#20.8x", -123) #=> " 0x..ffff85"
|
|
|
|
* sprintf("%#20.8b", -11) #=> " 0b..110101"
|
|
|
|
*
|
|
|
|
* # precision for `e' is number of
|
|
|
|
* # digits after the decimal point <------>
|
|
|
|
* sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-03-07 03:31:41 -05:00
|
|
|
* # precision for `f' is number of
|
|
|
|
* # digits after the decimal point <------>
|
|
|
|
* sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
|
2008-03-06 10:46:18 -05:00
|
|
|
*
|
2008-03-07 03:31:41 -05:00
|
|
|
* # precision for `g' is number of
|
|
|
|
* # significant digits <------->
|
|
|
|
* sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
|
2008-03-06 10:46:18 -05:00
|
|
|
*
|
2008-03-08 20:04:46 -05:00
|
|
|
* # <------->
|
2008-03-07 03:31:41 -05:00
|
|
|
* sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
|
2008-03-06 10:46:18 -05:00
|
|
|
*
|
2008-03-07 03:31:41 -05:00
|
|
|
* # precision for `s' is
|
|
|
|
* # maximum number of characters <------>
|
|
|
|
* sprintf("%20.8s", "string test") #=> " string t"
|
2008-03-06 10:46:18 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* sprintf("%d %04x", 123, 123) #=> "123 007b"
|
|
|
|
* sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
|
|
|
|
* sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
|
|
|
|
* sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
|
|
|
|
* sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
|
2008-01-23 09:55:39 -05:00
|
|
|
* sprintf("%u", -123) #=> "-123"
|
2010-07-21 02:16:07 -04:00
|
|
|
*
|
|
|
|
* For more complex formatting, Ruby supports a reference by name.
|
2010-09-14 18:27:54 -04:00
|
|
|
* %<name>s style uses format style, but %{name} style doesn't.
|
2010-07-21 02:16:07 -04:00
|
|
|
*
|
2013-07-11 00:49:33 -04:00
|
|
|
* Examples:
|
2010-09-15 19:30:46 -04:00
|
|
|
* sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 })
|
2010-07-21 02:16:07 -04:00
|
|
|
* #=> 1 : 2.000000
|
2010-09-15 19:30:46 -04:00
|
|
|
* sprintf("%{foo}f", { :foo => 1 })
|
2010-09-14 18:27:54 -04:00
|
|
|
* # => "1f"
|
2003-12-28 01:33:07 -05:00
|
|
|
*/
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_f_sprintf(int argc, const VALUE *argv)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2005-07-22 21:02:18 -04:00
|
|
|
return rb_str_format(argc - 1, argv + 1, GETNTHARG(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
2005-07-22 21:02:18 -04:00
|
|
|
{
|
2014-08-18 04:06:37 -04:00
|
|
|
enum {default_float_precision = 6};
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
rb_encoding *enc;
|
2004-07-17 19:14:52 -04:00
|
|
|
const char *p, *end;
|
|
|
|
char *buf;
|
2009-09-23 00:17:16 -04:00
|
|
|
long blen, bsiz;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE result;
|
|
|
|
|
2009-04-13 04:21:41 -04:00
|
|
|
long scanned = 0;
|
|
|
|
int coderange = ENC_CODERANGE_7BIT;
|
1999-01-19 23:59:39 -05:00
|
|
|
int width, prec, flags = FNONE;
|
2002-10-16 09:44:19 -04:00
|
|
|
int nextarg = 1;
|
|
|
|
int posarg = 0;
|
1999-12-06 04:04:03 -05:00
|
|
|
int tainted = 0;
|
2002-10-16 09:44:19 -04:00
|
|
|
VALUE nextvalue;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE tmp;
|
|
|
|
VALUE str;
|
2008-09-30 04:01:11 -04:00
|
|
|
volatile VALUE hash = Qundef;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-07-15 16:45:55 -04:00
|
|
|
#define CHECK_FOR_WIDTH(f) \
|
|
|
|
if ((f) & FWIDTH) { \
|
|
|
|
rb_raise(rb_eArgError, "width given twice"); \
|
|
|
|
} \
|
|
|
|
if ((f) & FPREC0) { \
|
|
|
|
rb_raise(rb_eArgError, "width after precision"); \
|
|
|
|
}
|
|
|
|
#define CHECK_FOR_FLAGS(f) \
|
|
|
|
if ((f) & FWIDTH) { \
|
|
|
|
rb_raise(rb_eArgError, "flag after width"); \
|
|
|
|
} \
|
|
|
|
if ((f) & FPREC0) { \
|
|
|
|
rb_raise(rb_eArgError, "flag after precision"); \
|
|
|
|
}
|
|
|
|
|
2005-07-22 21:02:18 -04:00
|
|
|
++argc;
|
|
|
|
--argv;
|
1999-12-06 04:04:03 -05:00
|
|
|
if (OBJ_TAINTED(fmt)) tainted = 1;
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(fmt);
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
enc = rb_enc_get(fmt);
|
2004-12-08 00:32:26 -05:00
|
|
|
fmt = rb_str_new4(fmt);
|
2006-08-31 06:47:44 -04:00
|
|
|
p = RSTRING_PTR(fmt);
|
|
|
|
end = p + RSTRING_LEN(fmt);
|
1998-01-16 07:13:05 -05:00
|
|
|
blen = 0;
|
|
|
|
bsiz = 120;
|
2002-10-16 09:44:19 -04:00
|
|
|
result = rb_str_buf_new(bsiz);
|
2007-12-17 00:01:47 -05:00
|
|
|
rb_enc_copy(result, fmt);
|
2006-08-31 06:47:44 -04:00
|
|
|
buf = RSTRING_PTR(result);
|
2007-12-17 11:50:17 -05:00
|
|
|
memset(buf, 0, bsiz);
|
2009-04-13 04:21:41 -04:00
|
|
|
ENC_CODERANGE_SET(result, coderange);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for (; p < end; p++) {
|
2004-07-17 19:14:52 -04:00
|
|
|
const char *t;
|
1999-11-10 23:08:26 -05:00
|
|
|
int n;
|
2014-07-09 02:14:41 -04:00
|
|
|
VALUE sym = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (t = p; t < end && *t != '%'; t++) ;
|
|
|
|
PUSH(p, t - p);
|
2009-08-11 19:53:21 -04:00
|
|
|
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
|
2010-02-08 10:08:15 -05:00
|
|
|
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
|
2009-08-11 19:53:21 -04:00
|
|
|
ENC_CODERANGE_SET(result, coderange);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (t >= end) {
|
|
|
|
/* end of fmt string */
|
|
|
|
goto sprint_exit;
|
|
|
|
}
|
|
|
|
p = t + 1; /* skip `%' */
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
width = prec = -1;
|
2002-10-16 09:44:19 -04:00
|
|
|
nextvalue = Qundef;
|
1998-01-16 07:13:05 -05:00
|
|
|
retry:
|
|
|
|
switch (*p) {
|
|
|
|
default:
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
if (rb_enc_isprint(*p, enc))
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%%c", *p);
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "malformed format string");
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_FLAGS(flags);
|
1999-01-19 23:59:39 -05:00
|
|
|
flags |= FSPACE;
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '#':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_FLAGS(flags);
|
1998-01-16 07:13:05 -05:00
|
|
|
flags |= FSHARP;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '+':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_FLAGS(flags);
|
1998-01-16 07:13:05 -05:00
|
|
|
flags |= FPLUS;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '-':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_FLAGS(flags);
|
1998-01-16 07:13:05 -05:00
|
|
|
flags |= FMINUS;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '0':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_FLAGS(flags);
|
1998-01-16 07:13:05 -05:00
|
|
|
flags |= FZERO;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
1999-11-10 23:08:26 -05:00
|
|
|
n = 0;
|
2007-07-15 16:45:55 -04:00
|
|
|
GETNUM(n, width);
|
1999-11-10 23:08:26 -05:00
|
|
|
if (*p == '$') {
|
2002-10-16 09:44:19 -04:00
|
|
|
if (nextvalue != Qundef) {
|
|
|
|
rb_raise(rb_eArgError, "value given twice - %d$", n);
|
|
|
|
}
|
|
|
|
nextvalue = GETPOSARG(n);
|
1999-11-10 23:08:26 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
}
|
2007-08-02 00:46:41 -04:00
|
|
|
CHECK_FOR_WIDTH(flags);
|
1999-11-10 23:08:26 -05:00
|
|
|
width = n;
|
|
|
|
flags |= FWIDTH;
|
1998-01-16 07:13:05 -05:00
|
|
|
goto retry;
|
|
|
|
|
2008-09-30 04:01:11 -04:00
|
|
|
case '<':
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
const char *start = p;
|
|
|
|
char term = (*p == '<') ? '>' : '}';
|
2012-04-10 03:26:35 -04:00
|
|
|
int len;
|
2008-09-30 04:01:11 -04:00
|
|
|
|
|
|
|
for (; p < end && *p != term; ) {
|
|
|
|
p += rb_enc_mbclen(p, end, enc);
|
|
|
|
}
|
|
|
|
if (p >= end) {
|
|
|
|
rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
|
|
|
|
}
|
2012-04-10 03:26:35 -04:00
|
|
|
#if SIZEOF_INT < SIZEOF_SIZE_T
|
|
|
|
if ((size_t)(p - start) >= INT_MAX) {
|
|
|
|
const int message_limit = 20;
|
|
|
|
len = (int)(rb_enc_right_char_head(start, start + message_limit, p, enc) - start);
|
2012-04-11 09:01:16 -04:00
|
|
|
rb_enc_raise(enc, rb_eArgError,
|
|
|
|
"too long name (%"PRIdSIZE" bytes) - %.*s...%c",
|
|
|
|
(size_t)(p - start - 2), len, start, term);
|
2012-04-10 03:26:35 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
len = (int)(p - start + 1); /* including parenthesis */
|
2014-07-09 02:14:41 -04:00
|
|
|
if (sym != Qnil) {
|
2014-07-30 10:31:53 -04:00
|
|
|
rb_enc_raise(enc, rb_eArgError, "named%.*s after <%"PRIsVALUE">",
|
|
|
|
len, start, rb_sym2str(sym));
|
2009-03-27 19:49:52 -04:00
|
|
|
}
|
2014-07-08 04:04:12 -04:00
|
|
|
CHECKNAMEARG(start, len, enc);
|
|
|
|
get_hash(&hash, argc, argv);
|
2014-07-09 02:14:41 -04:00
|
|
|
sym = rb_check_symbol_cstr(start + 1,
|
|
|
|
len - 2 /* without parenthesis */,
|
|
|
|
enc);
|
|
|
|
if (sym != Qnil) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
|
2009-03-27 19:49:52 -04:00
|
|
|
if (nextvalue == Qundef) {
|
2012-04-11 09:01:16 -04:00
|
|
|
rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
|
2009-03-27 19:49:52 -04:00
|
|
|
}
|
2008-09-30 04:01:11 -04:00
|
|
|
if (term == '}') goto format_s;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
case '*':
|
2007-07-15 16:45:55 -04:00
|
|
|
CHECK_FOR_WIDTH(flags);
|
1998-01-16 07:13:05 -05:00
|
|
|
flags |= FWIDTH;
|
1999-11-10 23:08:26 -05:00
|
|
|
GETASTER(width);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (width < 0) {
|
|
|
|
flags |= FMINUS;
|
1999-01-19 23:59:39 -05:00
|
|
|
width = -width;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '.':
|
2007-07-15 16:45:55 -04:00
|
|
|
if (flags & FPREC0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "precision given twice");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2007-07-15 16:45:55 -04:00
|
|
|
flags |= FPREC|FPREC0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
prec = 0;
|
|
|
|
p++;
|
|
|
|
if (*p == '*') {
|
1999-11-10 23:08:26 -05:00
|
|
|
GETASTER(prec);
|
2000-06-19 04:38:11 -04:00
|
|
|
if (prec < 0) { /* ignore negative precision */
|
|
|
|
flags &= ~FPREC;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2007-07-15 16:45:55 -04:00
|
|
|
GETNUM(prec, precision);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
case '\0':
|
2007-07-15 16:45:55 -04:00
|
|
|
p--;
|
1998-01-16 07:13:05 -05:00
|
|
|
case '%':
|
1999-09-01 05:48:03 -04:00
|
|
|
if (flags != FNONE) {
|
* regerror.c, string.c, io.c, lib/getoptlong.rb, lib/net/imap.rb,
compile.c, sprintf.c, parse.y, ext/win32ole/win32ole.c,
ext/tk/sample/demos-en/entry3.rb, ext/tk/lib/tcltk.rb,
ext/openssl/ossl_bn.c, numeric.c, vm.c,
benchmark/bm_so_meteor_contest.rb, bignum.c, ruby.c: don't "illegal"
for non law violation context.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-12-20 21:31:11 -05:00
|
|
|
rb_raise(rb_eArgError, "invalid format character - %%");
|
1999-09-01 05:48:03 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
PUSH("%", 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
VALUE val = GETARG();
|
2006-08-04 07:46:44 -04:00
|
|
|
VALUE tmp;
|
2008-09-11 06:34:59 -04:00
|
|
|
unsigned int c;
|
|
|
|
int n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-08-04 07:46:44 -04:00
|
|
|
tmp = rb_check_string_type(val);
|
|
|
|
if (!NIL_P(tmp)) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
if (rb_enc_strlen(RSTRING_PTR(tmp),RSTRING_END(tmp),enc) != 1) {
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
rb_raise(rb_eArgError, "%%c requires a character");
|
|
|
|
}
|
* encoding.c (rb_enc_codepoint_len): combine rb_enc_codepoint()
and rb_enc_codelen() in one function to reduce calls.
* encoding.c (rb_enc_codepoint): compatibility function.
* sprintf.c (rb_str_format): use rb_enc_codepoint_len().
* string.c (rb_str_inspect, rb_str_upcase_bang,
rb_str_downcase_bang, rb_str_capitalize_bang,
rb_str_swapcase_bang, trnext, tr_trans, rb_str_delete_bang,
rb_str_squeeze_bang, rb_str_count, rb_str_split_m,
rb_str_each_line, rb_str_each_codepoint, rb_str_lstrip_bang,
sym_printable): ditto.
* transcode.c (make_econv_exception): use rb_enc_mbc_to_codepoint()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-05-19 12:59:22 -04:00
|
|
|
c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, enc);
|
2011-08-22 10:48:10 -04:00
|
|
|
RB_GC_GUARD(tmp);
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
}
|
|
|
|
else {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
c = NUM2INT(val);
|
* encoding.c (rb_enc_codepoint_len): combine rb_enc_codepoint()
and rb_enc_codelen() in one function to reduce calls.
* encoding.c (rb_enc_codepoint): compatibility function.
* sprintf.c (rb_str_format): use rb_enc_codepoint_len().
* string.c (rb_str_inspect, rb_str_upcase_bang,
rb_str_downcase_bang, rb_str_capitalize_bang,
rb_str_swapcase_bang, trnext, tr_trans, rb_str_delete_bang,
rb_str_squeeze_bang, rb_str_count, rb_str_split_m,
rb_str_each_line, rb_str_each_codepoint, rb_str_lstrip_bang,
sym_printable): ditto.
* transcode.c (make_econv_exception): use rb_enc_mbc_to_codepoint()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-05-19 12:59:22 -04:00
|
|
|
n = rb_enc_codelen(c, enc);
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
}
|
2008-10-09 14:41:35 -04:00
|
|
|
if (n <= 0) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid character");
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
}
|
2005-07-22 21:02:18 -04:00
|
|
|
if (!(flags & FWIDTH)) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
CHECK(n);
|
|
|
|
rb_enc_mbcput(c, &buf[blen], enc);
|
|
|
|
blen += n;
|
|
|
|
}
|
|
|
|
else if ((flags & FMINUS)) {
|
|
|
|
CHECK(n);
|
|
|
|
rb_enc_mbcput(c, &buf[blen], enc);
|
|
|
|
blen += n;
|
|
|
|
FILL(' ', width-1);
|
2005-07-22 21:02:18 -04:00
|
|
|
}
|
|
|
|
else {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
FILL(' ', width-1);
|
|
|
|
CHECK(n);
|
|
|
|
rb_enc_mbcput(c, &buf[blen], enc);
|
|
|
|
blen += n;
|
2005-07-22 21:02:18 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2003-07-03 07:02:53 -04:00
|
|
|
case 'p':
|
2008-09-30 04:01:11 -04:00
|
|
|
format_s:
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE arg = GETARG();
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
long len, slen;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-07-03 07:02:53 -04:00
|
|
|
if (*p == 'p') arg = rb_inspect(arg);
|
1999-01-19 23:59:39 -05:00
|
|
|
str = rb_obj_as_string(arg);
|
1999-12-06 04:04:03 -05:00
|
|
|
if (OBJ_TAINTED(str)) tainted = 1;
|
2006-08-31 06:47:44 -04:00
|
|
|
len = RSTRING_LEN(str);
|
2009-04-12 22:40:20 -04:00
|
|
|
rb_str_set_len(result, blen);
|
2009-04-13 04:21:41 -04:00
|
|
|
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
|
|
|
|
int cr = coderange;
|
2010-02-08 10:08:15 -05:00
|
|
|
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr);
|
2009-04-13 04:21:41 -04:00
|
|
|
ENC_CODERANGE_SET(result,
|
|
|
|
(cr == ENC_CODERANGE_UNKNOWN ?
|
|
|
|
ENC_CODERANGE_BROKEN : (coderange = cr)));
|
|
|
|
}
|
2009-08-12 23:03:20 -04:00
|
|
|
enc = rb_enc_check(result, str);
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
if (flags&(FPREC|FWIDTH)) {
|
|
|
|
slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
|
|
|
|
if (slen < 0) {
|
|
|
|
rb_raise(rb_eArgError, "invalid mbstring sequence");
|
|
|
|
}
|
2007-12-30 16:08:36 -05:00
|
|
|
if ((flags&FPREC) && (prec < slen)) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
char *p = rb_enc_nth(RSTRING_PTR(str), RSTRING_END(str),
|
|
|
|
prec, enc);
|
|
|
|
slen = prec;
|
|
|
|
len = p - RSTRING_PTR(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2007-12-30 16:08:36 -05:00
|
|
|
/* need to adjust multi-byte string pos */
|
|
|
|
if ((flags&FWIDTH) && (width > slen)) {
|
2009-09-23 00:17:16 -04:00
|
|
|
width -= (int)slen;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!(flags&FMINUS)) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
CHECK(width);
|
1998-01-16 07:13:05 -05:00
|
|
|
while (width--) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
CHECK(len);
|
2006-08-31 06:47:44 -04:00
|
|
|
memcpy(&buf[blen], RSTRING_PTR(str), len);
|
2011-08-22 10:48:10 -04:00
|
|
|
RB_GC_GUARD(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
blen += len;
|
|
|
|
if (flags&FMINUS) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
CHECK(width);
|
1998-01-16 07:13:05 -05:00
|
|
|
while (width--) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
2008-01-18 10:37:22 -05:00
|
|
|
rb_enc_associate(result, enc);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-31 06:47:44 -04:00
|
|
|
PUSH(RSTRING_PTR(str), len);
|
2011-08-22 10:48:10 -04:00
|
|
|
RB_GC_GUARD(str);
|
2007-12-17 00:01:47 -05:00
|
|
|
rb_enc_associate(result, enc);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'd':
|
|
|
|
case 'i':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'o':
|
|
|
|
case 'x':
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'X':
|
|
|
|
case 'b':
|
2001-10-22 02:48:18 -04:00
|
|
|
case 'B':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
volatile VALUE val = GETARG();
|
2013-06-11 08:06:40 -04:00
|
|
|
int valsign;
|
2013-06-11 23:09:28 -04:00
|
|
|
char nbuf[64], *s;
|
2006-06-20 14:02:17 -04:00
|
|
|
const char *prefix = 0;
|
2008-01-24 05:29:22 -05:00
|
|
|
int sign = 0, dots = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
char sc = 0;
|
2003-01-16 13:48:20 -05:00
|
|
|
long v = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
int base, bignum = 0;
|
2011-04-25 03:56:42 -04:00
|
|
|
int len;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
2007-07-16 12:58:58 -04:00
|
|
|
case 'u':
|
1999-01-19 23:59:39 -05:00
|
|
|
sign = 1; break;
|
|
|
|
case 'o':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
case 'b':
|
2001-10-22 02:48:18 -04:00
|
|
|
case 'B':
|
1999-01-19 23:59:39 -05:00
|
|
|
if (flags&(FPLUS|FSPACE)) sign = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (flags & FSHARP) {
|
2003-01-16 13:48:20 -05:00
|
|
|
switch (*p) {
|
|
|
|
case 'o':
|
|
|
|
prefix = "0"; break;
|
|
|
|
case 'x':
|
|
|
|
prefix = "0x"; break;
|
|
|
|
case 'X':
|
|
|
|
prefix = "0X"; break;
|
|
|
|
case 'b':
|
|
|
|
prefix = "0b"; break;
|
|
|
|
case 'B':
|
|
|
|
prefix = "0B"; break;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
bin_retry:
|
|
|
|
switch (TYPE(val)) {
|
|
|
|
case T_FLOAT:
|
2008-03-06 01:44:41 -05:00
|
|
|
if (FIXABLE(RFLOAT_VALUE(val))) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 11:00:53 -05:00
|
|
|
val = LONG2FIX((long)RFLOAT_VALUE(val));
|
2007-08-16 18:22:24 -04:00
|
|
|
goto bin_retry;
|
|
|
|
}
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 11:00:53 -05:00
|
|
|
val = rb_dbl2big(RFLOAT_VALUE(val));
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(val)) goto bin_retry;
|
1998-01-16 07:13:05 -05:00
|
|
|
bignum = 1;
|
|
|
|
break;
|
|
|
|
case T_STRING:
|
2009-09-23 00:17:16 -04:00
|
|
|
val = rb_str_to_inum(val, 0, TRUE);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto bin_retry;
|
|
|
|
case T_BIGNUM:
|
|
|
|
bignum = 1;
|
|
|
|
break;
|
2000-01-04 23:41:21 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
v = FIX2LONG(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
2002-04-10 04:45:26 -04:00
|
|
|
default:
|
2002-06-04 03:34:19 -04:00
|
|
|
val = rb_Integer(val);
|
|
|
|
goto bin_retry;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2003-01-16 13:48:20 -05:00
|
|
|
switch (*p) {
|
|
|
|
case 'o':
|
|
|
|
base = 8; break;
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
base = 16; break;
|
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
base = 2; break;
|
|
|
|
case 'u':
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
default:
|
|
|
|
base = 10; break;
|
|
|
|
}
|
2004-02-17 20:24:13 -05:00
|
|
|
|
2013-06-11 08:06:40 -04:00
|
|
|
if (base != 10) {
|
|
|
|
int numbits = ffs(base)-1;
|
|
|
|
size_t abs_nlz_bits;
|
|
|
|
size_t numdigits = rb_absint_numwords(val, numbits, &abs_nlz_bits);
|
|
|
|
long i;
|
|
|
|
if (INT_MAX-1 < numdigits) /* INT_MAX is used because rb_long2int is used later. */
|
|
|
|
rb_raise(rb_eArgError, "size too big");
|
|
|
|
if (sign) {
|
|
|
|
if (numdigits == 0)
|
|
|
|
numdigits = 1;
|
|
|
|
tmp = rb_str_new(NULL, numdigits);
|
|
|
|
valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
|
|
|
|
1, CHAR_BIT-numbits, INTEGER_PACK_BIG_ENDIAN);
|
|
|
|
for (i = 0; i < RSTRING_LEN(tmp); i++)
|
|
|
|
RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
|
|
|
|
s = RSTRING_PTR(tmp);
|
|
|
|
if (valsign < 0) {
|
|
|
|
sc = '-';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FPLUS) {
|
|
|
|
sc = '+';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FSPACE) {
|
|
|
|
sc = ' ';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Following conditional "numdigits++" guarantees the
|
|
|
|
* most significant digit as
|
|
|
|
* - '1'(bin), '7'(oct) or 'f'(hex) for negative numbers
|
|
|
|
* - '0' for zero
|
|
|
|
* - not '0' for positive numbers.
|
|
|
|
*
|
|
|
|
* It also guarantees the most significant two
|
|
|
|
* digits will not be '11'(bin), '77'(oct), 'ff'(hex)
|
|
|
|
* or '00'. */
|
|
|
|
if (numdigits == 0 ||
|
|
|
|
((abs_nlz_bits != (size_t)(numbits-1) ||
|
|
|
|
!rb_absint_singlebit_p(val)) &&
|
2014-02-15 16:17:34 -05:00
|
|
|
(!bignum ? v < 0 : BIGNUM_NEGATIVE_P(val))))
|
2013-06-11 08:06:40 -04:00
|
|
|
numdigits++;
|
|
|
|
tmp = rb_str_new(NULL, numdigits);
|
2013-06-16 05:53:45 -04:00
|
|
|
valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
|
|
|
|
1, CHAR_BIT-numbits, INTEGER_PACK_2COMP | INTEGER_PACK_BIG_ENDIAN);
|
2013-06-11 08:06:40 -04:00
|
|
|
for (i = 0; i < RSTRING_LEN(tmp); i++)
|
|
|
|
RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
|
|
|
|
s = RSTRING_PTR(tmp);
|
|
|
|
dots = valsign < 0;
|
|
|
|
}
|
|
|
|
len = rb_long2int(RSTRING_END(tmp) - s);
|
|
|
|
}
|
|
|
|
else if (!bignum) {
|
|
|
|
valsign = 1;
|
|
|
|
if (v < 0) {
|
|
|
|
v = -v;
|
|
|
|
sc = '-';
|
|
|
|
width--;
|
|
|
|
valsign = -1;
|
|
|
|
}
|
|
|
|
else if (flags & FPLUS) {
|
|
|
|
sc = '+';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FSPACE) {
|
|
|
|
sc = ' ';
|
|
|
|
width--;
|
|
|
|
}
|
2013-06-11 23:09:28 -04:00
|
|
|
snprintf(nbuf, sizeof(nbuf), "%ld", v);
|
2013-06-11 08:06:40 -04:00
|
|
|
s = nbuf;
|
2009-09-23 00:17:16 -04:00
|
|
|
len = (int)strlen(s);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2004-02-17 20:24:13 -05:00
|
|
|
else {
|
2013-06-11 23:09:28 -04:00
|
|
|
tmp = rb_big2str(val, 10);
|
2013-06-11 08:06:40 -04:00
|
|
|
s = RSTRING_PTR(tmp);
|
|
|
|
valsign = 1;
|
|
|
|
if (s[0] == '-') {
|
|
|
|
s++;
|
|
|
|
sc = '-';
|
|
|
|
width--;
|
|
|
|
valsign = -1;
|
|
|
|
}
|
|
|
|
else if (flags & FPLUS) {
|
|
|
|
sc = '+';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FSPACE) {
|
|
|
|
sc = ' ';
|
|
|
|
width--;
|
|
|
|
}
|
2009-09-23 00:17:16 -04:00
|
|
|
len = rb_long2int(RSTRING_END(tmp) - s);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2008-01-24 05:29:22 -05:00
|
|
|
if (dots) {
|
|
|
|
prec -= 2;
|
|
|
|
width -= 2;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (*p == 'X') {
|
|
|
|
char *pp = s;
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
int c;
|
2007-11-27 12:42:12 -05:00
|
|
|
while ((c = (int)(unsigned char)*pp) != 0) {
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
*pp = rb_enc_toupper(c, enc);
|
1999-01-19 23:59:39 -05:00
|
|
|
pp++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2008-02-29 19:38:33 -05:00
|
|
|
if (prefix && !prefix[1]) { /* octal */
|
2010-10-15 04:31:58 -04:00
|
|
|
if (dots) {
|
|
|
|
prefix = 0;
|
|
|
|
}
|
|
|
|
else if (len == 1 && *s == '0') {
|
2008-02-29 19:38:33 -05:00
|
|
|
len = 0;
|
|
|
|
if (flags & FPREC) prec--;
|
2008-01-27 08:33:41 -05:00
|
|
|
}
|
2010-10-15 04:31:58 -04:00
|
|
|
else if ((flags & FPREC) && (prec > len)) {
|
2008-01-27 08:33:41 -05:00
|
|
|
prefix = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-02-29 19:38:33 -05:00
|
|
|
else if (len == 1 && *s == '0') {
|
|
|
|
prefix = 0;
|
|
|
|
}
|
2008-01-27 08:33:41 -05:00
|
|
|
if (prefix) {
|
2009-09-23 00:17:16 -04:00
|
|
|
width -= (int)strlen(prefix);
|
2008-01-27 08:33:41 -05:00
|
|
|
}
|
2008-01-27 09:33:37 -05:00
|
|
|
if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
|
2002-12-10 01:23:44 -05:00
|
|
|
prec = width;
|
|
|
|
width = 0;
|
|
|
|
}
|
|
|
|
else {
|
2008-01-27 09:33:37 -05:00
|
|
|
if (prec < len) {
|
2008-02-29 19:38:33 -05:00
|
|
|
if (!prefix && prec == 0 && len == 1 && *s == '0') len = 0;
|
|
|
|
prec = len;
|
2008-01-27 09:33:37 -05:00
|
|
|
}
|
2002-12-10 01:23:44 -05:00
|
|
|
width -= prec;
|
|
|
|
}
|
|
|
|
if (!(flags&FMINUS)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
CHECK(width);
|
2002-12-10 01:23:44 -05:00
|
|
|
while (width-- > 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
buf[blen++] = ' ';
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (sc) PUSH(&sc, 1);
|
2008-01-27 08:33:41 -05:00
|
|
|
if (prefix) {
|
2009-09-23 00:17:16 -04:00
|
|
|
int plen = (int)strlen(prefix);
|
2001-01-10 04:52:13 -05:00
|
|
|
PUSH(prefix, plen);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
CHECK(prec - len);
|
2008-01-26 09:46:36 -05:00
|
|
|
if (dots) PUSH("..", 2);
|
2013-06-11 08:06:40 -04:00
|
|
|
if (!sign && valsign < 0) {
|
2003-03-20 01:27:22 -05:00
|
|
|
char c = sign_bits(base, p);
|
2002-12-10 01:23:44 -05:00
|
|
|
while (len < prec--) {
|
|
|
|
buf[blen++] = c;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 09:33:37 -05:00
|
|
|
else if ((flags & (FMINUS|FPREC)) != FMINUS) {
|
2002-12-10 01:23:44 -05:00
|
|
|
while (len < prec--) {
|
2013-06-11 08:06:40 -04:00
|
|
|
buf[blen++] = '0';
|
2002-12-10 01:23:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PUSH(s, len);
|
2011-08-22 10:48:10 -04:00
|
|
|
RB_GC_GUARD(tmp);
|
2002-12-10 01:23:44 -05:00
|
|
|
CHECK(width);
|
|
|
|
while (width-- > 0) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2014-08-18 04:06:48 -04:00
|
|
|
{
|
|
|
|
VALUE val = GETARG(), num, den;
|
|
|
|
int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
|
2014-09-15 22:44:04 -04:00
|
|
|
long len, done = 0;
|
|
|
|
int prefix = 0;
|
2014-08-18 04:06:48 -04:00
|
|
|
if (!RB_TYPE_P(val, T_RATIONAL)) {
|
|
|
|
nextvalue = val;
|
|
|
|
goto float_value;
|
|
|
|
}
|
|
|
|
if (!(flags&FPREC)) prec = default_float_precision;
|
|
|
|
den = rb_rational_den(val);
|
|
|
|
num = rb_rational_num(val);
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if ((SIGNED_VALUE)num < 0) {
|
|
|
|
long n = -FIX2LONG(num);
|
|
|
|
num = LONG2FIX(n);
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rb_num_negative_p(num)) {
|
|
|
|
sign = -1;
|
|
|
|
num = rb_funcallv(num, idUMinus, 0, 0);
|
|
|
|
}
|
2014-08-19 09:28:32 -04:00
|
|
|
if (den != INT2FIX(1) || prec > 1) {
|
2014-08-18 04:06:48 -04:00
|
|
|
const ID idDiv = rb_intern("div");
|
|
|
|
VALUE p10 = rb_int_positive_pow(10, prec);
|
|
|
|
VALUE den_2 = rb_funcall(den, idDiv, 1, INT2FIX(2));
|
|
|
|
num = rb_funcallv(num, '*', 1, &p10);
|
|
|
|
num = rb_funcallv(num, '+', 1, &den_2);
|
|
|
|
num = rb_funcallv(num, idDiv, 1, &den);
|
|
|
|
}
|
|
|
|
else if (prec >= 0) {
|
|
|
|
zero = prec;
|
|
|
|
}
|
|
|
|
val = rb_obj_as_string(num);
|
|
|
|
len = RSTRING_LEN(val) + zero;
|
|
|
|
if (prec >= len) ++len; /* integer part 0 */
|
|
|
|
if (sign || (flags&FSPACE)) ++len;
|
|
|
|
if (prec > 0) ++len; /* period */
|
|
|
|
CHECK(len > width ? len : width);
|
2014-09-14 19:13:36 -04:00
|
|
|
if (sign || (flags&FSPACE)) {
|
|
|
|
buf[blen++] = sign > 0 ? '+' : sign < 0 ? '-' : ' ';
|
|
|
|
prefix++;
|
|
|
|
done++;
|
2014-08-18 04:06:48 -04:00
|
|
|
}
|
|
|
|
len = RSTRING_LEN(val) + zero;
|
|
|
|
t = RSTRING_PTR(val);
|
2014-09-14 19:13:36 -04:00
|
|
|
if (len > prec) {
|
2014-08-18 04:06:48 -04:00
|
|
|
memcpy(&buf[blen], t, len - prec);
|
2014-09-14 19:13:36 -04:00
|
|
|
blen += len - prec;
|
|
|
|
done += len - prec;
|
|
|
|
}
|
|
|
|
else {
|
2014-08-18 04:06:48 -04:00
|
|
|
buf[blen++] = '0';
|
2014-09-14 19:13:36 -04:00
|
|
|
done++;
|
|
|
|
}
|
|
|
|
if (prec > 0) {
|
|
|
|
buf[blen++] = '.';
|
|
|
|
done++;
|
|
|
|
}
|
|
|
|
if (zero) {
|
|
|
|
FILL('0', zero);
|
|
|
|
done += zero;
|
|
|
|
}
|
|
|
|
else if (prec > len) {
|
|
|
|
FILL('0', prec - len);
|
|
|
|
memcpy(&buf[blen], t, len);
|
|
|
|
blen += len;
|
|
|
|
done += prec;
|
|
|
|
}
|
2014-08-18 04:06:48 -04:00
|
|
|
else if (prec > 0) {
|
|
|
|
memcpy(&buf[blen], t + len - prec, prec);
|
|
|
|
blen += prec;
|
2014-09-14 19:13:36 -04:00
|
|
|
done += prec;
|
|
|
|
}
|
|
|
|
if ((flags & FWIDTH) && width > done) {
|
|
|
|
if (!(flags&FMINUS)) {
|
2014-09-15 22:44:04 -04:00
|
|
|
long i, shifting = (flags&FZERO) ? done - prefix : done;
|
2014-09-14 19:13:36 -04:00
|
|
|
for (i = 1; i <= shifting; i++)
|
|
|
|
buf[width - i] = buf[done - i];
|
|
|
|
blen -= shifting;
|
|
|
|
FILL((flags&FZERO) ? '0' : ' ', width - done);
|
|
|
|
blen += shifting;
|
|
|
|
} else {
|
|
|
|
FILL(' ', width - done);
|
|
|
|
}
|
2014-08-18 04:06:48 -04:00
|
|
|
}
|
|
|
|
RB_GC_GUARD(val);
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'g':
|
1999-08-13 01:45:20 -04:00
|
|
|
case 'G':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'e':
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'E':
|
2014-08-18 04:06:48 -04:00
|
|
|
/* TODO: rational support */
|
2010-04-01 00:32:57 -04:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
2014-08-18 04:06:48 -04:00
|
|
|
float_value:
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE val = GETARG();
|
|
|
|
double fval;
|
2014-08-18 04:06:37 -04:00
|
|
|
int i, need;
|
1998-01-16 07:13:05 -05:00
|
|
|
char fbuf[32];
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 11:00:53 -05:00
|
|
|
fval = RFLOAT_VALUE(rb_Float(val));
|
2004-06-22 06:28:12 -04:00
|
|
|
if (isnan(fval) || isinf(fval)) {
|
2006-06-20 14:02:17 -04:00
|
|
|
const char *expr;
|
2004-06-22 06:28:12 -04:00
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
if (isnan(fval)) {
|
2004-06-22 06:28:12 -04:00
|
|
|
expr = "NaN";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
expr = "Inf";
|
|
|
|
}
|
2009-09-23 00:17:16 -04:00
|
|
|
need = (int)strlen(expr);
|
2004-06-22 10:38:21 -04:00
|
|
|
if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
|
2004-06-22 06:28:12 -04:00
|
|
|
need++;
|
|
|
|
if ((flags & FWIDTH) && need < width)
|
|
|
|
need = width;
|
|
|
|
|
2008-12-22 10:18:12 -05:00
|
|
|
CHECK(need + 1);
|
|
|
|
snprintf(&buf[blen], need + 1, "%*s", need, "");
|
2004-06-22 06:28:12 -04:00
|
|
|
if (flags & FMINUS) {
|
2004-06-22 10:38:21 -04:00
|
|
|
if (!isnan(fval) && fval < 0.0)
|
2004-06-22 06:28:12 -04:00
|
|
|
buf[blen++] = '-';
|
|
|
|
else if (flags & FPLUS)
|
|
|
|
buf[blen++] = '+';
|
2004-06-22 10:38:21 -04:00
|
|
|
else if (flags & FSPACE)
|
|
|
|
blen++;
|
* dir.c, dln.c, parse.y, re.c, ruby.c, sprintf.c, strftime.c,
string.c, util.c, variable.c: use strlcpy, memcpy and snprintf
instead of strcpy, strncpy and sprintf.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-16 21:29:17 -04:00
|
|
|
memcpy(&buf[blen], expr, strlen(expr));
|
2004-06-22 10:38:21 -04:00
|
|
|
}
|
2004-06-22 06:28:12 -04:00
|
|
|
else {
|
2004-06-22 10:38:21 -04:00
|
|
|
if (!isnan(fval) && fval < 0.0)
|
2004-06-22 06:28:12 -04:00
|
|
|
buf[blen + need - strlen(expr) - 1] = '-';
|
|
|
|
else if (flags & FPLUS)
|
|
|
|
buf[blen + need - strlen(expr) - 1] = '+';
|
2008-03-06 03:26:55 -05:00
|
|
|
else if ((flags & FSPACE) && need > width)
|
|
|
|
blen++;
|
* dir.c, dln.c, parse.y, re.c, ruby.c, sprintf.c, strftime.c,
string.c, util.c, variable.c: use strlcpy, memcpy and snprintf
instead of strcpy, strncpy and sprintf.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-16 21:29:17 -04:00
|
|
|
memcpy(&buf[blen + need - strlen(expr)], expr,
|
|
|
|
strlen(expr));
|
2004-06-22 06:28:12 -04:00
|
|
|
}
|
|
|
|
blen += strlen(&buf[blen]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-21 22:05:20 -05:00
|
|
|
fmt_setup(fbuf, sizeof(fbuf), *p, flags, width, prec);
|
1999-08-13 01:45:20 -04:00
|
|
|
need = 0;
|
|
|
|
if (*p != 'e' && *p != 'E') {
|
|
|
|
i = INT_MIN;
|
|
|
|
frexp(fval, &i);
|
|
|
|
if (i > 0)
|
|
|
|
need = BIT_DIGITS(i);
|
|
|
|
}
|
2014-08-18 04:06:37 -04:00
|
|
|
need += (flags&FPREC) ? prec : default_float_precision;
|
1999-08-13 01:45:20 -04:00
|
|
|
if ((flags&FWIDTH) && need < width)
|
|
|
|
need = width;
|
|
|
|
need += 20;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
CHECK(need);
|
2008-12-21 22:05:20 -05:00
|
|
|
snprintf(&buf[blen], need, fbuf, fval);
|
1998-01-16 07:13:05 -05:00
|
|
|
blen += strlen(&buf[blen]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
flags = FNONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprint_exit:
|
2011-08-22 10:48:10 -04:00
|
|
|
RB_GC_GUARD(fmt);
|
2007-12-31 04:06:45 -05:00
|
|
|
/* XXX - We cannot validate the number of arguments if (digit)$ style used.
|
1999-11-10 23:08:26 -05:00
|
|
|
*/
|
2005-06-07 04:22:42 -04:00
|
|
|
if (posarg >= 0 && nextarg < argc) {
|
|
|
|
const char *mesg = "too many arguments for format string";
|
2008-07-01 07:59:50 -04:00
|
|
|
if (RTEST(ruby_debug)) rb_raise(rb_eArgError, "%s", mesg);
|
|
|
|
if (RTEST(ruby_verbose)) rb_warn("%s", mesg);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-10-16 09:44:19 -04:00
|
|
|
rb_str_resize(result, blen);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-12-06 04:04:03 -05:00
|
|
|
if (tainted) OBJ_TAINT(result);
|
1998-01-16 07:13:05 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-21 22:05:20 -05:00
|
|
|
fmt_setup(char *buf, size_t size, int c, int flags, int width, int prec)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2008-12-21 22:05:20 -05:00
|
|
|
char *end = buf + size;
|
1998-01-16 07:13:05 -05:00
|
|
|
*buf++ = '%';
|
|
|
|
if (flags & FSHARP) *buf++ = '#';
|
|
|
|
if (flags & FPLUS) *buf++ = '+';
|
|
|
|
if (flags & FMINUS) *buf++ = '-';
|
|
|
|
if (flags & FZERO) *buf++ = '0';
|
1999-01-19 23:59:39 -05:00
|
|
|
if (flags & FSPACE) *buf++ = ' ';
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (flags & FWIDTH) {
|
2008-12-21 22:05:20 -05:00
|
|
|
snprintf(buf, end - buf, "%d", width);
|
1998-01-16 07:13:05 -05:00
|
|
|
buf += strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FPREC) {
|
2008-12-21 22:05:20 -05:00
|
|
|
snprintf(buf, end - buf, ".%d", prec);
|
1998-01-16 07:13:05 -05:00
|
|
|
buf += strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf++ = c;
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
2005-07-22 21:02:18 -04:00
|
|
|
|
|
|
|
#undef FILE
|
|
|
|
#define FILE rb_printf_buffer
|
|
|
|
#define __sbuf rb_printf_sbuf
|
|
|
|
#define __sFILE rb_printf_sfile
|
|
|
|
#undef feof
|
|
|
|
#undef ferror
|
2005-07-28 08:49:31 -04:00
|
|
|
#undef clearerr
|
2005-07-22 21:02:18 -04:00
|
|
|
#undef fileno
|
2005-07-27 03:27:19 -04:00
|
|
|
#if SIZEOF_LONG < SIZEOF_VOIDP
|
|
|
|
# if SIZEOF_LONG_LONG == SIZEOF_VOIDP
|
|
|
|
# define _HAVE_SANE_QUAD_
|
|
|
|
# define _HAVE_LLP64_
|
2005-07-27 10:30:09 -04:00
|
|
|
# define quad_t LONG_LONG
|
2005-07-27 03:27:19 -04:00
|
|
|
# define u_quad_t unsigned LONG_LONG
|
|
|
|
# endif
|
2010-12-09 20:50:51 -05:00
|
|
|
#elif SIZEOF_LONG != SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == 8
|
|
|
|
# define _HAVE_SANE_QUAD_
|
|
|
|
# define quad_t LONG_LONG
|
|
|
|
# define u_quad_t unsigned LONG_LONG
|
2005-07-27 03:27:19 -04:00
|
|
|
#endif
|
2008-08-12 03:06:33 -04:00
|
|
|
#define FLOATING_POINT 1
|
2008-10-24 14:56:31 -04:00
|
|
|
#define BSD__dtoa ruby_dtoa
|
2010-07-20 22:55:10 -04:00
|
|
|
#define BSD__hdtoa ruby_hdtoa
|
2014-11-03 22:51:33 -05:00
|
|
|
#ifdef RUBY_PRI_VALUE_MARK
|
|
|
|
# define PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK
|
|
|
|
#endif
|
2015-02-13 02:07:39 -05:00
|
|
|
#define lower_hexdigits (ruby_hexdigits+0)
|
|
|
|
#define upper_hexdigits (ruby_hexdigits+16)
|
2010-04-03 21:10:53 -04:00
|
|
|
#include "vsnprintf.c"
|
2005-07-22 21:02:18 -04:00
|
|
|
|
2015-02-21 20:40:40 -05:00
|
|
|
int
|
|
|
|
ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
rb_printf_buffer f;
|
|
|
|
|
|
|
|
if ((int)n < 1)
|
|
|
|
return (EOF);
|
|
|
|
f._flags = __SWR | __SSTR;
|
|
|
|
f._bf._base = f._p = (unsigned char *)str;
|
|
|
|
f._bf._size = f._w = n - 1;
|
|
|
|
f.vwrite = BSD__sfvwrite;
|
|
|
|
f.vextra = 0;
|
|
|
|
ret = (int)BSD_vfprintf(&f, fmt, ap);
|
|
|
|
*f._p = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ruby_snprintf(char *str, size_t n, char const *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if ((int)n < 1)
|
|
|
|
return (EOF);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = ruby_vsnprintf(str, n, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-24 10:36:54 -04:00
|
|
|
typedef struct {
|
|
|
|
rb_printf_buffer base;
|
|
|
|
volatile VALUE value;
|
|
|
|
} rb_printf_buffer_extra;
|
|
|
|
|
2005-07-22 21:02:18 -04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
|
2005-07-22 21:02:18 -04:00
|
|
|
{
|
|
|
|
struct __siov *iov;
|
|
|
|
VALUE result = (VALUE)fp->_bf._base;
|
2005-07-27 22:33:28 -04:00
|
|
|
char *buf = (char*)fp->_p;
|
2005-07-22 21:02:18 -04:00
|
|
|
size_t len, n;
|
2008-07-21 02:29:35 -04:00
|
|
|
size_t blen = buf - RSTRING_PTR(result), bsiz = fp->_w;
|
2005-07-22 21:02:18 -04:00
|
|
|
|
|
|
|
if (RBASIC(result)->klass) {
|
|
|
|
rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
|
|
|
|
}
|
|
|
|
if ((len = uio->uio_resid) == 0)
|
|
|
|
return 0;
|
|
|
|
CHECK(len);
|
2005-08-22 12:21:27 -04:00
|
|
|
buf += blen;
|
2005-07-22 21:02:18 -04:00
|
|
|
fp->_w = bsiz;
|
|
|
|
for (iov = uio->uio_iov; len > 0; ++iov) {
|
|
|
|
MEMCPY(buf, iov->iov_base, char, n = iov->iov_len);
|
|
|
|
buf += n;
|
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
fp->_p = (unsigned char *)buf;
|
2012-08-15 03:23:38 -04:00
|
|
|
rb_str_set_len(result, buf - RSTRING_PTR(result));
|
2005-07-22 21:02:18 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-24 10:36:54 -04:00
|
|
|
static char *
|
2012-05-24 12:15:42 -04:00
|
|
|
ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
|
2012-05-24 10:36:54 -04:00
|
|
|
{
|
|
|
|
VALUE value, result = (VALUE)fp->_bf._base;
|
|
|
|
rb_encoding *enc;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (valsize != sizeof(VALUE)) return 0;
|
|
|
|
value = *(VALUE *)valp;
|
|
|
|
if (RBASIC(result)->klass) {
|
|
|
|
rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
|
|
|
|
}
|
2012-05-24 12:15:42 -04:00
|
|
|
if (sign == '+') {
|
|
|
|
value = rb_inspect(value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = rb_obj_as_string(value);
|
2013-07-30 04:16:20 -04:00
|
|
|
if (sign == ' ') value = QUOTE(value);
|
2012-05-24 12:15:42 -04:00
|
|
|
}
|
2012-05-24 10:36:54 -04:00
|
|
|
enc = rb_enc_compatible(result, value);
|
|
|
|
if (enc) {
|
|
|
|
rb_enc_associate(result, enc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enc = rb_enc_get(result);
|
|
|
|
value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc,
|
|
|
|
ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
|
|
|
|
Qnil);
|
|
|
|
*(volatile VALUE *)valp = value;
|
|
|
|
}
|
|
|
|
StringValueCStr(value);
|
|
|
|
RSTRING_GETMEM(value, cp, *sz);
|
|
|
|
((rb_printf_buffer_extra *)fp)->value = value;
|
2012-08-15 03:26:55 -04:00
|
|
|
OBJ_INFECT(result, value);
|
2012-05-24 10:36:54 -04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2005-07-22 21:02:18 -04:00
|
|
|
VALUE
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
|
2005-07-22 21:02:18 -04:00
|
|
|
{
|
2012-05-24 10:36:54 -04:00
|
|
|
rb_printf_buffer_extra buffer;
|
|
|
|
#define f buffer.base
|
2005-07-22 21:02:18 -04:00
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
f._flags = __SWR | __SSTR;
|
|
|
|
f._bf._size = 0;
|
|
|
|
f._w = 120;
|
|
|
|
result = rb_str_buf_new(f._w);
|
2012-01-13 04:41:22 -05:00
|
|
|
if (enc) {
|
2012-01-13 22:02:57 -05:00
|
|
|
if (rb_enc_mbminlen(enc) > 1) {
|
|
|
|
/* the implementation deeply depends on plain char */
|
|
|
|
rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
|
2012-01-13 04:41:22 -05:00
|
|
|
rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
rb_enc_associate(result, enc);
|
|
|
|
}
|
2005-07-22 21:02:18 -04:00
|
|
|
f._bf._base = (unsigned char *)result;
|
2006-08-31 06:47:44 -04:00
|
|
|
f._p = (unsigned char *)RSTRING_PTR(result);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_CLEAR_CLASS(result);
|
2005-07-22 21:02:18 -04:00
|
|
|
f.vwrite = ruby__sfvwrite;
|
2012-05-24 10:36:54 -04:00
|
|
|
f.vextra = ruby__sfvextra;
|
|
|
|
buffer.value = 0;
|
2005-07-22 21:02:18 -04:00
|
|
|
BSD_vfprintf(&f, fmt, ap);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_SET_CLASS_RAW(result, rb_cString);
|
2006-08-31 06:47:44 -04:00
|
|
|
rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
|
2012-05-24 10:36:54 -04:00
|
|
|
#undef f
|
2005-07-22 21:02:18 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-12-30 16:08:36 -05:00
|
|
|
VALUE
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_sprintf(rb_encoding *enc, const char *format, ...)
|
2007-12-30 16:08:36 -05:00
|
|
|
{
|
|
|
|
VALUE result;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
result = rb_enc_vsprintf(enc, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_vsprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
return rb_enc_vsprintf(NULL, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2005-07-22 21:02:18 -04:00
|
|
|
VALUE
|
|
|
|
rb_sprintf(const char *format, ...)
|
|
|
|
{
|
|
|
|
VALUE result;
|
|
|
|
va_list ap;
|
|
|
|
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
va_start(ap, format);
|
2005-07-22 21:02:18 -04:00
|
|
|
result = rb_vsprintf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2008-07-22 03:48:00 -04:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
|
|
|
|
{
|
2012-05-24 10:36:54 -04:00
|
|
|
rb_printf_buffer_extra buffer;
|
|
|
|
#define f buffer.base
|
2008-07-22 03:48:00 -04:00
|
|
|
VALUE klass;
|
|
|
|
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_modify(str);
|
|
|
|
f._flags = __SWR | __SSTR;
|
|
|
|
f._bf._size = 0;
|
|
|
|
f._w = rb_str_capacity(str);
|
|
|
|
f._bf._base = (unsigned char *)str;
|
|
|
|
f._p = (unsigned char *)RSTRING_END(str);
|
|
|
|
klass = RBASIC(str)->klass;
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_CLEAR_CLASS(str);
|
2008-07-22 03:48:00 -04:00
|
|
|
f.vwrite = ruby__sfvwrite;
|
2012-05-24 10:36:54 -04:00
|
|
|
f.vextra = ruby__sfvextra;
|
|
|
|
buffer.value = 0;
|
2008-07-22 03:48:00 -04:00
|
|
|
BSD_vfprintf(&f, fmt, ap);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_SET_CLASS_RAW(str, klass);
|
2008-07-22 03:48:00 -04:00
|
|
|
rb_str_resize(str, (char *)f._p - RSTRING_PTR(str));
|
2012-05-24 10:36:54 -04:00
|
|
|
#undef f
|
2008-07-22 03:48:00 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_catf(VALUE str, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
str = rb_str_vcatf(str, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|