2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
string.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Mon Aug 9 17:12:58 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-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-06-10 07:06:15 +04:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/re.h"
|
* 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-25 07:29:39 +04:00
|
|
|
#include "ruby/encoding.h"
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-18 02:43:38 +04:00
|
|
|
#include "internal.h"
|
2009-06-11 21:41:27 +04:00
|
|
|
#include <assert.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2011-01-06 18:38:14 +03:00
|
|
|
#define BEG(no) (regs->beg[(no)])
|
|
|
|
#define END(no) (regs->end[(no)])
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-09-26 13:11:47 +04:00
|
|
|
#include <math.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <ctype.h>
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2009-09-23 08:14:23 +04:00
|
|
|
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
|
|
|
|
|
2008-08-12 13:56:55 +04:00
|
|
|
#undef rb_str_new_cstr
|
|
|
|
#undef rb_tainted_str_new_cstr
|
|
|
|
#undef rb_usascii_str_new_cstr
|
2008-11-11 21:31:06 +03:00
|
|
|
#undef rb_external_str_new_cstr
|
|
|
|
#undef rb_locale_str_new_cstr
|
2008-06-27 08:18:54 +04:00
|
|
|
#undef rb_str_new2
|
2008-08-12 13:56:55 +04:00
|
|
|
#undef rb_str_new3
|
|
|
|
#undef rb_str_new4
|
|
|
|
#undef rb_str_new5
|
2008-06-27 08:18:54 +04:00
|
|
|
#undef rb_tainted_str_new2
|
|
|
|
#undef rb_usascii_str_new2
|
2008-08-12 13:56:55 +04:00
|
|
|
#undef rb_str_dup_frozen
|
|
|
|
#undef rb_str_buf_new_cstr
|
2008-07-21 23:26:18 +04:00
|
|
|
#undef rb_str_buf_new2
|
2008-07-21 21:51:49 +04:00
|
|
|
#undef rb_str_buf_cat2
|
|
|
|
#undef rb_str_cat2
|
2008-06-27 08:18:54 +04:00
|
|
|
|
2011-01-27 02:32:22 +03:00
|
|
|
static VALUE rb_str_clear(VALUE str);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cString;
|
2006-09-02 19:05:27 +04:00
|
|
|
VALUE rb_cSymbol;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-15 09:36:15 +04:00
|
|
|
#define RUBY_MAX_CHAR_LEN 16
|
2006-08-31 14:47:44 +04:00
|
|
|
#define STR_TMPLOCK FL_USER7
|
|
|
|
#define STR_NOEMBED FL_USER1
|
* 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-25 07:29:39 +04:00
|
|
|
#define STR_SHARED FL_USER2 /* = ELTS_SHARED */
|
2001-05-30 13:12:34 +04:00
|
|
|
#define STR_ASSOC FL_USER3
|
2011-01-06 18:38:14 +03:00
|
|
|
#define STR_SHARED_P(s) FL_ALL((s), STR_NOEMBED|ELTS_SHARED)
|
|
|
|
#define STR_ASSOC_P(s) FL_ALL((s), STR_NOEMBED|STR_ASSOC)
|
2006-08-31 14:47:44 +04:00
|
|
|
#define STR_NOCAPA (STR_NOEMBED|ELTS_SHARED|STR_ASSOC)
|
2011-01-06 18:38:14 +03:00
|
|
|
#define STR_NOCAPA_P(s) (FL_TEST((s),STR_NOEMBED) && FL_ANY((s),ELTS_SHARED|STR_ASSOC))
|
2006-08-31 14:47:44 +04:00
|
|
|
#define STR_UNSET_NOCAPA(s) do {\
|
2011-01-06 18:38:14 +03:00
|
|
|
if (FL_TEST((s),STR_NOEMBED)) FL_UNSET((s),(ELTS_SHARED|STR_ASSOC));\
|
2006-08-31 14:47:44 +04:00
|
|
|
} while (0)
|
2009-02-22 17:26:27 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
#define STR_SET_NOEMBED(str) do {\
|
2011-01-06 18:38:14 +03:00
|
|
|
FL_SET((str), STR_NOEMBED);\
|
|
|
|
STR_SET_EMBED_LEN((str), 0);\
|
2006-08-31 14:47:44 +04:00
|
|
|
} while (0)
|
2011-01-06 18:38:14 +03:00
|
|
|
#define STR_SET_EMBED(str) FL_UNSET((str), STR_NOEMBED)
|
|
|
|
#define STR_EMBED_P(str) (!FL_TEST((str), STR_NOEMBED))
|
2006-08-31 14:47:44 +04:00
|
|
|
#define STR_SET_EMBED_LEN(str, n) do { \
|
|
|
|
long tmp_n = (n);\
|
|
|
|
RBASIC(str)->flags &= ~RSTRING_EMBED_LEN_MASK;\
|
|
|
|
RBASIC(str)->flags |= (tmp_n) << RSTRING_EMBED_LEN_SHIFT;\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define STR_SET_LEN(str, n) do { \
|
|
|
|
if (STR_EMBED_P(str)) {\
|
2011-01-06 18:38:14 +03:00
|
|
|
STR_SET_EMBED_LEN((str), (n));\
|
2006-08-31 14:47:44 +04:00
|
|
|
}\
|
|
|
|
else {\
|
|
|
|
RSTRING(str)->as.heap.len = (n);\
|
|
|
|
}\
|
2009-02-22 17:26:27 +03:00
|
|
|
} while (0)
|
2006-08-31 14:47:44 +04:00
|
|
|
|
|
|
|
#define STR_DEC_LEN(str) do {\
|
|
|
|
if (STR_EMBED_P(str)) {\
|
|
|
|
long n = RSTRING_LEN(str);\
|
|
|
|
n--;\
|
2011-01-06 18:38:14 +03:00
|
|
|
STR_SET_EMBED_LEN((str), n);\
|
2006-08-31 14:47:44 +04:00
|
|
|
}\
|
|
|
|
else {\
|
|
|
|
RSTRING(str)->as.heap.len--;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2001-12-19 10:48:32 +03:00
|
|
|
#define RESIZE_CAPA(str,capacity) do {\
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_EMBED_P(str)) {\
|
|
|
|
if ((capacity) > RSTRING_EMBED_LEN_MAX) {\
|
2011-01-06 18:38:14 +03:00
|
|
|
char *tmp = ALLOC_N(char, (capacity)+1);\
|
2006-08-31 14:47:44 +04:00
|
|
|
memcpy(tmp, RSTRING_PTR(str), RSTRING_LEN(str));\
|
|
|
|
RSTRING(str)->as.heap.ptr = tmp;\
|
|
|
|
RSTRING(str)->as.heap.len = RSTRING_LEN(str);\
|
|
|
|
STR_SET_NOEMBED(str);\
|
|
|
|
RSTRING(str)->as.heap.aux.capa = (capacity);\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
else {\
|
|
|
|
REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+1);\
|
|
|
|
if (!STR_NOCAPA_P(str))\
|
|
|
|
RSTRING(str)->as.heap.aux.capa = (capacity);\
|
|
|
|
}\
|
2001-12-19 10:48:32 +03:00
|
|
|
} while (0)
|
|
|
|
|
2007-11-27 05:21:17 +03:00
|
|
|
#define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
|
2007-12-17 12:08:27 +03:00
|
|
|
#define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN)
|
2007-09-26 23:46:58 +04:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
#define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str))
|
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
static inline int
|
2007-12-31 13:39:20 +03:00
|
|
|
single_byte_optimizable(VALUE str)
|
|
|
|
{
|
2008-09-15 18:40:00 +04:00
|
|
|
rb_encoding *enc;
|
2007-12-31 13:39:20 +03:00
|
|
|
|
2008-01-05 06:35:13 +03:00
|
|
|
/* Conservative. It may be ENC_CODERANGE_UNKNOWN. */
|
2007-12-31 13:39:20 +03:00
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT)
|
|
|
|
return 1;
|
|
|
|
|
2008-09-15 18:40:00 +04:00
|
|
|
enc = STR_ENC_GET(str);
|
|
|
|
if (rb_enc_mbmaxlen(enc) == 1)
|
|
|
|
return 1;
|
|
|
|
|
2008-01-05 06:35:13 +03:00
|
|
|
/* Conservative. Possibly single byte.
|
2007-12-31 13:39:20 +03:00
|
|
|
* "\xa1" in Shift_JIS for example. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE rb_fs;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-01-19 16:42:50 +03:00
|
|
|
static inline const char *
|
|
|
|
search_nonascii(const char *p, const char *e)
|
|
|
|
{
|
2008-03-08 12:05:34 +03:00
|
|
|
#if SIZEOF_VALUE == 8
|
2010-07-05 19:32:35 +04:00
|
|
|
# define NONASCII_MASK 0x8080808080808080ULL
|
2008-03-08 12:05:34 +03:00
|
|
|
#elif SIZEOF_VALUE == 4
|
2008-01-19 16:42:50 +03:00
|
|
|
# define NONASCII_MASK 0x80808080UL
|
|
|
|
#endif
|
|
|
|
#ifdef NONASCII_MASK
|
2009-05-07 09:16:26 +04:00
|
|
|
if ((int)sizeof(VALUE) * 2 < e - p) {
|
2008-03-08 12:05:34 +03:00
|
|
|
const VALUE *s, *t;
|
|
|
|
const VALUE lowbits = sizeof(VALUE) - 1;
|
|
|
|
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
|
2008-01-19 16:42:50 +03:00
|
|
|
while (p < (const char *)s) {
|
|
|
|
if (!ISASCII(*p))
|
|
|
|
return p;
|
|
|
|
p++;
|
|
|
|
}
|
2008-03-08 12:05:34 +03:00
|
|
|
t = (const VALUE*)(~lowbits & (VALUE)e);
|
2008-01-19 16:42:50 +03:00
|
|
|
while (s < t) {
|
|
|
|
if (*s & NONASCII_MASK) {
|
|
|
|
t = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
p = (const char *)t;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (p < e) {
|
|
|
|
if (!ISASCII(*p))
|
|
|
|
return p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-06 12:25:09 +03:00
|
|
|
static int
|
|
|
|
coderange_scan(const char *p, long len, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
const char *e = p + len;
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
if (rb_enc_to_index(enc) == 0) {
|
|
|
|
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
|
2008-01-19 16:42:50 +03:00
|
|
|
p = search_nonascii(p, e);
|
|
|
|
return p ? ENC_CODERANGE_VALID : ENC_CODERANGE_7BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_enc_asciicompat(enc)) {
|
|
|
|
p = search_nonascii(p, e);
|
|
|
|
if (!p) {
|
|
|
|
return ENC_CODERANGE_7BIT;
|
|
|
|
}
|
2008-01-06 19:38:04 +03:00
|
|
|
while (p < e) {
|
2008-01-19 16:42:50 +03:00
|
|
|
int ret = rb_enc_precise_mbclen(p, e, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_CHARFOUND_P(ret)) {
|
2008-01-19 16:42:50 +03:00
|
|
|
return ENC_CODERANGE_BROKEN;
|
|
|
|
}
|
2008-01-27 17:27:07 +03:00
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
2008-01-19 16:42:50 +03:00
|
|
|
if (p < e) {
|
|
|
|
p = search_nonascii(p, e);
|
|
|
|
if (!p) {
|
|
|
|
return ENC_CODERANGE_VALID;
|
|
|
|
}
|
2008-01-06 19:38:04 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-19 16:42:50 +03:00
|
|
|
if (e < p) {
|
|
|
|
return ENC_CODERANGE_BROKEN;
|
|
|
|
}
|
|
|
|
return ENC_CODERANGE_VALID;
|
2008-01-06 19:38:04 +03:00
|
|
|
}
|
|
|
|
|
2008-01-06 12:25:09 +03:00
|
|
|
while (p < e) {
|
|
|
|
int ret = rb_enc_precise_mbclen(p, e, enc);
|
|
|
|
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_CHARFOUND_P(ret)) {
|
2008-01-19 16:42:50 +03:00
|
|
|
return ENC_CODERANGE_BROKEN;
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
2008-01-27 17:27:07 +03:00
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
2008-01-19 16:42:50 +03:00
|
|
|
if (e < p) {
|
|
|
|
return ENC_CODERANGE_BROKEN;
|
|
|
|
}
|
|
|
|
return ENC_CODERANGE_VALID;
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
|
|
|
|
2008-02-27 18:19:22 +03:00
|
|
|
long
|
|
|
|
rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding *enc, int *cr)
|
|
|
|
{
|
2008-02-28 05:13:51 +03:00
|
|
|
const char *p = s;
|
2008-02-27 18:19:22 +03:00
|
|
|
|
|
|
|
if (*cr == ENC_CODERANGE_BROKEN)
|
|
|
|
return e - s;
|
|
|
|
|
|
|
|
if (rb_enc_to_index(enc) == 0) {
|
|
|
|
/* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
|
|
|
|
p = search_nonascii(p, e);
|
|
|
|
*cr = (!p && *cr != ENC_CODERANGE_VALID) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
|
|
|
|
return e - s;
|
|
|
|
}
|
|
|
|
else if (rb_enc_asciicompat(enc)) {
|
|
|
|
p = search_nonascii(p, e);
|
|
|
|
if (!p) {
|
|
|
|
if (*cr != ENC_CODERANGE_VALID) *cr = ENC_CODERANGE_7BIT;
|
|
|
|
return e - s;
|
|
|
|
}
|
|
|
|
while (p < e) {
|
|
|
|
int ret = rb_enc_precise_mbclen(p, e, enc);
|
|
|
|
if (!MBCLEN_CHARFOUND_P(ret)) {
|
2008-03-01 04:02:30 +03:00
|
|
|
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
|
|
|
|
return p - s;
|
2008-02-27 18:19:22 +03:00
|
|
|
}
|
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
|
|
|
if (p < e) {
|
|
|
|
p = search_nonascii(p, e);
|
|
|
|
if (!p) {
|
|
|
|
*cr = ENC_CODERANGE_VALID;
|
|
|
|
return e - s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*cr = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
|
|
|
|
return p - s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (p < e) {
|
|
|
|
int ret = rb_enc_precise_mbclen(p, e, enc);
|
|
|
|
if (!MBCLEN_CHARFOUND_P(ret)) {
|
2008-03-01 04:02:30 +03:00
|
|
|
*cr = MBCLEN_INVALID_P(ret) ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_UNKNOWN;
|
2008-02-27 18:19:22 +03:00
|
|
|
return p - s;
|
|
|
|
}
|
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
|
|
|
}
|
|
|
|
*cr = e < p ? ENC_CODERANGE_BROKEN: ENC_CODERANGE_VALID;
|
|
|
|
return p - s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-15 12:19:38 +03:00
|
|
|
static inline void
|
|
|
|
str_enc_copy(VALUE str1, VALUE str2)
|
|
|
|
{
|
2008-05-19 12:25:03 +04:00
|
|
|
rb_enc_set_index(str1, ENCODING_GET(str2));
|
2008-03-15 12:19:38 +03:00
|
|
|
}
|
|
|
|
|
2008-02-23 13:29:13 +03:00
|
|
|
static void
|
|
|
|
rb_enc_cr_str_copy_for_substr(VALUE dest, VALUE src)
|
2008-02-20 13:20:43 +03:00
|
|
|
{
|
2008-02-23 13:29:13 +03:00
|
|
|
/* this function is designed for copying encoding and coderange
|
|
|
|
* from src to new string "dest" which is made from the part of src.
|
|
|
|
*/
|
2008-03-15 12:19:38 +03:00
|
|
|
str_enc_copy(dest, src);
|
2008-02-22 15:15:56 +03:00
|
|
|
switch (ENC_CODERANGE(src)) {
|
2008-02-28 05:13:51 +03:00
|
|
|
case ENC_CODERANGE_7BIT:
|
2008-02-22 15:15:56 +03:00
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
|
|
|
|
break;
|
2008-02-28 05:13:51 +03:00
|
|
|
case ENC_CODERANGE_VALID:
|
2008-02-22 18:02:40 +03:00
|
|
|
if (!rb_enc_asciicompat(STR_ENC_GET(src)) ||
|
2008-02-22 15:56:21 +03:00
|
|
|
search_nonascii(RSTRING_PTR(dest), RSTRING_END(dest)))
|
2008-02-22 15:15:56 +03:00
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
|
|
|
|
else
|
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
|
|
|
|
break;
|
2008-02-28 05:13:51 +03:00
|
|
|
default:
|
2008-02-23 13:29:13 +03:00
|
|
|
if (RSTRING_LEN(dest) == 0) {
|
|
|
|
if (!rb_enc_asciicompat(STR_ENC_GET(src)))
|
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE_VALID);
|
|
|
|
else
|
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE_7BIT);
|
|
|
|
}
|
|
|
|
break;
|
2008-02-22 15:15:56 +03:00
|
|
|
}
|
2008-02-21 05:42:51 +03:00
|
|
|
}
|
|
|
|
|
2008-02-23 13:29:13 +03:00
|
|
|
static void
|
2008-02-21 05:42:51 +03:00
|
|
|
rb_enc_cr_str_exact_copy(VALUE dest, VALUE src)
|
|
|
|
{
|
2008-03-15 12:19:38 +03:00
|
|
|
str_enc_copy(dest, src);
|
2008-02-21 05:42:51 +03:00
|
|
|
ENC_CODERANGE_SET(dest, ENC_CODERANGE(src));
|
2008-02-20 13:20:43 +03:00
|
|
|
}
|
|
|
|
|
2007-09-26 23:46:58 +04:00
|
|
|
int
|
|
|
|
rb_enc_str_coderange(VALUE str)
|
|
|
|
{
|
|
|
|
int cr = ENC_CODERANGE(str);
|
|
|
|
|
|
|
|
if (cr == ENC_CODERANGE_UNKNOWN) {
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2008-01-06 12:25:09 +03:00
|
|
|
cr = coderange_scan(RSTRING_PTR(str), RSTRING_LEN(str), enc);
|
2007-12-17 10:41:21 +03:00
|
|
|
ENC_CODERANGE_SET(str, cr);
|
2007-09-26 23:46:58 +04:00
|
|
|
}
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
2008-02-28 05:13:51 +03:00
|
|
|
int
|
|
|
|
rb_enc_str_asciionly_p(VALUE str)
|
2007-11-25 16:25:34 +03:00
|
|
|
{
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2007-11-25 16:25:34 +03:00
|
|
|
|
2007-12-17 11:08:23 +03:00
|
|
|
if (!rb_enc_asciicompat(enc))
|
2009-09-18 19:17:17 +04:00
|
|
|
return FALSE;
|
2007-12-17 11:08:23 +03:00
|
|
|
else if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
|
2009-09-18 19:17:17 +04:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
2007-11-25 16:25:34 +03:00
|
|
|
}
|
|
|
|
|
2004-10-27 04:18:08 +04:00
|
|
|
static inline void
|
2008-01-23 05:53:28 +03:00
|
|
|
str_mod_check(VALUE s, const char *p, long len)
|
2004-10-27 04:18:08 +04:00
|
|
|
{
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_PTR(s) != p || RSTRING_LEN(s) != len){
|
2004-10-27 04:18:08 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "string modified");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 11:48:00 +04:00
|
|
|
size_t
|
|
|
|
rb_str_capacity(VALUE str)
|
|
|
|
{
|
|
|
|
if (STR_EMBED_P(str)) {
|
|
|
|
return RSTRING_EMBED_LEN_MAX;
|
|
|
|
}
|
|
|
|
else if (STR_NOCAPA_P(str)) {
|
|
|
|
return RSTRING(str)->as.heap.len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return RSTRING(str)->as.heap.aux.capa;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-19 12:39:03 +04:00
|
|
|
static inline 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 14:44:21 +04:00
|
|
|
str_alloc(VALUE klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
NEWOBJ(str, struct RString);
|
2001-10-02 08:31:23 +04:00
|
|
|
OBJSETUP(str, klass, T_STRING);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
str->as.heap.ptr = 0;
|
|
|
|
str->as.heap.len = 0;
|
|
|
|
str->as.heap.aux.capa = 0;
|
2001-10-03 11:19:19 +04:00
|
|
|
|
|
|
|
return (VALUE)str;
|
|
|
|
}
|
|
|
|
|
2007-12-10 11:46:06 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
str_new(VALUE klass, const char *ptr, long len)
|
2001-10-03 11:19:19 +04:00
|
|
|
{
|
2002-07-11 12:22:18 +04:00
|
|
|
VALUE str;
|
2001-10-03 11:19:19 +04:00
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
|
2002-12-20 11:33:17 +03:00
|
|
|
str = str_alloc(klass);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len > RSTRING_EMBED_LEN_MAX) {
|
|
|
|
RSTRING(str)->as.heap.aux.capa = len;
|
|
|
|
RSTRING(str)->as.heap.ptr = ALLOC_N(char,len+1);
|
|
|
|
STR_SET_NOEMBED(str);
|
|
|
|
}
|
2008-09-28 07:04:21 +04:00
|
|
|
else if (len == 0) {
|
|
|
|
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (ptr) {
|
2006-08-31 14:47:44 +04:00
|
|
|
memcpy(RSTRING_PTR(str), ptr, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
2001-10-03 11:19:19 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04: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 14:44:21 +04:00
|
|
|
rb_str_new(const char *ptr, long len)
|
2001-10-02 08:31:23 +04:00
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
return str_new(rb_cString, ptr, len);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
|
|
|
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
VALUE
|
|
|
|
rb_usascii_str_new(const char *ptr, long len)
|
|
|
|
{
|
2008-05-21 02:48:49 +04:00
|
|
|
VALUE str = rb_str_new(ptr, len);
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
|
|
|
return 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-25 07:29:39 +04:00
|
|
|
VALUE
|
|
|
|
rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
|
|
|
|
{
|
2008-05-21 02:48:49 +04:00
|
|
|
VALUE str = rb_str_new(ptr, len);
|
* 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-25 07:29:39 +04:00
|
|
|
rb_enc_associate(str, enc);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_new_cstr(const char *ptr)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2002-01-07 08:27:01 +03:00
|
|
|
if (!ptr) {
|
|
|
|
rb_raise(rb_eArgError, "NULL pointer given");
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_str_new(ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_new2(const char *ptr), rb_str_new_cstr, (ptr))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_str_new2 rb_str_new_cstr
|
|
|
|
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_usascii_str_new_cstr(const char *ptr)
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
{
|
2008-05-21 02:48:49 +04:00
|
|
|
VALUE str = rb_str_new2(ptr);
|
|
|
|
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
|
|
|
return str;
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_usascii_str_new2(const char *ptr), rb_usascii_str_new_cstr, (ptr))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_usascii_str_new2 rb_usascii_str_new_cstr
|
|
|
|
|
1999-01-20 07:59:39 +03: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 14:44:21 +04:00
|
|
|
rb_tainted_str_new(const char *ptr, long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE str = rb_str_new(ptr, len);
|
|
|
|
|
1999-12-14 09:50:43 +03:00
|
|
|
OBJ_TAINT(str);
|
1999-08-13 09:45:20 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_tainted_str_new_cstr(const char *ptr)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE str = rb_str_new2(ptr);
|
|
|
|
|
1999-12-14 09:50:43 +03:00
|
|
|
OBJ_TAINT(str);
|
1999-08-13 09:45:20 +04:00
|
|
|
return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_tainted_str_new2(const char *ptr), rb_tainted_str_new_cstr, (ptr))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_tainted_str_new2 rb_tainted_str_new_cstr
|
|
|
|
|
2008-10-21 13:18:34 +04:00
|
|
|
VALUE
|
2008-10-22 20:53:50 +04:00
|
|
|
rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
|
2008-10-20 12:05:44 +04:00
|
|
|
{
|
|
|
|
rb_econv_t *ec;
|
|
|
|
rb_econv_result_t ret;
|
|
|
|
long len;
|
|
|
|
VALUE newstr;
|
|
|
|
const unsigned char *sp;
|
|
|
|
unsigned char *dp;
|
|
|
|
|
|
|
|
if (!to) return str;
|
2012-03-15 09:49:34 +04:00
|
|
|
if (!from) from = rb_enc_get(str);
|
2008-10-20 12:05:44 +04:00
|
|
|
if (from == to) return str;
|
2008-10-21 19:52:07 +04:00
|
|
|
if ((rb_enc_asciicompat(to) && ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) ||
|
|
|
|
to == rb_ascii8bit_encoding()) {
|
2008-10-21 13:18:34 +04:00
|
|
|
if (STR_ENC_GET(str) != to) {
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_enc_associate(str, to);
|
|
|
|
}
|
2008-10-20 12:05:44 +04:00
|
|
|
return str;
|
2008-10-21 13:18:34 +04:00
|
|
|
}
|
2008-10-20 12:05:44 +04:00
|
|
|
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
newstr = rb_str_new(0, len);
|
|
|
|
|
|
|
|
retry:
|
2008-10-22 20:53:50 +04:00
|
|
|
ec = rb_econv_open_opts(from->name, to->name, ecflags, ecopts);
|
2008-10-20 12:05:44 +04:00
|
|
|
if (!ec) return str;
|
|
|
|
|
|
|
|
sp = (unsigned char*)RSTRING_PTR(str);
|
|
|
|
dp = (unsigned char*)RSTRING_PTR(newstr);
|
|
|
|
ret = rb_econv_convert(ec, &sp, (unsigned char*)RSTRING_END(str),
|
|
|
|
&dp, (unsigned char*)RSTRING_END(newstr), 0);
|
|
|
|
rb_econv_close(ec);
|
|
|
|
switch (ret) {
|
|
|
|
case econv_destination_buffer_full:
|
|
|
|
/* destination buffer short */
|
2010-07-08 12:18:07 +04:00
|
|
|
len = len < 2 ? 2 : len * 2;
|
2008-10-20 12:05:44 +04:00
|
|
|
rb_str_resize(newstr, len);
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case econv_finished:
|
|
|
|
len = dp - (unsigned char*)RSTRING_PTR(newstr);
|
|
|
|
rb_str_set_len(newstr, len);
|
|
|
|
rb_enc_associate(newstr, to);
|
|
|
|
return newstr;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* some error, return original */
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-22 20:53:50 +04:00
|
|
|
VALUE
|
|
|
|
rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
|
|
|
|
{
|
|
|
|
return rb_str_conv_enc_opts(str, from, to, 0, Qnil);
|
|
|
|
}
|
|
|
|
|
2008-10-18 14:36:20 +04:00
|
|
|
VALUE
|
|
|
|
rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc)
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
str = rb_tainted_str_new(ptr, len);
|
2009-02-14 01:31:46 +03:00
|
|
|
if (eenc == rb_usascii_encoding() &&
|
|
|
|
rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
|
2008-12-23 18:13:18 +03:00
|
|
|
rb_enc_associate(str, rb_ascii8bit_encoding());
|
|
|
|
return str;
|
|
|
|
}
|
2008-10-18 14:36:20 +04:00
|
|
|
rb_enc_associate(str, eenc);
|
2008-10-21 13:18:34 +04:00
|
|
|
return rb_str_conv_enc(str, eenc, rb_default_internal_encoding());
|
2008-10-18 14:36:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_external_str_new(const char *ptr, long len)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, len, rb_default_external_encoding());
|
|
|
|
}
|
|
|
|
|
2008-10-22 09:55:22 +04:00
|
|
|
VALUE
|
|
|
|
rb_external_str_new_cstr(const char *ptr)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_default_external_encoding());
|
|
|
|
}
|
|
|
|
|
2008-10-20 11:15:19 +04:00
|
|
|
VALUE
|
|
|
|
rb_locale_str_new(const char *ptr, long len)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, len, rb_locale_encoding());
|
|
|
|
}
|
|
|
|
|
2008-10-22 09:55:22 +04:00
|
|
|
VALUE
|
|
|
|
rb_locale_str_new_cstr(const char *ptr)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_locale_encoding());
|
|
|
|
}
|
|
|
|
|
2009-09-28 23:24:18 +04:00
|
|
|
VALUE
|
|
|
|
rb_filesystem_str_new(const char *ptr, long len)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, len, rb_filesystem_encoding());
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_filesystem_str_new_cstr(const char *ptr)
|
|
|
|
{
|
|
|
|
return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_filesystem_encoding());
|
|
|
|
}
|
|
|
|
|
2008-10-20 12:05:44 +04:00
|
|
|
VALUE
|
|
|
|
rb_str_export(VALUE str)
|
|
|
|
{
|
2008-10-21 13:18:34 +04:00
|
|
|
return rb_str_conv_enc(str, STR_ENC_GET(str), rb_default_external_encoding());
|
2008-10-20 12:05:44 +04:00
|
|
|
}
|
|
|
|
|
2008-11-08 00:42:24 +03:00
|
|
|
VALUE
|
|
|
|
rb_str_export_locale(VALUE str)
|
|
|
|
{
|
|
|
|
return rb_str_conv_enc(str, STR_ENC_GET(str), rb_locale_encoding());
|
|
|
|
}
|
|
|
|
|
2008-10-20 12:05:44 +04:00
|
|
|
VALUE
|
|
|
|
rb_str_export_to_enc(VALUE str, rb_encoding *enc)
|
|
|
|
{
|
2008-10-21 13:18:34 +04:00
|
|
|
return rb_str_conv_enc(str, STR_ENC_GET(str), enc);
|
2008-10-20 12:05:44 +04:00
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
static VALUE
|
2008-02-07 10:33:50 +03:00
|
|
|
str_replace_shared(VALUE str2, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2006-09-22 02:50:32 +04:00
|
|
|
if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) {
|
2007-12-23 21:58:09 +03:00
|
|
|
STR_SET_EMBED(str2);
|
2006-09-22 02:50:32 +04:00
|
|
|
memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), RSTRING_LEN(str)+1);
|
|
|
|
STR_SET_EMBED_LEN(str2, RSTRING_LEN(str));
|
|
|
|
}
|
|
|
|
else {
|
2009-06-11 21:41:27 +04:00
|
|
|
str = rb_str_new_frozen(str);
|
2010-05-29 18:32:18 +04:00
|
|
|
FL_SET(str2, STR_NOEMBED);
|
2006-09-22 02:50:32 +04:00
|
|
|
RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
|
|
|
|
RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
|
|
|
|
RSTRING(str2)->as.heap.aux.shared = str;
|
2007-08-18 09:11:49 +04:00
|
|
|
FL_SET(str2, ELTS_SHARED);
|
2006-09-22 02:50:32 +04:00
|
|
|
}
|
2008-02-21 05:42:51 +03:00
|
|
|
rb_enc_cr_str_exact_copy(str2, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-10-03 11:19:19 +04:00
|
|
|
return str2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-02-07 10:33:50 +03:00
|
|
|
static VALUE
|
|
|
|
str_new_shared(VALUE klass, VALUE str)
|
|
|
|
{
|
|
|
|
return str_replace_shared(str_alloc(klass), str);
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:27:10 +04:00
|
|
|
static VALUE
|
|
|
|
str_new3(VALUE klass, VALUE str)
|
|
|
|
{
|
2008-02-20 13:20:43 +03:00
|
|
|
return str_new_shared(klass, str);
|
2007-09-28 23:27:10 +04:00
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_new_shared(VALUE str)
|
2001-12-10 10:18:16 +03:00
|
|
|
{
|
2006-10-22 11:48:53 +04:00
|
|
|
VALUE str2 = str_new3(rb_obj_class(str), str);
|
|
|
|
|
|
|
|
OBJ_INFECT(str2, str);
|
|
|
|
return str2;
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_new3(VALUE str), rb_str_new_shared, (str))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_str_new3 rb_str_new_shared
|
|
|
|
|
2002-12-17 21:20:46 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
str_new4(VALUE klass, VALUE str)
|
2002-12-17 21:20:46 +03:00
|
|
|
{
|
2006-08-31 14:47:44 +04:00
|
|
|
VALUE str2;
|
2002-12-17 21:20:46 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
str2 = str_alloc(klass);
|
|
|
|
STR_SET_NOEMBED(str2);
|
|
|
|
RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
|
|
|
|
RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
|
|
|
|
if (STR_SHARED_P(str)) {
|
2009-06-11 21:41:27 +04:00
|
|
|
VALUE shared = RSTRING(str)->as.heap.aux.shared;
|
|
|
|
assert(OBJ_FROZEN(shared));
|
2003-08-05 13:27:21 +04:00
|
|
|
FL_SET(str2, ELTS_SHARED);
|
2009-06-11 21:41:27 +04:00
|
|
|
RSTRING(str2)->as.heap.aux.shared = shared;
|
2003-08-05 13:27:21 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
FL_SET(str, ELTS_SHARED);
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.aux.shared = str2;
|
2003-08-05 13:27:21 +04:00
|
|
|
}
|
2008-02-21 05:42:51 +03:00
|
|
|
rb_enc_cr_str_exact_copy(str2, str);
|
2006-08-31 14:47:44 +04:00
|
|
|
OBJ_INFECT(str2, str);
|
2002-12-17 21:20:46 +03:00
|
|
|
return str2;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_new_frozen(VALUE orig)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
VALUE klass, str;
|
2000-12-25 09:29:27 +03:00
|
|
|
|
2003-07-03 15:02:53 +04:00
|
|
|
if (OBJ_FROZEN(orig)) return orig;
|
2001-08-20 08:29:58 +04:00
|
|
|
klass = rb_obj_class(orig);
|
2008-02-07 10:33:50 +03:00
|
|
|
if (STR_SHARED_P(orig) && (str = RSTRING(orig)->as.heap.aux.shared)) {
|
2002-12-17 21:20:46 +03:00
|
|
|
long ofs;
|
2009-06-11 21:41:27 +04:00
|
|
|
assert(OBJ_FROZEN(str));
|
2006-08-31 14:47:44 +04:00
|
|
|
ofs = RSTRING_LEN(str) - RSTRING_LEN(orig);
|
2008-02-07 10:33:50 +03:00
|
|
|
if ((ofs > 0) || (klass != RBASIC(str)->klass) ||
|
2012-07-12 06:44:27 +04:00
|
|
|
((RBASIC(str)->flags ^ RBASIC(orig)->flags) & (FL_TAINT|FL_UNTRUSTED)) ||
|
2009-08-12 08:00:03 +04:00
|
|
|
ENCODING_GET(str) != ENCODING_GET(orig)) {
|
2003-07-03 15:02:53 +04:00
|
|
|
str = str_new3(klass, str);
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.ptr += ofs;
|
|
|
|
RSTRING(str)->as.heap.len -= ofs;
|
2009-08-12 08:00:03 +04:00
|
|
|
rb_enc_cr_str_exact_copy(str, orig);
|
|
|
|
OBJ_INFECT(str, orig);
|
2003-07-03 15:02:53 +04:00
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
2008-02-07 10:33:50 +03:00
|
|
|
else if (STR_EMBED_P(orig)) {
|
2006-08-31 14:47:44 +04:00
|
|
|
str = str_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig));
|
2008-02-21 05:42:51 +03:00
|
|
|
rb_enc_cr_str_exact_copy(str, orig);
|
2008-02-07 10:33:50 +03:00
|
|
|
OBJ_INFECT(str, orig);
|
|
|
|
}
|
|
|
|
else if (STR_ASSOC_P(orig)) {
|
|
|
|
VALUE assoc = RSTRING(orig)->as.heap.aux.shared;
|
|
|
|
FL_UNSET(orig, STR_ASSOC);
|
|
|
|
str = str_new4(klass, orig);
|
|
|
|
FL_SET(str, STR_ASSOC);
|
|
|
|
RSTRING(str)->as.heap.aux.shared = assoc;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2002-12-17 21:20:46 +03:00
|
|
|
str = str_new4(klass, orig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
OBJ_FREEZE(str);
|
|
|
|
return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_new4(VALUE orig), rb_str_new_frozen, (orig))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_str_new4 rb_str_new_frozen
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_new_with_class(VALUE obj, const char *ptr, long len)
|
2001-10-02 08:31:23 +04:00
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
return str_new(rb_obj_class(obj), ptr, len);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_new5(VALUE obj, const char *ptr, long len),
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_new_with_class, (obj, ptr, len))
|
|
|
|
#define rb_str_new5 rb_str_new_with_class
|
|
|
|
|
2010-02-23 18:41:34 +03:00
|
|
|
static VALUE
|
|
|
|
str_new_empty(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE v = rb_str_new5(str, 0, 0);
|
2012-03-27 08:13:54 +04:00
|
|
|
rb_enc_copy(v, str);
|
2010-02-23 18:41:34 +03:00
|
|
|
OBJ_INFECT(v, str);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2001-05-30 13:12:34 +04:00
|
|
|
#define STR_BUF_MIN_SIZE 128
|
|
|
|
|
|
|
|
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 14:44:21 +04:00
|
|
|
rb_str_buf_new(long capa)
|
2001-05-30 13:12:34 +04:00
|
|
|
{
|
2002-12-20 11:33:17 +03:00
|
|
|
VALUE str = str_alloc(rb_cString);
|
2001-05-30 13:12:34 +04:00
|
|
|
|
2002-07-11 12:22:18 +04:00
|
|
|
if (capa < STR_BUF_MIN_SIZE) {
|
2001-05-30 13:12:34 +04:00
|
|
|
capa = STR_BUF_MIN_SIZE;
|
2002-07-11 12:22:18 +04:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
FL_SET(str, STR_NOEMBED);
|
|
|
|
RSTRING(str)->as.heap.aux.capa = capa;
|
|
|
|
RSTRING(str)->as.heap.ptr = ALLOC_N(char, capa+1);
|
|
|
|
RSTRING(str)->as.heap.ptr[0] = '\0';
|
2001-05-30 13:12:34 +04:00
|
|
|
|
2001-10-03 11:19:19 +04:00
|
|
|
return str;
|
2001-05-30 13:12:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2008-08-12 13:56:55 +04:00
|
|
|
rb_str_buf_new_cstr(const char *ptr)
|
2001-05-30 13:12:34 +04:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
long len = strlen(ptr);
|
|
|
|
|
2002-07-11 12:22:18 +04:00
|
|
|
str = rb_str_buf_new(len);
|
|
|
|
rb_str_buf_cat(str, ptr, len);
|
2001-05-30 13:12:34 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_buf_new2(const char *ptr), rb_str_buf_new_cstr, (ptr))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_str_buf_new2 rb_str_buf_new_cstr
|
|
|
|
|
2007-12-10 11:46:06 +03:00
|
|
|
VALUE
|
|
|
|
rb_str_tmp_new(long len)
|
|
|
|
{
|
|
|
|
return str_new(0, 0, len);
|
|
|
|
}
|
|
|
|
|
2011-01-27 02:32:22 +03:00
|
|
|
void *
|
|
|
|
rb_alloc_tmp_buffer(volatile VALUE *store, long len)
|
|
|
|
{
|
|
|
|
VALUE s = rb_str_tmp_new(len);
|
|
|
|
*store = s;
|
|
|
|
return RSTRING_PTR(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_free_tmp_buffer(volatile VALUE *store)
|
|
|
|
{
|
|
|
|
VALUE s = *store;
|
|
|
|
*store = 0;
|
|
|
|
if (s) rb_str_clear(s);
|
|
|
|
}
|
|
|
|
|
2006-09-27 02:46:16 +04:00
|
|
|
void
|
|
|
|
rb_str_free(VALUE str)
|
|
|
|
{
|
|
|
|
if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
|
|
|
|
xfree(RSTRING(str)->as.heap.ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-14 10:33:06 +04:00
|
|
|
RUBY_FUNC_EXPORTED size_t
|
2009-06-17 02:36:27 +04:00
|
|
|
rb_str_memsize(VALUE str)
|
|
|
|
{
|
|
|
|
if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
|
|
|
|
return RSTRING(str)->as.heap.aux.capa;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03: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 14:44:21 +04:00
|
|
|
rb_str_to_str(VALUE str)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
return rb_convert_type(str, T_STRING, "String", "to_str");
|
|
|
|
}
|
|
|
|
|
2009-02-17 06:50:59 +03:00
|
|
|
static inline void str_discard(VALUE str);
|
|
|
|
|
2007-12-10 11:46:06 +03:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
rb_str_shared_replace(VALUE str, VALUE str2)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2008-01-06 12:25:09 +03:00
|
|
|
rb_encoding *enc;
|
|
|
|
int cr;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (str == str2) return;
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str2);
|
2008-01-06 12:25:09 +03:00
|
|
|
cr = ENC_CODERANGE(str2);
|
2009-02-17 06:50:59 +03:00
|
|
|
str_discard(str);
|
2008-08-03 05:41:56 +04:00
|
|
|
OBJ_INFECT(str, str2);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str2) <= RSTRING_EMBED_LEN_MAX) {
|
2006-09-02 18:42:08 +04:00
|
|
|
STR_SET_EMBED(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), RSTRING_LEN(str2)+1);
|
|
|
|
STR_SET_EMBED_LEN(str, RSTRING_LEN(str2));
|
2008-01-06 12:25:09 +03:00
|
|
|
rb_enc_associate(str, enc);
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
2006-08-31 14:47:44 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-12-13 14:52:15 +03:00
|
|
|
STR_SET_NOEMBED(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_UNSET_NOCAPA(str);
|
|
|
|
RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
|
|
|
|
RSTRING(str)->as.heap.len = RSTRING_LEN(str2);
|
|
|
|
if (STR_NOCAPA_P(str2)) {
|
2004-10-27 13:29:26 +04:00
|
|
|
FL_SET(str, RBASIC(str2)->flags & STR_NOCAPA);
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.aux.shared = RSTRING(str2)->as.heap.aux.shared;
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
|
|
|
else {
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.aux.capa = RSTRING(str2)->as.heap.aux.capa;
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
2008-08-28 18:40:53 +04:00
|
|
|
STR_SET_EMBED(str2); /* abandon str2 */
|
|
|
|
RSTRING_PTR(str2)[0] = 0;
|
|
|
|
STR_SET_EMBED_LEN(str2, 0);
|
2008-01-06 12:25:09 +03:00
|
|
|
rb_enc_associate(str, enc);
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-03-26 12:57:16 +04:00
|
|
|
static ID id_to_s;
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_obj_as_string(VALUE obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(obj, T_STRING)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
return obj;
|
|
|
|
}
|
2001-03-26 12:57:16 +04:00
|
|
|
str = rb_funcall(obj, id_to_s, 0);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(str, T_STRING))
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_any_to_s(obj);
|
|
|
|
if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-05-25 04:43:23 +04:00
|
|
|
static VALUE
|
|
|
|
str_replace(VALUE str, VALUE str2)
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
|
|
|
|
len = RSTRING_LEN(str2);
|
|
|
|
if (STR_ASSOC_P(str2)) {
|
|
|
|
str2 = rb_str_new4(str2);
|
|
|
|
}
|
|
|
|
if (STR_SHARED_P(str2)) {
|
2009-06-11 21:41:27 +04:00
|
|
|
VALUE shared = RSTRING(str2)->as.heap.aux.shared;
|
|
|
|
assert(OBJ_FROZEN(shared));
|
2009-05-25 04:43:23 +04:00
|
|
|
STR_SET_NOEMBED(str);
|
|
|
|
RSTRING(str)->as.heap.len = len;
|
|
|
|
RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2);
|
|
|
|
FL_SET(str, ELTS_SHARED);
|
|
|
|
FL_UNSET(str, STR_ASSOC);
|
2009-06-11 21:41:27 +04:00
|
|
|
RSTRING(str)->as.heap.aux.shared = shared;
|
2009-05-25 04:43:23 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
str_replace_shared(str, str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_INFECT(str, str2);
|
|
|
|
rb_enc_cr_str_exact_copy(str, str2);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-02-12 13:42:36 +03:00
|
|
|
static VALUE
|
|
|
|
str_duplicate(VALUE klass, VALUE str)
|
|
|
|
{
|
|
|
|
VALUE dup = str_alloc(klass);
|
2009-05-25 04:43:23 +04:00
|
|
|
str_replace(dup, str);
|
2009-02-12 13:42:36 +03:00
|
|
|
return dup;
|
|
|
|
}
|
2000-03-13 10:18:45 +03:00
|
|
|
|
2001-12-10 10:18:16 +03: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 14:44:21 +04:00
|
|
|
rb_str_dup(VALUE str)
|
2001-12-10 10:18:16 +03:00
|
|
|
{
|
2009-02-12 13:42:36 +03:00
|
|
|
return str_duplicate(rb_obj_class(str), str);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2009-02-18 08:33:36 +03:00
|
|
|
VALUE
|
|
|
|
rb_str_resurrect(VALUE str)
|
|
|
|
{
|
2009-05-25 04:43:23 +04:00
|
|
|
return str_replace(str_alloc(rb_cString), str);
|
2009-02-18 08:33:36 +03:00
|
|
|
}
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* String.new(str="") -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a new string object containing a copy of <i>str</i>.
|
|
|
|
*/
|
|
|
|
|
2001-09-19 10:54:11 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_init(int argc, VALUE *argv, VALUE str)
|
2001-09-19 10:54:11 +04:00
|
|
|
{
|
|
|
|
VALUE orig;
|
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
if (argc > 0 && rb_scan_args(argc, argv, "01", &orig) == 1)
|
2001-09-19 10:54:11 +04:00
|
|
|
rb_str_replace(str, orig);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-05-20 08:44:36 +04:00
|
|
|
static inline long
|
|
|
|
enc_strlen(const char *p, const char *e, rb_encoding *enc, int cr)
|
2008-02-16 05:55:08 +03:00
|
|
|
{
|
|
|
|
long c;
|
|
|
|
const char *q;
|
|
|
|
|
|
|
|
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
2008-02-22 09:52:54 +03:00
|
|
|
return (e - p + rb_enc_mbminlen(enc) - 1) / rb_enc_mbminlen(enc);
|
2008-02-16 05:55:08 +03:00
|
|
|
}
|
|
|
|
else if (rb_enc_asciicompat(enc)) {
|
|
|
|
c = 0;
|
2009-05-20 08:44:36 +04:00
|
|
|
if (cr == ENC_CODERANGE_7BIT || cr == ENC_CODERANGE_VALID) {
|
|
|
|
while (p < e) {
|
|
|
|
if (ISASCII(*p)) {
|
|
|
|
q = search_nonascii(p, e);
|
|
|
|
if (!q)
|
|
|
|
return c + (e - p);
|
|
|
|
c += q - p;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
p += rb_enc_fast_mbclen(p, e, enc);
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (p < e) {
|
|
|
|
if (ISASCII(*p)) {
|
|
|
|
q = search_nonascii(p, e);
|
|
|
|
if (!q)
|
|
|
|
return c + (e - p);
|
|
|
|
c += q - p;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
p += rb_enc_mbclen(p, e, enc);
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
2008-02-16 05:55:08 +03:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c=0; p<e; c++) {
|
|
|
|
p += rb_enc_mbclen(p, e, enc);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-05-20 08:44:36 +04:00
|
|
|
long
|
|
|
|
rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return enc_strlen(p, e, enc, ENC_CODERANGE_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2008-02-19 15:18:03 +03:00
|
|
|
long
|
|
|
|
rb_enc_strlen_cr(const char *p, const char *e, rb_encoding *enc, int *cr)
|
|
|
|
{
|
|
|
|
long c;
|
|
|
|
const char *q;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*cr = 0;
|
|
|
|
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
2008-02-22 09:52:54 +03:00
|
|
|
return (e - p + rb_enc_mbminlen(enc) - 1) / rb_enc_mbminlen(enc);
|
2008-02-19 15:18:03 +03:00
|
|
|
}
|
|
|
|
else if (rb_enc_asciicompat(enc)) {
|
|
|
|
c = 0;
|
|
|
|
while (p < e) {
|
|
|
|
if (ISASCII(*p)) {
|
|
|
|
q = search_nonascii(p, e);
|
|
|
|
if (!q) {
|
2008-05-20 11:59:33 +04:00
|
|
|
if (!*cr) *cr = ENC_CODERANGE_7BIT;
|
2008-02-19 15:18:03 +03:00
|
|
|
return c + (e - p);
|
|
|
|
}
|
|
|
|
c += q - p;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
ret = rb_enc_precise_mbclen(p, e, enc);
|
|
|
|
if (MBCLEN_CHARFOUND_P(ret)) {
|
|
|
|
*cr |= ENC_CODERANGE_VALID;
|
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*cr = ENC_CODERANGE_BROKEN;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
if (!*cr) *cr = ENC_CODERANGE_7BIT;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c=0; p<e; c++) {
|
|
|
|
ret = rb_enc_precise_mbclen(p, e, enc);
|
|
|
|
if (MBCLEN_CHARFOUND_P(ret)) {
|
|
|
|
*cr |= ENC_CODERANGE_VALID;
|
|
|
|
p += MBCLEN_CHARFOUND_LEN(ret);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*cr = ENC_CODERANGE_BROKEN;
|
2010-01-24 07:59:31 +03:00
|
|
|
if (p + rb_enc_mbminlen(enc) <= e)
|
|
|
|
p += rb_enc_mbminlen(enc);
|
|
|
|
else
|
|
|
|
p = e;
|
2008-02-19 15:18:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*cr) *cr = ENC_CODERANGE_7BIT;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-09-18 16:53:25 +04:00
|
|
|
#ifdef NONASCII_MASK
|
|
|
|
#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80)
|
2011-02-03 19:54:52 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* UTF-8 leading bytes have either 0xxxxxxx or 11xxxxxx
|
|
|
|
* bit represention. (see http://en.wikipedia.org/wiki/UTF-8)
|
|
|
|
* Therefore, following pseudo code can detect UTF-8 leading byte.
|
|
|
|
*
|
|
|
|
* if (!(byte & 0x80))
|
|
|
|
* byte |= 0x40; // turn on bit6
|
|
|
|
* return ((byte>>6) & 1); // bit6 represent it's leading byte or not.
|
|
|
|
*
|
|
|
|
* This function calculate every bytes in the argument word `s'
|
|
|
|
* using the above logic concurrently. and gather every bytes result.
|
|
|
|
*/
|
2008-09-18 16:53:25 +04:00
|
|
|
static inline VALUE
|
|
|
|
count_utf8_lead_bytes_with_word(const VALUE *s)
|
|
|
|
{
|
|
|
|
VALUE d = *s;
|
2011-02-03 19:54:52 +03:00
|
|
|
|
|
|
|
/* Transform into bit0 represent UTF-8 leading or not. */
|
2008-09-18 16:53:25 +04:00
|
|
|
d |= ~(d>>1);
|
|
|
|
d >>= 6;
|
|
|
|
d &= NONASCII_MASK >> 7;
|
2011-02-03 19:54:52 +03:00
|
|
|
|
|
|
|
/* Gather every bytes. */
|
2008-09-18 16:53:25 +04:00
|
|
|
d += (d>>8);
|
|
|
|
d += (d>>16);
|
|
|
|
#if SIZEOF_VALUE == 8
|
|
|
|
d += (d>>32);
|
|
|
|
#endif
|
|
|
|
return (d&0xF);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-28 10:45:32 +04:00
|
|
|
static long
|
* 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-25 07:29:39 +04:00
|
|
|
str_strlen(VALUE str, rb_encoding *enc)
|
|
|
|
{
|
2008-02-15 11:14:40 +03:00
|
|
|
const char *p, *e;
|
2009-05-07 09:16:26 +04:00
|
|
|
long n;
|
|
|
|
int cr;
|
* 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-25 07:29:39 +04:00
|
|
|
|
2007-12-31 13:39:20 +03:00
|
|
|
if (single_byte_optimizable(str)) return RSTRING_LEN(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
if (!enc) enc = STR_ENC_GET(str);
|
2008-02-15 11:14:40 +03:00
|
|
|
p = RSTRING_PTR(str);
|
|
|
|
e = RSTRING_END(str);
|
2009-05-20 08:44:36 +04:00
|
|
|
cr = ENC_CODERANGE(str);
|
2008-09-18 16:53:25 +04:00
|
|
|
#ifdef NONASCII_MASK
|
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
|
|
|
|
enc == rb_utf8_encoding()) {
|
2009-05-20 08:44:36 +04:00
|
|
|
|
|
|
|
VALUE len = 0;
|
2009-05-07 09:16:26 +04:00
|
|
|
if ((int)sizeof(VALUE) * 2 < e - p) {
|
2008-09-18 16:53:25 +04:00
|
|
|
const VALUE *s, *t;
|
|
|
|
const VALUE lowbits = sizeof(VALUE) - 1;
|
|
|
|
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
|
|
|
|
t = (const VALUE*)(~lowbits & (VALUE)e);
|
|
|
|
while (p < (const char *)s) {
|
|
|
|
if (is_utf8_lead_byte(*p)) len++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
while (s < t) {
|
|
|
|
len += count_utf8_lead_bytes_with_word(s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
p = (const char *)s;
|
|
|
|
}
|
|
|
|
while (p < e) {
|
|
|
|
if (is_utf8_lead_byte(*p)) len++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return (long)len;
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-19 15:18:03 +03:00
|
|
|
n = rb_enc_strlen_cr(p, e, enc, &cr);
|
|
|
|
if (cr) {
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
|
|
|
}
|
|
|
|
return n;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
|
2010-01-11 10:58:26 +03:00
|
|
|
long
|
|
|
|
rb_str_strlen(VALUE str)
|
|
|
|
{
|
|
|
|
return str_strlen(str, STR_ENC_GET(str));
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.length -> integer
|
|
|
|
* str.size -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* Returns the character length of <i>str</i>.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2007-08-25 12:54:29 +04: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 14:44:21 +04:00
|
|
|
rb_str_length(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-05-07 09:16:26 +04:00
|
|
|
long len;
|
* 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-25 07:29:39 +04:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
len = str_strlen(str, STR_ENC_GET(str));
|
2009-05-07 09:16:26 +04:00
|
|
|
return LONG2NUM(len);
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.bytesize -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-06 00:39:15 +04:00
|
|
|
* Returns the length of +str+ in bytes.
|
|
|
|
*
|
|
|
|
* "\x80\u3042".bytesize #=> 4
|
|
|
|
* "hello".bytesize #=> 5
|
* 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-25 07:29:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-12-22 11:38:53 +03:00
|
|
|
rb_str_bytesize(VALUE 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-25 07:29:39 +04:00
|
|
|
{
|
2011-05-27 21:23:30 +04:00
|
|
|
return LONG2NUM(RSTRING_LEN(str));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.empty? -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns <code>true</code> if <i>str</i> has a length of zero.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".empty? #=> false
|
2012-07-06 00:39:15 +04:00
|
|
|
* " ".empty? #=> false
|
2003-12-25 00:14:46 +03:00
|
|
|
* "".empty? #=> true
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_empty(VALUE str)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) == 0)
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str + other_str -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Concatenation---Returns a new <code>String</code> containing
|
|
|
|
* <i>other_str</i> concatenated to <i>str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "Hello from " + self.to_s #=> "Hello from main"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_str_plus(VALUE str1, VALUE str2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str3;
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str2);
|
* 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-25 07:29:39 +04:00
|
|
|
enc = rb_enc_check(str1, str2);
|
2006-08-31 14:47:44 +04:00
|
|
|
str3 = rb_str_new(0, RSTRING_LEN(str1)+RSTRING_LEN(str2));
|
|
|
|
memcpy(RSTRING_PTR(str3), RSTRING_PTR(str1), RSTRING_LEN(str1));
|
|
|
|
memcpy(RSTRING_PTR(str3) + RSTRING_LEN(str1),
|
|
|
|
RSTRING_PTR(str2), RSTRING_LEN(str2));
|
|
|
|
RSTRING_PTR(str3)[RSTRING_LEN(str3)] = '\0';
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (OBJ_TAINTED(str1) || OBJ_TAINTED(str2))
|
|
|
|
OBJ_TAINT(str3);
|
2008-02-17 09:49:11 +03:00
|
|
|
ENCODING_CODERANGE_SET(str3, rb_enc_to_index(enc),
|
|
|
|
ENC_CODERANGE_AND(ENC_CODERANGE(str1), ENC_CODERANGE(str2)));
|
1999-01-20 07:59:39 +03:00
|
|
|
return str3;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str * integer -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-06 00:39:15 +04:00
|
|
|
* Copy --- Returns a new String containing +integer+ copies of the receiver.
|
|
|
|
* +integer+ must be greater than or equal to 0.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "Ho! " * 3 #=> "Ho! Ho! Ho! "
|
2012-07-06 00:39:15 +04:00
|
|
|
* "Ho! " * 0 #=> ""
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_str_times(VALUE str, VALUE times)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str2;
|
2008-02-17 03:18:16 +03:00
|
|
|
long n, len;
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
char *ptr2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
len = NUM2LONG(times);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (len < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "negative argument");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len && LONG_MAX/len < RSTRING_LEN(str)) {
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_raise(rb_eArgError, "argument too big");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-09-20 20:41:56 +04:00
|
|
|
str2 = rb_str_new5(str, 0, len *= RSTRING_LEN(str));
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
ptr2 = RSTRING_PTR(str2);
|
2008-02-17 03:18:16 +03:00
|
|
|
if (len) {
|
|
|
|
n = RSTRING_LEN(str);
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
memcpy(ptr2, RSTRING_PTR(str), n);
|
2008-02-17 03:18:16 +03:00
|
|
|
while (n <= len/2) {
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
memcpy(ptr2 + n, ptr2, n);
|
2008-02-17 03:18:16 +03:00
|
|
|
n *= 2;
|
|
|
|
}
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
memcpy(ptr2 + n, ptr2, len-n);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
ptr2[RSTRING_LEN(str2)] = '\0';
|
2001-10-02 08:31:23 +04:00
|
|
|
OBJ_INFECT(str2, str);
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(str2, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return str2;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str % arg -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Format---Uses <i>str</i> as a format specification, and returns the result
|
|
|
|
* of applying it to <i>arg</i>. If the format specification contains more than
|
2010-09-15 02:27:54 +04:00
|
|
|
* one substitution, then <i>arg</i> must be an <code>Array</code> or <code>Hash</code>
|
|
|
|
* containing the values to be substituted. See <code>Kernel::sprintf</code> for
|
|
|
|
* details of the format string.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-03-09 04:04:46 +03:00
|
|
|
* "%05d" % 123 #=> "00123"
|
|
|
|
* "%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
|
2010-09-15 02:27:54 +04:00
|
|
|
* "foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_format_m(VALUE str, VALUE arg)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2008-06-29 18:03:38 +04:00
|
|
|
volatile VALUE tmp = rb_check_array_type(arg);
|
* 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-25 07:29:39 +04:00
|
|
|
|
|
|
|
if (!NIL_P(tmp)) {
|
2009-09-18 19:17:17 +04:00
|
|
|
return rb_str_format(RARRAY_LENINT(tmp), RARRAY_PTR(tmp), str);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2005-07-23 05:02:18 +04:00
|
|
|
return rb_str_format(1, &arg, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
static inline void
|
2007-09-26 23:46:58 +04:00
|
|
|
str_modifiable(VALUE str)
|
2001-12-18 11:47:06 +03:00
|
|
|
{
|
2004-10-31 20:37:52 +03:00
|
|
|
if (FL_TEST(str, STR_TMPLOCK)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "can't modify string; temporarily locked");
|
|
|
|
}
|
* array.c, gc.c, hash.c, object.c, string.c, struct.c,
transcode.c, variable.c, vm.c, vm_insnhelper.c, vm_method.c:
replace calls to rb_error_frozen() with rb_check_frozen(). a
patch from Run Paint Run Run at [ruby-core:32014]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-24 12:14:05 +04:00
|
|
|
rb_check_frozen(str);
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 11:25:05 +04:00
|
|
|
if (!OBJ_UNTRUSTED(str) && rb_safe_level() >= 4)
|
2001-12-18 11:47:06 +03:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify string");
|
2007-09-26 23:46:58 +04:00
|
|
|
}
|
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
static inline int
|
2007-09-26 23:46:58 +04:00
|
|
|
str_independent(VALUE str)
|
|
|
|
{
|
|
|
|
str_modifiable(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (!STR_SHARED_P(str)) return 1;
|
|
|
|
if (STR_EMBED_P(str)) return 1;
|
2001-12-18 11:47:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-01 18:04:39 +04:00
|
|
|
str_make_independent_expand(VALUE str, long expand)
|
2001-12-18 11:47:06 +03:00
|
|
|
{
|
|
|
|
char *ptr;
|
2006-08-31 14:47:44 +04:00
|
|
|
long len = RSTRING_LEN(str);
|
2010-08-17 02:42:46 +04:00
|
|
|
long capa = len + expand;
|
2001-12-18 11:47:06 +03:00
|
|
|
|
2010-08-17 02:42:46 +04:00
|
|
|
if (len > capa) len = capa;
|
|
|
|
ptr = ALLOC_N(char, capa + 1);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_PTR(str)) {
|
2010-08-17 02:42:46 +04:00
|
|
|
memcpy(ptr, RSTRING_PTR(str), len);
|
2001-12-18 11:47:06 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_NOEMBED(str);
|
2010-08-17 02:42:46 +04:00
|
|
|
STR_UNSET_NOCAPA(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
ptr[len] = 0;
|
|
|
|
RSTRING(str)->as.heap.ptr = ptr;
|
2008-01-23 09:04:13 +03:00
|
|
|
RSTRING(str)->as.heap.len = len;
|
2010-08-17 02:42:46 +04:00
|
|
|
RSTRING(str)->as.heap.aux.capa = capa;
|
2001-12-18 11:47:06 +03:00
|
|
|
}
|
|
|
|
|
2011-01-06 18:38:14 +03:00
|
|
|
#define str_make_independent(str) str_make_independent_expand((str), 0L)
|
2010-08-01 18:04:39 +04:00
|
|
|
|
2001-12-18 11:47:06 +03:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
rb_str_modify(VALUE str)
|
2001-12-18 11:47:06 +03:00
|
|
|
{
|
2002-04-18 12:46:18 +04:00
|
|
|
if (!str_independent(str))
|
|
|
|
str_make_independent(str);
|
2007-09-26 23:46:58 +04:00
|
|
|
ENC_CODERANGE_CLEAR(str);
|
2001-12-18 11:47:06 +03:00
|
|
|
}
|
|
|
|
|
2010-08-01 18:04:39 +04:00
|
|
|
void
|
|
|
|
rb_str_modify_expand(VALUE str, long expand)
|
|
|
|
{
|
|
|
|
if (expand < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative expanding string size");
|
|
|
|
}
|
2012-02-08 17:30:04 +04:00
|
|
|
if (!str_independent(str)) {
|
2010-08-01 18:04:39 +04:00
|
|
|
str_make_independent_expand(str, expand);
|
|
|
|
}
|
2012-02-08 17:30:04 +04:00
|
|
|
else if (expand > 0) {
|
|
|
|
long len = RSTRING_LEN(str);
|
|
|
|
long capa = len + expand;
|
|
|
|
if (!STR_EMBED_P(str)) {
|
|
|
|
REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1);
|
|
|
|
RSTRING(str)->as.heap.aux.capa = capa;
|
|
|
|
}
|
|
|
|
else if (capa > RSTRING_EMBED_LEN_MAX) {
|
|
|
|
str_make_independent_expand(str, expand);
|
|
|
|
}
|
|
|
|
}
|
2010-08-01 18:04:39 +04:00
|
|
|
ENC_CODERANGE_CLEAR(str);
|
|
|
|
}
|
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
/* As rb_str_modify(), but don't clear coderange */
|
|
|
|
static void
|
|
|
|
str_modify_keep_cr(VALUE str)
|
|
|
|
{
|
|
|
|
if (!str_independent(str))
|
|
|
|
str_make_independent(str);
|
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_BROKEN)
|
|
|
|
/* Force re-scan later */
|
|
|
|
ENC_CODERANGE_CLEAR(str);
|
|
|
|
}
|
|
|
|
|
2009-02-17 06:50:59 +03:00
|
|
|
static inline void
|
|
|
|
str_discard(VALUE str)
|
|
|
|
{
|
|
|
|
str_modifiable(str);
|
|
|
|
if (!STR_SHARED_P(str) && !STR_EMBED_P(str)) {
|
|
|
|
xfree(RSTRING_PTR(str));
|
|
|
|
RSTRING(str)->as.heap.ptr = 0;
|
|
|
|
RSTRING(str)->as.heap.len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
rb_str_associate(VALUE str, VALUE add)
|
2002-09-12 10:27:15 +04:00
|
|
|
{
|
2008-02-07 10:33:50 +03:00
|
|
|
/* sanity check */
|
* array.c, gc.c, hash.c, object.c, string.c, struct.c,
transcode.c, variable.c, vm.c, vm_insnhelper.c, vm_method.c:
replace calls to rb_error_frozen() with rb_check_frozen(). a
patch from Run Paint Run Run at [ruby-core:32014]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-24 12:14:05 +04:00
|
|
|
rb_check_frozen(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_ASSOC_P(str)) {
|
2002-09-12 10:27:15 +04:00
|
|
|
/* already associated */
|
2006-08-31 14:47:44 +04:00
|
|
|
rb_ary_concat(RSTRING(str)->as.heap.aux.shared, add);
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-07 10:33:50 +03:00
|
|
|
if (STR_SHARED_P(str)) {
|
|
|
|
VALUE assoc = RSTRING(str)->as.heap.aux.shared;
|
|
|
|
str_make_independent(str);
|
|
|
|
if (STR_ASSOC_P(assoc)) {
|
|
|
|
assoc = RSTRING(assoc)->as.heap.aux.shared;
|
|
|
|
rb_ary_concat(assoc, add);
|
|
|
|
add = assoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (STR_EMBED_P(str)) {
|
2002-09-12 10:27:15 +04:00
|
|
|
str_make_independent(str);
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
else if (RSTRING(str)->as.heap.aux.capa != RSTRING_LEN(str)) {
|
|
|
|
RESIZE_CAPA(str, RSTRING_LEN(str));
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
|
|
|
FL_SET(str, STR_ASSOC);
|
2008-01-23 09:04:13 +03:00
|
|
|
RBASIC(add)->klass = 0;
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.aux.shared = add;
|
2002-09-12 10:27:15 +04: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 14:44:21 +04:00
|
|
|
rb_str_associated(VALUE str)
|
2002-09-12 10:27:15 +04:00
|
|
|
{
|
2008-02-07 10:33:50 +03:00
|
|
|
if (STR_SHARED_P(str)) str = RSTRING(str)->as.heap.aux.shared;
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_ASSOC_P(str)) {
|
2008-02-07 10:33:50 +03:00
|
|
|
return RSTRING(str)->as.heap.aux.shared;
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2012-03-23 08:19:24 +04:00
|
|
|
void
|
|
|
|
rb_must_asciicompat(VALUE str)
|
|
|
|
{
|
|
|
|
rb_encoding *enc = rb_enc_get(str);
|
|
|
|
if (!rb_enc_asciicompat(enc)) {
|
|
|
|
rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-18 11:47:06 +03: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 14:44:21 +04:00
|
|
|
rb_string_value(volatile VALUE *ptr)
|
2002-08-29 13:08:18 +04:00
|
|
|
{
|
|
|
|
VALUE s = *ptr;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(s, T_STRING)) {
|
2010-01-31 16:44:19 +03:00
|
|
|
s = rb_str_to_str(s);
|
|
|
|
*ptr = s;
|
2002-08-29 13:08:18 +04:00
|
|
|
}
|
2003-06-23 12:41:07 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:44:21 +04:00
|
|
|
rb_string_value_ptr(volatile VALUE *ptr)
|
2003-06-23 12:41:07 +04:00
|
|
|
{
|
2008-12-12 04:19:23 +03:00
|
|
|
VALUE str = rb_string_value(ptr);
|
|
|
|
return RSTRING_PTR(str);
|
2002-08-29 13:08:18 +04:00
|
|
|
}
|
|
|
|
|
2003-07-20 21:17:52 +04:00
|
|
|
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 14:44:21 +04:00
|
|
|
rb_string_value_cstr(volatile VALUE *ptr)
|
2003-07-20 21:17:52 +04:00
|
|
|
{
|
|
|
|
VALUE str = rb_string_value(ptr);
|
2006-08-31 14:47:44 +04:00
|
|
|
char *s = RSTRING_PTR(str);
|
2010-04-14 10:58:13 +04:00
|
|
|
long len = RSTRING_LEN(str);
|
2003-07-20 21:17:52 +04:00
|
|
|
|
2010-04-14 10:58:13 +04:00
|
|
|
if (!s || memchr(s, 0, len)) {
|
2003-07-20 21:17:52 +04:00
|
|
|
rb_raise(rb_eArgError, "string contains null byte");
|
|
|
|
}
|
2011-01-25 01:00:55 +03:00
|
|
|
if (s[len]) {
|
|
|
|
rb_str_modify(str);
|
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
s[RSTRING_LEN(str)] = 0;
|
|
|
|
}
|
2003-07-20 21:17:52 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-12-19 12:20:20 +03: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 14:44:21 +04:00
|
|
|
rb_check_string_type(VALUE str)
|
2002-12-19 12:20:20 +03:00
|
|
|
{
|
2003-06-23 10:52:39 +04:00
|
|
|
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
|
|
|
|
return str;
|
2002-12-19 12:20:20 +03:00
|
|
|
}
|
|
|
|
|
2007-08-24 21:47:09 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* String.try_convert(obj) -> string or nil
|
|
|
|
*
|
|
|
|
* Try to convert <i>obj</i> into a String, using to_str method.
|
2009-10-14 17:41:33 +04:00
|
|
|
* Returns converted string or nil if <i>obj</i> cannot be converted
|
2007-08-24 21:47:09 +04:00
|
|
|
* for any reason.
|
|
|
|
*
|
2010-05-18 01:07:46 +04:00
|
|
|
* String.try_convert("str") #=> "str"
|
2010-05-18 01:07:33 +04:00
|
|
|
* String.try_convert(/re/) #=> nil
|
2007-08-24 21:47:09 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_str_s_try_convert(VALUE dummy, VALUE str)
|
|
|
|
{
|
|
|
|
return rb_check_string_type(str);
|
|
|
|
}
|
|
|
|
|
2011-01-23 02:00:12 +03:00
|
|
|
static char*
|
|
|
|
str_nth_len(const char *p, const char *e, long *nthp, rb_encoding *enc)
|
2008-02-15 21:48:27 +03:00
|
|
|
{
|
2011-01-23 02:00:12 +03:00
|
|
|
long nth = *nthp;
|
2008-02-15 21:48:27 +03:00
|
|
|
if (rb_enc_mbmaxlen(enc) == 1) {
|
|
|
|
p += nth;
|
|
|
|
}
|
|
|
|
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
|
|
|
p += nth * rb_enc_mbmaxlen(enc);
|
|
|
|
}
|
|
|
|
else if (rb_enc_asciicompat(enc)) {
|
|
|
|
const char *p2, *e2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
while (p < e && 0 < nth) {
|
|
|
|
e2 = p + nth;
|
2011-01-23 02:00:12 +03:00
|
|
|
if (e < e2) {
|
|
|
|
*nthp = nth;
|
2008-02-15 21:48:27 +03:00
|
|
|
return (char *)e;
|
2011-01-23 02:00:12 +03:00
|
|
|
}
|
2008-02-15 21:48:27 +03:00
|
|
|
if (ISASCII(*p)) {
|
|
|
|
p2 = search_nonascii(p, e2);
|
2011-01-23 02:00:12 +03:00
|
|
|
if (!p2) {
|
2012-01-04 09:24:39 +04:00
|
|
|
nth -= e2 - p;
|
2011-01-23 02:00:12 +03:00
|
|
|
*nthp = nth;
|
2008-02-15 21:48:27 +03:00
|
|
|
return (char *)e2;
|
2011-01-23 02:00:12 +03:00
|
|
|
}
|
2008-02-15 21:48:27 +03:00
|
|
|
nth -= p2 - p;
|
|
|
|
p = p2;
|
|
|
|
}
|
|
|
|
n = rb_enc_mbclen(p, e, enc);
|
|
|
|
p += n;
|
|
|
|
nth--;
|
|
|
|
}
|
2011-01-23 02:00:12 +03:00
|
|
|
*nthp = nth;
|
|
|
|
if (nth != 0) {
|
2008-02-15 21:48:27 +03:00
|
|
|
return (char *)e;
|
2011-01-23 02:00:12 +03:00
|
|
|
}
|
2008-02-15 21:48:27 +03:00
|
|
|
return (char *)p;
|
|
|
|
}
|
|
|
|
else {
|
2011-01-23 02:00:12 +03:00
|
|
|
while (p < e && nth--) {
|
2008-02-16 12:30:03 +03:00
|
|
|
p += rb_enc_mbclen(p, e, enc);
|
2008-02-15 21:48:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > e) p = e;
|
2011-01-23 02:00:12 +03:00
|
|
|
*nthp = nth;
|
2008-02-15 21:48:27 +03:00
|
|
|
return (char*)p;
|
|
|
|
}
|
|
|
|
|
2011-01-23 02:00:12 +03:00
|
|
|
char*
|
|
|
|
rb_enc_nth(const char *p, const char *e, long nth, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return str_nth_len(p, e, &nth, 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-25 07:29:39 +04:00
|
|
|
static char*
|
2009-05-07 09:16:26 +04:00
|
|
|
str_nth(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
|
* 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-25 07:29:39 +04:00
|
|
|
{
|
2007-12-31 13:39:20 +03:00
|
|
|
if (singlebyte)
|
2007-12-13 21:00:50 +03:00
|
|
|
p += nth;
|
2008-02-15 11:14:40 +03:00
|
|
|
else {
|
2011-01-23 02:00:12 +03:00
|
|
|
p = str_nth_len(p, e, &nth, enc);
|
2008-02-15 11:14:40 +03:00
|
|
|
}
|
2007-12-18 08:44:30 +03:00
|
|
|
if (!p) return 0;
|
2008-01-23 05:53:28 +03:00
|
|
|
if (p > e) p = e;
|
|
|
|
return (char *)p;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:19:21 +03:00
|
|
|
/* char offset to byte offset */
|
2009-05-07 09:16:26 +04:00
|
|
|
static long
|
|
|
|
str_offset(const char *p, const char *e, long nth, rb_encoding *enc, int singlebyte)
|
* 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-25 07:29:39 +04:00
|
|
|
{
|
2007-12-31 13:39:20 +03:00
|
|
|
const char *pp = str_nth(p, e, nth, enc, singlebyte);
|
2007-12-18 08:44:30 +03:00
|
|
|
if (!pp) return e - p;
|
* 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-25 07:29:39 +04:00
|
|
|
return pp - p;
|
|
|
|
}
|
|
|
|
|
2009-06-30 13:06:48 +04:00
|
|
|
long
|
|
|
|
rb_str_offset(VALUE str, long pos)
|
|
|
|
{
|
|
|
|
return str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
|
|
|
|
STR_ENC_GET(str), single_byte_optimizable(str));
|
|
|
|
}
|
|
|
|
|
2008-09-18 16:53:25 +04:00
|
|
|
#ifdef NONASCII_MASK
|
|
|
|
static char *
|
2011-01-23 02:00:12 +03:00
|
|
|
str_utf8_nth(const char *p, const char *e, long *nthp)
|
2008-09-18 16:53:25 +04:00
|
|
|
{
|
2011-01-23 02:00:12 +03:00
|
|
|
long nth = *nthp;
|
2011-02-04 06:14:57 +03:00
|
|
|
if ((int)SIZEOF_VALUE * 2 < e - p && (int)SIZEOF_VALUE * 2 < nth) {
|
2008-09-18 16:53:25 +04:00
|
|
|
const VALUE *s, *t;
|
|
|
|
const VALUE lowbits = sizeof(VALUE) - 1;
|
|
|
|
s = (const VALUE*)(~lowbits & ((VALUE)p + lowbits));
|
|
|
|
t = (const VALUE*)(~lowbits & (VALUE)e);
|
|
|
|
while (p < (const char *)s) {
|
|
|
|
if (is_utf8_lead_byte(*p)) nth--;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
nth -= count_utf8_lead_bytes_with_word(s);
|
|
|
|
s++;
|
2009-05-07 09:16:26 +04:00
|
|
|
} while (s < t && (int)sizeof(VALUE) <= nth);
|
2008-09-18 16:53:25 +04:00
|
|
|
p = (char *)s;
|
|
|
|
}
|
|
|
|
while (p < e) {
|
|
|
|
if (is_utf8_lead_byte(*p)) {
|
|
|
|
if (nth == 0) break;
|
|
|
|
nth--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2011-01-23 02:00:12 +03:00
|
|
|
*nthp = nth;
|
2008-09-18 16:53:25 +04:00
|
|
|
return (char *)p;
|
|
|
|
}
|
|
|
|
|
2009-05-07 09:16:26 +04:00
|
|
|
static long
|
|
|
|
str_utf8_offset(const char *p, const char *e, long nth)
|
2008-09-18 16:53:25 +04:00
|
|
|
{
|
2011-01-23 02:00:12 +03:00
|
|
|
const char *pp = str_utf8_nth(p, e, &nth);
|
2008-09-18 16:53:25 +04:00
|
|
|
return pp - p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-29 09:19:21 +03:00
|
|
|
/* byte offset to char offset */
|
2008-02-23 14:18:07 +03:00
|
|
|
long
|
|
|
|
rb_str_sublen(VALUE str, long pos)
|
* 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-25 07:29:39 +04:00
|
|
|
{
|
2008-02-23 14:18:07 +03:00
|
|
|
if (single_byte_optimizable(str) || pos < 0)
|
2008-02-17 07:04:14 +03:00
|
|
|
return pos;
|
* 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-25 07:29:39 +04:00
|
|
|
else {
|
|
|
|
char *p = RSTRING_PTR(str);
|
2009-05-20 08:44:36 +04:00
|
|
|
return enc_strlen(p, p + pos, STR_ENC_GET(str), ENC_CODERANGE(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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
}
|
2007-12-19 17:15:05 +03:00
|
|
|
|
2007-08-28 10:45:32 +04:00
|
|
|
VALUE
|
|
|
|
rb_str_subseq(VALUE str, long beg, long len)
|
|
|
|
{
|
2008-09-29 15:56:43 +04:00
|
|
|
VALUE str2;
|
|
|
|
|
2008-09-29 18:39:20 +04:00
|
|
|
if (RSTRING_LEN(str) == beg + len &&
|
|
|
|
RSTRING_EMBED_LEN_MAX < len) {
|
|
|
|
str2 = rb_str_new_shared(rb_str_new_frozen(str));
|
2008-09-29 15:56:43 +04:00
|
|
|
rb_str_drop_bytes(str2, beg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = rb_str_new5(str, RSTRING_PTR(str)+beg, len);
|
|
|
|
}
|
2007-08-28 10:45:32 +04:00
|
|
|
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(str2, str);
|
2007-08-28 10:45:32 +04:00
|
|
|
OBJ_INFECT(str2, str);
|
|
|
|
|
|
|
|
return str2;
|
|
|
|
}
|
|
|
|
|
2012-06-18 12:10:13 +04:00
|
|
|
static char *
|
|
|
|
rb_str_subpos(VALUE str, long beg, long *lenp)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-06-18 12:10:13 +04:00
|
|
|
long len = *lenp;
|
|
|
|
long slen = -1L;
|
|
|
|
long blen = RSTRING_LEN(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2012-06-18 12:10:13 +04:00
|
|
|
char *p, *s = RSTRING_PTR(str), *e = s + blen;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-06-18 12:10:13 +04:00
|
|
|
if (len < 0) return 0;
|
|
|
|
if (!blen) {
|
2007-10-29 11:58:17 +03:00
|
|
|
len = 0;
|
|
|
|
}
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (single_byte_optimizable(str)) {
|
2012-06-18 12:10:13 +04:00
|
|
|
if (beg > blen) return 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (beg < 0) {
|
2012-06-18 12:10:13 +04:00
|
|
|
beg += blen;
|
|
|
|
if (beg < 0) return 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
2012-06-18 12:10:13 +04:00
|
|
|
if (beg + len > blen)
|
|
|
|
len = blen - beg;
|
|
|
|
if (len < 0) return 0;
|
|
|
|
p = s + beg;
|
|
|
|
goto end;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (beg < 0) {
|
2007-10-29 11:58:17 +03:00
|
|
|
if (len > -beg) len = -beg;
|
|
|
|
if (-beg * rb_enc_mbmaxlen(enc) < RSTRING_LEN(str) / 8) {
|
|
|
|
beg = -beg;
|
2008-09-13 22:22:04 +04:00
|
|
|
while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
|
2007-10-29 11:58:17 +03:00
|
|
|
p = e;
|
2012-06-18 12:10:13 +04:00
|
|
|
if (!p) return 0;
|
2008-09-13 22:22:04 +04:00
|
|
|
while (len-- > 0 && (p = rb_enc_prev_char(s, p, e, enc)) != 0);
|
2012-06-18 12:10:13 +04:00
|
|
|
if (!p) return 0;
|
2007-10-29 11:58:17 +03:00
|
|
|
len = e - p;
|
2012-06-18 12:10:13 +04:00
|
|
|
goto end;
|
2007-10-29 11:58:17 +03:00
|
|
|
}
|
|
|
|
else {
|
2012-06-18 12:10:13 +04:00
|
|
|
slen = str_strlen(str, enc);
|
|
|
|
beg += slen;
|
|
|
|
if (beg < 0) return 0;
|
|
|
|
p = s + beg;
|
|
|
|
if (len == 0) goto end;
|
2007-10-29 11:58:17 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2011-01-23 02:00:12 +03:00
|
|
|
else if (beg > 0 && beg > RSTRING_LEN(str)) {
|
2012-06-18 12:10:13 +04:00
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2007-10-29 11:58:17 +03:00
|
|
|
if (len == 0) {
|
2012-06-18 12:10:13 +04:00
|
|
|
if (beg > str_strlen(str, enc)) return 0;
|
|
|
|
p = s + beg;
|
2007-10-29 11:58:17 +03:00
|
|
|
}
|
2008-09-18 16:53:25 +04:00
|
|
|
#ifdef NONASCII_MASK
|
|
|
|
else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID &&
|
|
|
|
enc == rb_utf8_encoding()) {
|
2011-01-23 02:00:12 +03:00
|
|
|
p = str_utf8_nth(s, e, &beg);
|
2012-06-18 12:10:13 +04:00
|
|
|
if (beg > 0) return 0;
|
2008-09-18 16:53:25 +04:00
|
|
|
len = str_utf8_offset(p, e, len);
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-29 11:58:17 +03:00
|
|
|
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
int char_sz = rb_enc_mbmaxlen(enc);
|
|
|
|
|
|
|
|
p = s + beg * char_sz;
|
|
|
|
if (p > e) {
|
2012-06-18 12:10:13 +04:00
|
|
|
return 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
|
|
|
else if (len * char_sz > e - p)
|
2008-02-22 09:52:54 +03:00
|
|
|
len = e - p;
|
|
|
|
else
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
len *= char_sz;
|
|
|
|
}
|
2011-01-23 02:00:12 +03:00
|
|
|
else if ((p = str_nth_len(s, e, &beg, enc)) == e) {
|
2012-06-18 12:10:13 +04:00
|
|
|
if (beg > 0) return 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
len = 0;
|
2005-09-17 18:40:06 +04:00
|
|
|
}
|
2002-12-17 13:34:30 +03:00
|
|
|
else {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
len = str_offset(p, e, len, enc, 0);
|
2002-12-17 13:34:30 +03:00
|
|
|
}
|
2012-06-18 12:10:13 +04:00
|
|
|
end:
|
|
|
|
*lenp = len;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_substr(VALUE str, long beg, long len)
|
|
|
|
{
|
|
|
|
VALUE str2;
|
|
|
|
char *p = rb_str_subpos(str, beg, &len);
|
|
|
|
|
|
|
|
if (!p) return Qnil;
|
|
|
|
if (len > RSTRING_EMBED_LEN_MAX && p + len == RSTRING_END(str)) {
|
2008-02-07 10:33:50 +03:00
|
|
|
str2 = rb_str_new4(str);
|
|
|
|
str2 = str_new3(rb_obj_class(str2), str2);
|
|
|
|
RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len;
|
|
|
|
RSTRING(str2)->as.heap.len = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = rb_str_new5(str, p, len);
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(str2, str);
|
2008-02-07 10:33:50 +03:00
|
|
|
OBJ_INFECT(str2, str);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
return str2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-02-17 10:11:22 +03: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 14:44:21 +04:00
|
|
|
rb_str_freeze(VALUE str)
|
2000-02-17 10:11:22 +03:00
|
|
|
{
|
2008-01-23 09:04:13 +03:00
|
|
|
if (STR_ASSOC_P(str)) {
|
|
|
|
VALUE ary = RSTRING(str)->as.heap.aux.shared;
|
|
|
|
OBJ_FREEZE(ary);
|
|
|
|
}
|
2000-02-17 10:11:22 +03:00
|
|
|
return rb_obj_freeze(str);
|
|
|
|
}
|
|
|
|
|
2008-08-13 19:43:55 +04:00
|
|
|
RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
|
2008-08-12 13:56:55 +04:00
|
|
|
#define rb_str_dup_frozen rb_str_new_frozen
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-10-31 20:37:52 +03: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 14:44:21 +04:00
|
|
|
rb_str_locktmp(VALUE str)
|
2004-10-31 20:37:52 +03:00
|
|
|
{
|
2004-11-10 10:17:53 +03:00
|
|
|
if (FL_TEST(str, STR_TMPLOCK)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "temporal locking already locked string");
|
|
|
|
}
|
2004-10-31 20:37:52 +03:00
|
|
|
FL_SET(str, STR_TMPLOCK);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:44:21 +04:00
|
|
|
rb_str_unlocktmp(VALUE str)
|
2004-10-31 20:37:52 +03:00
|
|
|
{
|
2004-11-10 10:17:53 +03:00
|
|
|
if (!FL_TEST(str, STR_TMPLOCK)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "temporal unlocking already unlocked string");
|
|
|
|
}
|
2004-10-31 20:37:52 +03:00
|
|
|
FL_UNSET(str, STR_TMPLOCK);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
void
|
|
|
|
rb_str_set_len(VALUE str, long len)
|
|
|
|
{
|
2010-08-05 15:00:18 +04:00
|
|
|
long capa;
|
|
|
|
|
2010-08-04 14:37:05 +04:00
|
|
|
str_modifiable(str);
|
2010-08-05 11:42:47 +04:00
|
|
|
if (STR_SHARED_P(str)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "can't set length of shared string");
|
|
|
|
}
|
2010-08-05 15:00:18 +04:00
|
|
|
if (len > (capa = (long)rb_str_capacity(str))) {
|
|
|
|
rb_bug("probable buffer overflow: %ld for %ld", len, capa);
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_str_resize(VALUE str, long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-12-31 17:27:20 +03:00
|
|
|
long slen;
|
2010-08-01 18:04:39 +04:00
|
|
|
int independent;
|
2007-12-31 17:27:20 +03:00
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
2004-10-19 03:47:32 +04:00
|
|
|
|
2010-08-01 18:04:39 +04:00
|
|
|
independent = str_independent(str);
|
|
|
|
ENC_CODERANGE_CLEAR(str);
|
2007-12-31 17:27:20 +03:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
if (len != slen) {
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_EMBED_P(str)) {
|
|
|
|
if (len <= RSTRING_EMBED_LEN_MAX) {
|
|
|
|
STR_SET_EMBED_LEN(str, len);
|
2007-12-31 17:27:20 +03:00
|
|
|
RSTRING(str)->as.ary[len] = '\0';
|
2006-08-31 14:47:44 +04:00
|
|
|
return str;
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
2010-08-01 18:04:39 +04:00
|
|
|
str_make_independent_expand(str, len - slen);
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_NOEMBED(str);
|
2007-07-21 18:54:51 +04:00
|
|
|
}
|
2007-12-31 17:27:20 +03:00
|
|
|
else if (len <= RSTRING_EMBED_LEN_MAX) {
|
|
|
|
char *ptr = RSTRING(str)->as.heap.ptr;
|
|
|
|
STR_SET_EMBED(str);
|
2011-01-25 02:28:22 +03:00
|
|
|
if (slen > len) slen = len;
|
|
|
|
if (slen > 0) MEMCPY(RSTRING(str)->as.ary, ptr, char, slen);
|
2007-12-31 17:27:20 +03:00
|
|
|
RSTRING(str)->as.ary[len] = '\0';
|
|
|
|
STR_SET_EMBED_LEN(str, len);
|
2010-08-01 18:04:39 +04:00
|
|
|
if (independent) xfree(ptr);
|
2007-12-31 17:27:20 +03:00
|
|
|
return str;
|
|
|
|
}
|
2010-08-01 18:04:39 +04:00
|
|
|
else if (!independent) {
|
|
|
|
str_make_independent_expand(str, len - slen);
|
|
|
|
}
|
2007-12-31 17:27:20 +03:00
|
|
|
else if (slen < len || slen - len > 1024) {
|
2006-08-31 14:47:44 +04:00
|
|
|
REALLOC_N(RSTRING(str)->as.heap.ptr, char, len+1);
|
|
|
|
}
|
|
|
|
if (!STR_NOCAPA_P(str)) {
|
|
|
|
RSTRING(str)->as.heap.aux.capa = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING(str)->as.heap.len = len;
|
|
|
|
RSTRING(str)->as.heap.ptr[len] = '\0'; /* sentinel */
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-07-21 23:26:18 +04:00
|
|
|
static VALUE
|
2008-06-20 10:42:07 +04:00
|
|
|
str_buf_cat(VALUE str, const char *ptr, long len)
|
2001-05-30 13:12:34 +04:00
|
|
|
{
|
2008-06-20 10:42:07 +04:00
|
|
|
long capa, total, off = -1;
|
2001-05-30 13:12:34 +04:00
|
|
|
|
2008-06-20 10:42:07 +04:00
|
|
|
if (ptr >= RSTRING_PTR(str) && ptr <= RSTRING_END(str)) {
|
|
|
|
off = ptr - RSTRING_PTR(str);
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
|
|
|
rb_str_modify(str);
|
2008-06-20 10:42:07 +04:00
|
|
|
if (len == 0) return 0;
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_ASSOC_P(str)) {
|
2002-09-12 10:27:15 +04:00
|
|
|
FL_UNSET(str, STR_ASSOC);
|
2006-08-31 14:47:44 +04:00
|
|
|
capa = RSTRING(str)->as.heap.aux.capa = RSTRING_LEN(str);
|
|
|
|
}
|
|
|
|
else if (STR_EMBED_P(str)) {
|
|
|
|
capa = RSTRING_EMBED_LEN_MAX;
|
2002-09-12 10:27:15 +04:00
|
|
|
}
|
|
|
|
else {
|
2006-08-31 14:47:44 +04:00
|
|
|
capa = RSTRING(str)->as.heap.aux.capa;
|
2001-05-30 13:12:34 +04:00
|
|
|
}
|
2008-06-20 10:42:07 +04:00
|
|
|
if (RSTRING_LEN(str) >= LONG_MAX - len) {
|
|
|
|
rb_raise(rb_eArgError, "string sizes too big");
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
total = RSTRING_LEN(str)+len;
|
2001-05-30 13:12:34 +04:00
|
|
|
if (capa <= total) {
|
|
|
|
while (total > capa) {
|
2008-06-20 10:42:07 +04:00
|
|
|
if (capa + 1 >= LONG_MAX / 2) {
|
|
|
|
capa = (total + 4095) / 4096;
|
|
|
|
break;
|
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
capa = (capa + 1) * 2;
|
|
|
|
}
|
2001-12-19 10:48:32 +03:00
|
|
|
RESIZE_CAPA(str, capa);
|
2001-05-30 13:12:34 +04:00
|
|
|
}
|
2008-06-20 10:42:07 +04:00
|
|
|
if (off != -1) {
|
|
|
|
ptr = RSTRING_PTR(str) + off;
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
memcpy(RSTRING_PTR(str) + RSTRING_LEN(str), ptr, len);
|
|
|
|
STR_SET_LEN(str, total);
|
|
|
|
RSTRING_PTR(str)[total] = '\0'; /* sentinel */
|
2001-05-30 13:12:34 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:38:14 +03:00
|
|
|
#define str_buf_cat2(str, ptr) str_buf_cat((str), (ptr), strlen(ptr))
|
2009-09-27 00:17:52 +04:00
|
|
|
|
2008-06-20 10:42:07 +04:00
|
|
|
VALUE
|
|
|
|
rb_str_buf_cat(VALUE str, const char *ptr, long len)
|
|
|
|
{
|
|
|
|
if (len == 0) return str;
|
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
return str_buf_cat(str, ptr, len);
|
|
|
|
}
|
|
|
|
|
2001-05-30 13:12:34 +04: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 14:44:21 +04:00
|
|
|
rb_str_buf_cat2(VALUE str, const char *ptr)
|
2001-05-30 13:12:34 +04:00
|
|
|
{
|
|
|
|
return rb_str_buf_cat(str, ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_str_cat(VALUE str, const char *ptr, long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-09-12 10:27:15 +04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
if (STR_ASSOC_P(str)) {
|
2010-08-01 18:04:39 +04:00
|
|
|
char *p;
|
|
|
|
rb_str_modify_expand(str, len);
|
|
|
|
p = RSTRING(str)->as.heap.ptr;
|
|
|
|
memcpy(p + RSTRING(str)->as.heap.len, ptr, len);
|
|
|
|
len = RSTRING(str)->as.heap.len += len;
|
|
|
|
p[len] = '\0'; /* sentinel */
|
2002-09-12 10:27:15 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
return rb_str_buf_cat(str, ptr, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-04-10 09:48:43 +04: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 14:44:21 +04:00
|
|
|
rb_str_cat2(VALUE str, const char *ptr)
|
2000-04-10 09:48:43 +04:00
|
|
|
{
|
|
|
|
return rb_str_cat(str, ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
static VALUE
|
|
|
|
rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len,
|
|
|
|
int ptr_encindex, int ptr_cr, int *ptr_cr_ret)
|
2008-01-06 12:25:09 +03:00
|
|
|
{
|
2008-01-07 19:28:43 +03:00
|
|
|
int str_encindex = ENCODING_GET(str);
|
|
|
|
int res_encindex;
|
|
|
|
int str_cr, res_cr;
|
2008-01-06 12:25:09 +03:00
|
|
|
|
|
|
|
str_cr = ENC_CODERANGE(str);
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
if (str_encindex == ptr_encindex) {
|
2012-06-02 12:02:23 +04:00
|
|
|
if (str_cr == ENC_CODERANGE_UNKNOWN)
|
2008-01-06 19:38:04 +03:00
|
|
|
ptr_cr = ENC_CODERANGE_UNKNOWN;
|
2008-01-09 07:07:11 +03:00
|
|
|
else if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
|
2008-01-07 19:28:43 +03:00
|
|
|
ptr_cr = coderange_scan(ptr, len, rb_enc_from_index(ptr_encindex));
|
2008-01-06 19:38:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-01-24 05:17:26 +03:00
|
|
|
rb_encoding *str_enc = rb_enc_from_index(str_encindex);
|
|
|
|
rb_encoding *ptr_enc = rb_enc_from_index(ptr_encindex);
|
|
|
|
if (!rb_enc_asciicompat(str_enc) || !rb_enc_asciicompat(ptr_enc)) {
|
2008-02-16 12:02:12 +03:00
|
|
|
if (len == 0)
|
|
|
|
return str;
|
|
|
|
if (RSTRING_LEN(str) == 0) {
|
|
|
|
rb_str_buf_cat(str, ptr, len);
|
|
|
|
ENCODING_CODERANGE_SET(str, ptr_encindex, ptr_cr);
|
|
|
|
return str;
|
|
|
|
}
|
2008-01-24 05:17:26 +03:00
|
|
|
goto incompatible;
|
|
|
|
}
|
2008-01-09 07:07:11 +03:00
|
|
|
if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
|
2008-01-24 05:17:26 +03:00
|
|
|
ptr_cr = coderange_scan(ptr, len, ptr_enc);
|
2008-01-09 07:07:11 +03:00
|
|
|
}
|
2008-01-06 19:38:04 +03:00
|
|
|
if (str_cr == ENC_CODERANGE_UNKNOWN) {
|
2011-02-15 10:08:12 +03:00
|
|
|
if (ENCODING_IS_ASCII8BIT(str) || ptr_cr != ENC_CODERANGE_7BIT) {
|
2008-01-06 19:38:04 +03:00
|
|
|
str_cr = rb_enc_str_coderange(str);
|
|
|
|
}
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-07 19:28:43 +03:00
|
|
|
if (ptr_cr_ret)
|
|
|
|
*ptr_cr_ret = ptr_cr;
|
2008-01-06 12:25:09 +03:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
if (str_encindex != ptr_encindex &&
|
2008-01-06 12:25:09 +03:00
|
|
|
str_cr != ENC_CODERANGE_7BIT &&
|
|
|
|
ptr_cr != ENC_CODERANGE_7BIT) {
|
2008-02-07 10:33:50 +03:00
|
|
|
incompatible:
|
2008-08-12 22:17:05 +04:00
|
|
|
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_enc_name(rb_enc_from_index(str_encindex)),
|
|
|
|
rb_enc_name(rb_enc_from_index(ptr_encindex)));
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str_cr == ENC_CODERANGE_UNKNOWN) {
|
2008-01-07 19:28:43 +03:00
|
|
|
res_encindex = str_encindex;
|
2008-01-06 12:25:09 +03:00
|
|
|
res_cr = ENC_CODERANGE_UNKNOWN;
|
|
|
|
}
|
|
|
|
else if (str_cr == ENC_CODERANGE_7BIT) {
|
|
|
|
if (ptr_cr == ENC_CODERANGE_7BIT) {
|
2011-02-15 10:08:12 +03:00
|
|
|
res_encindex = str_encindex;
|
2008-01-06 12:25:09 +03:00
|
|
|
res_cr = ENC_CODERANGE_7BIT;
|
|
|
|
}
|
|
|
|
else {
|
2008-01-07 19:28:43 +03:00
|
|
|
res_encindex = ptr_encindex;
|
2008-01-06 12:25:09 +03:00
|
|
|
res_cr = ptr_cr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (str_cr == ENC_CODERANGE_VALID) {
|
2008-01-07 19:28:43 +03:00
|
|
|
res_encindex = str_encindex;
|
2010-11-03 08:13:54 +03:00
|
|
|
if (ptr_cr == ENC_CODERANGE_7BIT || ptr_cr == ENC_CODERANGE_VALID)
|
|
|
|
res_cr = str_cr;
|
|
|
|
else
|
|
|
|
res_cr = ptr_cr;
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
|
|
|
else { /* str_cr == ENC_CODERANGE_BROKEN */
|
2008-01-07 19:28:43 +03:00
|
|
|
res_encindex = str_encindex;
|
2008-01-06 12:25:09 +03:00
|
|
|
res_cr = str_cr;
|
|
|
|
if (0 < len) res_cr = ENC_CODERANGE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
2008-06-20 10:42:07 +04:00
|
|
|
str_buf_cat(str, ptr, len);
|
2008-01-07 19:28:43 +03:00
|
|
|
ENCODING_CODERANGE_SET(str, res_encindex, res_cr);
|
2008-01-06 12:25:09 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
VALUE
|
|
|
|
rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *ptr_enc)
|
|
|
|
{
|
|
|
|
return rb_enc_cr_str_buf_cat(str, ptr, len,
|
|
|
|
rb_enc_to_index(ptr_enc), ENC_CODERANGE_UNKNOWN, NULL);
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:50:12 +03:00
|
|
|
VALUE
|
|
|
|
rb_str_buf_cat_ascii(VALUE str, const char *ptr)
|
|
|
|
{
|
|
|
|
/* ptr must reference NUL terminated ASCII string. */
|
|
|
|
int encindex = ENCODING_GET(str);
|
|
|
|
rb_encoding *enc = rb_enc_from_index(encindex);
|
|
|
|
if (rb_enc_asciicompat(enc)) {
|
|
|
|
return rb_enc_cr_str_buf_cat(str, ptr, strlen(ptr),
|
|
|
|
encindex, ENC_CODERANGE_7BIT, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *buf = ALLOCA_N(char, rb_enc_mbmaxlen(enc));
|
|
|
|
while (*ptr) {
|
2008-09-15 21:05:00 +04:00
|
|
|
unsigned int c = (unsigned char)*ptr;
|
2008-01-25 15:50:12 +03:00
|
|
|
int len = rb_enc_codelen(c, enc);
|
|
|
|
rb_enc_mbcput(c, buf, enc);
|
|
|
|
rb_enc_cr_str_buf_cat(str, buf, len,
|
|
|
|
encindex, ENC_CODERANGE_VALID, 0);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-10 09:48:43 +04: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 14:44:21 +04:00
|
|
|
rb_str_buf_append(VALUE str, VALUE str2)
|
2000-04-10 09:48:43 +04:00
|
|
|
{
|
2008-01-08 01:57:00 +03:00
|
|
|
int str2_cr;
|
|
|
|
|
|
|
|
str2_cr = ENC_CODERANGE(str2);
|
2008-01-07 19:28:43 +03:00
|
|
|
|
|
|
|
rb_enc_cr_str_buf_cat(str, RSTRING_PTR(str2), RSTRING_LEN(str2),
|
|
|
|
ENCODING_GET(str2), str2_cr, &str2_cr);
|
2001-05-30 13:12:34 +04:00
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
OBJ_INFECT(str, str2);
|
2008-01-07 19:28:43 +03:00
|
|
|
ENC_CODERANGE_SET(str2, str2_cr);
|
2001-05-30 13:12:34 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:44:21 +04:00
|
|
|
rb_str_append(VALUE str, VALUE str2)
|
2001-05-30 13:12:34 +04:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
2007-09-30 12:13:28 +04:00
|
|
|
int cr, cr2;
|
2010-08-01 18:04:39 +04:00
|
|
|
long len2;
|
* 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-25 07:29:39 +04:00
|
|
|
|
2002-09-13 12:26:35 +04:00
|
|
|
StringValue(str2);
|
2010-08-01 18:04:39 +04:00
|
|
|
if ((len2 = RSTRING_LEN(str2)) > 0 && STR_ASSOC_P(str)) {
|
|
|
|
long len = RSTRING_LEN(str) + len2;
|
2008-01-11 17:26:26 +03:00
|
|
|
enc = rb_enc_check(str, str2);
|
|
|
|
cr = ENC_CODERANGE(str);
|
|
|
|
if ((cr2 = ENC_CODERANGE(str2)) > cr) cr = cr2;
|
2010-08-01 18:04:39 +04:00
|
|
|
rb_str_modify_expand(str, len2);
|
2008-01-11 17:26:26 +03:00
|
|
|
memcpy(RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len,
|
2010-08-01 18:04:39 +04:00
|
|
|
RSTRING_PTR(str2), len2+1);
|
2008-01-11 17:26:26 +03:00
|
|
|
RSTRING(str)->as.heap.len = len;
|
|
|
|
rb_enc_associate(str, enc);
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
|
|
|
OBJ_INFECT(str, str2);
|
|
|
|
return str;
|
2001-05-30 13:12:34 +04:00
|
|
|
}
|
2008-01-11 17:26:26 +03:00
|
|
|
return rb_str_buf_append(str, str2);
|
2000-04-10 09:48:43 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str << integer -> str
|
|
|
|
* str.concat(integer) -> str
|
|
|
|
* str << obj -> str
|
|
|
|
* str.concat(obj) -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Append---Concatenates the given object to <i>str</i>. If the object is a
|
2008-09-16 17:23:16 +04:00
|
|
|
* <code>Integer</code>, it is considered as a codepoint, and is converted
|
* 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-25 07:29:39 +04:00
|
|
|
* to a character before concatenation.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a = "hello "
|
|
|
|
* a << "world" #=> "hello world"
|
|
|
|
* a.concat(33) #=> "hello world!"
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03: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 14:44:21 +04:00
|
|
|
rb_str_concat(VALUE str1, VALUE str2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-01-09 00:42:45 +04:00
|
|
|
unsigned int code;
|
|
|
|
rb_encoding *enc = STR_ENC_GET(str1);
|
2010-01-14 06:50:39 +03:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (FIXNUM_P(str2) || RB_TYPE_P(str2, T_BIGNUM)) {
|
2012-01-09 00:42:45 +04:00
|
|
|
if (rb_num_to_uint(str2, &code) == 0) {
|
2010-10-13 07:56:31 +04:00
|
|
|
}
|
|
|
|
else if (FIXNUM_P(str2)) {
|
|
|
|
rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRangeError, "bignum out of char range");
|
|
|
|
}
|
2010-01-14 03:39:40 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_str_append(str1, str2);
|
|
|
|
}
|
2012-01-09 00:42:45 +04:00
|
|
|
|
|
|
|
if (enc == rb_usascii_encoding()) {
|
|
|
|
/* US-ASCII automatically extended to ASCII-8BIT */
|
2012-01-11 19:00:57 +04:00
|
|
|
char buf[1];
|
|
|
|
buf[0] = (char)code;
|
2012-01-09 00:42:45 +04:00
|
|
|
if (code > 0xFF) {
|
|
|
|
rb_raise(rb_eRangeError, "%u out of char range", code);
|
|
|
|
}
|
|
|
|
rb_str_cat(str1, buf, 1);
|
|
|
|
if (code > 127) {
|
|
|
|
rb_enc_associate(str1, rb_ascii8bit_encoding());
|
|
|
|
ENC_CODERANGE_SET(str1, ENC_CODERANGE_VALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-05-07 09:16:26 +04:00
|
|
|
long pos = RSTRING_LEN(str1);
|
2008-02-23 13:29:13 +03:00
|
|
|
int cr = ENC_CODERANGE(str1);
|
2010-10-12 13:25:33 +04:00
|
|
|
int len;
|
2012-01-09 00:42:45 +04:00
|
|
|
char *buf;
|
|
|
|
|
|
|
|
switch (len = rb_enc_codelen(code, enc)) {
|
|
|
|
case ONIGERR_INVALID_CODE_POINT_VALUE:
|
|
|
|
rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
|
|
|
|
break;
|
|
|
|
case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
|
|
|
|
case 0:
|
|
|
|
rb_raise(rb_eRangeError, "%u out of char range", code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = ALLOCA_N(char, len + 1);
|
|
|
|
rb_enc_mbcput(code, buf, enc);
|
|
|
|
if (rb_enc_precise_mbclen(buf, buf + len + 1, enc) != len) {
|
|
|
|
rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
|
2010-01-14 06:50:39 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_resize(str1, pos+len);
|
2012-09-30 16:20:09 +04:00
|
|
|
memcpy(RSTRING_PTR(str1) + pos, buf, len);
|
2012-01-09 00:42:45 +04:00
|
|
|
if (cr == ENC_CODERANGE_7BIT && code > 127)
|
2010-11-22 06:01:45 +03:00
|
|
|
cr = ENC_CODERANGE_VALID;
|
2008-02-23 13:29:13 +03:00
|
|
|
ENC_CODERANGE_SET(str1, cr);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2012-01-09 00:42:45 +04:00
|
|
|
return str1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2010-08-27 11:33:19 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* str.prepend(other_str) -> str
|
|
|
|
*
|
|
|
|
* Prepend---Prepend the given string to <i>str</i>.
|
|
|
|
*
|
2012-02-24 06:56:30 +04:00
|
|
|
* a = "world"
|
|
|
|
* a.prepend("hello ") #=> "hello world"
|
|
|
|
* a #=> "hello world"
|
2010-08-27 11:33:19 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_prepend(VALUE str, VALUE str2)
|
|
|
|
{
|
|
|
|
StringValue(str2);
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_update(str, 0L, 0L, str2);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t
|
* string.c (rb_hash_uint32, rb_hash_uint, rb_hash_start, rb_hash_end),
include/ruby/intern.h: add Murmurhash API. [ruby-dev:37784]
* complex.c (nucomp_hash), array.c (rb_ary_hash), time.c (time_hash),
string.c (rb_str_hsah), object.c (rb_obj_hash), range.c
(range_hash), struct.c (rb_struct_hash), hash.c (rb_any_hash),
rational.c (nurat_hash): use Murmurhash. [ruby-dev:37784]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22317 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-14 22:55:34 +03:00
|
|
|
rb_memhash(const void *ptr, long len)
|
|
|
|
{
|
2010-12-01 08:36:39 +03:00
|
|
|
return st_hash(ptr, len, rb_hash_start((st_index_t)len));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t
|
2006-08-30 07:02:35 +04:00
|
|
|
rb_str_hash(VALUE str)
|
|
|
|
{
|
2008-12-17 05:01:37 +03:00
|
|
|
int e = ENCODING_GET(str);
|
2009-05-25 04:43:23 +04:00
|
|
|
if (e && rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
|
|
|
|
e = 0;
|
2008-12-17 05:01:37 +03:00
|
|
|
}
|
2009-09-08 17:10:04 +04:00
|
|
|
return rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e;
|
2006-08-30 07:02:35 +04:00
|
|
|
}
|
|
|
|
|
2008-02-12 06:17:43 +03:00
|
|
|
int
|
|
|
|
rb_str_hash_cmp(VALUE str1, VALUE str2)
|
|
|
|
{
|
2009-07-18 12:43:30 +04:00
|
|
|
long len;
|
2008-02-12 06:17:43 +03:00
|
|
|
|
|
|
|
if (!rb_str_comparable(str1, str2)) return 1;
|
|
|
|
if (RSTRING_LEN(str1) == (len = RSTRING_LEN(str2)) &&
|
|
|
|
memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.hash -> fixnum
|
2003-12-30 19:38:32 +03:00
|
|
|
*
|
|
|
|
* Return a hash based on the string's length and content.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_hash_m(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t hval = rb_str_hash(str);
|
* 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-10 01:20:17 +04:00
|
|
|
return INT2FIX(hval);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define lesser(a,b) (((a)>(b))?(b):(a))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-09-26 13:39:08 +04:00
|
|
|
int
|
|
|
|
rb_str_comparable(VALUE str1, VALUE str2)
|
|
|
|
{
|
2008-03-11 03:55:02 +03:00
|
|
|
int idx1, idx2;
|
2007-12-23 19:07:09 +03:00
|
|
|
int rc1, rc2;
|
2007-09-26 13:39:08 +04:00
|
|
|
|
2009-09-18 19:17:17 +04:00
|
|
|
if (RSTRING_LEN(str1) == 0) return TRUE;
|
|
|
|
if (RSTRING_LEN(str2) == 0) return TRUE;
|
2008-03-11 03:55:02 +03:00
|
|
|
idx1 = ENCODING_GET(str1);
|
|
|
|
idx2 = ENCODING_GET(str2);
|
2009-09-18 19:17:17 +04:00
|
|
|
if (idx1 == idx2) return TRUE;
|
2007-12-23 19:07:09 +03:00
|
|
|
rc1 = rb_enc_str_coderange(str1);
|
|
|
|
rc2 = rb_enc_str_coderange(str2);
|
2007-12-24 05:40:55 +03:00
|
|
|
if (rc1 == ENC_CODERANGE_7BIT) {
|
2009-09-18 19:17:17 +04:00
|
|
|
if (rc2 == ENC_CODERANGE_7BIT) return TRUE;
|
2008-03-11 03:55:02 +03:00
|
|
|
if (rb_enc_asciicompat(rb_enc_from_index(idx2)))
|
2009-09-18 19:17:17 +04:00
|
|
|
return TRUE;
|
2007-12-24 05:40:55 +03:00
|
|
|
}
|
|
|
|
if (rc2 == ENC_CODERANGE_7BIT) {
|
2008-03-11 03:55:02 +03:00
|
|
|
if (rb_enc_asciicompat(rb_enc_from_index(idx1)))
|
2009-09-18 19:17:17 +04:00
|
|
|
return TRUE;
|
2007-12-24 05:40:55 +03:00
|
|
|
}
|
2009-09-18 19:17:17 +04:00
|
|
|
return FALSE;
|
2007-09-26 13:39:08 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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 14:44:21 +04:00
|
|
|
rb_str_cmp(VALUE str1, VALUE str2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-07-20 13:39:22 +04:00
|
|
|
long len1, len2;
|
|
|
|
const char *ptr1, *ptr2;
|
1998-01-16 15:13:05 +03:00
|
|
|
int retval;
|
|
|
|
|
2010-07-20 13:39:22 +04:00
|
|
|
if (str1 == str2) return 0;
|
|
|
|
RSTRING_GETMEM(str1, ptr1, len1);
|
|
|
|
RSTRING_GETMEM(str2, ptr2, len2);
|
|
|
|
if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) {
|
|
|
|
if (len1 == len2) {
|
2008-09-18 17:17:41 +04:00
|
|
|
if (!rb_str_comparable(str1, str2)) {
|
2008-10-29 19:30:02 +03:00
|
|
|
if (ENCODING_GET(str1) > ENCODING_GET(str2))
|
2007-12-24 05:40:55 +03:00
|
|
|
return 1;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-12-14 11:37:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2010-07-20 13:39:22 +04:00
|
|
|
if (len1 > len2) return 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
return -1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (retval > 0) return 1;
|
|
|
|
return -1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2009-03-05 04:29:12 +03:00
|
|
|
/* expect tail call optimization */
|
|
|
|
static VALUE
|
|
|
|
str_eql(const VALUE str1, const VALUE str2)
|
|
|
|
{
|
|
|
|
const long len = RSTRING_LEN(str1);
|
2010-07-20 13:39:22 +04:00
|
|
|
const char *ptr1, *ptr2;
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2009-03-05 04:29:12 +03:00
|
|
|
if (len != RSTRING_LEN(str2)) return Qfalse;
|
|
|
|
if (!rb_str_comparable(str1, str2)) return Qfalse;
|
2010-07-20 13:39:22 +04:00
|
|
|
if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
|
|
|
|
return Qtrue;
|
|
|
|
if (memcmp(ptr1, ptr2, len) == 0)
|
2009-03-05 04:29:12 +03:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str == obj -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Equality---If <i>obj</i> is not a <code>String</code>, returns
|
|
|
|
* <code>false</code>. Otherwise, returns <code>true</code> if <i>str</i>
|
|
|
|
* <code><=></code> <i>obj</i> returns zero.
|
|
|
|
*/
|
|
|
|
|
2007-09-26 13:39:08 +04: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 14:44:21 +04:00
|
|
|
rb_str_equal(VALUE str1, VALUE str2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-08-29 10:28:51 +04:00
|
|
|
if (str1 == str2) return Qtrue;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(str2, T_STRING)) {
|
2003-02-04 10:27:43 +03:00
|
|
|
if (!rb_respond_to(str2, rb_intern("to_str"))) {
|
2004-05-07 12:44:24 +04:00
|
|
|
return Qfalse;
|
2003-02-04 10:27:43 +03:00
|
|
|
}
|
|
|
|
return rb_equal(str2, str1);
|
|
|
|
}
|
2009-03-05 04:29:12 +03:00
|
|
|
return str_eql(str1, str2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.eql?(other) -> true or false
|
2003-12-30 19:38:32 +03:00
|
|
|
*
|
2009-10-17 15:12:05 +04:00
|
|
|
* Two strings are equal if they have the same length and content.
|
2003-12-30 19:38:32 +03:00
|
|
|
*/
|
|
|
|
|
2001-07-24 13:07:33 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_eql(VALUE str1, VALUE str2)
|
2001-07-24 13:07:33 +04:00
|
|
|
{
|
2010-07-20 13:39:22 +04:00
|
|
|
if (str1 == str2) return Qtrue;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(str2, T_STRING)) return Qfalse;
|
2009-03-05 04:29:12 +03:00
|
|
|
return str_eql(str1, str2);
|
2001-07-24 13:07:33 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str <=> other_str -> -1, 0, +1 or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-12-05 04:01:53 +03:00
|
|
|
* Comparison---Returns -1 if <i>other_str</i> is greater than, 0 if
|
|
|
|
* <i>other_str</i> is equal to, and +1 if <i>other_str</i> is less than
|
2003-12-25 00:14:46 +03:00
|
|
|
* <i>str</i>. If the strings are of different lengths, and the strings are
|
|
|
|
* equal when compared up to the shortest length, then the longer string is
|
2007-10-04 12:09:06 +04:00
|
|
|
* considered greater than the shorter one. In older versions of Ruby, setting
|
2003-12-25 00:14:46 +03:00
|
|
|
* <code>$=</code> allowed case-insensitive comparisons; this is now deprecated
|
|
|
|
* in favor of using <code>String#casecmp</code>.
|
|
|
|
*
|
|
|
|
* <code><=></code> is the basis for the methods <code><</code>,
|
|
|
|
* <code><=</code>, <code>></code>, <code>>=</code>, and <code>between?</code>,
|
|
|
|
* included from module <code>Comparable</code>. The method
|
|
|
|
* <code>String#==</code> does not use <code>Comparable#==</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "abcdef" <=> "abcde" #=> 1
|
|
|
|
* "abcdef" <=> "abcdef" #=> 0
|
|
|
|
* "abcdef" <=> "abcdefg" #=> -1
|
|
|
|
* "abcdef" <=> "ABCDEF" #=> 1
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_cmp_m(VALUE str1, VALUE str2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-02-20 23:40:20 +03:00
|
|
|
long result;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(str2, T_STRING)) {
|
2003-02-20 23:40:20 +03:00
|
|
|
if (!rb_respond_to(str2, rb_intern("to_str"))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
else if (!rb_respond_to(str2, rb_intern("<=>"))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE tmp = rb_funcall(str2, rb_intern("<=>"), 1, str1);
|
|
|
|
|
2003-02-21 06:52:27 +03:00
|
|
|
if (NIL_P(tmp)) return Qnil;
|
2003-02-20 23:40:20 +03:00
|
|
|
if (!FIXNUM_P(tmp)) {
|
2003-02-21 06:52:27 +03:00
|
|
|
return rb_funcall(LONG2FIX(0), '-', 1, tmp);
|
2003-02-20 23:40:20 +03:00
|
|
|
}
|
2003-02-21 06:52:27 +03:00
|
|
|
result = -FIX2LONG(tmp);
|
2003-02-20 23:40:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = rb_str_cmp(str1, str2);
|
2002-12-19 12:20:20 +03:00
|
|
|
}
|
2003-02-21 12:32:18 +03:00
|
|
|
return LONG2NUM(result);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.casecmp(other_str) -> -1, 0, +1 or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Case-insensitive version of <code>String#<=></code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "abcdef".casecmp("abcde") #=> 1
|
|
|
|
* "aBcDeF".casecmp("abcdef") #=> 0
|
|
|
|
* "abcdef".casecmp("abcdefg") #=> -1
|
|
|
|
* "abcdef".casecmp("ABCDEF") #=> 0
|
|
|
|
*/
|
|
|
|
|
2001-07-24 13:07:33 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_casecmp(VALUE str1, VALUE str2)
|
2001-07-24 13:07:33 +04:00
|
|
|
{
|
2008-09-18 16:53:25 +04:00
|
|
|
long len;
|
2007-12-14 11:37:39 +03:00
|
|
|
rb_encoding *enc;
|
|
|
|
char *p1, *p1end, *p2, *p2end;
|
2001-07-24 13:07:33 +04:00
|
|
|
|
|
|
|
StringValue(str2);
|
2007-12-14 11:37:39 +03:00
|
|
|
enc = rb_enc_compatible(str1, str2);
|
|
|
|
if (!enc) {
|
2008-02-12 06:17:43 +03:00
|
|
|
return Qnil;
|
2007-12-14 11:37:39 +03:00
|
|
|
}
|
|
|
|
|
2007-12-27 09:02:55 +03:00
|
|
|
p1 = RSTRING_PTR(str1); p1end = RSTRING_END(str1);
|
2007-12-14 11:37:39 +03:00
|
|
|
p2 = RSTRING_PTR(str2); p2end = RSTRING_END(str2);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (single_byte_optimizable(str1) && single_byte_optimizable(str2)) {
|
|
|
|
while (p1 < p1end && p2 < p2end) {
|
|
|
|
if (*p1 != *p2) {
|
2008-09-23 16:27:35 +04:00
|
|
|
unsigned int c1 = TOUPPER(*p1 & 0xff);
|
|
|
|
unsigned int c2 = TOUPPER(*p2 & 0xff);
|
|
|
|
if (c1 != c2)
|
|
|
|
return INT2FIX(c1 < c2 ? -1 : 1);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
|
|
|
p1++;
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (p1 < p1end && p2 < p2end) {
|
2008-09-23 16:27:35 +04:00
|
|
|
int l1, c1 = rb_enc_ascget(p1, p1end, &l1, enc);
|
|
|
|
int l2, c2 = rb_enc_ascget(p2, p2end, &l2, enc);
|
|
|
|
|
|
|
|
if (0 <= c1 && 0 <= c2) {
|
|
|
|
c1 = TOUPPER(c1);
|
|
|
|
c2 = TOUPPER(c2);
|
|
|
|
if (c1 != c2)
|
|
|
|
return INT2FIX(c1 < c2 ? -1 : 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int r;
|
|
|
|
l1 = rb_enc_mbclen(p1, p1end, enc);
|
|
|
|
l2 = rb_enc_mbclen(p2, p2end, enc);
|
|
|
|
len = l1 < l2 ? l1 : l2;
|
|
|
|
r = memcmp(p1, p2, len);
|
|
|
|
if (r != 0)
|
|
|
|
return INT2FIX(r < 0 ? -1 : 1);
|
|
|
|
if (l1 != l2)
|
|
|
|
return INT2FIX(l1 < l2 ? -1 : 1);
|
|
|
|
}
|
|
|
|
p1 += l1;
|
|
|
|
p2 += l2;
|
2007-12-14 11:37:39 +03:00
|
|
|
}
|
2001-07-24 13:07:33 +04:00
|
|
|
}
|
2007-12-14 11:37:39 +03:00
|
|
|
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);
|
|
|
|
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return INT2FIX(1);
|
2001-07-24 13:07:33 +04:00
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static long
|
* 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 14:44:21 +04:00
|
|
|
rb_str_index(VALUE str, VALUE sub, long offset)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-02-07 09:35:26 +03:00
|
|
|
long pos;
|
2008-09-13 20:40:31 +04:00
|
|
|
char *s, *sptr, *e;
|
* 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-25 07:29:39 +04:00
|
|
|
long len, slen;
|
|
|
|
rb_encoding *enc;
|
2003-02-07 09:35:26 +03:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
enc = rb_enc_check(str, sub);
|
2007-12-17 12:08:27 +03:00
|
|
|
if (is_broken_string(sub)) {
|
|
|
|
return -1;
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
len = str_strlen(str, enc);
|
|
|
|
slen = str_strlen(sub, enc);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (offset < 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-25 07:29:39 +04:00
|
|
|
offset += len;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (offset < 0) return -1;
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
if (len - offset < slen) return -1;
|
2007-12-10 07:50:35 +03:00
|
|
|
s = RSTRING_PTR(str);
|
2008-09-13 20:40:31 +04:00
|
|
|
e = s + RSTRING_LEN(str);
|
2007-12-10 07:50:35 +03:00
|
|
|
if (offset) {
|
2007-12-31 13:39:20 +03:00
|
|
|
offset = str_offset(s, RSTRING_END(str), offset, enc, single_byte_optimizable(str));
|
2007-12-20 03:08:23 +03:00
|
|
|
s += offset;
|
2007-12-10 07:50:35 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
if (slen == 0) return offset;
|
|
|
|
/* need proceed one character at a time */
|
2007-12-10 07:50:35 +03:00
|
|
|
sptr = RSTRING_PTR(sub);
|
|
|
|
slen = RSTRING_LEN(sub);
|
|
|
|
len = RSTRING_LEN(str) - offset;
|
|
|
|
for (;;) {
|
|
|
|
char *t;
|
2008-03-17 22:04:29 +03:00
|
|
|
pos = rb_memsearch(sptr, slen, s, len, enc);
|
2007-12-10 07:50:35 +03:00
|
|
|
if (pos < 0) return pos;
|
2008-09-13 20:40:31 +04:00
|
|
|
t = rb_enc_right_char_head(s, s+pos, e, enc);
|
2008-03-09 21:51:46 +03:00
|
|
|
if (t == s + pos) break;
|
2007-12-10 07:50:35 +03:00
|
|
|
if ((len -= t - s) <= 0) return -1;
|
|
|
|
offset += t - s;
|
|
|
|
s = t;
|
|
|
|
}
|
2003-02-07 09:35:26 +03:00
|
|
|
return pos + offset;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.index(substring [, offset]) -> fixnum or nil
|
|
|
|
* str.index(regexp [, offset]) -> fixnum or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-06-26 18:58:59 +04:00
|
|
|
* Returns the index of the first occurrence of the given <i>substring</i> or
|
|
|
|
* pattern (<i>regexp</i>) in <i>str</i>. Returns <code>nil</code> if not
|
|
|
|
* found. If the second parameter is present, it specifies the position in the
|
|
|
|
* string to begin the search.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".index('e') #=> 1
|
|
|
|
* "hello".index('lo') #=> 3
|
|
|
|
* "hello".index('a') #=> nil
|
2008-03-09 04:04:46 +03:00
|
|
|
* "hello".index(?e) #=> 1
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".index(/[aeiou]/, -3) #=> 4
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_index_m(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE sub;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE initpos;
|
1999-08-13 09:45:20 +04:00
|
|
|
long pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
|
1999-08-13 09:45:20 +04:00
|
|
|
pos = NUM2LONG(initpos);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos = 0;
|
|
|
|
}
|
2000-02-17 10:11:22 +03:00
|
|
|
if (pos < 0) {
|
2008-01-07 19:28:43 +03:00
|
|
|
pos += str_strlen(str, STR_ENC_GET(str));
|
2002-03-19 12:03:11 +03:00
|
|
|
if (pos < 0) {
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(sub, T_REGEXP)) {
|
2002-03-19 12:03:11 +03:00
|
|
|
rb_backref_set(Qnil);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2000-02-17 10:11:22 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (SPECIAL_CONST_P(sub)) goto generic;
|
|
|
|
switch (BUILTIN_TYPE(sub)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case T_REGEXP:
|
2009-07-14 12:30:54 +04:00
|
|
|
if (pos > str_strlen(str, STR_ENC_GET(str)))
|
|
|
|
return Qnil;
|
2008-08-29 13:22:17 +04:00
|
|
|
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
|
2009-06-30 13:06:48 +04:00
|
|
|
rb_enc_check(str, sub), single_byte_optimizable(str));
|
2008-08-29 13:22:17 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
pos = rb_reg_search(sub, str, pos, 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-25 07:29:39 +04:00
|
|
|
pos = rb_str_sublen(str, pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
generic:
|
2003-01-31 06:15:33 +03:00
|
|
|
default: {
|
2007-07-16 13:29:35 +04:00
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_check_string_type(sub);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
|
|
|
rb_obj_classname(sub));
|
|
|
|
}
|
|
|
|
sub = tmp;
|
2003-01-31 06:15:33 +03:00
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case T_STRING:
|
|
|
|
pos = rb_str_index(str, sub, pos);
|
* 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-25 07:29:39 +04:00
|
|
|
pos = rb_str_sublen(str, pos);
|
2003-01-31 06:15:33 +03:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == -1) return Qnil;
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2NUM(pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-07-26 10:12:39 +04:00
|
|
|
static long
|
* 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 14:44:21 +04:00
|
|
|
rb_str_rindex(VALUE str, VALUE sub, long pos)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
long len, slen;
|
|
|
|
char *s, *sbeg, *e, *t;
|
|
|
|
rb_encoding *enc;
|
2007-12-31 13:39:20 +03:00
|
|
|
int singlebyte = single_byte_optimizable(str);
|
2002-07-26 10:12:39 +04:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
enc = rb_enc_check(str, sub);
|
2007-12-17 12:08:27 +03:00
|
|
|
if (is_broken_string(sub)) {
|
|
|
|
return -1;
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
len = str_strlen(str, enc);
|
|
|
|
slen = str_strlen(sub, enc);
|
2002-07-26 10:12:39 +04:00
|
|
|
/* substring longer than string */
|
* 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-25 07:29:39 +04:00
|
|
|
if (len < slen) return -1;
|
|
|
|
if (len - pos < slen) {
|
|
|
|
pos = len - slen;
|
|
|
|
}
|
|
|
|
if (len == 0) {
|
|
|
|
return pos;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
sbeg = RSTRING_PTR(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-25 07:29:39 +04:00
|
|
|
e = RSTRING_END(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
t = RSTRING_PTR(sub);
|
2007-12-19 17:43:37 +03:00
|
|
|
slen = RSTRING_LEN(sub);
|
2010-09-24 10:28:35 +04:00
|
|
|
s = str_nth(sbeg, e, pos, enc, singlebyte);
|
|
|
|
while (s) {
|
2007-10-06 09:32:37 +04:00
|
|
|
if (memcmp(s, t, slen) == 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-25 07:29:39 +04:00
|
|
|
return pos;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
if (pos == 0) break;
|
|
|
|
pos--;
|
2010-09-24 10:28:35 +04:00
|
|
|
s = rb_enc_prev_char(sbeg, s, e, enc);
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
return -1;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.rindex(substring [, fixnum]) -> fixnum or nil
|
|
|
|
* str.rindex(regexp [, fixnum]) -> fixnum or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-06-26 18:58:59 +04:00
|
|
|
* Returns the index of the last occurrence of the given <i>substring</i> or
|
|
|
|
* pattern (<i>regexp</i>) in <i>str</i>. Returns <code>nil</code> if not
|
|
|
|
* found. If the second parameter is present, it specifies the position in the
|
|
|
|
* string to end the search---characters beyond this point will not be
|
|
|
|
* considered.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".rindex('e') #=> 1
|
|
|
|
* "hello".rindex('l') #=> 3
|
|
|
|
* "hello".rindex('a') #=> nil
|
2008-03-09 04:04:46 +03:00
|
|
|
* "hello".rindex(?e) #=> 1
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".rindex(/[aeiou]/, -2) #=> 1
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE sub;
|
* 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-25 07:29:39 +04:00
|
|
|
VALUE vpos;
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2007-08-28 10:45:32 +04:00
|
|
|
long pos, len = str_strlen(str, enc);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &sub, &vpos) == 2) {
|
|
|
|
pos = NUM2LONG(vpos);
|
2004-10-19 04:03:01 +04:00
|
|
|
if (pos < 0) {
|
2007-08-28 10:45:32 +04:00
|
|
|
pos += len;
|
2002-03-19 12:03:11 +03:00
|
|
|
if (pos < 0) {
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(sub, T_REGEXP)) {
|
2002-03-19 12:03:11 +03:00
|
|
|
rb_backref_set(Qnil);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
2007-08-28 10:45:32 +04:00
|
|
|
if (pos > len) pos = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2007-08-28 10:45:32 +04:00
|
|
|
pos = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (SPECIAL_CONST_P(sub)) goto generic;
|
|
|
|
switch (BUILTIN_TYPE(sub)) {
|
1998-01-16 15:19:22 +03:00
|
|
|
case T_REGEXP:
|
* 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-25 07:29:39 +04:00
|
|
|
/* enc = rb_get_check(str, sub); */
|
2008-08-29 13:22:17 +04:00
|
|
|
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
|
2008-10-17 02:21:42 +04:00
|
|
|
STR_ENC_GET(str), single_byte_optimizable(str));
|
2008-08-29 13:22:17 +04:00
|
|
|
|
2008-06-28 16:25:45 +04:00
|
|
|
if (!RREGEXP(sub)->ptr || RREGEXP_SRC_LEN(sub)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
pos = rb_reg_search(sub, str, pos, 1);
|
* 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-25 07:29:39 +04:00
|
|
|
pos = rb_str_sublen(str, pos);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2002-08-21 19:47:54 +04:00
|
|
|
if (pos >= 0) return LONG2NUM(pos);
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
generic:
|
2007-07-16 13:29:35 +04:00
|
|
|
default: {
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_check_string_type(sub);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
|
|
|
rb_obj_classname(sub));
|
|
|
|
}
|
|
|
|
sub = tmp;
|
|
|
|
}
|
|
|
|
/* fall through */
|
1998-01-16 15:19:22 +03:00
|
|
|
case T_STRING:
|
2002-07-26 10:12:39 +04:00
|
|
|
pos = rb_str_rindex(str, sub, pos);
|
2002-08-21 19:47:54 +04:00
|
|
|
if (pos >= 0) return LONG2NUM(pos);
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str =~ obj -> fixnum or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Match---If <i>obj</i> is a <code>Regexp</code>, use it as a pattern to match
|
2009-02-22 17:26:27 +03:00
|
|
|
* against <i>str</i>,and returns the position the match starts, or
|
2006-06-27 18:14:25 +04:00
|
|
|
* <code>nil</code> if there is no match. Otherwise, invokes
|
2003-12-25 00:14:46 +03:00
|
|
|
* <i>obj.=~</i>, passing <i>str</i> as an argument. The default
|
2011-01-26 04:38:05 +03:00
|
|
|
* <code>=~</code> in <code>Object</code> returns <code>nil</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-10-06 23:38:18 +04:00
|
|
|
* Note: <code>str =~ regexp</code> is not the same as
|
|
|
|
* <code>regexp =~ str</code>. Strings captured from named capture groups
|
|
|
|
* are assigned to local variables only in the second case.
|
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "cat o' 9 tails" =~ /\d/ #=> 7
|
2008-03-09 04:04:46 +03:00
|
|
|
* "cat o' 9 tails" =~ 9 #=> nil
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2002-07-11 12:22:18 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_match(VALUE x, VALUE y)
|
2002-07-11 12:22:18 +04:00
|
|
|
{
|
2012-08-02 02:29:26 +04:00
|
|
|
if (SPECIAL_CONST_P(y)) goto generic;
|
|
|
|
switch (BUILTIN_TYPE(y)) {
|
2002-07-11 12:22:18 +04:00
|
|
|
case T_STRING:
|
2004-02-10 08:36:34 +03:00
|
|
|
rb_raise(rb_eTypeError, "type mismatch: String given");
|
|
|
|
|
2003-11-04 12:13:57 +03:00
|
|
|
case T_REGEXP:
|
|
|
|
return rb_reg_match(y, x);
|
2002-07-11 12:22:18 +04:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
generic:
|
2002-07-11 12:22:18 +04:00
|
|
|
default:
|
|
|
|
return rb_funcall(y, rb_intern("=~"), 1, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static VALUE get_pat(VALUE, int);
|
2002-09-11 08:05:36 +04:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.match(pattern) -> matchdata or nil
|
|
|
|
* str.match(pattern, pos) -> matchdata or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Converts <i>pattern</i> to a <code>Regexp</code> (if it isn't already one),
|
2004-07-17 12:02:20 +04:00
|
|
|
* then invokes its <code>match</code> method on <i>str</i>. If the second
|
|
|
|
* parameter is present, it specifies the position in the string to begin the
|
|
|
|
* search.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-03-09 04:04:46 +03:00
|
|
|
* 'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l">
|
2003-12-25 00:14:46 +03:00
|
|
|
* 'hello'.match('(.)\1')[0] #=> "ll"
|
|
|
|
* 'hello'.match(/(.)\1/)[0] #=> "ll"
|
|
|
|
* 'hello'.match('xx') #=> nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-09-20 21:14:01 +04:00
|
|
|
* If a block is given, invoke the block with MatchData if match succeed, so
|
|
|
|
* that you can write
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-09-20 21:14:01 +04:00
|
|
|
* str.match(pat) {|m| ...}
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-09-20 21:14:01 +04:00
|
|
|
* instead of
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-09-20 21:14:01 +04:00
|
|
|
* if m = str.match(pat)
|
|
|
|
* ...
|
|
|
|
* end
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-12-31 08:52:59 +03:00
|
|
|
* The return value is a value from block execution in this case.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2002-07-11 12:22:18 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_match_m(int argc, VALUE *argv, VALUE str)
|
2002-07-11 12:22:18 +04:00
|
|
|
{
|
2007-09-20 21:14:01 +04:00
|
|
|
VALUE re, result;
|
2004-10-19 03:47:32 +04:00
|
|
|
if (argc < 1)
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
2004-07-17 12:02:20 +04:00
|
|
|
re = argv[0];
|
|
|
|
argv[0] = str;
|
2007-09-20 21:14:01 +04:00
|
|
|
result = rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv);
|
|
|
|
if (!NIL_P(result) && rb_block_given_p()) {
|
|
|
|
return rb_yield(result);
|
|
|
|
}
|
|
|
|
return result;
|
2002-07-11 12:22:18 +04:00
|
|
|
}
|
|
|
|
|
2008-01-27 11:21:24 +03:00
|
|
|
enum neighbor_char {
|
|
|
|
NEIGHBOR_NOT_CHAR,
|
|
|
|
NEIGHBOR_FOUND,
|
|
|
|
NEIGHBOR_WRAPPED
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum neighbor_char
|
2009-09-18 19:17:17 +04:00
|
|
|
enc_succ_char(char *p, long len, rb_encoding *enc)
|
2008-01-27 11:21:24 +03:00
|
|
|
{
|
2009-09-18 19:17:17 +04:00
|
|
|
long i;
|
|
|
|
int l;
|
2008-01-27 11:21:24 +03:00
|
|
|
while (1) {
|
2008-01-27 13:08:58 +03:00
|
|
|
for (i = len-1; 0 <= i && (unsigned char)p[i] == 0xff; i--)
|
|
|
|
p[i] = '\0';
|
2008-01-27 11:21:24 +03:00
|
|
|
if (i < 0)
|
|
|
|
return NEIGHBOR_WRAPPED;
|
2008-01-27 13:08:58 +03:00
|
|
|
++((unsigned char*)p)[i];
|
2008-09-18 16:53:25 +04:00
|
|
|
l = rb_enc_precise_mbclen(p, p+len, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (MBCLEN_CHARFOUND_P(l)) {
|
|
|
|
l = MBCLEN_CHARFOUND_LEN(l);
|
2008-01-27 11:21:24 +03:00
|
|
|
if (l == len) {
|
|
|
|
return NEIGHBOR_FOUND;
|
|
|
|
}
|
|
|
|
else {
|
2008-01-27 13:08:58 +03:00
|
|
|
memset(p+l, 0xff, len-l);
|
2008-01-27 11:21:24 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 17:27:07 +03:00
|
|
|
if (MBCLEN_INVALID_P(l) && i < len-1) {
|
2009-09-18 19:17:17 +04:00
|
|
|
long len2;
|
|
|
|
int l2;
|
2008-01-27 11:21:24 +03:00
|
|
|
for (len2 = len-1; 0 < len2; len2--) {
|
2008-09-18 16:53:25 +04:00
|
|
|
l2 = rb_enc_precise_mbclen(p, p+len2, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_INVALID_P(l2))
|
2008-01-27 11:21:24 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-01-27 13:08:58 +03:00
|
|
|
memset(p+len2+1, 0xff, len-(len2+1));
|
2008-01-27 11:21:24 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-01-27 11:21:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum neighbor_char
|
2009-09-18 19:17:17 +04:00
|
|
|
enc_pred_char(char *p, long len, rb_encoding *enc)
|
2008-01-27 11:21:24 +03:00
|
|
|
{
|
2009-09-18 19:17:17 +04:00
|
|
|
long i;
|
|
|
|
int l;
|
2008-01-27 11:21:24 +03:00
|
|
|
while (1) {
|
2008-01-27 13:08:58 +03:00
|
|
|
for (i = len-1; 0 <= i && (unsigned char)p[i] == 0; i--)
|
|
|
|
p[i] = '\xff';
|
2008-01-27 11:21:24 +03:00
|
|
|
if (i < 0)
|
|
|
|
return NEIGHBOR_WRAPPED;
|
2008-01-27 13:08:58 +03:00
|
|
|
--((unsigned char*)p)[i];
|
2008-09-18 16:53:25 +04:00
|
|
|
l = rb_enc_precise_mbclen(p, p+len, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (MBCLEN_CHARFOUND_P(l)) {
|
|
|
|
l = MBCLEN_CHARFOUND_LEN(l);
|
2008-01-27 11:21:24 +03:00
|
|
|
if (l == len) {
|
|
|
|
return NEIGHBOR_FOUND;
|
|
|
|
}
|
|
|
|
else {
|
2008-01-27 13:08:58 +03:00
|
|
|
memset(p+l, 0, len-l);
|
2008-01-27 11:21:24 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 17:27:07 +03:00
|
|
|
if (MBCLEN_INVALID_P(l) && i < len-1) {
|
2009-09-18 19:17:17 +04:00
|
|
|
long len2;
|
|
|
|
int l2;
|
2008-01-27 11:21:24 +03:00
|
|
|
for (len2 = len-1; 0 < len2; len2--) {
|
2008-09-18 16:53:25 +04:00
|
|
|
l2 = rb_enc_precise_mbclen(p, p+len2, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_INVALID_P(l2))
|
2008-01-27 11:21:24 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-01-27 13:08:58 +03:00
|
|
|
memset(p+len2+1, 0, len-(len2+1));
|
2008-01-27 11:21:24 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-06 07:42:12 +04:00
|
|
|
/*
|
2008-01-27 11:21:24 +03:00
|
|
|
overwrite +p+ by succeeding letter in +enc+ and returns
|
|
|
|
NEIGHBOR_FOUND or NEIGHBOR_WRAPPED.
|
|
|
|
When NEIGHBOR_WRAPPED, carried-out letter is stored into carry.
|
|
|
|
assuming each ranges are successive, and mbclen
|
2007-09-06 07:42:12 +04:00
|
|
|
never change in each ranges.
|
2008-01-27 11:21:24 +03:00
|
|
|
NEIGHBOR_NOT_CHAR is returned if invalid character or the range has only one
|
|
|
|
character.
|
2007-09-06 07:42:12 +04:00
|
|
|
*/
|
2008-01-27 11:21:24 +03:00
|
|
|
static enum neighbor_char
|
2009-09-18 19:17:17 +04:00
|
|
|
enc_succ_alnum_char(char *p, long len, rb_encoding *enc, char *carry)
|
2008-01-27 11:21:24 +03:00
|
|
|
{
|
|
|
|
enum neighbor_char ret;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int c;
|
2008-01-27 11:21:24 +03:00
|
|
|
int ctype;
|
|
|
|
int range;
|
|
|
|
char save[ONIGENC_CODE_TO_MBC_MAXLEN];
|
|
|
|
|
|
|
|
c = rb_enc_mbc_to_codepoint(p, p+len, enc);
|
|
|
|
if (rb_enc_isctype(c, ONIGENC_CTYPE_DIGIT, enc))
|
|
|
|
ctype = ONIGENC_CTYPE_DIGIT;
|
|
|
|
else if (rb_enc_isctype(c, ONIGENC_CTYPE_ALPHA, enc))
|
|
|
|
ctype = ONIGENC_CTYPE_ALPHA;
|
|
|
|
else
|
|
|
|
return NEIGHBOR_NOT_CHAR;
|
|
|
|
|
|
|
|
MEMCPY(save, p, char, len);
|
|
|
|
ret = enc_succ_char(p, len, enc);
|
|
|
|
if (ret == NEIGHBOR_FOUND) {
|
|
|
|
c = rb_enc_mbc_to_codepoint(p, p+len, enc);
|
|
|
|
if (rb_enc_isctype(c, ctype, enc))
|
|
|
|
return NEIGHBOR_FOUND;
|
|
|
|
}
|
|
|
|
MEMCPY(p, save, char, len);
|
|
|
|
range = 1;
|
|
|
|
while (1) {
|
|
|
|
MEMCPY(save, p, char, len);
|
|
|
|
ret = enc_pred_char(p, len, enc);
|
|
|
|
if (ret == NEIGHBOR_FOUND) {
|
|
|
|
c = rb_enc_mbc_to_codepoint(p, p+len, enc);
|
|
|
|
if (!rb_enc_isctype(c, ctype, enc)) {
|
|
|
|
MEMCPY(p, save, char, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MEMCPY(p, save, char, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
range++;
|
|
|
|
}
|
|
|
|
if (range == 1) {
|
|
|
|
return NEIGHBOR_NOT_CHAR;
|
|
|
|
}
|
2007-09-06 07:42:12 +04:00
|
|
|
|
2008-01-27 11:21:24 +03:00
|
|
|
if (ctype != ONIGENC_CTYPE_DIGIT) {
|
|
|
|
MEMCPY(carry, p, char, len);
|
|
|
|
return NEIGHBOR_WRAPPED;
|
2007-09-06 07:42:12 +04:00
|
|
|
}
|
2008-01-27 11:21:24 +03:00
|
|
|
|
|
|
|
MEMCPY(carry, p, char, len);
|
|
|
|
enc_succ_char(carry, len, enc);
|
|
|
|
return NEIGHBOR_WRAPPED;
|
2007-09-06 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.succ -> new_str
|
|
|
|
* str.next -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns the successor to <i>str</i>. The successor is calculated by
|
|
|
|
* incrementing characters starting from the rightmost alphanumeric (or
|
|
|
|
* the rightmost character if there are no alphanumerics) in the
|
|
|
|
* string. Incrementing a digit always results in another digit, and
|
|
|
|
* incrementing a letter results in another letter of the same case.
|
|
|
|
* Incrementing nonalphanumerics uses the underlying character set's
|
|
|
|
* collating sequence.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If the increment generates a ``carry,'' the character to the left of
|
|
|
|
* it is incremented. This process repeats until there is no carry,
|
|
|
|
* adding an additional character if necessary.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "abcd".succ #=> "abce"
|
|
|
|
* "THX1138".succ #=> "THX1139"
|
|
|
|
* "<<koala>>".succ #=> "<<koalb>>"
|
|
|
|
* "1999zzz".succ #=> "2000aaa"
|
|
|
|
* "ZZZ9999".succ #=> "AAAA0000"
|
|
|
|
* "***".succ #=> "**+"
|
|
|
|
*/
|
|
|
|
|
2006-12-31 18:02:22 +03: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 14:44:21 +04:00
|
|
|
rb_str_succ(VALUE orig)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
2000-05-12 13:07:57 +04:00
|
|
|
VALUE str;
|
2008-07-08 20:38:40 +04:00
|
|
|
char *sbeg, *s, *e, *last_alnum = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
int c = -1;
|
2008-09-18 16:53:25 +04:00
|
|
|
long l;
|
2008-01-27 13:08:58 +03:00
|
|
|
char carry[ONIGENC_CODE_TO_MBC_MAXLEN] = "\1";
|
2009-09-18 19:17:17 +04:00
|
|
|
long carry_pos = 0, carry_len = 1;
|
2008-07-08 20:38:40 +04:00
|
|
|
enum neighbor_char neighbor = NEIGHBOR_FOUND;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
str = rb_str_new5(orig, RSTRING_PTR(orig), RSTRING_LEN(orig));
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(str, orig);
|
2000-05-01 13:42:38 +04:00
|
|
|
OBJ_INFECT(str, orig);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) == 0) return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(orig);
|
2007-09-06 07:42:12 +04:00
|
|
|
sbeg = RSTRING_PTR(str);
|
|
|
|
s = e = sbeg + RSTRING_LEN(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-13 22:22:04 +04:00
|
|
|
while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
|
2008-07-08 20:38:40 +04:00
|
|
|
if (neighbor == NEIGHBOR_NOT_CHAR && last_alnum) {
|
|
|
|
if (ISALPHA(*last_alnum) ? ISDIGIT(*s) :
|
|
|
|
ISDIGIT(*last_alnum) ? ISALPHA(*s) : 0) {
|
|
|
|
s = last_alnum;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-18 16:53:25 +04:00
|
|
|
if ((l = rb_enc_precise_mbclen(s, e, enc)) <= 0) continue;
|
2008-01-27 11:21:24 +03:00
|
|
|
neighbor = enc_succ_alnum_char(s, l, enc, carry);
|
2008-07-08 20:38:40 +04:00
|
|
|
switch (neighbor) {
|
|
|
|
case NEIGHBOR_NOT_CHAR:
|
|
|
|
continue;
|
|
|
|
case NEIGHBOR_FOUND:
|
|
|
|
return str;
|
|
|
|
case NEIGHBOR_WRAPPED:
|
|
|
|
last_alnum = s;
|
|
|
|
break;
|
2008-07-07 19:23:01 +04:00
|
|
|
}
|
2008-01-27 11:21:24 +03:00
|
|
|
c = 1;
|
|
|
|
carry_pos = s - sbeg;
|
|
|
|
carry_len = l;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-05-01 13:42:38 +04:00
|
|
|
if (c == -1) { /* str contains no alnum */
|
2007-09-06 07:42:12 +04:00
|
|
|
s = e;
|
2008-09-13 22:22:04 +04:00
|
|
|
while ((s = rb_enc_prev_char(sbeg, s, e, enc)) != 0) {
|
2008-01-27 11:21:24 +03:00
|
|
|
enum neighbor_char neighbor;
|
2008-09-18 16:53:25 +04:00
|
|
|
if ((l = rb_enc_precise_mbclen(s, e, enc)) <= 0) continue;
|
2008-01-27 11:21:24 +03:00
|
|
|
neighbor = enc_succ_char(s, l, enc);
|
|
|
|
if (neighbor == NEIGHBOR_FOUND)
|
|
|
|
return str;
|
2008-09-18 16:53:25 +04:00
|
|
|
if (rb_enc_precise_mbclen(s, s+l, enc) != l) {
|
2008-01-27 11:21:24 +03:00
|
|
|
/* wrapped to \0...\0. search next valid char. */
|
|
|
|
enc_succ_char(s, l, enc);
|
|
|
|
}
|
2008-01-30 08:29:37 +03:00
|
|
|
if (!rb_enc_asciicompat(enc)) {
|
|
|
|
MEMCPY(carry, s, char, l);
|
|
|
|
carry_len = l;
|
|
|
|
}
|
2008-01-27 11:21:24 +03:00
|
|
|
carry_pos = s - sbeg;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2008-01-27 13:08:58 +03:00
|
|
|
RESIZE_CAPA(str, RSTRING_LEN(str) + carry_len);
|
|
|
|
s = RSTRING_PTR(str) + carry_pos;
|
|
|
|
memmove(s + carry_len, s, RSTRING_LEN(str) - carry_pos);
|
|
|
|
memmove(s, carry, carry_len);
|
|
|
|
STR_SET_LEN(str, RSTRING_LEN(str) + carry_len);
|
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
2008-02-21 05:42:51 +03:00
|
|
|
rb_enc_str_coderange(str);
|
1998-01-16 15:19:22 +03:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.succ! -> str
|
|
|
|
* str.next! -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Equivalent to <code>String#succ</code>, but modifies the receiver in
|
|
|
|
* place.
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_succ_bang(VALUE str)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2002-09-03 09:20:14 +04:00
|
|
|
rb_str_shared_replace(str, rb_str_succ(str));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.upto(other_str, exclusive=false) {|s| block } -> str
|
|
|
|
* str.upto(other_str, exclusive=false) -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Iterates through successive values, starting at <i>str</i> and
|
|
|
|
* ending at <i>other_str</i> inclusive, passing each value in turn to
|
|
|
|
* the block. The <code>String#succ</code> method is used to generate
|
2010-05-13 09:49:55 +04:00
|
|
|
* each value. If optional second argument exclusive is omitted or is false,
|
2007-06-22 20:23:16 +04:00
|
|
|
* the last value will be included; otherwise it will be excluded.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-05-13 09:49:55 +04:00
|
|
|
* If no block is given, an enumerator is returned instead.
|
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "a8".upto("b6") {|s| print s, ' ' }
|
|
|
|
* for s in "a8".."b6"
|
|
|
|
* print s, ' '
|
|
|
|
* end
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a8 a9 b0 b1 b2 b3 b4 b5 b6
|
|
|
|
* a8 a9 b0 b1 b2 b3 b4 b5 b6
|
2009-10-06 19:58:12 +04:00
|
|
|
*
|
|
|
|
* If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
|
|
|
|
* both are recognized as decimal numbers. In addition, the width of
|
|
|
|
* string (e.g. leading zeros) is handled appropriately.
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* "9".upto("11").to_a #=> ["9", "10", "11"]
|
|
|
|
* "25".upto("5").to_a #=> []
|
|
|
|
* "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
2007-06-22 20:23:16 +04:00
|
|
|
rb_str_upto(int argc, VALUE *argv, VALUE beg)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
2007-06-22 20:23:16 +04:00
|
|
|
VALUE end, exclusive;
|
|
|
|
VALUE current, after_end;
|
|
|
|
ID succ;
|
2009-08-17 02:43:48 +04:00
|
|
|
int n, excl, ascii;
|
2008-02-11 20:46:52 +03:00
|
|
|
rb_encoding *enc;
|
2007-06-22 20:23:16 +04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &end, &exclusive);
|
2008-12-22 05:12:37 +03:00
|
|
|
RETURN_ENUMERATOR(beg, argc, argv);
|
2007-06-22 20:23:16 +04:00
|
|
|
excl = RTEST(exclusive);
|
2008-06-09 13:25:32 +04:00
|
|
|
CONST_ID(succ, "succ");
|
2007-06-22 20:23:16 +04:00
|
|
|
StringValue(end);
|
2008-02-11 20:46:52 +03:00
|
|
|
enc = rb_enc_check(beg, end);
|
2009-08-17 02:43:48 +04:00
|
|
|
ascii = (is_ascii_string(beg) && is_ascii_string(end));
|
|
|
|
/* single character */
|
|
|
|
if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 && ascii) {
|
2008-02-11 20:46:52 +03:00
|
|
|
char c = RSTRING_PTR(beg)[0];
|
|
|
|
char e = RSTRING_PTR(end)[0];
|
|
|
|
|
|
|
|
if (c > e || (excl && c == e)) return beg;
|
|
|
|
for (;;) {
|
|
|
|
rb_yield(rb_enc_str_new(&c, 1, enc));
|
|
|
|
if (!excl && c == e) break;
|
|
|
|
c++;
|
|
|
|
if (excl && c == e) break;
|
|
|
|
}
|
|
|
|
return beg;
|
|
|
|
}
|
2009-08-17 02:43:48 +04:00
|
|
|
/* both edges are all digits */
|
|
|
|
if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0])) {
|
|
|
|
char *s, *send;
|
|
|
|
VALUE b, e;
|
2009-09-23 08:14:23 +04:00
|
|
|
int width;
|
2009-08-17 02:43:48 +04:00
|
|
|
|
|
|
|
s = RSTRING_PTR(beg); send = RSTRING_END(beg);
|
2009-09-23 08:14:23 +04:00
|
|
|
width = rb_long2int(send - s);
|
2009-08-17 02:43:48 +04:00
|
|
|
while (s < send) {
|
|
|
|
if (!ISDIGIT(*s)) goto no_digits;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
s = RSTRING_PTR(end); send = RSTRING_END(end);
|
|
|
|
while (s < send) {
|
|
|
|
if (!ISDIGIT(*s)) goto no_digits;
|
|
|
|
s++;
|
|
|
|
}
|
2009-09-18 19:17:17 +04:00
|
|
|
b = rb_str_to_inum(beg, 10, FALSE);
|
|
|
|
e = rb_str_to_inum(end, 10, FALSE);
|
2009-08-17 02:43:48 +04:00
|
|
|
if (FIXNUM_P(b) && FIXNUM_P(e)) {
|
|
|
|
long bi = FIX2LONG(b);
|
|
|
|
long ei = FIX2LONG(e);
|
2009-09-23 08:14:23 +04:00
|
|
|
rb_encoding *usascii = rb_usascii_encoding();
|
2009-08-17 02:43:48 +04:00
|
|
|
|
|
|
|
while (bi <= ei) {
|
|
|
|
if (excl && bi == ei) break;
|
2009-09-23 08:14:23 +04:00
|
|
|
rb_yield(rb_enc_sprintf(usascii, "%.*ld", width, bi));
|
2009-08-17 02:43:48 +04:00
|
|
|
bi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ID op = excl ? '<' : rb_intern("<=");
|
2009-09-23 08:14:23 +04:00
|
|
|
VALUE args[2], fmt = rb_obj_freeze(rb_usascii_str_new_cstr("%.*d"));
|
2009-08-17 02:43:48 +04:00
|
|
|
|
2009-09-23 08:14:23 +04:00
|
|
|
args[0] = INT2FIX(width);
|
2009-08-17 02:43:48 +04:00
|
|
|
while (rb_funcall(b, op, 1, e)) {
|
2009-09-23 08:14:23 +04:00
|
|
|
args[1] = b;
|
|
|
|
rb_yield(rb_str_format(numberof(args), args, fmt));
|
2009-08-17 02:43:48 +04:00
|
|
|
b = rb_funcall(b, succ, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return beg;
|
|
|
|
}
|
|
|
|
/* normal case */
|
|
|
|
no_digits:
|
2007-06-22 20:23:16 +04:00
|
|
|
n = rb_str_cmp(beg, end);
|
|
|
|
if (n > 0 || (excl && n == 0)) return beg;
|
2009-02-22 17:26:27 +03:00
|
|
|
|
2007-06-22 20:23:16 +04:00
|
|
|
after_end = rb_funcall(end, succ, 0, 0);
|
2009-11-03 18:16:55 +03:00
|
|
|
current = rb_str_dup(beg);
|
2007-06-22 20:23:16 +04:00
|
|
|
while (!rb_str_equal(current, after_end)) {
|
2009-11-03 18:16:55 +03:00
|
|
|
VALUE next = Qnil;
|
|
|
|
if (excl || !rb_str_equal(current, end))
|
|
|
|
next = rb_funcall(current, succ, 0, 0);
|
2007-06-22 20:23:16 +04:00
|
|
|
rb_yield(current);
|
2009-11-03 18:16:55 +03:00
|
|
|
if (NIL_P(next)) break;
|
|
|
|
current = next;
|
2007-06-22 20:23:16 +04:00
|
|
|
StringValue(current);
|
|
|
|
if (excl && rb_str_equal(current, end)) break;
|
|
|
|
if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return beg;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
static VALUE
|
2009-03-14 21:04:21 +03:00
|
|
|
rb_str_subpat(VALUE str, VALUE re, VALUE backref)
|
2001-10-02 08:31:23 +04:00
|
|
|
{
|
|
|
|
if (rb_reg_search(re, str, 0, 0) >= 0) {
|
2009-03-14 21:04:21 +03:00
|
|
|
VALUE match = rb_backref_get();
|
|
|
|
int nth = rb_reg_backref_number(match, backref);
|
|
|
|
return rb_reg_nth_match(nth, match);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2004-10-19 03:47:32 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_aref(VALUE str, VALUE indx)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long idx;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (FIXNUM_P(indx)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
idx = FIX2LONG(indx);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
num_index:
|
* 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-25 07:29:39 +04:00
|
|
|
str = rb_str_substr(str, idx, 1);
|
|
|
|
if (!NIL_P(str) && RSTRING_LEN(str) == 0) return Qnil;
|
|
|
|
return str;
|
2012-08-02 02:29:26 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (SPECIAL_CONST_P(indx)) goto generic;
|
|
|
|
switch (BUILTIN_TYPE(indx)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case T_REGEXP:
|
2009-03-14 21:04:21 +03:00
|
|
|
return rb_str_subpat(str, indx, INT2FIX(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
case T_STRING:
|
2001-10-31 09:53:22 +03:00
|
|
|
if (rb_str_index(str, indx, 0) != -1)
|
|
|
|
return rb_str_dup(indx);
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
generic:
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
|
|
|
/* check if indx is Range */
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, len;
|
2005-09-17 18:40:06 +04:00
|
|
|
VALUE tmp;
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
len = str_strlen(str, STR_ENC_GET(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-25 07:29:39 +04:00
|
|
|
switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
return Qnil;
|
|
|
|
default:
|
2005-09-17 18:40:06 +04:00
|
|
|
tmp = rb_str_substr(str, beg, len);
|
|
|
|
return tmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-02-29 11:05:32 +03:00
|
|
|
idx = NUM2LONG(indx);
|
|
|
|
goto num_index;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2012-04-14 04:36:26 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-07-04 03:29:56 +04:00
|
|
|
* str[index] -> new_str or nil
|
|
|
|
* str[start, length] -> new_str or nil
|
|
|
|
* str[range] -> new_str or nil
|
|
|
|
* str[regexp] -> new_str or nil
|
|
|
|
* str[regexp, capture] -> new_str or nil
|
|
|
|
* str[match_str] -> new_str or nil
|
|
|
|
* str.slice(index) -> new_str or nil
|
|
|
|
* str.slice(start, length) -> new_str or nil
|
|
|
|
* str.slice(range) -> new_str or nil
|
|
|
|
* str.slice(regexp) -> new_str or nil
|
|
|
|
* str.slice(regexp, capture) -> new_str or nil
|
|
|
|
* str.slice(match_str) -> new_str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-04 03:29:56 +04:00
|
|
|
* Element Reference --- If passed a single +index+, returns a substring of
|
|
|
|
* one character at that index. If passed a +start+ index and a +length+,
|
|
|
|
* returns a substring containing +length+ characters starting at the
|
2012-07-06 01:50:13 +04:00
|
|
|
* +index+. If passed a +range+, its beginning and end are interpreted as
|
|
|
|
* offsets delimiting the substring to be returned.
|
|
|
|
*
|
|
|
|
* In these three cases, if an index is negative, it is counted from the end
|
|
|
|
* of the string. For the +start+ and +range+ cases the starting index
|
|
|
|
* is just before a character and an index matching the string's size.
|
|
|
|
* Additionally, an empty string is returned when the starting index for a
|
|
|
|
* character range is at the end of the string.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-04 03:29:56 +04:00
|
|
|
* Returns +nil+ if the initial index falls outside the string or the length
|
|
|
|
* is negative.
|
|
|
|
*
|
|
|
|
* If a +Regexp+ is supplied, the matching portion of the string is
|
|
|
|
* returned. If a +capture+ follows the regular expression, which may be a
|
|
|
|
* capture group index or name, follows the regular expression that component
|
|
|
|
* of the MatchData is returned instead.
|
|
|
|
*
|
|
|
|
* If a +match_str+ is given, that string is returned if it occurs in
|
|
|
|
* the string.
|
|
|
|
*
|
|
|
|
* Returns +nil+ if the regular expression does not match or the match string
|
|
|
|
* cannot be found.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a = "hello there"
|
2012-07-04 03:29:56 +04:00
|
|
|
*
|
* 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-10 01:20:17 +04:00
|
|
|
* a[1] #=> "e"
|
2012-03-06 08:09:04 +04:00
|
|
|
* a[2, 3] #=> "llo"
|
|
|
|
* a[2..3] #=> "ll"
|
2012-07-04 03:29:56 +04:00
|
|
|
*
|
2012-03-06 08:09:04 +04:00
|
|
|
* a[-3, 2] #=> "er"
|
|
|
|
* a[7..-2] #=> "her"
|
2003-12-25 00:14:46 +03:00
|
|
|
* a[-4..-2] #=> "her"
|
2004-07-24 13:48:21 +04:00
|
|
|
* a[-2..-4] #=> ""
|
2012-07-04 03:29:56 +04:00
|
|
|
*
|
|
|
|
* a[11, 0] #=> ""
|
|
|
|
* a[11] #=> nil
|
|
|
|
* a[12, 0] #=> nil
|
2012-03-06 08:09:04 +04:00
|
|
|
* a[12..-1] #=> nil
|
2012-07-04 03:29:56 +04:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a[/[aeiou](.)\1/] #=> "ell"
|
|
|
|
* a[/[aeiou](.)\1/, 0] #=> "ell"
|
|
|
|
* a[/[aeiou](.)\1/, 1] #=> "l"
|
|
|
|
* a[/[aeiou](.)\1/, 2] #=> nil
|
2012-07-04 03:29:56 +04:00
|
|
|
*
|
|
|
|
* a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] #=> "l"
|
|
|
|
* a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "vowel"] #=> "e"
|
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a["lo"] #=> "lo"
|
|
|
|
* a["bye"] #=> nil
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_aref_m(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-24 08:34:26 +04:00
|
|
|
if (argc == 2) {
|
2012-05-23 11:13:21 +04:00
|
|
|
if (RB_TYPE_P(argv[0], T_REGEXP)) {
|
2009-03-14 21:04:21 +03:00
|
|
|
return rb_str_subpat(str, argv[0], argv[1]);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
2002-08-21 19:47:54 +04:00
|
|
|
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
|
2000-05-24 08:34:26 +04:00
|
|
|
}
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
2000-05-24 08:34:26 +04:00
|
|
|
return rb_str_aref(str, argv[0]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-08-12 14:07:08 +04:00
|
|
|
VALUE
|
2008-08-12 14:08:48 +04:00
|
|
|
rb_str_drop_bytes(VALUE str, long len)
|
2008-08-12 14:07:08 +04:00
|
|
|
{
|
|
|
|
char *ptr = RSTRING_PTR(str);
|
|
|
|
long olen = RSTRING_LEN(str), nlen;
|
|
|
|
|
|
|
|
str_modifiable(str);
|
|
|
|
if (len > olen) len = olen;
|
|
|
|
nlen = olen - len;
|
|
|
|
if (nlen <= RSTRING_EMBED_LEN_MAX) {
|
|
|
|
char *oldptr = ptr;
|
2009-09-18 19:17:17 +04:00
|
|
|
int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|ELTS_SHARED));
|
2008-08-12 14:07:08 +04:00
|
|
|
STR_SET_EMBED(str);
|
|
|
|
STR_SET_EMBED_LEN(str, nlen);
|
|
|
|
ptr = RSTRING(str)->as.ary;
|
2008-08-15 14:34:06 +04:00
|
|
|
memmove(ptr, oldptr + len, nlen);
|
2008-08-12 14:07:08 +04:00
|
|
|
if (fl == STR_NOEMBED) xfree(oldptr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!STR_SHARED_P(str)) rb_str_new4(str);
|
|
|
|
ptr = RSTRING(str)->as.heap.ptr += len;
|
|
|
|
RSTRING(str)->as.heap.len = nlen;
|
|
|
|
}
|
|
|
|
ptr[nlen] = 0;
|
|
|
|
ENC_CODERANGE_CLEAR(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 11:49:20 +03:00
|
|
|
static void
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-08-12 14:07:08 +04:00
|
|
|
if (beg == 0 && RSTRING_LEN(val) == 0) {
|
2008-08-12 18:16:43 +04:00
|
|
|
rb_str_drop_bytes(str, len);
|
2008-08-12 14:07:08 +04:00
|
|
|
OBJ_INFECT(str, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-28 09:23:33 +04:00
|
|
|
rb_str_modify(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len < RSTRING_LEN(val)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
/* expand string */
|
2006-08-31 14:47:44 +04:00
|
|
|
RESIZE_CAPA(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len + 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(val) != len) {
|
|
|
|
memmove(RSTRING_PTR(str) + beg + RSTRING_LEN(val),
|
|
|
|
RSTRING_PTR(str) + beg + len,
|
|
|
|
RSTRING_LEN(str) - (beg + len));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
if (RSTRING_LEN(val) < beg && len < 0) {
|
2006-08-31 14:47:44 +04:00
|
|
|
MEMZERO(RSTRING_PTR(str) + RSTRING_LEN(str), char, -len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(val) > 0) {
|
|
|
|
memmove(RSTRING_PTR(str)+beg, RSTRING_PTR(val), RSTRING_LEN(val));
|
2000-03-23 11:37:35 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_LEN(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len);
|
|
|
|
if (RSTRING_PTR(str)) {
|
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
2003-11-30 03:35:28 +03:00
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
OBJ_INFECT(str, val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
static void
|
|
|
|
rb_str_splice(VALUE str, long beg, long len, VALUE val)
|
|
|
|
{
|
|
|
|
long slen;
|
|
|
|
char *p, *e;
|
|
|
|
rb_encoding *enc;
|
2007-12-31 13:39:20 +03:00
|
|
|
int singlebyte = single_byte_optimizable(str);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
int cr;
|
* 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-25 07:29:39 +04:00
|
|
|
|
|
|
|
if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
|
|
|
|
|
|
|
|
StringValue(val);
|
|
|
|
enc = rb_enc_check(str, val);
|
|
|
|
slen = str_strlen(str, enc);
|
|
|
|
|
|
|
|
if (slen < beg) {
|
|
|
|
out_of_range:
|
|
|
|
rb_raise(rb_eIndexError, "index %ld out of string", beg);
|
|
|
|
}
|
|
|
|
if (beg < 0) {
|
|
|
|
if (-beg > slen) {
|
|
|
|
goto out_of_range;
|
|
|
|
}
|
|
|
|
beg += slen;
|
|
|
|
}
|
2007-09-05 17:06:01 +04:00
|
|
|
if (slen < len || slen < beg + len) {
|
* 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-25 07:29:39 +04:00
|
|
|
len = slen - beg;
|
|
|
|
}
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2007-12-31 13:39:20 +03:00
|
|
|
p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, singlebyte);
|
2007-12-18 08:44:30 +03:00
|
|
|
if (!p) p = RSTRING_END(str);
|
2007-12-31 13:39:20 +03:00
|
|
|
e = str_nth(p, RSTRING_END(str), len, enc, singlebyte);
|
2007-12-18 08:44:30 +03:00
|
|
|
if (!e) e = RSTRING_END(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-25 07:29:39 +04:00
|
|
|
/* error check */
|
|
|
|
beg = p - RSTRING_PTR(str); /* physical position */
|
|
|
|
len = e - p; /* physical length */
|
2007-08-25 11:14:01 +04:00
|
|
|
rb_str_splice_0(str, beg, len, val);
|
2007-12-18 12:14:46 +03:00
|
|
|
rb_enc_associate(str, enc);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
cr = ENC_CODERANGE_AND(ENC_CODERANGE(str), ENC_CODERANGE(val));
|
|
|
|
if (cr != ENC_CODERANGE_BROKEN)
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 11:49:20 +03:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
rb_str_update(VALUE str, long beg, long len, VALUE val)
|
2003-12-25 11:49:20 +03:00
|
|
|
{
|
2003-12-25 16:30:39 +03:00
|
|
|
rb_str_splice(str, beg, len, val);
|
2003-12-25 11:49:20 +03:00
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
static void
|
2009-03-14 21:04:21 +03:00
|
|
|
rb_str_subpat_set(VALUE str, VALUE re, VALUE backref, VALUE val)
|
2001-10-02 08:31:23 +04:00
|
|
|
{
|
2009-03-14 21:04:21 +03:00
|
|
|
int nth;
|
2001-10-02 08:31:23 +04:00
|
|
|
VALUE match;
|
2002-08-28 12:05:23 +04:00
|
|
|
long start, end, len;
|
2007-12-18 12:14:46 +03:00
|
|
|
rb_encoding *enc;
|
2008-02-16 23:08:35 +03:00
|
|
|
struct re_registers *regs;
|
2001-10-02 08:31:23 +04:00
|
|
|
|
|
|
|
if (rb_reg_search(re, str, 0, 0) < 0) {
|
|
|
|
rb_raise(rb_eIndexError, "regexp not matched");
|
|
|
|
}
|
|
|
|
match = rb_backref_get();
|
2009-03-14 21:04:21 +03:00
|
|
|
nth = rb_reg_backref_number(match, backref);
|
2008-02-16 23:08:35 +03:00
|
|
|
regs = RMATCH_REGS(match);
|
|
|
|
if (nth >= regs->num_regs) {
|
2001-10-03 11:19:19 +04:00
|
|
|
out_of_range:
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of regexp", nth);
|
|
|
|
}
|
|
|
|
if (nth < 0) {
|
2008-02-16 23:08:35 +03:00
|
|
|
if (-nth >= regs->num_regs) {
|
2001-10-03 11:19:19 +04:00
|
|
|
goto out_of_range;
|
|
|
|
}
|
2008-02-16 23:08:35 +03:00
|
|
|
nth += regs->num_regs;
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
|
|
|
|
2008-02-16 23:08:35 +03:00
|
|
|
start = BEG(nth);
|
2001-10-02 08:31:23 +04:00
|
|
|
if (start == -1) {
|
2001-10-03 11:19:19 +04:00
|
|
|
rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
2008-02-16 23:08:35 +03:00
|
|
|
end = END(nth);
|
2001-10-02 08:31:23 +04:00
|
|
|
len = end - start;
|
2007-09-15 12:04:10 +04:00
|
|
|
StringValue(val);
|
2007-12-18 12:14:46 +03:00
|
|
|
enc = rb_enc_check(str, val);
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_splice_0(str, start, len, val);
|
2007-12-18 12:14:46 +03:00
|
|
|
rb_enc_associate(str, enc);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_aset(VALUE str, VALUE indx, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-29 11:05:32 +03:00
|
|
|
long idx, beg;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (FIXNUM_P(indx)) {
|
2004-03-15 05:27:29 +03:00
|
|
|
idx = FIX2LONG(indx);
|
2006-12-11 03:45:32 +03:00
|
|
|
num_index:
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_splice(str, idx, 1, val);
|
1998-01-16 15:13:05 +03:00
|
|
|
return val;
|
2012-08-02 02:29:26 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
if (SPECIAL_CONST_P(indx)) goto generic;
|
|
|
|
switch (TYPE(indx)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case T_REGEXP:
|
2009-03-14 21:04:21 +03:00
|
|
|
rb_str_subpat_set(str, indx, INT2FIX(0), val);
|
1998-01-16 15:13:05 +03:00
|
|
|
return val;
|
|
|
|
|
|
|
|
case T_STRING:
|
1999-01-20 07:59:39 +03:00
|
|
|
beg = rb_str_index(str, indx, 0);
|
2002-06-04 11:34:19 +04:00
|
|
|
if (beg < 0) {
|
|
|
|
rb_raise(rb_eIndexError, "string not matched");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
beg = rb_str_sublen(str, beg);
|
|
|
|
rb_str_splice(str, beg, str_strlen(indx, 0), val);
|
1998-01-16 15:13:05 +03:00
|
|
|
return val;
|
|
|
|
|
2012-08-02 02:29:26 +04:00
|
|
|
generic:
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
|
|
|
/* check if indx is Range */
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, len;
|
* 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-25 07:29:39 +04:00
|
|
|
if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, 0), 2)) {
|
2003-12-25 11:49:20 +03:00
|
|
|
rb_str_splice(str, beg, len, val);
|
1998-01-16 15:13:05 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2000-02-29 11:05:32 +03:00
|
|
|
idx = NUM2LONG(indx);
|
|
|
|
goto num_index;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* str[fixnum] = new_str
|
|
|
|
* str[fixnum, fixnum] = new_str
|
|
|
|
* str[range] = aString
|
|
|
|
* str[regexp] = new_str
|
|
|
|
* str[regexp, fixnum] = new_str
|
2009-03-14 21:04:21 +03:00
|
|
|
* str[regexp, name] = new_str
|
2003-12-25 00:14:46 +03:00
|
|
|
* str[other_str] = new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Element Assignment---Replaces some or all of the content of <i>str</i>. The
|
|
|
|
* portion of the string affected is determined using the same criteria as
|
|
|
|
* <code>String#[]</code>. If the replacement string is not the same length as
|
|
|
|
* the text it is replacing, the string will be adjusted accordingly. If the
|
|
|
|
* regular expression or string is used as the index doesn't match a position
|
|
|
|
* in the string, <code>IndexError</code> is raised. If the regular expression
|
|
|
|
* form is used, the optional second <code>Fixnum</code> allows you to specify
|
|
|
|
* which portion of the match to replace (effectively using the
|
|
|
|
* <code>MatchData</code> indexing rules. The forms that take a
|
|
|
|
* <code>Fixnum</code> will raise an <code>IndexError</code> if the value is
|
|
|
|
* out of range; the <code>Range</code> form will raise a
|
|
|
|
* <code>RangeError</code>, and the <code>Regexp</code> and <code>String</code>
|
|
|
|
* forms will silently ignore the assignment.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_aset_m(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-25 06:51:23 +03:00
|
|
|
if (argc == 3) {
|
2012-05-23 11:13:21 +04:00
|
|
|
if (RB_TYPE_P(argv[0], T_REGEXP)) {
|
2009-03-14 21:04:21 +03:00
|
|
|
rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
|
|
|
else {
|
2003-12-25 11:49:20 +03:00
|
|
|
rb_str_splice(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
|
2001-10-02 08:31:23 +04:00
|
|
|
}
|
2000-02-25 06:51:23 +03:00
|
|
|
return argv[2];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 2, 3);
|
2000-02-25 06:51:23 +03:00
|
|
|
return rb_str_aset(str, argv[0], argv[1]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.insert(index, other_str) -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Inserts <i>other_str</i> before the character at the given
|
|
|
|
* <i>index</i>, modifying <i>str</i>. Negative indices count from the
|
|
|
|
* end of the string, and insert <em>after</em> the given character.
|
|
|
|
* The intent is insert <i>aString</i> so that it starts at the given
|
|
|
|
* <i>index</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "abcd".insert(0, 'X') #=> "Xabcd"
|
|
|
|
* "abcd".insert(3, 'X') #=> "abcXd"
|
|
|
|
* "abcd".insert(4, 'X') #=> "abcdX"
|
|
|
|
* "abcd".insert(-3, 'X') #=> "abXcd"
|
|
|
|
* "abcd".insert(-1, 'X') #=> "abcdX"
|
|
|
|
*/
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_insert(VALUE str, VALUE idx, VALUE str2)
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
long pos = NUM2LONG(idx);
|
|
|
|
|
|
|
|
if (pos == -1) {
|
2007-12-10 10:52:19 +03:00
|
|
|
return rb_str_append(str, str2);
|
2001-05-06 19:06:00 +04:00
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos++;
|
|
|
|
}
|
2003-12-25 11:49:20 +03:00
|
|
|
rb_str_splice(str, pos, 0, str2);
|
2001-05-02 08:22:21 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.slice!(fixnum) -> fixnum or nil
|
|
|
|
* str.slice!(fixnum, fixnum) -> new_str or nil
|
|
|
|
* str.slice!(range) -> new_str or nil
|
|
|
|
* str.slice!(regexp) -> new_str or nil
|
|
|
|
* str.slice!(other_str) -> new_str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Deletes the specified portion from <i>str</i>, and returns the portion
|
2006-12-21 19:41:17 +03:00
|
|
|
* deleted.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* string = "this is a string"
|
2008-03-09 04:04:46 +03:00
|
|
|
* string.slice!(2) #=> "i"
|
2003-12-25 00:14:46 +03:00
|
|
|
* string.slice!(3..6) #=> " is "
|
|
|
|
* string.slice!(/s.*t/) #=> "sa st"
|
|
|
|
* string.slice!("r") #=> "r"
|
|
|
|
* string #=> "thing"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-05-24 08:34:26 +04:00
|
|
|
VALUE result;
|
|
|
|
VALUE buf[3];
|
|
|
|
int i;
|
2000-02-01 06:12:21 +03:00
|
|
|
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
2000-05-24 08:34:26 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
buf[i] = argv[i];
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
2011-03-03 12:59:51 +03:00
|
|
|
str_modify_keep_cr(str);
|
2000-06-05 12:46:59 +04:00
|
|
|
result = rb_str_aref_m(argc, buf, str);
|
2002-07-11 12:22:18 +04:00
|
|
|
if (!NIL_P(result)) {
|
2011-03-02 08:09:37 +03:00
|
|
|
buf[i] = rb_str_new(0,0);
|
2002-07-11 12:22:18 +04:00
|
|
|
rb_str_aset_m(argc+1, buf, str);
|
|
|
|
}
|
2000-05-24 08:34:26 +04:00
|
|
|
return result;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
get_pat(VALUE pat, int quote)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-07-26 10:12:39 +04:00
|
|
|
VALUE val;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
switch (TYPE(pat)) {
|
|
|
|
case T_REGEXP:
|
2002-07-26 10:12:39 +04:00
|
|
|
return pat;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
default:
|
2002-12-19 12:20:20 +03:00
|
|
|
val = rb_check_string_type(pat);
|
2002-07-26 10:12:39 +04:00
|
|
|
if (NIL_P(val)) {
|
|
|
|
Check_Type(pat, T_REGEXP);
|
|
|
|
}
|
|
|
|
pat = val;
|
|
|
|
}
|
2002-09-11 08:05:36 +04:00
|
|
|
|
|
|
|
if (quote) {
|
2003-11-04 12:13:57 +03:00
|
|
|
pat = rb_reg_quote(pat);
|
2002-09-11 08:05:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rb_reg_regcomp(pat);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.sub!(pattern, replacement) -> str or nil
|
|
|
|
* str.sub!(pattern) {|match| block } -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-24 02:03:30 +04:00
|
|
|
* Performs the same substitution as String#sub in-place.
|
|
|
|
*
|
|
|
|
* Returns +str+ if a substitution was performed or +nil+ if no substitution
|
|
|
|
* was performed.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-06-19 21:11:55 +04:00
|
|
|
VALUE pat, repl, hash = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
int iter = 0;
|
2000-06-16 11:44:16 +04:00
|
|
|
int tainted = 0;
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 11:25:05 +04:00
|
|
|
int untrusted = 0;
|
2000-02-29 11:05:32 +03:00
|
|
|
long plen;
|
2012-03-15 01:10:34 +04:00
|
|
|
int min_arity = rb_block_given_p() ? 1 : 2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, min_arity, 2);
|
|
|
|
if (argc == 1) {
|
1999-01-20 07:59:39 +03:00
|
|
|
iter = 1;
|
|
|
|
}
|
2012-03-15 01:10:34 +04:00
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
repl = argv[1];
|
2012-06-26 11:46:24 +04:00
|
|
|
hash = rb_check_hash_type(argv[1]);
|
2008-02-15 12:23:55 +03:00
|
|
|
if (NIL_P(hash)) {
|
|
|
|
StringValue(repl);
|
|
|
|
}
|
2000-06-16 11:44:16 +04:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 11:25:05 +04:00
|
|
|
if (OBJ_UNTRUSTED(repl)) untrusted = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-09-11 08:05:36 +04:00
|
|
|
pat = get_pat(argv[0], 1);
|
2010-04-30 06:48:49 +04:00
|
|
|
str_modifiable(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_reg_search(pat, str, 0, 0) >= 0) {
|
2007-12-19 15:42:19 +03:00
|
|
|
rb_encoding *enc;
|
2007-12-29 16:44:32 +03:00
|
|
|
int cr = ENC_CODERANGE(str);
|
2008-06-19 21:11:55 +04:00
|
|
|
VALUE match = rb_backref_get();
|
|
|
|
struct re_registers *regs = RMATCH_REGS(match);
|
2008-06-20 07:14:23 +04:00
|
|
|
long beg0 = BEG(0);
|
|
|
|
long end0 = END(0);
|
2009-10-29 22:44:58 +03:00
|
|
|
char *p, *rp;
|
|
|
|
long len, rlen;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-02-16 07:17:52 +03:00
|
|
|
if (iter || !NIL_P(hash)) {
|
2009-10-29 22:44:58 +03:00
|
|
|
p = RSTRING_PTR(str); len = RSTRING_LEN(str);
|
2004-09-24 09:53:43 +04:00
|
|
|
|
2008-02-16 07:17:52 +03:00
|
|
|
if (iter) {
|
|
|
|
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
|
|
|
}
|
|
|
|
else {
|
2008-06-20 07:14:23 +04:00
|
|
|
repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
|
2008-02-16 07:17:52 +03:00
|
|
|
repl = rb_obj_as_string(repl);
|
|
|
|
}
|
2004-11-14 18:41:40 +03:00
|
|
|
str_mod_check(str, p, len);
|
* array.c, gc.c, hash.c, object.c, string.c, struct.c,
transcode.c, variable.c, vm.c, vm_insnhelper.c, vm_method.c:
replace calls to rb_error_frozen() with rb_check_frozen(). a
patch from Run Paint Run Run at [ruby-core:32014]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-24 12:14:05 +04:00
|
|
|
rb_check_frozen(str);
|
2008-02-15 12:23:55 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2006-03-22 18:03:40 +03:00
|
|
|
repl = rb_reg_regsub(repl, str, regs, pat);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2008-01-06 12:25:09 +03:00
|
|
|
enc = rb_enc_compatible(str, repl);
|
|
|
|
if (!enc) {
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *str_enc = STR_ENC_GET(str);
|
2009-10-29 22:44:58 +03:00
|
|
|
p = RSTRING_PTR(str); len = RSTRING_LEN(str);
|
|
|
|
if (coderange_scan(p, beg0, str_enc) != ENC_CODERANGE_7BIT ||
|
|
|
|
coderange_scan(p+end0, len-end0, str_enc) != ENC_CODERANGE_7BIT) {
|
2008-08-12 22:17:05 +04:00
|
|
|
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
2008-02-15 11:14:40 +03:00
|
|
|
rb_enc_name(str_enc),
|
|
|
|
rb_enc_name(STR_ENC_GET(repl)));
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(repl);
|
2008-01-06 12:25:09 +03:00
|
|
|
}
|
2007-01-30 11:02:30 +03:00
|
|
|
rb_str_modify(str);
|
2007-12-19 15:42:19 +03:00
|
|
|
rb_enc_associate(str, enc);
|
2000-06-16 11:44:16 +04:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 11:25:05 +04:00
|
|
|
if (OBJ_UNTRUSTED(repl)) untrusted = 1;
|
2007-12-29 16:44:32 +03:00
|
|
|
if (ENC_CODERANGE_UNKNOWN < cr && cr < ENC_CODERANGE_BROKEN) {
|
|
|
|
int cr2 = ENC_CODERANGE(repl);
|
2008-10-02 15:46:40 +04:00
|
|
|
if (cr2 == ENC_CODERANGE_BROKEN ||
|
|
|
|
(cr == ENC_CODERANGE_VALID && cr2 == ENC_CODERANGE_7BIT))
|
|
|
|
cr = ENC_CODERANGE_UNKNOWN;
|
|
|
|
else
|
|
|
|
cr = cr2;
|
2007-12-29 16:44:32 +03:00
|
|
|
}
|
2008-06-20 07:14:23 +04:00
|
|
|
plen = end0 - beg0;
|
2009-10-29 22:44:58 +03:00
|
|
|
rp = RSTRING_PTR(repl); rlen = RSTRING_LEN(repl);
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
if (rlen > plen) {
|
|
|
|
RESIZE_CAPA(str, len + rlen - plen);
|
|
|
|
}
|
|
|
|
p = RSTRING_PTR(str);
|
|
|
|
if (rlen != plen) {
|
|
|
|
memmove(p + beg0 + rlen, p + beg0 + plen, len - beg0 - plen);
|
|
|
|
}
|
|
|
|
memcpy(p + beg0, rp, rlen);
|
|
|
|
len += rlen - plen;
|
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
2007-12-29 16:44:32 +03:00
|
|
|
ENC_CODERANGE_SET(str, cr);
|
2000-06-16 11:44:16 +04:00
|
|
|
if (tainted) OBJ_TAINT(str);
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 11:25:05 +04:00
|
|
|
if (untrusted) OBJ_UNTRUST(str);
|
2000-06-16 11:44:16 +04:00
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.sub(pattern, replacement) -> new_str
|
2010-07-01 02:14:38 +04:00
|
|
|
* str.sub(pattern, hash) -> new_str
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.sub(pattern) {|match| block } -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-24 02:03:30 +04:00
|
|
|
* Returns a copy of +str+ with the _first_ occurrence of +pattern+
|
|
|
|
* replaced by the second argument. The +pattern+ is typically a Regexp; if
|
|
|
|
* given as a String, any regular expression metacharacters it contains will
|
|
|
|
* be interpreted literally, e.g. <code>'\\\d'</code> will match a backlash
|
|
|
|
* followed by 'd', instead of a digit.
|
|
|
|
*
|
|
|
|
* If +replacement+ is a String it will be substituted for the matched text.
|
|
|
|
* It may contain back-references to the pattern's capture groups of the form
|
|
|
|
* <code>"\\d"</code>, where <i>d</i> is a group number, or
|
|
|
|
* <code>"\\k<n>"</code>, where <i>n</i> is a group name. If it is a
|
2010-07-01 02:14:38 +04:00
|
|
|
* double-quoted string, both back-references must be preceded by an
|
2012-07-24 02:03:30 +04:00
|
|
|
* additional backslash. However, within +replacement+ the special match
|
2010-07-01 02:14:38 +04:00
|
|
|
* variables, such as <code>&$</code>, will not refer to the current match.
|
|
|
|
*
|
2012-07-24 02:03:30 +04:00
|
|
|
* If the second argument is a Hash, and the matched text is one of its keys,
|
|
|
|
* the corresponding value is the replacement string.
|
2010-07-01 02:14:38 +04:00
|
|
|
*
|
|
|
|
* In the block form, the current match string is passed in as a parameter,
|
|
|
|
* and variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
|
2003-12-25 00:14:46 +03:00
|
|
|
* <code>$&</code>, and <code>$'</code> will be set appropriately. The value
|
|
|
|
* returned by the block will be substituted for the match on each call.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* The result inherits any tainting in the original string or any supplied
|
|
|
|
* replacement string.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-03-24 15:14:18 +03:00
|
|
|
* "hello".sub(/[aeiou]/, '*') #=> "h*llo"
|
|
|
|
* "hello".sub(/([aeiou])/, '<\1>') #=> "h<e>llo"
|
2010-07-01 02:14:38 +04:00
|
|
|
* "hello".sub(/./) {|s| s.ord.to_s + ' ' } #=> "104 ello"
|
2006-03-24 15:14:18 +03:00
|
|
|
* "hello".sub(/(?<foo>[aeiou])/, '*\k<foo>*') #=> "h*e*llo"
|
2010-07-01 02:14:38 +04:00
|
|
|
* 'Is SHELL your preferred shell?'.sub(/[[:upper:]]{2,}/, ENV)
|
|
|
|
* #=> "Is /bin/bash your preferred shell?"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_sub(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_sub_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static 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 14:44:21 +04:00
|
|
|
str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-02-15 12:23:55 +03:00
|
|
|
VALUE pat, val, repl, match, dest, hash = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
struct re_registers *regs;
|
2000-02-29 11:05:32 +03:00
|
|
|
long beg, n;
|
2008-06-19 21:11:55 +04:00
|
|
|
long beg0, end0;
|
|
|
|
long offset, blen, slen, len, last;
|
1999-01-20 07:59:39 +03:00
|
|
|
int iter = 0;
|
2008-01-06 12:25:09 +03:00
|
|
|
char *sp, *cp;
|
2000-01-05 07:41:21 +03:00
|
|
|
int tainted = 0;
|
2008-01-07 12:11:28 +03:00
|
|
|
rb_encoding *str_enc;
|
2009-02-22 17:26:27 +03:00
|
|
|
|
2005-08-30 18:49:51 +04:00
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
RETURN_ENUMERATOR(str, argc, argv);
|
1999-01-20 07:59:39 +03:00
|
|
|
iter = 1;
|
2005-08-30 18:49:51 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
2001-05-02 08:22:21 +04:00
|
|
|
repl = argv[1];
|
2012-06-26 11:46:24 +04:00
|
|
|
hash = rb_check_hash_type(argv[1]);
|
2008-02-15 12:23:55 +03:00
|
|
|
if (NIL_P(hash)) {
|
|
|
|
StringValue(repl);
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
2005-08-30 18:49:51 +04:00
|
|
|
break;
|
|
|
|
default:
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2002-09-11 08:05:36 +04:00
|
|
|
pat = get_pat(argv[0], 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
beg = rb_reg_search(pat, str, 0, 0);
|
2000-03-13 10:18:45 +03:00
|
|
|
if (beg < 0) {
|
|
|
|
if (bang) return Qnil; /* no match, no substitution */
|
|
|
|
return rb_str_dup(str);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-06-19 21:11:55 +04:00
|
|
|
offset = 0;
|
|
|
|
n = 0;
|
2006-08-31 14:47:44 +04:00
|
|
|
blen = RSTRING_LEN(str) + 30; /* len + margin */
|
2008-01-06 12:25:09 +03:00
|
|
|
dest = rb_str_buf_new(blen);
|
|
|
|
sp = RSTRING_PTR(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
slen = RSTRING_LEN(str);
|
2008-01-06 12:25:09 +03:00
|
|
|
cp = sp;
|
2008-01-07 19:28:43 +03:00
|
|
|
str_enc = STR_ENC_GET(str);
|
2011-02-06 06:54:50 +03:00
|
|
|
rb_enc_associate(dest, str_enc);
|
2011-02-15 10:08:12 +03:00
|
|
|
ENC_CODERANGE_SET(dest, rb_enc_asciicompat(str_enc) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2007-08-25 11:14:01 +04:00
|
|
|
do {
|
1999-01-20 07:59:39 +03:00
|
|
|
n++;
|
|
|
|
match = rb_backref_get();
|
2008-02-16 23:08:35 +03:00
|
|
|
regs = RMATCH_REGS(match);
|
2008-06-19 21:11:55 +04:00
|
|
|
beg0 = BEG(0);
|
|
|
|
end0 = END(0);
|
2008-02-16 07:17:52 +03:00
|
|
|
if (iter || !NIL_P(hash)) {
|
|
|
|
if (iter) {
|
|
|
|
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val = rb_hash_aref(hash, rb_str_subseq(str, BEG(0), END(0) - BEG(0)));
|
|
|
|
val = rb_obj_as_string(val);
|
|
|
|
}
|
2004-11-04 15:27:57 +03:00
|
|
|
str_mod_check(str, sp, slen);
|
2007-12-31 08:52:59 +03:00
|
|
|
if (val == dest) { /* paranoid check [ruby-dev:24827] */
|
2004-11-14 18:41:40 +03:00
|
|
|
rb_raise(rb_eRuntimeError, "block should not cheat");
|
|
|
|
}
|
2008-02-15 12:23:55 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2006-03-22 18:03:40 +03:00
|
|
|
val = rb_reg_regsub(repl, str, regs, pat);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2008-01-06 12:25:09 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
if (OBJ_TAINTED(val)) tainted = 1;
|
2008-01-06 12:25:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
len = beg - offset; /* copy pre-match substr */
|
2008-01-08 09:04:26 +03:00
|
|
|
if (len) {
|
|
|
|
rb_enc_str_buf_cat(dest, cp, len, str_enc);
|
|
|
|
}
|
2008-01-06 12:25:09 +03:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_str_buf_append(dest, val);
|
2008-01-06 12:25:09 +03:00
|
|
|
|
2008-06-19 21:11:55 +04:00
|
|
|
last = offset;
|
|
|
|
offset = end0;
|
|
|
|
if (beg0 == end0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
/*
|
|
|
|
* Always consume at least one character of the input string
|
|
|
|
* in order to prevent infinite loops.
|
|
|
|
*/
|
2008-06-19 21:11:55 +04:00
|
|
|
if (RSTRING_LEN(str) <= end0) break;
|
2009-05-20 08:44:36 +04:00
|
|
|
len = rb_enc_fast_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
|
2008-06-19 21:11:55 +04:00
|
|
|
rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+end0, len, str_enc);
|
|
|
|
offset = end0 + len;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
cp = RSTRING_PTR(str) + offset;
|
|
|
|
if (offset > RSTRING_LEN(str)) break;
|
1999-01-20 07:59:39 +03:00
|
|
|
beg = rb_reg_search(pat, str, offset, 0);
|
2007-08-25 11:14:01 +04:00
|
|
|
} while (beg >= 0);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > offset) {
|
2008-01-07 12:11:28 +03:00
|
|
|
rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-06-19 21:11:55 +04:00
|
|
|
rb_reg_search(pat, str, last, 0);
|
2000-03-13 10:18:45 +03:00
|
|
|
if (bang) {
|
2008-01-06 12:25:09 +03:00
|
|
|
rb_str_shared_replace(str, dest);
|
2000-03-13 10:18:45 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-11-19 19:59:11 +03:00
|
|
|
RBASIC(dest)->klass = rb_obj_class(str);
|
2004-10-27 04:18:08 +04:00
|
|
|
OBJ_INFECT(dest, str);
|
|
|
|
str = dest;
|
2000-03-13 10:18:45 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2000-03-13 10:18:45 +03:00
|
|
|
if (tainted) OBJ_TAINT(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.gsub!(pattern, replacement) -> str or nil
|
|
|
|
* str.gsub!(pattern) {|match| block } -> str or nil
|
2010-07-01 02:14:38 +04:00
|
|
|
* str.gsub!(pattern) -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Performs the substitutions of <code>String#gsub</code> in place, returning
|
|
|
|
* <i>str</i>, or <code>nil</code> if no substitutions were performed.
|
2010-07-01 02:14:38 +04:00
|
|
|
* If no block and no <i>replacement</i> is given, an enumerator is returned instead.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-03-13 10:18:45 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_gsub_bang(int argc, VALUE *argv, VALUE str)
|
2000-03-13 10:18:45 +03:00
|
|
|
{
|
2009-06-02 19:24:23 +04:00
|
|
|
str_modify_keep_cr(str);
|
2000-03-13 10:18:45 +03:00
|
|
|
return str_gsub(argc, argv, str, 1);
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.gsub(pattern, replacement) -> new_str
|
2010-07-01 02:14:38 +04:00
|
|
|
* str.gsub(pattern, hash) -> new_str
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.gsub(pattern) {|match| block } -> new_str
|
2010-07-01 02:14:38 +04:00
|
|
|
* str.gsub(pattern) -> enumerator
|
|
|
|
*
|
|
|
|
* Returns a copy of <i>str</i> with the <em>all</em> occurrences of
|
|
|
|
* <i>pattern</i> substituted for the second argument. The <i>pattern</i> is
|
|
|
|
* typically a <code>Regexp</code>; if given as a <code>String</code>, any
|
|
|
|
* regular expression metacharacters it contains will be interpreted
|
|
|
|
* literally, e.g. <code>'\\\d'</code> will match a backlash followed by 'd',
|
|
|
|
* instead of a digit.
|
|
|
|
*
|
|
|
|
* If <i>replacement</i> is a <code>String</code> it will be substituted for
|
|
|
|
* the matched text. It may contain back-references to the pattern's capture
|
|
|
|
* groups of the form <code>\\\d</code>, where <i>d</i> is a group number, or
|
|
|
|
* <code>\\\k<n></code>, where <i>n</i> is a group name. If it is a
|
|
|
|
* double-quoted string, both back-references must be preceded by an
|
|
|
|
* additional backslash. However, within <i>replacement</i> the special match
|
|
|
|
* variables, such as <code>&$</code>, will not refer to the current match.
|
|
|
|
*
|
|
|
|
* If the second argument is a <code>Hash</code>, and the matched text is one
|
|
|
|
* of its keys, the corresponding value is the replacement string.
|
|
|
|
*
|
|
|
|
* In the block form, the current match string is passed in as a parameter,
|
|
|
|
* and variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
|
2003-12-25 00:14:46 +03:00
|
|
|
* <code>$&</code>, and <code>$'</code> will be set appropriately. The value
|
|
|
|
* returned by the block will be substituted for the match on each call.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* The result inherits any tainting in the original string or any supplied
|
|
|
|
* replacement string.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-07-01 02:14:38 +04:00
|
|
|
* When neither a block nor a second argument is supplied, an
|
|
|
|
* <code>Enumerator</code> is returned.
|
|
|
|
*
|
2006-03-24 15:14:18 +03:00
|
|
|
* "hello".gsub(/[aeiou]/, '*') #=> "h*ll*"
|
|
|
|
* "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>"
|
2010-07-01 02:14:38 +04:00
|
|
|
* "hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101 108 108 111 "
|
2006-03-24 15:14:18 +03:00
|
|
|
* "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}') #=> "h{e}ll{o}"
|
2010-07-01 02:14:38 +04:00
|
|
|
* 'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_gsub(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-03-13 10:18:45 +03:00
|
|
|
return str_gsub(argc, argv, str, 0);
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.replace(other_str) -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Replaces the contents and taintedness of <i>str</i> with the corresponding
|
|
|
|
* values in <i>other_str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* s = "hello" #=> "hello"
|
|
|
|
* s.replace "world" #=> "world"
|
|
|
|
*/
|
|
|
|
|
2009-02-12 13:42:36 +03: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 14:44:21 +04:00
|
|
|
rb_str_replace(VALUE str, VALUE str2)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2010-02-10 10:31:06 +03:00
|
|
|
str_modifiable(str);
|
2000-12-26 11:08:50 +03:00
|
|
|
if (str == str2) return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str2);
|
2009-02-17 06:50:59 +03:00
|
|
|
str_discard(str);
|
2009-05-25 04:43:23 +04:00
|
|
|
return str_replace(str, str2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-10-19 03:47:32 +04:00
|
|
|
/*
|
2004-08-19 11:33:15 +04:00
|
|
|
* call-seq:
|
|
|
|
* string.clear -> string
|
|
|
|
*
|
|
|
|
* Makes string empty.
|
|
|
|
*
|
|
|
|
* a = "abcde"
|
|
|
|
* a.clear #=> ""
|
|
|
|
*/
|
|
|
|
|
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_clear(VALUE str)
|
2004-08-19 11:33:15 +04:00
|
|
|
{
|
2009-02-17 06:50:59 +03:00
|
|
|
str_discard(str);
|
2006-09-02 18:42:08 +04:00
|
|
|
STR_SET_EMBED(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_EMBED_LEN(str, 0);
|
|
|
|
RSTRING_PTR(str)[0] = 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (rb_enc_asciicompat(STR_ENC_GET(str)))
|
|
|
|
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
|
|
|
|
else
|
|
|
|
ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
|
2004-08-19 11:33:15 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
* 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-10 01:20:17 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* string.chr -> string
|
|
|
|
*
|
|
|
|
* Returns a one-character string at the beginning of the string.
|
|
|
|
*
|
|
|
|
* a = "abcde"
|
|
|
|
* a.chr #=> "a"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_chr(VALUE str)
|
|
|
|
{
|
|
|
|
return rb_str_substr(str, 0, 1);
|
|
|
|
}
|
|
|
|
|
2008-02-15 09:35:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.getbyte(index) -> 0 .. 255
|
2008-02-15 09:35:20 +03:00
|
|
|
*
|
|
|
|
* returns the <i>index</i>th byte as an integer.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_str_getbyte(VALUE str, VALUE index)
|
|
|
|
{
|
|
|
|
long pos = NUM2LONG(index);
|
|
|
|
|
|
|
|
if (pos < 0)
|
|
|
|
pos += RSTRING_LEN(str);
|
|
|
|
if (pos < 0 || RSTRING_LEN(str) <= pos)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
return INT2FIX((unsigned char)RSTRING_PTR(str)[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.setbyte(index, int) -> int
|
2008-02-15 09:35:20 +03:00
|
|
|
*
|
|
|
|
* modifies the <i>index</i>th byte as <i>int</i>.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_str_setbyte(VALUE str, VALUE index, VALUE value)
|
|
|
|
{
|
|
|
|
long pos = NUM2LONG(index);
|
|
|
|
int byte = NUM2INT(value);
|
|
|
|
|
|
|
|
rb_str_modify(str);
|
|
|
|
|
|
|
|
if (pos < -RSTRING_LEN(str) || RSTRING_LEN(str) <= pos)
|
|
|
|
rb_raise(rb_eIndexError, "index %ld out of string", pos);
|
|
|
|
if (pos < 0)
|
|
|
|
pos += RSTRING_LEN(str);
|
|
|
|
|
|
|
|
RSTRING_PTR(str)[pos] = byte;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2011-03-01 04:35:17 +03:00
|
|
|
static VALUE
|
|
|
|
str_byte_substr(VALUE str, long beg, long len)
|
|
|
|
{
|
2011-03-02 17:20:06 +03:00
|
|
|
char *p, *s = RSTRING_PTR(str);
|
|
|
|
long n = RSTRING_LEN(str);
|
|
|
|
VALUE str2;
|
2011-03-01 04:35:17 +03:00
|
|
|
|
2011-03-02 17:20:06 +03:00
|
|
|
if (beg > n || len < 0) return Qnil;
|
|
|
|
if (beg < 0) {
|
|
|
|
beg += n;
|
|
|
|
if (beg < 0) return Qnil;
|
|
|
|
}
|
|
|
|
if (beg + len > n)
|
|
|
|
len = n - beg;
|
|
|
|
if (len <= 0) {
|
|
|
|
len = 0;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p = s + beg;
|
2011-03-01 04:35:17 +03:00
|
|
|
|
2011-03-02 17:20:06 +03:00
|
|
|
if (len > RSTRING_EMBED_LEN_MAX && beg + len == n) {
|
|
|
|
str2 = rb_str_new4(str);
|
|
|
|
str2 = str_new3(rb_obj_class(str2), str2);
|
|
|
|
RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len;
|
|
|
|
RSTRING(str2)->as.heap.len = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = rb_str_new5(str, p, len);
|
|
|
|
rb_enc_cr_str_copy_for_substr(str2, str);
|
|
|
|
OBJ_INFECT(str2, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str2;
|
2011-03-01 04:35:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
str_byte_aref(VALUE str, VALUE indx)
|
|
|
|
{
|
|
|
|
long idx;
|
|
|
|
switch (TYPE(indx)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
idx = FIX2LONG(indx);
|
|
|
|
|
|
|
|
num_index:
|
|
|
|
str = str_byte_substr(str, idx, 1);
|
2011-03-01 10:02:08 +03:00
|
|
|
if (NIL_P(str) || RSTRING_LEN(str) == 0) return Qnil;
|
2011-03-01 04:35:17 +03:00
|
|
|
return str;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* check if indx is Range */
|
|
|
|
{
|
|
|
|
long beg, len = RSTRING_LEN(str);
|
|
|
|
|
|
|
|
switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
|
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
return Qnil;
|
|
|
|
default:
|
2011-03-01 10:02:08 +03:00
|
|
|
return str_byte_substr(str, beg, len);
|
2011-03-01 04:35:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
idx = NUM2LONG(indx);
|
|
|
|
goto num_index;
|
|
|
|
}
|
2012-04-14 04:36:26 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2011-03-01 04:35:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* str.byteslice(fixnum) -> new_str or nil
|
|
|
|
* str.byteslice(fixnum, fixnum) -> new_str or nil
|
|
|
|
* str.byteslice(range) -> new_str or nil
|
|
|
|
*
|
|
|
|
* Byte Reference---If passed a single <code>Fixnum</code>, returns a
|
|
|
|
* substring of one byte at that position. If passed two <code>Fixnum</code>
|
|
|
|
* objects, returns a substring starting at the offset given by the first, and
|
2011-03-01 07:28:58 +03:00
|
|
|
* a length given by the second. If given a <code>Range</code>, a substring containing
|
2011-03-01 04:35:17 +03:00
|
|
|
* bytes at offsets given by the range is returned. In all three cases, if
|
|
|
|
* an offset is negative, it is counted from the end of <i>str</i>. Returns
|
|
|
|
* <code>nil</code> if the initial offset falls outside the string, the length
|
|
|
|
* is negative, or the beginning of the range is greater than the end.
|
2011-03-01 10:02:08 +03:00
|
|
|
* The encoding of the resulted string keeps original encoding.
|
2011-03-01 04:35:17 +03:00
|
|
|
*
|
|
|
|
* "hello".byteslice(1) #=> "e"
|
|
|
|
* "hello".byteslice(-1) #=> "o"
|
|
|
|
* "hello".byteslice(1, 2) #=> "el"
|
2011-03-02 08:09:37 +03:00
|
|
|
* "\x80\u3042".byteslice(1, 3) #=> "\u3042"
|
2012-03-05 06:53:36 +04:00
|
|
|
* "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
|
2011-03-01 04:35:17 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_byteslice(int argc, VALUE *argv, VALUE str)
|
|
|
|
{
|
|
|
|
if (argc == 2) {
|
|
|
|
return str_byte_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
|
|
|
|
}
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
2011-03-01 04:35:17 +03:00
|
|
|
return str_byte_aref(str, argv[0]);
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.reverse -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* Returns a new string with the characters from <i>str</i> in reverse order.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* "stressed".reverse #=> "desserts"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_reverse(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
2008-05-20 11:59:33 +04:00
|
|
|
VALUE rev;
|
* 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-25 07:29:39 +04:00
|
|
|
char *s, *e, *p;
|
2008-05-20 11:59:33 +04:00
|
|
|
int single = 1;
|
* 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-25 07:29:39 +04:00
|
|
|
|
|
|
|
if (RSTRING_LEN(str) <= 1) return rb_str_dup(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-05-20 11:59:33 +04:00
|
|
|
rev = rb_str_new5(str, 0, RSTRING_LEN(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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); e = RSTRING_END(str);
|
2008-05-20 11:59:33 +04:00
|
|
|
p = RSTRING_END(rev);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > 1) {
|
2008-02-23 14:18:07 +03:00
|
|
|
if (single_byte_optimizable(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-25 07:29:39 +04:00
|
|
|
while (s < e) {
|
|
|
|
*--p = *s++;
|
|
|
|
}
|
|
|
|
}
|
2009-05-20 08:44:36 +04:00
|
|
|
else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID) {
|
|
|
|
while (s < e) {
|
|
|
|
int clen = rb_enc_fast_mbclen(s, e, enc);
|
|
|
|
|
|
|
|
if (clen > 1 || (*s & 0x80)) single = 0;
|
|
|
|
p -= clen;
|
|
|
|
memcpy(p, s, clen);
|
|
|
|
s += clen;
|
|
|
|
}
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
else {
|
|
|
|
while (s < e) {
|
2007-09-06 16:33:45 +04:00
|
|
|
int clen = rb_enc_mbclen(s, e, 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-25 07:29:39 +04:00
|
|
|
|
2008-05-20 11:59:33 +04:00
|
|
|
if (clen > 1 || (*s & 0x80)) single = 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-25 07:29:39 +04:00
|
|
|
p -= clen;
|
|
|
|
memcpy(p, s, clen);
|
|
|
|
s += clen;
|
|
|
|
}
|
2003-09-05 09:07:54 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-05-20 11:59:33 +04:00
|
|
|
STR_SET_LEN(rev, RSTRING_LEN(str));
|
|
|
|
OBJ_INFECT(rev, str);
|
|
|
|
if (ENC_CODERANGE(str) == ENC_CODERANGE_UNKNOWN) {
|
|
|
|
if (single) {
|
|
|
|
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_enc_cr_str_copy_for_substr(rev, 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-25 07:29:39 +04:00
|
|
|
|
2008-05-20 11:59:33 +04:00
|
|
|
return rev;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.reverse! -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* Reverses <i>str</i> in place.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* 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-25 07:29:39 +04:00
|
|
|
rb_str_reverse_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
if (RSTRING_LEN(str) > 1) {
|
2008-02-23 14:18:07 +03:00
|
|
|
if (single_byte_optimizable(str)) {
|
2008-05-20 11:59:33 +04:00
|
|
|
char *s, *e, c;
|
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-05-20 11:59:33 +04:00
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
e = RSTRING_END(str) - 1;
|
* 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-25 07:29:39 +04:00
|
|
|
while (s < e) {
|
|
|
|
c = *s;
|
|
|
|
*s++ = *e;
|
2009-11-26 08:25:08 +03:00
|
|
|
*e-- = c;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_shared_replace(str, rb_str_reverse(str));
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2009-06-02 19:24:23 +04:00
|
|
|
else {
|
|
|
|
str_modify_keep_cr(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-25 07:29:39 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.include? other_str -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns <code>true</code> if <i>str</i> contains the given string or
|
|
|
|
* character.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".include? "lo" #=> true
|
|
|
|
* "hello".include? "ol" #=> false
|
|
|
|
* "hello".include? ?h #=> true
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_include(VALUE str, VALUE arg)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
i = rb_str_index(str, arg, 0);
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (i == -1) return Qfalse;
|
1999-09-22 08:30:11 +04:00
|
|
|
return Qtrue;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.to_i(base=10) -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns the result of interpreting leading characters in <i>str</i> as an
|
2007-10-06 18:17:35 +04:00
|
|
|
* integer base <i>base</i> (between 2 and 36). Extraneous characters past the
|
2003-12-25 00:14:46 +03:00
|
|
|
* end of a valid number are ignored. If there is not a valid number at the
|
|
|
|
* start of <i>str</i>, <code>0</code> is returned. This method never raises an
|
2010-09-24 10:52:25 +04:00
|
|
|
* exception when <i>base</i> is valid.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "12345".to_i #=> 12345
|
|
|
|
* "99 red balloons".to_i #=> 99
|
|
|
|
* "0a".to_i #=> 0
|
|
|
|
* "0a".to_i(16) #=> 10
|
|
|
|
* "hello".to_i #=> 0
|
|
|
|
* "1100101".to_i(2) #=> 101
|
|
|
|
* "1100101".to_i(8) #=> 294977
|
|
|
|
* "1100101".to_i(10) #=> 1100101
|
|
|
|
* "1100101".to_i(16) #=> 17826049
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_to_i(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-01-11 12:18:54 +03:00
|
|
|
int base;
|
|
|
|
|
|
|
|
if (argc == 0) base = 10;
|
2008-03-05 08:22:17 +03:00
|
|
|
else {
|
|
|
|
VALUE b;
|
2002-01-11 12:18:54 +03:00
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
rb_scan_args(argc, argv, "01", &b);
|
|
|
|
base = NUM2INT(b);
|
|
|
|
}
|
2003-04-27 18:13:25 +04:00
|
|
|
if (base < 0) {
|
* 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-21 05:31:11 +03:00
|
|
|
rb_raise(rb_eArgError, "invalid radix %d", base);
|
2003-04-27 18:13:25 +04:00
|
|
|
}
|
2009-09-18 19:17:17 +04:00
|
|
|
return rb_str_to_inum(str, base, FALSE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.to_f -> float
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns the result of interpreting leading characters in <i>str</i> as a
|
|
|
|
* floating point number. Extraneous characters past the end of a valid number
|
|
|
|
* are ignored. If there is not a valid number at the start of <i>str</i>,
|
|
|
|
* <code>0.0</code> is returned. This method never raises an exception.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "123.45e1".to_f #=> 1234.5
|
|
|
|
* "45.67 degrees".to_f #=> 45.67
|
|
|
|
* "thx1138".to_f #=> 0.0
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_to_f(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-09-18 19:17:17 +04:00
|
|
|
return DBL2NUM(rb_str_to_dbl(str, FALSE));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.to_s -> str
|
|
|
|
* str.to_str -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns the receiver.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_to_s(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-02-10 12:40:13 +03:00
|
|
|
if (rb_obj_class(str) != rb_cString) {
|
2009-02-12 13:42:36 +03:00
|
|
|
return str_duplicate(rb_cString, str);
|
2003-02-10 12:40:13 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-09-27 07:01:44 +04:00
|
|
|
#if 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-25 07:29:39 +04:00
|
|
|
static void
|
2008-09-15 21:05:00 +04:00
|
|
|
str_cat_char(VALUE str, unsigned int c, rb_encoding *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-25 07:29:39 +04:00
|
|
|
{
|
2008-09-15 09:36:15 +04:00
|
|
|
char s[RUBY_MAX_CHAR_LEN];
|
* 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-25 07:29:39 +04:00
|
|
|
int n = rb_enc_codelen(c, enc);
|
|
|
|
|
|
|
|
rb_enc_mbcput(c, s, enc);
|
2008-01-06 20:37:54 +03:00
|
|
|
rb_enc_str_buf_cat(str, s, n, 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-25 07:29:39 +04:00
|
|
|
}
|
2009-09-27 07:01:44 +04:00
|
|
|
#endif
|
* 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-25 07:29:39 +04:00
|
|
|
|
2010-06-05 15:31:54 +04:00
|
|
|
#define CHAR_ESC_LEN 13 /* sizeof(\x{ hex of 32bit unsigned int } \0) */
|
|
|
|
|
|
|
|
int
|
2010-06-06 17:37:23 +04:00
|
|
|
rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
|
|
|
|
{
|
2010-06-05 15:31:54 +04:00
|
|
|
char buf[CHAR_ESC_LEN + 1];
|
|
|
|
int l;
|
2010-06-06 17:37:23 +04:00
|
|
|
|
|
|
|
#if SIZEOF_INT > 4
|
|
|
|
c &= 0xffffffff;
|
|
|
|
#endif
|
2010-06-05 15:31:54 +04:00
|
|
|
if (unicode_p) {
|
|
|
|
if (c < 0x7F && ISPRINT(c)) {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "%c", c);
|
|
|
|
}
|
|
|
|
else if (c < 0x10000) {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (c < 0x100) {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
|
|
|
|
}
|
|
|
|
}
|
2010-06-06 17:37:23 +04:00
|
|
|
l = (int)strlen(buf); /* CHAR_ESC_LEN cannot exceed INT_MAX */
|
2010-06-05 15:31:54 +04:00
|
|
|
rb_str_buf_cat(result, buf, l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.inspect -> string
|
2003-12-30 19:38:32 +03:00
|
|
|
*
|
2007-12-31 08:52:59 +03:00
|
|
|
* Returns a printable version of _str_, surrounded by quote marks,
|
2006-09-09 20:19:33 +04:00
|
|
|
* with special characters escaped.
|
2003-12-30 19:38:32 +03:00
|
|
|
*
|
|
|
|
* str = "hello"
|
2006-09-09 20:19:33 +04:00
|
|
|
* str[3] = "\b"
|
2008-03-09 04:04:46 +03:00
|
|
|
* str.inspect #=> "\"hel\\bo\""
|
2003-12-30 19:38:32 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03: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 14:44:21 +04:00
|
|
|
rb_str_inspect(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2009-10-07 21:46:34 +04:00
|
|
|
const char *p, *pend, *prev;
|
2009-10-07 04:35:24 +04:00
|
|
|
char buf[CHAR_ESC_LEN + 1];
|
2008-07-21 23:26:18 +04:00
|
|
|
VALUE result = rb_str_buf_new(0);
|
2009-09-27 00:17:52 +04:00
|
|
|
rb_encoding *resenc = rb_default_internal_encoding();
|
2009-09-29 04:34:06 +04:00
|
|
|
int unicode_p = rb_enc_unicode_p(enc);
|
2010-05-28 13:37:06 +04:00
|
|
|
int asciicompat = rb_enc_asciicompat(enc);
|
2010-11-24 05:20:11 +03:00
|
|
|
static rb_encoding *utf16, *utf32;
|
2009-09-27 00:17:52 +04:00
|
|
|
|
2010-11-24 05:20:11 +03:00
|
|
|
if (!utf16) utf16 = rb_enc_find("UTF-16");
|
|
|
|
if (!utf32) utf32 = rb_enc_find("UTF-32");
|
2009-09-27 00:17:52 +04:00
|
|
|
if (resenc == NULL) resenc = rb_default_external_encoding();
|
|
|
|
if (!rb_enc_asciicompat(resenc)) resenc = rb_usascii_encoding();
|
|
|
|
rb_enc_associate(result, resenc);
|
|
|
|
str_buf_cat2(result, "\"");
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
p = RSTRING_PTR(str); pend = RSTRING_END(str);
|
2009-10-07 21:46:34 +04:00
|
|
|
prev = p;
|
2010-11-24 05:20:11 +03:00
|
|
|
if (enc == utf16) {
|
2010-11-30 19:47:24 +03:00
|
|
|
const unsigned char *q = (const unsigned char *)p;
|
|
|
|
if (q[0] == 0xFE && q[1] == 0xFF)
|
|
|
|
enc = rb_enc_find("UTF-16BE");
|
2010-12-01 04:20:56 +03:00
|
|
|
else if (q[0] == 0xFF && q[1] == 0xFE)
|
2010-11-30 19:47:24 +03:00
|
|
|
enc = rb_enc_find("UTF-16LE");
|
|
|
|
else
|
|
|
|
unicode_p = 0;
|
2010-11-24 05:20:11 +03:00
|
|
|
}
|
|
|
|
else if (enc == utf32) {
|
2010-11-30 19:47:24 +03:00
|
|
|
const unsigned char *q = (const unsigned char *)p;
|
|
|
|
if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF)
|
|
|
|
enc = rb_enc_find("UTF-32BE");
|
|
|
|
else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF)
|
|
|
|
enc = rb_enc_find("UTF-32LE");
|
|
|
|
else
|
|
|
|
unicode_p = 0;
|
2010-11-24 05:20:11 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend) {
|
2009-10-07 04:35:24 +04:00
|
|
|
unsigned int c, cc;
|
2007-12-06 12:28:26 +03:00
|
|
|
int n;
|
* 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-25 07:29:39 +04:00
|
|
|
|
2007-12-06 12:28:26 +03:00
|
|
|
n = rb_enc_precise_mbclen(p, pend, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_CHARFOUND_P(n)) {
|
2009-10-07 21:46:34 +04:00
|
|
|
if (p > prev) str_buf_cat(result, prev, p - prev);
|
2010-01-24 08:22:51 +03:00
|
|
|
n = rb_enc_mbminlen(enc);
|
|
|
|
if (pend < p + n)
|
2010-01-25 05:50:05 +03:00
|
|
|
n = (int)(pend - p);
|
2010-01-24 08:22:51 +03:00
|
|
|
while (n--) {
|
|
|
|
snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
|
|
|
|
str_buf_cat(result, buf, strlen(buf));
|
|
|
|
prev = ++p;
|
|
|
|
}
|
2009-10-07 04:35:24 +04:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-27 17:27:07 +03:00
|
|
|
n = MBCLEN_CHARFOUND_LEN(n);
|
2009-09-14 05:11:05 +04:00
|
|
|
c = rb_enc_mbc_to_codepoint(p, pend, 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-25 07:29:39 +04:00
|
|
|
p += n;
|
2010-12-11 22:03:26 +03:00
|
|
|
if ((asciicompat || unicode_p) &&
|
|
|
|
(c == '"'|| c == '\\' ||
|
2008-01-27 14:09:41 +03:00
|
|
|
(c == '#' &&
|
|
|
|
p < pend &&
|
2008-01-27 17:27:07 +03:00
|
|
|
MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
|
2008-01-27 14:09:41 +03:00
|
|
|
(cc = rb_enc_codepoint(p,pend,enc),
|
2010-12-11 22:03:26 +03:00
|
|
|
(cc == '$' || cc == '@' || cc == '{'))))) {
|
2009-10-07 21:46:34 +04:00
|
|
|
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
|
2009-09-27 00:17:52 +04:00
|
|
|
str_buf_cat2(result, "\\");
|
2010-11-22 15:30:58 +03:00
|
|
|
if (asciicompat || enc == resenc) {
|
2010-11-22 12:33:32 +03:00
|
|
|
prev = p - n;
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-02 20:20:16 +04:00
|
|
|
}
|
2009-10-07 21:46:34 +04:00
|
|
|
switch (c) {
|
|
|
|
case '\n': cc = 'n'; break;
|
|
|
|
case '\r': cc = 'r'; break;
|
|
|
|
case '\t': cc = 't'; break;
|
|
|
|
case '\f': cc = 'f'; break;
|
|
|
|
case '\013': cc = 'v'; break;
|
|
|
|
case '\010': cc = 'b'; break;
|
|
|
|
case '\007': cc = 'a'; break;
|
|
|
|
case 033: cc = 'e'; break;
|
|
|
|
default: cc = 0; break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2009-10-07 21:46:34 +04:00
|
|
|
if (cc) {
|
|
|
|
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
|
|
|
|
buf[0] = '\\';
|
|
|
|
buf[1] = (char)cc;
|
|
|
|
str_buf_cat(result, buf, 2);
|
|
|
|
prev = p;
|
|
|
|
continue;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
2009-10-07 21:46:34 +04:00
|
|
|
if ((enc == resenc && rb_enc_isprint(c, enc)) ||
|
2010-05-28 13:37:06 +04:00
|
|
|
(asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
|
2009-10-07 21:46:34 +04:00
|
|
|
continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-10-07 21:46:34 +04:00
|
|
|
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
|
2010-06-05 15:31:54 +04:00
|
|
|
rb_str_buf_cat_escaped_char(result, c, unicode_p);
|
2009-10-07 21:46:34 +04:00
|
|
|
prev = p;
|
|
|
|
continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 21:46:34 +04:00
|
|
|
if (p > prev) str_buf_cat(result, prev, p - prev);
|
2009-09-27 00:17:52 +04:00
|
|
|
str_buf_cat2(result, "\"");
|
2000-12-05 12:36:54 +03:00
|
|
|
|
|
|
|
OBJ_INFECT(result, str);
|
|
|
|
return result;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
#define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.dump -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-06 00:39:15 +04:00
|
|
|
* Produces a version of +str+ with all non-printing characters replaced by
|
2003-12-25 00:14:46 +03:00
|
|
|
* <code>\nnn</code> notation and all special characters escaped.
|
2012-07-06 00:39:15 +04:00
|
|
|
*
|
|
|
|
* "hello \n ''".dump #=> "\"hello \\n ''\"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2002-10-23 14:17:30 +04: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 14:44:21 +04:00
|
|
|
rb_str_dump(VALUE str)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2008-10-23 10:53:55 +04:00
|
|
|
rb_encoding *enc = rb_enc_get(str);
|
2000-02-29 11:05:32 +03:00
|
|
|
long len;
|
2008-01-23 05:53:28 +03:00
|
|
|
const char *p, *pend;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *q, *qend;
|
|
|
|
VALUE result;
|
2008-10-23 10:53:55 +04:00
|
|
|
int u8 = (enc == rb_utf8_encoding());
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
len = 2; /* "" */
|
2006-08-31 14:47:44 +04:00
|
|
|
p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
while (p < pend) {
|
2008-01-23 05:53:28 +03:00
|
|
|
unsigned char c = *p++;
|
1999-01-20 07:59:39 +03:00
|
|
|
switch (c) {
|
|
|
|
case '"': case '\\':
|
|
|
|
case '\n': case '\r':
|
2004-12-09 08:38:59 +03:00
|
|
|
case '\t': case '\f':
|
2006-07-11 12:34:28 +04:00
|
|
|
case '\013': case '\010': case '\007': case '\033':
|
1999-01-20 07:59:39 +03:00
|
|
|
len += 2;
|
|
|
|
break;
|
|
|
|
|
2004-12-09 08:38:59 +03:00
|
|
|
case '#':
|
|
|
|
len += IS_EVSTR(p, pend) ? 2 : 1;
|
|
|
|
break;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
default:
|
2008-01-23 05:53:28 +03:00
|
|
|
if (ISPRINT(c)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
len++;
|
|
|
|
}
|
|
|
|
else {
|
2008-10-23 10:53:55 +04:00
|
|
|
if (u8) { /* \u{NN} */
|
2009-02-04 15:58:30 +03:00
|
|
|
int n = rb_enc_precise_mbclen(p-1, pend, enc);
|
2010-10-28 22:04:23 +04:00
|
|
|
if (MBCLEN_CHARFOUND_P(n-1)) {
|
|
|
|
unsigned int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
|
|
|
|
while (cc >>= 4) len++;
|
|
|
|
len += 5;
|
2009-02-04 15:58:30 +03:00
|
|
|
p += MBCLEN_CHARFOUND_LEN(n)-1;
|
2008-10-23 10:53:55 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len += 4; /* \xNN */
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-23 10:53:55 +04:00
|
|
|
if (!rb_enc_asciicompat(enc)) {
|
2008-01-18 07:49:14 +03:00
|
|
|
len += 19; /* ".force_encoding('')" */
|
2008-10-23 10:53:55 +04:00
|
|
|
len += strlen(enc->name);
|
2008-01-18 07:49:14 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
result = rb_str_new5(str, 0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
|
2009-04-08 17:05:07 +04:00
|
|
|
q = RSTRING_PTR(result); qend = q + len + 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
*q++ = '"';
|
|
|
|
while (p < pend) {
|
2008-01-23 05:53:28 +03:00
|
|
|
unsigned char c = *p++;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
if (c == '"' || c == '\\') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = c;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
else if (c == '#') {
|
2004-12-09 08:38:59 +03:00
|
|
|
if (IS_EVSTR(p, pend)) *q++ = '\\';
|
1999-08-13 09:45:20 +04:00
|
|
|
*q++ = '#';
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (c == '\n') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'n';
|
|
|
|
}
|
|
|
|
else if (c == '\r') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'r';
|
|
|
|
}
|
|
|
|
else if (c == '\t') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 't';
|
|
|
|
}
|
|
|
|
else if (c == '\f') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'f';
|
|
|
|
}
|
|
|
|
else if (c == '\013') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'v';
|
|
|
|
}
|
2006-07-02 20:20:16 +04:00
|
|
|
else if (c == '\010') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'b';
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (c == '\007') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'a';
|
|
|
|
}
|
|
|
|
else if (c == '\033') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'e';
|
|
|
|
}
|
2008-01-23 05:53:28 +03:00
|
|
|
else if (ISPRINT(c)) {
|
* 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-25 07:29:39 +04:00
|
|
|
*q++ = c;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
|
|
|
*q++ = '\\';
|
2008-10-23 10:53:55 +04:00
|
|
|
if (u8) {
|
|
|
|
int n = rb_enc_precise_mbclen(p-1, pend, enc) - 1;
|
|
|
|
if (MBCLEN_CHARFOUND_P(n)) {
|
* 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 20:59:22 +04:00
|
|
|
int cc = rb_enc_mbc_to_codepoint(p-1, pend, enc);
|
2008-10-23 10:53:55 +04:00
|
|
|
p += n;
|
* 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-17 04:29:17 +03:00
|
|
|
snprintf(q, qend-q, "u{%x}", cc);
|
2008-10-23 10:53:55 +04:00
|
|
|
q += strlen(q);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
* 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-17 04:29:17 +03:00
|
|
|
snprintf(q, qend-q, "x%02X", c);
|
1999-01-20 07:59:39 +03:00
|
|
|
q += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q++ = '"';
|
2008-10-23 10:53:55 +04:00
|
|
|
*q = '\0';
|
|
|
|
if (!rb_enc_asciicompat(enc)) {
|
* 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-17 04:29:17 +03:00
|
|
|
snprintf(q, qend-q, ".force_encoding(\"%s\")", enc->name);
|
2008-10-23 10:53:55 +04:00
|
|
|
enc = rb_ascii8bit_encoding();
|
2008-01-18 07:49:14 +03:00
|
|
|
}
|
2000-12-05 12:36:54 +03:00
|
|
|
OBJ_INFECT(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-25 07:29:39 +04:00
|
|
|
/* result from dump is ASCII */
|
2008-10-23 10:53:55 +04:00
|
|
|
rb_enc_associate(result, enc);
|
|
|
|
ENC_CODERANGE_SET(result, ENC_CODERANGE_7BIT);
|
1999-01-20 07:59:39 +03:00
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2008-10-29 19:44:24 +03:00
|
|
|
static void
|
|
|
|
rb_str_check_dummy_enc(rb_encoding *enc)
|
|
|
|
{
|
|
|
|
if (rb_enc_dummy_p(enc)) {
|
|
|
|
rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s",
|
|
|
|
rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.upcase! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Upcases the contents of <i>str</i>, returning <code>nil</code> if no changes
|
|
|
|
* were made.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case replacement is effective only in ASCII region.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_upcase_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
2000-02-08 11:54:01 +03:00
|
|
|
int modify = 0;
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-10-29 19:44:24 +03:00
|
|
|
rb_str_check_dummy_enc(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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); send = RSTRING_END(str);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (single_byte_optimizable(str)) {
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c = *(unsigned char*)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-25 07:29:39 +04:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (rb_enc_isascii(c, enc) && 'a' <= c && c <= 'z') {
|
|
|
|
*s = 'A' + (c - 'a');
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ascompat = rb_enc_asciicompat(enc);
|
|
|
|
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
if (ascompat && (c = *(unsigned char*)s) < 0x80) {
|
|
|
|
if (rb_enc_isascii(c, enc) && 'a' <= c && c <= 'z') {
|
|
|
|
*s = 'A' + (c - 'a');
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &n, enc);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (rb_enc_islower(c, enc)) {
|
|
|
|
/* assuming toupper returns codepoint with same size */
|
|
|
|
rb_enc_mbcput(rb_enc_toupper(c, enc), s, enc);
|
|
|
|
modify = 1;
|
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
|
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.upcase -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with all lowercase letters replaced with their
|
|
|
|
* uppercase counterparts. The operation is locale insensitive---only
|
|
|
|
* characters ``a'' to ``z'' are affected.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case replacement is effective only in ASCII region.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hEllO".upcase #=> "HELLO"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_upcase(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_upcase_bang(str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.downcase! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Downcases the contents of <i>str</i>, returning <code>nil</code> if no
|
|
|
|
* changes were made.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case replacement is effective only in ASCII region.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_downcase_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
2000-02-08 11:54:01 +03:00
|
|
|
int modify = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-10-29 19:44:24 +03:00
|
|
|
rb_str_check_dummy_enc(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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); send = RSTRING_END(str);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (single_byte_optimizable(str)) {
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c = *(unsigned char*)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-25 07:29:39 +04:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (rb_enc_isascii(c, enc) && 'A' <= c && c <= 'Z') {
|
|
|
|
*s = 'a' + (c - 'A');
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ascompat = rb_enc_asciicompat(enc);
|
|
|
|
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c;
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
|
|
|
|
if (ascompat && (c = *(unsigned char*)s) < 0x80) {
|
|
|
|
if (rb_enc_isascii(c, enc) && 'A' <= c && c <= 'Z') {
|
|
|
|
*s = 'a' + (c - 'A');
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &n, enc);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (rb_enc_isupper(c, enc)) {
|
|
|
|
/* assuming toupper returns codepoint with same size */
|
|
|
|
rb_enc_mbcput(rb_enc_tolower(c, enc), s, enc);
|
|
|
|
modify = 1;
|
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
|
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.downcase -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with all uppercase letters replaced with their
|
|
|
|
* lowercase counterparts. The operation is locale insensitive---only
|
|
|
|
* characters ``A'' to ``Z'' are affected.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case replacement is effective only in ASCII region.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hEllO".downcase #=> "hello"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_downcase(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_downcase_bang(str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.capitalize! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Modifies <i>str</i> by converting the first character to uppercase and the
|
|
|
|
* remainder to lowercase. Returns <code>nil</code> if no changes are made.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case conversion is effective only in ASCII region.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a = "hello"
|
|
|
|
* a.capitalize! #=> "Hello"
|
|
|
|
* a #=> "Hello"
|
|
|
|
* a.capitalize! #=> nil
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_capitalize_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
2000-02-08 11:54:01 +03:00
|
|
|
int modify = 0;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int c;
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-10-29 19:44:24 +03:00
|
|
|
rb_str_check_dummy_enc(enc);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
|
* 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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); send = RSTRING_END(str);
|
|
|
|
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &n, 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-25 07:29:39 +04:00
|
|
|
if (rb_enc_islower(c, enc)) {
|
|
|
|
rb_enc_mbcput(rb_enc_toupper(c, enc), s, enc);
|
2000-02-08 11:54:01 +03:00
|
|
|
modify = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
* 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-25 07:29:39 +04:00
|
|
|
while (s < send) {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &n, 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-25 07:29:39 +04:00
|
|
|
if (rb_enc_isupper(c, enc)) {
|
|
|
|
rb_enc_mbcput(rb_enc_tolower(c, enc), s, enc);
|
2000-02-08 11:54:01 +03:00
|
|
|
modify = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-02-23 13:29:13 +03:00
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.capitalize -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with the first character converted to uppercase
|
|
|
|
* and the remainder to lowercase.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case conversion is effective only in ASCII region.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".capitalize #=> "Hello"
|
|
|
|
* "HELLO".capitalize #=> "Hello"
|
|
|
|
* "123ABC".capitalize #=> "123abc"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_capitalize(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_capitalize_bang(str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
2009-02-22 17:26:27 +03:00
|
|
|
* call-seq:
|
2011-01-08 16:07:20 +03:00
|
|
|
* str.swapcase! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Equivalent to <code>String#swapcase</code>, but modifies the receiver in
|
|
|
|
* place, returning <i>str</i>, or <code>nil</code> if no changes were made.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case conversion is effective only in ASCII region.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_swapcase_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
2000-02-08 11:54:01 +03:00
|
|
|
int modify = 0;
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-10-29 19:44:24 +03:00
|
|
|
rb_str_check_dummy_enc(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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); send = RSTRING_END(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (s < send) {
|
* 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 20:59:22 +04:00
|
|
|
unsigned int c = rb_enc_codepoint_len(s, send, &n, 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-25 07:29:39 +04:00
|
|
|
|
|
|
|
if (rb_enc_isupper(c, enc)) {
|
|
|
|
/* assuming toupper returns codepoint with same size */
|
|
|
|
rb_enc_mbcput(rb_enc_tolower(c, enc), s, enc);
|
2000-02-08 11:54:01 +03:00
|
|
|
modify = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
else if (rb_enc_islower(c, enc)) {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
/* assuming tolower returns codepoint with same size */
|
* 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-25 07:29:39 +04:00
|
|
|
rb_enc_mbcput(rb_enc_toupper(c, enc), s, enc);
|
2000-02-08 11:54:01 +03:00
|
|
|
modify = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.swapcase -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with uppercase alphabetic characters converted
|
|
|
|
* to lowercase and lowercase characters converted to uppercase.
|
2006-10-22 12:06:16 +04:00
|
|
|
* Note: case conversion is effective only in ASCII region.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "Hello".swapcase #=> "hELLO"
|
|
|
|
* "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_swapcase(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_swapcase_bang(str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
typedef unsigned char *USTR;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
struct tr {
|
2009-02-22 17:26:27 +03:00
|
|
|
int gen;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int now, max;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *p, *pend;
|
|
|
|
};
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-11 14:34:59 +04:00
|
|
|
static unsigned int
|
* 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-25 07:29:39 +04:00
|
|
|
trnext(struct tr *t, rb_encoding *enc)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
for (;;) {
|
|
|
|
if (!t->gen) {
|
2012-05-20 17:32:16 +04:00
|
|
|
nextpart:
|
1998-01-16 15:13:05 +03:00
|
|
|
if (t->p == t->pend) return -1;
|
2012-03-16 20:49:44 +04:00
|
|
|
if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
|
2012-03-16 15:06:27 +04:00
|
|
|
t->p += n;
|
2008-01-04 20:15:22 +03:00
|
|
|
}
|
* 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 20:59:22 +04:00
|
|
|
t->now = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
|
|
|
|
t->p += n;
|
2012-03-16 20:49:44 +04:00
|
|
|
if (rb_enc_ascget(t->p, t->pend, &n, enc) == '-' && t->p + n < t->pend) {
|
2012-03-16 15:06:27 +04:00
|
|
|
t->p += n;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (t->p < t->pend) {
|
* 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 20:59:22 +04:00
|
|
|
unsigned int c = rb_enc_codepoint_len(t->p, t->pend, &n, enc);
|
|
|
|
t->p += n;
|
2009-10-30 12:58:25 +03:00
|
|
|
if (t->now > c) {
|
|
|
|
if (t->now < 0x80 && c < 0x80) {
|
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"invalid range \"%c-%c\" in string transliteration",
|
|
|
|
t->now, c);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "invalid range in string transliteration");
|
|
|
|
}
|
|
|
|
continue; /* not reached */
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
t->gen = 1;
|
* 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-25 07:29:39 +04:00
|
|
|
t->max = c;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return t->now;
|
|
|
|
}
|
|
|
|
else {
|
2012-05-20 17:32:16 +04:00
|
|
|
while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
|
|
|
|
if (t->now == t->max) {
|
|
|
|
t->gen = 0;
|
|
|
|
goto nextpart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t->now < t->max) {
|
|
|
|
return t->now;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->gen = 0;
|
|
|
|
return t->max;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static VALUE rb_str_delete_bang(int,VALUE*,VALUE);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
tr_trans(VALUE str, VALUE src, VALUE repl, int sflag)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-09-11 14:34:59 +04:00
|
|
|
const unsigned int errc = -1;
|
|
|
|
unsigned int trans[256];
|
2007-12-20 18:27:35 +03:00
|
|
|
rb_encoding *enc, *e1, *e2;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct tr trsrc, trrepl;
|
|
|
|
int cflag = 0;
|
2011-03-07 03:05:35 +03:00
|
|
|
unsigned int c, c0, last = 0;
|
|
|
|
int modify = 0, i, l;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
2007-11-03 22:04:53 +03:00
|
|
|
VALUE hash = 0;
|
2008-05-20 11:59:33 +04:00
|
|
|
int singlebyte = single_byte_optimizable(str);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
int cr;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2009-02-22 16:42:22 +03:00
|
|
|
#define CHECK_IF_ASCII(c) \
|
|
|
|
(void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
|
|
|
|
(cr = ENC_CODERANGE_VALID) : 0)
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(src);
|
|
|
|
StringValue(repl);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
|
|
|
|
if (RSTRING_LEN(repl) == 0) {
|
2000-02-08 11:54:01 +03:00
|
|
|
return rb_str_delete_bang(1, &src, str);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2008-05-07 20:15:45 +04:00
|
|
|
|
2008-09-15 18:40:00 +04:00
|
|
|
cr = ENC_CODERANGE(str);
|
2007-12-20 18:27:35 +03:00
|
|
|
e1 = rb_enc_check(str, src);
|
|
|
|
e2 = rb_enc_check(str, repl);
|
|
|
|
if (e1 == e2) {
|
|
|
|
enc = e1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enc = rb_enc_check(src, repl);
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
2008-05-07 20:15:45 +04:00
|
|
|
trsrc.p = RSTRING_PTR(src); trsrc.pend = trsrc.p + RSTRING_LEN(src);
|
2008-05-10 21:10:10 +04:00
|
|
|
if (RSTRING_LEN(src) > 1 &&
|
2008-05-10 17:15:13 +04:00
|
|
|
rb_enc_ascget(trsrc.p, trsrc.pend, &l, enc) == '^' &&
|
|
|
|
trsrc.p + l < trsrc.pend) {
|
2008-05-07 20:15:45 +04:00
|
|
|
cflag = 1;
|
|
|
|
trsrc.p += l;
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
trrepl.p = RSTRING_PTR(repl);
|
|
|
|
trrepl.pend = trrepl.p + RSTRING_LEN(repl);
|
1998-01-16 15:13:05 +03:00
|
|
|
trsrc.gen = trrepl.gen = 0;
|
|
|
|
trsrc.now = trrepl.now = 0;
|
|
|
|
trsrc.max = trrepl.max = 0;
|
|
|
|
|
|
|
|
if (cflag) {
|
2007-11-03 22:04:53 +03:00
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
trans[i] = 1;
|
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
while ((c = trnext(&trsrc, enc)) != errc) {
|
2007-11-03 22:04:53 +03:00
|
|
|
if (c < 256) {
|
2008-09-11 14:34:59 +04:00
|
|
|
trans[c] = errc;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!hash) hash = rb_hash_new();
|
2008-09-11 14:34:59 +04:00
|
|
|
rb_hash_aset(hash, UINT2NUM(c), Qtrue);
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
while ((c = trnext(&trrepl, enc)) != errc)
|
1999-08-13 09:45:20 +04:00
|
|
|
/* retrieve last replacer */;
|
* 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-25 07:29:39 +04:00
|
|
|
last = trrepl.now;
|
2007-11-03 22:04:53 +03:00
|
|
|
for (i=0; i<256; i++) {
|
2008-09-11 14:34:59 +04:00
|
|
|
if (trans[i] != errc) {
|
2007-11-03 22:04:53 +03:00
|
|
|
trans[i] = last;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int r;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-11-03 22:04:53 +03:00
|
|
|
for (i=0; i<256; i++) {
|
2008-09-11 14:34:59 +04:00
|
|
|
trans[i] = errc;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
while ((c = trnext(&trsrc, enc)) != errc) {
|
* 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-25 07:29:39 +04:00
|
|
|
r = trnext(&trrepl, enc);
|
2008-09-11 14:34:59 +04:00
|
|
|
if (r == errc) r = trrepl.now;
|
2007-11-03 22:04:53 +03:00
|
|
|
if (c < 256) {
|
2008-05-07 20:15:45 +04:00
|
|
|
trans[c] = r;
|
2009-08-01 14:25:48 +04:00
|
|
|
if (rb_enc_codelen(r, enc) != 1) singlebyte = 0;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!hash) hash = rb_hash_new();
|
2008-09-11 14:34:59 +04:00
|
|
|
rb_hash_aset(hash, UINT2NUM(c), UINT2NUM(r));
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 16:42:22 +03:00
|
|
|
if (cr == ENC_CODERANGE_VALID)
|
|
|
|
cr = ENC_CODERANGE_7BIT;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(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-25 07:29:39 +04:00
|
|
|
s = RSTRING_PTR(str); send = RSTRING_END(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (sflag) {
|
2009-09-18 19:17:17 +04:00
|
|
|
int clen, tlen;
|
|
|
|
long offset, max = RSTRING_LEN(str);
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
unsigned int save = -1;
|
* 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-25 07:29:39 +04:00
|
|
|
char *buf = ALLOC_N(char, max), *t = buf;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (s < send) {
|
2009-02-22 08:33:07 +03:00
|
|
|
int may_modify = 0;
|
* 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 20:59:22 +04:00
|
|
|
|
|
|
|
c0 = c = rb_enc_codepoint_len(s, send, &clen, e1);
|
2009-02-22 08:33:07 +03:00
|
|
|
tlen = enc == e1 ? clen : 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-25 07:29:39 +04:00
|
|
|
|
|
|
|
s += clen;
|
2007-11-03 22:04:53 +03:00
|
|
|
if (c < 256) {
|
2008-05-07 20:15:45 +04:00
|
|
|
c = trans[c];
|
|
|
|
}
|
|
|
|
else if (hash) {
|
2008-09-11 14:34:59 +04:00
|
|
|
VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
|
2008-05-07 20:15:45 +04:00
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
if (cflag) c = last;
|
2008-09-11 14:34:59 +04:00
|
|
|
else c = errc;
|
2008-05-07 20:15:45 +04:00
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
else if (cflag) c = errc;
|
2008-05-07 20:15:45 +04:00
|
|
|
else c = NUM2INT(tmp);
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-09-11 14:34:59 +04:00
|
|
|
c = errc;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
2009-05-07 09:16:26 +04:00
|
|
|
if (c != (unsigned int)-1) {
|
2009-02-22 16:42:22 +03:00
|
|
|
if (save == c) {
|
|
|
|
CHECK_IF_ASCII(c);
|
|
|
|
continue;
|
|
|
|
}
|
2008-05-07 20:15:45 +04:00
|
|
|
save = c;
|
|
|
|
tlen = rb_enc_codelen(c, enc);
|
|
|
|
modify = 1;
|
1999-08-13 09:45:20 +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-25 07:29:39 +04:00
|
|
|
save = -1;
|
2008-05-07 20:15:45 +04:00
|
|
|
c = c0;
|
2009-02-22 08:33:07 +03:00
|
|
|
if (enc != e1) may_modify = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
while (t - buf + tlen >= max) {
|
|
|
|
offset = t - buf;
|
|
|
|
max *= 2;
|
|
|
|
REALLOC_N(buf, char, max);
|
|
|
|
t = buf + offset;
|
|
|
|
}
|
|
|
|
rb_enc_mbcput(c, t, enc);
|
2009-02-22 08:33:07 +03:00
|
|
|
if (may_modify && memcmp(s, t, tlen) != 0) {
|
|
|
|
modify = 1;
|
|
|
|
}
|
2009-02-22 16:42:22 +03:00
|
|
|
CHECK_IF_ASCII(c);
|
* 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-25 07:29:39 +04:00
|
|
|
t += tlen;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2011-07-01 09:28:48 +04:00
|
|
|
if (!STR_EMBED_P(str)) {
|
|
|
|
xfree(RSTRING(str)->as.heap.ptr);
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
*t = '\0';
|
|
|
|
RSTRING(str)->as.heap.ptr = buf;
|
|
|
|
RSTRING(str)->as.heap.len = t - buf;
|
|
|
|
STR_SET_NOEMBED(str);
|
|
|
|
RSTRING(str)->as.heap.aux.capa = max;
|
|
|
|
}
|
2008-05-20 11:59:33 +04:00
|
|
|
else if (rb_enc_mbmaxlen(enc) == 1 || (singlebyte && !hash)) {
|
* 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-25 07:29:39 +04:00
|
|
|
while (s < send) {
|
2007-11-10 17:53:26 +03:00
|
|
|
c = (unsigned char)*s;
|
2008-09-11 14:34:59 +04:00
|
|
|
if (trans[c] != errc) {
|
2007-08-27 08:51:45 +04:00
|
|
|
if (!cflag) {
|
2008-05-07 20:15:45 +04:00
|
|
|
c = trans[c];
|
2007-11-03 22:04:53 +03:00
|
|
|
*s = c;
|
2007-08-27 08:51:45 +04:00
|
|
|
modify = 1;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
2007-11-08 01:07:13 +03:00
|
|
|
else {
|
|
|
|
*s = last;
|
|
|
|
modify = 1;
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
2009-02-22 16:42:22 +03:00
|
|
|
CHECK_IF_ASCII(c);
|
* 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-25 07:29:39 +04:00
|
|
|
s++;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-03-10 08:43:14 +03:00
|
|
|
int clen, tlen, max = (int)(RSTRING_LEN(str) * 1.2);
|
2009-09-18 19:17:17 +04:00
|
|
|
long offset;
|
* 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-25 07:29:39 +04:00
|
|
|
char *buf = ALLOC_N(char, max), *t = buf;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (s < send) {
|
2009-02-22 08:33:07 +03:00
|
|
|
int may_modify = 0;
|
* 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 20:59:22 +04:00
|
|
|
c0 = c = rb_enc_codepoint_len(s, send, &clen, e1);
|
2009-02-22 08:33:07 +03:00
|
|
|
tlen = enc == e1 ? clen : 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-25 07:29:39 +04:00
|
|
|
|
2007-11-03 22:04:53 +03:00
|
|
|
if (c < 256) {
|
2008-05-07 20:15:45 +04:00
|
|
|
c = trans[c];
|
|
|
|
}
|
|
|
|
else if (hash) {
|
2008-09-11 14:34:59 +04:00
|
|
|
VALUE tmp = rb_hash_lookup(hash, UINT2NUM(c));
|
2008-05-07 20:15:45 +04:00
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
if (cflag) c = last;
|
2008-09-11 14:34:59 +04:00
|
|
|
else c = errc;
|
2008-05-07 20:15:45 +04:00
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
else if (cflag) c = errc;
|
2008-05-07 20:15:45 +04:00
|
|
|
else c = NUM2INT(tmp);
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
else {
|
2011-03-07 11:44:45 +03:00
|
|
|
c = cflag ? last : errc;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
2008-09-11 14:34:59 +04:00
|
|
|
if (c != errc) {
|
2008-05-07 20:15:45 +04:00
|
|
|
tlen = rb_enc_codelen(c, enc);
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = c0;
|
2009-02-22 08:33:07 +03:00
|
|
|
if (enc != e1) may_modify = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
while (t - buf + tlen >= max) {
|
|
|
|
offset = t - buf;
|
|
|
|
max *= 2;
|
|
|
|
REALLOC_N(buf, char, max);
|
|
|
|
t = buf + offset;
|
|
|
|
}
|
2009-02-22 08:33:07 +03:00
|
|
|
if (s != t) {
|
|
|
|
rb_enc_mbcput(c, t, enc);
|
|
|
|
if (may_modify && memcmp(s, t, tlen) != 0) {
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 16:42:22 +03:00
|
|
|
CHECK_IF_ASCII(c);
|
* 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-25 07:29:39 +04:00
|
|
|
s += clen;
|
|
|
|
t += tlen;
|
|
|
|
}
|
|
|
|
if (!STR_EMBED_P(str)) {
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 14:01:40 +04:00
|
|
|
xfree(RSTRING(str)->as.heap.ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
*t = '\0';
|
|
|
|
RSTRING(str)->as.heap.ptr = buf;
|
|
|
|
RSTRING(str)->as.heap.len = t - buf;
|
|
|
|
STR_SET_NOEMBED(str);
|
|
|
|
RSTRING(str)->as.heap.aux.capa = max;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2009-02-22 17:26:27 +03:00
|
|
|
|
2007-12-23 23:13:55 +03:00
|
|
|
if (modify) {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (cr != ENC_CODERANGE_BROKEN)
|
|
|
|
ENC_CODERANGE_SET(str, cr);
|
2007-12-23 23:13:55 +03:00
|
|
|
rb_enc_associate(str, enc);
|
|
|
|
return str;
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.tr!(from_str, to_str) -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Translates <i>str</i> in place, using the same rules as
|
|
|
|
* <code>String#tr</code>. Returns <i>str</i>, or <code>nil</code> if no
|
|
|
|
* changes were made.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_tr_bang(VALUE str, VALUE src, VALUE repl)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-08 11:54:01 +03:00
|
|
|
return tr_trans(str, src, repl, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-06-02 02:29:09 +04:00
|
|
|
* str.tr(from_str, to_str) => new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-04-11 01:11:37 +04:00
|
|
|
* Returns a copy of +str+ with the characters in +from_str+ replaced by the
|
|
|
|
* corresponding characters in +to_str+. If +to_str+ is shorter than
|
|
|
|
* +from_str+, it is padded with its last character in order to maintain the
|
|
|
|
* correspondence.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".tr('el', 'ip') #=> "hippo"
|
2011-06-02 02:29:09 +04:00
|
|
|
* "hello".tr('aeiou', '*') #=> "h*ll*"
|
2012-04-11 01:11:37 +04:00
|
|
|
* "hello".tr('aeiou', 'AA*') #=> "hAll*"
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2012-04-11 01:11:37 +04:00
|
|
|
* Both strings may use the <code>c1-c2</code> notation to denote ranges of
|
|
|
|
* characters, and +from_str+ may start with a <code>^</code>, which denotes
|
|
|
|
* all characters except those listed.
|
2011-06-02 02:29:09 +04:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".tr('a-y', 'b-z') #=> "ifmmp"
|
2011-06-02 02:29:09 +04:00
|
|
|
* "hello".tr('^aeiou', '*') #=> "*e**o"
|
2012-04-11 01:11:37 +04:00
|
|
|
*
|
|
|
|
* The backslash character <code>\</code> can be used to escape
|
|
|
|
* <code>^</code> or <code>-</code> and is otherwise ignored unless it
|
|
|
|
* appears at the end of a range or the end of the +from_str+ or +to_str+:
|
|
|
|
*
|
|
|
|
* "hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld"
|
|
|
|
* "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld"
|
|
|
|
*
|
|
|
|
* "hello\r\nworld".tr("\r", "") #=> "hello\nworld"
|
|
|
|
* "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold"
|
|
|
|
* "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld"
|
|
|
|
*
|
|
|
|
* "X['\\b']".tr("X\\", "") #=> "['b']"
|
|
|
|
* "X['\\b']".tr("X-\\]", "") #=> "'b'"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_tr(VALUE str, VALUE src, VALUE repl)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
tr_trans(str, src, repl, 0);
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2010-09-01 11:52:16 +04:00
|
|
|
#define TR_TABLE_SIZE 257
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2010-09-01 11:52:16 +04:00
|
|
|
tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
|
2007-11-03 22:04:53 +03:00
|
|
|
VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-09-11 14:34:59 +04:00
|
|
|
const unsigned int errc = -1;
|
2007-11-03 22:04:53 +03:00
|
|
|
char buf[256];
|
1998-01-16 15:13:05 +03:00
|
|
|
struct tr tr;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int c;
|
2007-11-09 09:20:15 +03:00
|
|
|
VALUE table = 0, ptable = 0;
|
2008-09-11 14:34:59 +04:00
|
|
|
int i, l, cflag = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
tr.p = RSTRING_PTR(str); tr.pend = tr.p + RSTRING_LEN(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
tr.gen = tr.now = tr.max = 0;
|
2009-02-22 17:26:27 +03:00
|
|
|
|
2008-03-01 04:09:43 +03:00
|
|
|
if (RSTRING_LEN(str) > 1 && rb_enc_ascget(tr.p, tr.pend, &l, enc) == '^') {
|
|
|
|
cflag = 1;
|
|
|
|
tr.p += l;
|
2010-08-30 23:45:30 +04:00
|
|
|
}
|
2007-11-08 01:07:13 +03:00
|
|
|
if (first) {
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
stable[i] = 1;
|
|
|
|
}
|
2010-09-01 11:52:16 +04:00
|
|
|
stable[256] = cflag;
|
|
|
|
}
|
|
|
|
else if (stable[256] && !cflag) {
|
|
|
|
stable[256] = 0;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
buf[i] = cflag;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-09-11 14:34:59 +04:00
|
|
|
while ((c = trnext(&tr, enc)) != errc) {
|
2007-11-03 22:04:53 +03:00
|
|
|
if (c < 256) {
|
|
|
|
buf[c & 0xff] = !cflag;
|
|
|
|
}
|
|
|
|
else {
|
2008-09-11 14:34:59 +04:00
|
|
|
VALUE key = UINT2NUM(c);
|
* 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-25 07:29:39 +04:00
|
|
|
|
2012-03-25 13:20:04 +04:00
|
|
|
if (!table && (first || *tablep || stable[256])) {
|
2010-09-01 11:52:16 +04:00
|
|
|
if (cflag) {
|
|
|
|
ptable = *ctablep;
|
2012-03-25 13:20:04 +04:00
|
|
|
table = ptable ? ptable : rb_hash_new();
|
2010-09-01 11:52:16 +04:00
|
|
|
*ctablep = table;
|
|
|
|
}
|
|
|
|
else {
|
2012-03-25 13:20:04 +04:00
|
|
|
table = rb_hash_new();
|
2010-09-01 11:52:16 +04:00
|
|
|
ptable = *tablep;
|
|
|
|
*tablep = table;
|
|
|
|
}
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
2012-03-25 13:20:04 +04:00
|
|
|
if (table && (!ptable || (cflag ^ !NIL_P(rb_hash_aref(ptable, key))))) {
|
2007-11-03 22:04:53 +03:00
|
|
|
rb_hash_aset(table, key, Qtrue);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-03 22:04:53 +03:00
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
stable[i] = stable[i] && buf[i];
|
|
|
|
}
|
2012-03-25 13:20:04 +04:00
|
|
|
if (!table && !cflag) {
|
|
|
|
*tablep = 0;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2007-11-03 22:04:53 +03:00
|
|
|
static int
|
2010-09-01 11:52:16 +04:00
|
|
|
tr_find(unsigned int c, char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
|
2007-11-03 22:04:53 +03:00
|
|
|
{
|
|
|
|
if (c < 256) {
|
2009-09-18 19:17:17 +04:00
|
|
|
return table[c] != 0;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-09-11 14:34:59 +04:00
|
|
|
VALUE v = UINT2NUM(c);
|
2007-11-03 22:04:53 +03:00
|
|
|
|
2010-08-30 10:01:14 +04:00
|
|
|
if (del) {
|
|
|
|
if (!NIL_P(rb_hash_lookup(del, v)) &&
|
|
|
|
(!nodel || NIL_P(rb_hash_lookup(nodel, v)))) {
|
2009-09-18 19:17:17 +04:00
|
|
|
return TRUE;
|
2008-05-15 18:03:21 +04:00
|
|
|
}
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
2010-09-01 11:52:16 +04:00
|
|
|
else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
|
|
|
|
return FALSE;
|
2010-08-30 10:01:14 +04:00
|
|
|
}
|
2010-09-01 11:52:16 +04:00
|
|
|
return table[256] ? TRUE : FALSE;
|
2007-11-03 22:04:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.delete!([other_str]+) -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Performs a <code>delete</code> operation in place, returning <i>str</i>, or
|
|
|
|
* <code>nil</code> if <i>str</i> was not modified.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-09-01 11:52:16 +04:00
|
|
|
char squeez[TR_TABLE_SIZE];
|
2007-09-26 23:46:58 +04:00
|
|
|
rb_encoding *enc = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send, *t;
|
* 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-25 07:29:39 +04:00
|
|
|
VALUE del = 0, nodel = 0;
|
2000-02-08 11:54:01 +03:00
|
|
|
int modify = 0;
|
2009-02-24 20:17:35 +03:00
|
|
|
int i, ascompat, cr;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-05-07 20:15:45 +04:00
|
|
|
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
1999-08-13 09:45:20 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
VALUE s = argv[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(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-25 07:29:39 +04:00
|
|
|
enc = rb_enc_check(str, s);
|
2007-11-08 01:07:13 +03:00
|
|
|
tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
|
|
|
ascompat = rb_enc_asciicompat(enc);
|
2006-08-31 14:47:44 +04:00
|
|
|
s = t = RSTRING_PTR(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-25 07:29:39 +04:00
|
|
|
send = RSTRING_END(str);
|
2009-02-24 20:17:35 +03:00
|
|
|
cr = ascompat ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (s < send) {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
unsigned int c;
|
|
|
|
int clen;
|
* 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-25 07:29:39 +04:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (ascompat && (c = *(unsigned char*)s) < 0x80) {
|
|
|
|
if (squeez[c]) {
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (t != s) *t = c;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
else {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &clen, enc);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
|
|
|
|
if (tr_find(c, squeez, del, nodel)) {
|
|
|
|
modify = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (t != s) rb_enc_mbcput(c, t, enc);
|
|
|
|
t += clen;
|
2009-02-24 20:17:35 +03:00
|
|
|
if (cr == ENC_CODERANGE_7BIT) cr = ENC_CODERANGE_VALID;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
|
|
|
s += clen;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
*t = '\0';
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_LEN(str, t - RSTRING_PTR(str));
|
2009-02-24 20:17:35 +03:00
|
|
|
ENC_CODERANGE_SET(str, cr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-02-02 11:10:32 +03:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.delete([other_str]+) -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with all characters in the intersection of its
|
|
|
|
* arguments deleted. Uses the same rules for building the set of characters as
|
|
|
|
* <code>String#count</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".delete "l","lo" #=> "heo"
|
|
|
|
* "hello".delete "lo" #=> "he"
|
|
|
|
* "hello".delete "aeiou", "^e" #=> "hell"
|
|
|
|
* "hello".delete "ej-m" #=> "ho"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_delete(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_delete_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.squeeze!([other_str]*) -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Squeezes <i>str</i> in place, returning either <i>str</i>, or
|
|
|
|
* <code>nil</code> if no changes were made.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-09-01 11:52:16 +04:00
|
|
|
char squeez[TR_TABLE_SIZE];
|
2007-08-30 06:44:20 +04:00
|
|
|
rb_encoding *enc = 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-25 07:29:39 +04:00
|
|
|
VALUE del = 0, nodel = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send, *t;
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
int i, modify = 0;
|
2008-09-15 18:40:00 +04:00
|
|
|
int ascompat, singlebyte = single_byte_optimizable(str);
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
unsigned int save;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (argc == 0) {
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
VALUE s = argv[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(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-25 07:29:39 +04:00
|
|
|
enc = rb_enc_check(str, s);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (singlebyte && !single_byte_optimizable(s))
|
|
|
|
singlebyte = 0;
|
2007-11-08 01:07:13 +03:00
|
|
|
tr_setup_table(s, squeez, i==0, &del, &nodel, enc);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
s = t = RSTRING_PTR(str);
|
|
|
|
if (!s || RSTRING_LEN(str) == 0) return Qnil;
|
* 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-25 07:29:39 +04:00
|
|
|
send = RSTRING_END(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
save = -1;
|
2008-09-15 18:40:00 +04:00
|
|
|
ascompat = rb_enc_asciicompat(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-25 07:29:39 +04:00
|
|
|
|
2008-09-15 18:40:00 +04:00
|
|
|
if (singlebyte) {
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c = *(unsigned char*)s++;
|
|
|
|
if (c != save || (argc > 0 && !squeez[c])) {
|
|
|
|
*t++ = save = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (s < send) {
|
|
|
|
unsigned int c;
|
|
|
|
int clen;
|
|
|
|
|
|
|
|
if (ascompat && (c = *(unsigned char*)s) < 0x80) {
|
|
|
|
if (c != save || (argc > 0 && !squeez[c])) {
|
|
|
|
*t++ = save = c;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &clen, enc);
|
2008-09-15 18:40:00 +04:00
|
|
|
|
|
|
|
if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) {
|
|
|
|
if (t != s) rb_enc_mbcput(c, t, enc);
|
|
|
|
save = c;
|
|
|
|
t += clen;
|
|
|
|
}
|
|
|
|
s += clen;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-15 18:40:00 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
*t = '\0';
|
2006-08-31 14:47:44 +04:00
|
|
|
if (t - RSTRING_PTR(str) != RSTRING_LEN(str)) {
|
|
|
|
STR_SET_LEN(str, t - RSTRING_PTR(str));
|
2000-02-08 11:54:01 +03:00
|
|
|
modify = 1;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-08 11:54:01 +03:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2009-02-22 17:26:27 +03:00
|
|
|
/*
|
2003-12-25 00:14:46 +03:00
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.squeeze([other_str]*) -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Builds a set of characters from the <i>other_str</i> parameter(s) using the
|
|
|
|
* procedure described for <code>String#count</code>. Returns a new string
|
|
|
|
* where runs of the same character that occur in this set are replaced by a
|
|
|
|
* single character. If no arguments are given, all runs of identical
|
|
|
|
* characters are replaced by a single character.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "yellow moon".squeeze #=> "yelow mon"
|
|
|
|
* " now is the".squeeze(" ") #=> " now is the"
|
|
|
|
* "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_squeeze(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_squeeze_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.tr_s!(from_str, to_str) -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Performs <code>String#tr_s</code> processing on <i>str</i> in place,
|
|
|
|
* returning <i>str</i>, or <code>nil</code> if no changes were made.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_tr_s_bang(VALUE str, VALUE src, VALUE repl)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-08 11:54:01 +03:00
|
|
|
return tr_trans(str, src, repl, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.tr_s(from_str, to_str) -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Processes a copy of <i>str</i> as described under <code>String#tr</code>,
|
|
|
|
* then removes duplicate characters in regions that were affected by the
|
|
|
|
* translation.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".tr_s('l', 'r') #=> "hero"
|
|
|
|
* "hello".tr_s('el', '*') #=> "h*o"
|
|
|
|
* "hello".tr_s('el', 'hx') #=> "hhxo"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
tr_trans(str, src, repl, 1);
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.count([other_str]+) -> fixnum
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-04-11 01:11:37 +04:00
|
|
|
* Each +other_str+ parameter defines a set of characters to count. The
|
|
|
|
* intersection of these sets defines the characters to count in +str+. Any
|
|
|
|
* +other_str+ that starts with a caret <code>^</code> is negated. The
|
|
|
|
* sequence <code>c1-c2</code> means all characters between c1 and c2. The
|
|
|
|
* backslash character <code>\</code> can be used to escape <code>^</code> or
|
|
|
|
* <code>-</code> and is otherwise ignored unless it appears at the end of a
|
|
|
|
* sequence or the end of a +other_str+.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a = "hello world"
|
2012-04-11 01:11:37 +04:00
|
|
|
* a.count "lo" #=> 5
|
|
|
|
* a.count "lo", "o" #=> 2
|
|
|
|
* a.count "hello", "^l" #=> 4
|
|
|
|
* a.count "ej-m" #=> 4
|
|
|
|
*
|
|
|
|
* "hello^world".count "\\^aeiou" #=> 4
|
|
|
|
* "hello-world".count "a\\-eo" #=> 4
|
|
|
|
*
|
|
|
|
* c = "hello world\\r\\n"
|
|
|
|
* c.count "\\" #=> 2
|
|
|
|
* c.count "\\A" #=> 0
|
|
|
|
* c.count "X-\\w" #=> 3
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_count(int argc, VALUE *argv, VALUE str)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
2010-09-01 11:52:16 +04:00
|
|
|
char table[TR_TABLE_SIZE];
|
2007-09-26 23:46:58 +04:00
|
|
|
rb_encoding *enc = 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-25 07:29:39 +04:00
|
|
|
VALUE del = 0, nodel = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
char *s, *send;
|
|
|
|
int i;
|
2008-09-15 18:40:00 +04:00
|
|
|
int ascompat;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
1999-08-13 09:45:20 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
2009-05-22 00:08:03 +04:00
|
|
|
VALUE tstr = argv[i];
|
|
|
|
unsigned char c;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2009-05-22 00:08:03 +04:00
|
|
|
StringValue(tstr);
|
|
|
|
enc = rb_enc_check(str, tstr);
|
|
|
|
if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
|
|
|
|
(c = RSTRING_PTR(tstr)[0]) < 0x80 && !is_broken_string(str)) {
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
|
|
|
|
send = RSTRING_END(str);
|
|
|
|
while (s < send) {
|
|
|
|
if (*(unsigned char*)s++ == c) n++;
|
|
|
|
}
|
|
|
|
return INT2NUM(n);
|
|
|
|
}
|
|
|
|
tr_setup_table(tstr, table, i==0, &del, &nodel, enc);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
if (!s || RSTRING_LEN(str) == 0) return INT2FIX(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-25 07:29:39 +04:00
|
|
|
send = RSTRING_END(str);
|
2008-09-15 18:40:00 +04:00
|
|
|
ascompat = rb_enc_asciicompat(enc);
|
2009-05-22 00:08:03 +04:00
|
|
|
i = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
while (s < send) {
|
2008-09-15 18:40:00 +04:00
|
|
|
unsigned int c;
|
* 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-25 07:29:39 +04:00
|
|
|
|
2008-09-15 18:40:00 +04:00
|
|
|
if (ascompat && (c = *(unsigned char*)s) < 0x80) {
|
|
|
|
if (table[c]) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
2010-08-30 10:01:14 +04:00
|
|
|
int clen;
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(s, send, &clen, enc);
|
2008-09-15 18:40:00 +04:00
|
|
|
if (tr_find(c, table, del, nodel)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
s += clen;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-15 18:40:00 +04:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
return INT2NUM(i);
|
|
|
|
}
|
|
|
|
|
2009-04-20 19:04:18 +04:00
|
|
|
static const char isspacetable[256] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ascii_isspace(c) isspacetable[(unsigned char)(c)]
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.split(pattern=$;, [limit]) -> anArray
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Divides <i>str</i> into substrings based on a delimiter, returning an array
|
|
|
|
* of these substrings.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If <i>pattern</i> is a <code>String</code>, then its contents are used as
|
|
|
|
* the delimiter when splitting <i>str</i>. If <i>pattern</i> is a single
|
|
|
|
* space, <i>str</i> is split on whitespace, with leading whitespace and runs
|
|
|
|
* of contiguous whitespace characters ignored.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If <i>pattern</i> is a <code>Regexp</code>, <i>str</i> is divided where the
|
|
|
|
* pattern matches. Whenever the pattern matches a zero-length string,
|
2006-08-13 09:35:35 +04:00
|
|
|
* <i>str</i> is split into individual characters. If <i>pattern</i> contains
|
|
|
|
* groups, the respective matches will be returned in the array as well.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If <i>pattern</i> is omitted, the value of <code>$;</code> is used. If
|
|
|
|
* <code>$;</code> is <code>nil</code> (which is the default), <i>str</i> is
|
|
|
|
* split on whitespace as if ` ' were specified.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If the <i>limit</i> parameter is omitted, trailing null fields are
|
* array.c, enum.c, eval.c, file.c, io.c, numeric.c, object.c, prec.c,
process.c, re.c, string.c: typos in RDoc comments. [ruby-core:02783]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-14 08:06:52 +04:00
|
|
|
* suppressed. If <i>limit</i> is a positive number, at most that number of
|
2003-12-25 00:14:46 +03:00
|
|
|
* fields will be returned (if <i>limit</i> is <code>1</code>, the entire
|
|
|
|
* string is returned as the only entry in an array). If negative, there is no
|
|
|
|
* limit to the number of fields returned, and trailing null fields are not
|
* array.c, enum.c, eval.c, file.c, io.c, numeric.c, object.c, prec.c,
process.c, re.c, string.c: typos in RDoc comments. [ruby-core:02783]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-14 08:06:52 +04:00
|
|
|
* suppressed.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-04-10 04:57:40 +04:00
|
|
|
* When the input +str+ is empty an empty Array is returned as the string is
|
|
|
|
* considered to have no fields to split.
|
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " now's the time".split #=> ["now's", "the", "time"]
|
|
|
|
* " now's the time".split(' ') #=> ["now's", "the", "time"]
|
|
|
|
* " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
|
|
|
|
* "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
|
|
|
|
* "hello".split(//) #=> ["h", "e", "l", "l", "o"]
|
|
|
|
* "hello".split(//, 3) #=> ["h", "e", "llo"]
|
|
|
|
* "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "mellow yellow".split("ello") #=> ["m", "w y", "w"]
|
|
|
|
* "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
|
|
|
|
* "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
|
|
|
|
* "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
|
2012-04-10 04:57:40 +04:00
|
|
|
*
|
|
|
|
* "".split(',', -1) #=> []
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_split_m(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE spat;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE limit;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
enum {awk, string, regexp} split_type;
|
2002-05-29 09:20:39 +04:00
|
|
|
long beg, end, i = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
int lim = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE result, tmp;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
|
1998-01-16 15:13:05 +03:00
|
|
|
lim = NUM2INT(limit);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (lim <= 0) limit = Qnil;
|
2003-07-24 09:18:47 +04:00
|
|
|
else if (lim == 1) {
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) == 0)
|
2003-07-24 09:18:47 +04:00
|
|
|
return rb_ary_new2(0);
|
|
|
|
return rb_ary_new3(1, str);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2002-07-11 12:22:18 +04:00
|
|
|
if (NIL_P(spat)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!NIL_P(rb_fs)) {
|
|
|
|
spat = rb_fs;
|
1998-01-16 15:13:05 +03:00
|
|
|
goto fs_set;
|
|
|
|
}
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
split_type = awk;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-01-05 07:41:21 +03:00
|
|
|
fs_set:
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(spat, T_STRING)) {
|
2008-02-29 16:33:26 +03:00
|
|
|
rb_encoding *enc2 = STR_ENC_GET(spat);
|
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
split_type = string;
|
|
|
|
if (RSTRING_LEN(spat) == 0) {
|
|
|
|
/* Special case - split into chars */
|
|
|
|
spat = rb_reg_regcomp(spat);
|
|
|
|
split_type = regexp;
|
|
|
|
}
|
2008-09-30 20:18:27 +04:00
|
|
|
else if (rb_enc_asciicompat(enc2) == 1) {
|
2008-02-29 16:33:26 +03:00
|
|
|
if (RSTRING_LEN(spat) == 1 && RSTRING_PTR(spat)[0] == ' '){
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
split_type = awk;
|
2008-02-29 16:33:26 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-03-01 04:09:43 +03:00
|
|
|
int l;
|
|
|
|
if (rb_enc_ascget(RSTRING_PTR(spat), RSTRING_END(spat), &l, enc2) == ' ' &&
|
|
|
|
RSTRING_LEN(spat) == l) {
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
split_type = awk;
|
2008-02-29 16:33:26 +03:00
|
|
|
}
|
|
|
|
}
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-09-11 08:05:36 +04:00
|
|
|
spat = get_pat(spat, 1);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
split_type = regexp;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
result = rb_ary_new();
|
1998-01-16 15:13:05 +03:00
|
|
|
beg = 0;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (split_type == awk) {
|
2006-08-31 14:47:44 +04:00
|
|
|
char *ptr = RSTRING_PTR(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-25 07:29:39 +04:00
|
|
|
char *eptr = RSTRING_END(str);
|
2007-12-10 07:50:35 +03:00
|
|
|
char *bptr = ptr;
|
2002-07-26 10:12:39 +04:00
|
|
|
int skip = 1;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int c;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-08-30 06:44:20 +04:00
|
|
|
end = beg;
|
2009-04-20 19:04:18 +04:00
|
|
|
if (is_ascii_string(str)) {
|
|
|
|
while (ptr < eptr) {
|
|
|
|
c = (unsigned char)*ptr++;
|
|
|
|
if (skip) {
|
|
|
|
if (ascii_isspace(c)) {
|
|
|
|
beg = ptr - bptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
end = ptr - bptr;
|
|
|
|
skip = 0;
|
|
|
|
if (!NIL_P(limit) && lim <= i) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ascii_isspace(c)) {
|
|
|
|
rb_ary_push(result, rb_str_subseq(str, beg, end-beg));
|
|
|
|
skip = 1;
|
2007-12-10 07:50:35 +03:00
|
|
|
beg = ptr - bptr;
|
2009-04-20 19:04:18 +04:00
|
|
|
if (!NIL_P(limit)) ++i;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2007-12-10 07:50:35 +03:00
|
|
|
end = ptr - bptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2009-04-20 19:04:18 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (ptr < eptr) {
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
c = rb_enc_codepoint_len(ptr, eptr, &n, enc);
|
|
|
|
ptr += n;
|
2009-04-20 19:04:18 +04:00
|
|
|
if (skip) {
|
2009-09-15 09:27:29 +04:00
|
|
|
if (rb_isspace(c)) {
|
2009-04-20 19:04:18 +04:00
|
|
|
beg = ptr - bptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
end = ptr - bptr;
|
|
|
|
skip = 0;
|
|
|
|
if (!NIL_P(limit) && lim <= i) break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-15 09:27:29 +04:00
|
|
|
else if (rb_isspace(c)) {
|
2007-12-10 07:50:35 +03:00
|
|
|
rb_ary_push(result, rb_str_subseq(str, beg, end-beg));
|
2002-07-26 10:12:39 +04:00
|
|
|
skip = 1;
|
2007-12-10 07:50:35 +03:00
|
|
|
beg = ptr - bptr;
|
2003-06-27 09:41:28 +04:00
|
|
|
if (!NIL_P(limit)) ++i;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-07-26 10:12:39 +04:00
|
|
|
else {
|
2007-12-10 07:50:35 +03:00
|
|
|
end = ptr - bptr;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
else if (split_type == string) {
|
|
|
|
char *ptr = RSTRING_PTR(str);
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
char *temp = ptr;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
char *eptr = RSTRING_END(str);
|
|
|
|
char *sptr = RSTRING_PTR(spat);
|
2009-09-18 19:17:17 +04:00
|
|
|
long slen = RSTRING_LEN(spat);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
|
|
|
|
if (is_broken_string(str)) {
|
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(str)));
|
|
|
|
}
|
|
|
|
if (is_broken_string(spat)) {
|
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(STR_ENC_GET(spat)));
|
|
|
|
}
|
|
|
|
enc = rb_enc_check(str, spat);
|
|
|
|
while (ptr < eptr &&
|
|
|
|
(end = rb_memsearch(sptr, slen, ptr, eptr - ptr, enc)) >= 0) {
|
|
|
|
/* Check we are at the start of a char */
|
|
|
|
char *t = rb_enc_right_char_head(ptr, ptr + end, eptr, enc);
|
|
|
|
if (t != ptr + end) {
|
|
|
|
ptr = t;
|
|
|
|
continue;
|
|
|
|
}
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
rb_ary_push(result, rb_str_subseq(str, ptr - temp, end));
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
ptr += end + slen;
|
|
|
|
if (!NIL_P(limit) && lim <= ++i) break;
|
|
|
|
}
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
beg = ptr - temp;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
char *ptr = RSTRING_PTR(str);
|
|
|
|
long len = RSTRING_LEN(str);
|
2000-02-29 11:05:32 +03:00
|
|
|
long start = beg;
|
|
|
|
long idx;
|
1998-01-16 15:13:05 +03:00
|
|
|
int last_null = 0;
|
|
|
|
struct re_registers *regs;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while ((end = rb_reg_search(spat, str, start, 0)) >= 0) {
|
2008-02-16 23:08:35 +03:00
|
|
|
regs = RMATCH_REGS(rb_backref_get());
|
1998-01-16 15:13:05 +03:00
|
|
|
if (start == end && BEG(0) == END(0)) {
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
if (!ptr) {
|
2010-02-23 18:41:34 +03:00
|
|
|
rb_ary_push(result, str_new_empty(str));
|
2002-10-24 21:54:53 +04:00
|
|
|
break;
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
2002-10-24 21:54:53 +04:00
|
|
|
else if (last_null == 1) {
|
2007-08-28 10:45:32 +04:00
|
|
|
rb_ary_push(result, rb_str_subseq(str, beg,
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
rb_enc_fast_mbclen(ptr+beg,
|
|
|
|
ptr+len,
|
2009-05-20 08:44:36 +04:00
|
|
|
enc)));
|
1998-01-16 15:13:05 +03:00
|
|
|
beg = start;
|
|
|
|
}
|
|
|
|
else {
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
if (ptr+start == ptr+len)
|
2007-12-08 05:50:43 +03:00
|
|
|
start++;
|
|
|
|
else
|
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
calls inside of the loop by keeping pointers in local
variables. a patch from Masahiro Kanai (CanI) in [ruby-dev:39406].
It was found and fixed at Security and Programming camp 2009.
* string.c (rb_str_{times, split_m}): ditto.
* struct.c (rb_struct_{getmember, set, aref_id, aset_id}, {make,
inspect}_struct, recursive_{equal, hash, eql}): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-10-05 18:35:39 +04:00
|
|
|
start += rb_enc_fast_mbclen(ptr+start,ptr+len,enc);
|
1998-01-16 15:13:05 +03:00
|
|
|
last_null = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-08-28 10:45:32 +04:00
|
|
|
rb_ary_push(result, rb_str_subseq(str, beg, end-beg));
|
1998-01-16 15:13:05 +03:00
|
|
|
beg = start = END(0);
|
|
|
|
}
|
|
|
|
last_null = 0;
|
|
|
|
|
|
|
|
for (idx=1; idx < regs->num_regs; idx++) {
|
|
|
|
if (BEG(idx) == -1) continue;
|
|
|
|
if (BEG(idx) == END(idx))
|
2010-02-23 18:41:34 +03:00
|
|
|
tmp = str_new_empty(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
2007-08-28 10:45:32 +04:00
|
|
|
tmp = rb_str_subseq(str, BEG(idx), END(idx)-BEG(idx));
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(result, tmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
if (!NIL_P(limit) && lim <= ++i) break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > 0 && (!NIL_P(limit) || RSTRING_LEN(str) > beg || lim < 0)) {
|
|
|
|
if (RSTRING_LEN(str) == beg)
|
2010-02-23 18:41:34 +03:00
|
|
|
tmp = str_new_empty(str);
|
2000-06-23 11:05:59 +04:00
|
|
|
else
|
2007-08-28 10:45:32 +04:00
|
|
|
tmp = rb_str_subseq(str, beg, RSTRING_LEN(str)-beg);
|
2000-06-23 11:05:59 +04:00
|
|
|
rb_ary_push(result, tmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
if (NIL_P(limit) && lim == 0) {
|
2008-12-12 04:19:23 +03:00
|
|
|
long len;
|
|
|
|
while ((len = RARRAY_LEN(result)) > 0 &&
|
|
|
|
(tmp = RARRAY_PTR(result)[len-1], RSTRING_LEN(tmp) == 0))
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_pop(result);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:44:21 +04:00
|
|
|
rb_str_split(VALUE str, const char *sep0)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE sep;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
sep = rb_str_new2(sep0);
|
2000-01-05 07:41:21 +03:00
|
|
|
return rb_str_split_m(1, &sep, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2006-09-21 04:43:16 +04:00
|
|
|
|
|
|
|
/*
|
2003-12-25 00:14:46 +03:00
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.each_line(separator=$/) {|substr| block } -> str
|
|
|
|
* str.each_line(separator=$/) -> an_enumerator
|
2010-03-07 13:22:05 +03:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.lines(separator=$/) {|substr| block } -> str
|
|
|
|
* str.lines(separator=$/) -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Splits <i>str</i> using the supplied parameter as the record separator
|
|
|
|
* (<code>$/</code> by default), passing each substring in turn to the supplied
|
2008-05-10 13:17:59 +04:00
|
|
|
* block. If a zero-length record separator is supplied, the string is split
|
|
|
|
* into paragraphs delimited by multiple successive newlines.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-03-07 13:22:05 +03:00
|
|
|
* If no block is given, an enumerator is returned instead.
|
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* print "Example one\n"
|
2009-11-09 09:50:29 +03:00
|
|
|
* "hello\nworld".each_line {|s| p s}
|
2003-12-25 00:14:46 +03:00
|
|
|
* print "Example two\n"
|
2009-11-09 09:50:29 +03:00
|
|
|
* "hello\nworld".each_line('l') {|s| p s}
|
2003-12-25 00:14:46 +03:00
|
|
|
* print "Example three\n"
|
2009-11-09 09:50:29 +03:00
|
|
|
* "hello\n\n\nworld".each_line('') {|s| p s}
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Example one
|
|
|
|
* "hello\n"
|
|
|
|
* "world"
|
|
|
|
* Example two
|
|
|
|
* "hel"
|
|
|
|
* "l"
|
|
|
|
* "o\nworl"
|
|
|
|
* "d"
|
|
|
|
* Example three
|
|
|
|
* "hello\n\n\n"
|
|
|
|
* "world"
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_each_line(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE rs;
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int newline;
|
2009-02-25 06:29:25 +03:00
|
|
|
const char *p, *pend, *s, *ptr;
|
2009-02-22 17:26:27 +03:00
|
|
|
long len, rslen;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE line;
|
2008-01-21 22:47:26 +03:00
|
|
|
int n;
|
2008-05-04 07:59:01 +04:00
|
|
|
VALUE orig = str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
if (argc == 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rs = rb_rs;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-03-05 08:22:17 +03:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &rs);
|
|
|
|
}
|
2005-08-30 18:49:51 +04:00
|
|
|
RETURN_ENUMERATOR(str, argc, argv);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (NIL_P(rs)) {
|
|
|
|
rb_yield(str);
|
2008-05-04 07:59:01 +04:00
|
|
|
return orig;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2008-01-23 21:43:51 +03:00
|
|
|
str = rb_str_new4(str);
|
|
|
|
ptr = p = s = RSTRING_PTR(str);
|
|
|
|
pend = p + RSTRING_LEN(str);
|
|
|
|
len = RSTRING_LEN(str);
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(rs);
|
2008-01-21 22:47:26 +03:00
|
|
|
if (rs == rb_default_rs) {
|
2008-01-23 21:43:51 +03:00
|
|
|
enc = rb_enc_get(str);
|
2008-01-21 22:47:26 +03:00
|
|
|
while (p < pend) {
|
2008-01-23 21:43:51 +03:00
|
|
|
char *p0;
|
|
|
|
|
|
|
|
p = memchr(p, '\n', pend - p);
|
|
|
|
if (!p) break;
|
2008-09-13 23:23:52 +04:00
|
|
|
p0 = rb_enc_left_char_head(s, p, pend, enc);
|
2008-01-23 21:43:51 +03:00
|
|
|
if (!rb_enc_is_newline(p0, pend, enc)) {
|
|
|
|
p++;
|
|
|
|
continue;
|
2008-01-21 22:47:26 +03:00
|
|
|
}
|
2008-02-18 09:18:31 +03:00
|
|
|
p = p0 + rb_enc_mbclen(p0, pend, enc);
|
2008-01-23 21:43:51 +03:00
|
|
|
line = rb_str_new5(str, s, p - s);
|
|
|
|
OBJ_INFECT(line, str);
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(line, str);
|
2008-01-23 21:43:51 +03:00
|
|
|
rb_yield(line);
|
|
|
|
str_mod_check(str, ptr, len);
|
|
|
|
s = p;
|
2008-01-21 22:47:26 +03:00
|
|
|
}
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2008-01-23 21:43:51 +03:00
|
|
|
enc = rb_enc_check(str, rs);
|
2006-08-31 14:47:44 +04:00
|
|
|
rslen = RSTRING_LEN(rs);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (rslen == 0) {
|
|
|
|
newline = '\n';
|
|
|
|
}
|
|
|
|
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-25 07:29:39 +04:00
|
|
|
newline = rb_enc_codepoint(RSTRING_PTR(rs), RSTRING_END(rs), enc);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
while (p < pend) {
|
* 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 20:59:22 +04:00
|
|
|
unsigned int c = rb_enc_codepoint_len(p, pend, &n, 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-25 07:29:39 +04:00
|
|
|
|
2008-05-10 13:17:59 +04:00
|
|
|
again:
|
2007-08-27 18:32:15 +04:00
|
|
|
if (rslen == 0 && c == newline) {
|
2008-05-10 13:17:59 +04:00
|
|
|
p += n;
|
* 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 20:59:22 +04:00
|
|
|
if (p < pend && (c = rb_enc_codepoint_len(p, pend, &n, enc)) != newline) {
|
2008-05-10 13:17:59 +04:00
|
|
|
goto again;
|
|
|
|
}
|
2007-12-25 12:28:31 +03:00
|
|
|
while (p < pend && rb_enc_codepoint(p, pend, enc) == newline) {
|
2007-08-27 18:32:15 +04:00
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
p -= n;
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
if (c == newline &&
|
2011-04-20 09:43:27 +04:00
|
|
|
(rslen <= 1 ||
|
|
|
|
(pend - p >= rslen && memcmp(RSTRING_PTR(rs), p, rslen) == 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-25 07:29:39 +04:00
|
|
|
line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
|
2001-10-03 11:19:19 +04:00
|
|
|
OBJ_INFECT(line, str);
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(line, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(line);
|
2004-10-01 19:56:05 +04:00
|
|
|
str_mod_check(str, ptr, len);
|
2007-12-19 20:46:17 +03:00
|
|
|
s = p + (rslen ? rslen : n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
p += n;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-01-21 22:47:26 +03:00
|
|
|
finish:
|
1998-01-16 15:13:05 +03:00
|
|
|
if (s != pend) {
|
2008-01-23 21:43:51 +03:00
|
|
|
line = rb_str_new5(str, s, pend - s);
|
2000-02-01 06:12:21 +03:00
|
|
|
OBJ_INFECT(line, str);
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(line, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(line);
|
|
|
|
}
|
|
|
|
|
2008-05-04 07:59:01 +04:00
|
|
|
return orig;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
2006-10-18 08:30:40 +04:00
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.bytes {|fixnum| block } -> str
|
|
|
|
* str.bytes -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.each_byte {|fixnum| block } -> str
|
|
|
|
* str.each_byte -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-03-07 13:22:05 +03:00
|
|
|
* Passes each byte in <i>str</i> to the given block, or returns
|
|
|
|
* an enumerator if no block is given.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".each_byte {|c| print c, ' ' }
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* 104 101 108 108 111
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_each_byte(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-29 11:05:32 +03:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2005-08-30 18:49:51 +04:00
|
|
|
RETURN_ENUMERATOR(str, 0, 0);
|
2006-08-31 14:47:44 +04:00
|
|
|
for (i=0; i<RSTRING_LEN(str); i++) {
|
|
|
|
rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.chars {|cstr| block } -> str
|
|
|
|
* str.chars -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.each_char {|cstr| block } -> str
|
|
|
|
* str.each_char -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-03-07 13:22:05 +03:00
|
|
|
* Passes each character in <i>str</i> to the given block, or returns
|
|
|
|
* an enumerator if no block is given.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* "hello".each_char {|c| print c, ' ' }
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* 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-25 07:29:39 +04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
|
|
|
* h e l l o
|
* 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-25 07:29:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_each_char(VALUE str)
|
|
|
|
{
|
2009-05-23 19:59:09 +04:00
|
|
|
VALUE orig = str;
|
2009-05-25 22:38:46 +04:00
|
|
|
long i, len, n;
|
2008-01-22 03:26:49 +03:00
|
|
|
const char *ptr;
|
2008-01-20 07:06:03 +03:00
|
|
|
rb_encoding *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-25 07:29:39 +04:00
|
|
|
|
|
|
|
RETURN_ENUMERATOR(str, 0, 0);
|
2008-01-22 03:26:49 +03:00
|
|
|
str = rb_str_new4(str);
|
2008-01-20 07:06:03 +03:00
|
|
|
ptr = RSTRING_PTR(str);
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
enc = rb_enc_get(str);
|
2009-05-20 08:44:36 +04:00
|
|
|
switch (ENC_CODERANGE(str)) {
|
|
|
|
case ENC_CODERANGE_VALID:
|
|
|
|
case ENC_CODERANGE_7BIT:
|
|
|
|
for (i = 0; i < len; i += n) {
|
|
|
|
n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
|
|
|
|
rb_yield(rb_str_subseq(str, i, n));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
for (i = 0; i < len; i += n) {
|
|
|
|
n = rb_enc_mbclen(ptr + i, ptr + len, enc);
|
|
|
|
rb_yield(rb_str_subseq(str, i, n));
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
2009-05-23 19:59:09 +04:00
|
|
|
return orig;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
|
2008-09-16 07:14:41 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.codepoints {|integer| block } -> str
|
|
|
|
* str.codepoints -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.each_codepoint {|integer| block } -> str
|
|
|
|
* str.each_codepoint -> an_enumerator
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-09-16 07:14:41 +04:00
|
|
|
* Passes the <code>Integer</code> ordinal of each character in <i>str</i>,
|
|
|
|
* also known as a <i>codepoint</i> when applied to Unicode strings to the
|
|
|
|
* given block.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2010-03-07 13:22:05 +03:00
|
|
|
* If no block is given, an enumerator is returned instead.
|
|
|
|
*
|
2008-09-16 07:14:41 +04:00
|
|
|
* "hello\u0639".each_codepoint {|c| print c, ' ' }
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-09-16 07:14:41 +04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2008-09-16 07:14:41 +04:00
|
|
|
* 104 101 108 108 111 1593
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_each_codepoint(VALUE str)
|
|
|
|
{
|
2009-05-23 19:59:09 +04:00
|
|
|
VALUE orig = str;
|
2009-05-25 22:38:46 +04:00
|
|
|
int n;
|
2008-09-16 07:14:41 +04:00
|
|
|
unsigned int c;
|
|
|
|
const char *ptr, *end;
|
|
|
|
rb_encoding *enc;
|
|
|
|
|
|
|
|
if (single_byte_optimizable(str)) return rb_str_each_byte(str);
|
|
|
|
RETURN_ENUMERATOR(str, 0, 0);
|
2009-05-23 19:59:09 +04:00
|
|
|
str = rb_str_new4(str);
|
2008-09-16 07:14:41 +04:00
|
|
|
ptr = RSTRING_PTR(str);
|
|
|
|
end = RSTRING_END(str);
|
|
|
|
enc = STR_ENC_GET(str);
|
|
|
|
while (ptr < end) {
|
* 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 20:59:22 +04:00
|
|
|
c = rb_enc_codepoint_len(ptr, end, &n, enc);
|
2008-09-16 17:23:16 +04:00
|
|
|
rb_yield(UINT2NUM(c));
|
2008-09-16 07:14:41 +04:00
|
|
|
ptr += n;
|
|
|
|
}
|
2009-05-23 19:59:09 +04:00
|
|
|
return orig;
|
2008-09-16 07:14:41 +04:00
|
|
|
}
|
|
|
|
|
2007-09-06 07:42:12 +04:00
|
|
|
static long
|
|
|
|
chopped_length(VALUE str)
|
|
|
|
{
|
2008-01-07 19:28:43 +03:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
2007-09-06 07:42:12 +04:00
|
|
|
const char *p, *p2, *beg, *end;
|
|
|
|
|
|
|
|
beg = RSTRING_PTR(str);
|
|
|
|
end = beg + RSTRING_LEN(str);
|
|
|
|
if (beg > end) return 0;
|
2008-09-13 22:22:04 +04:00
|
|
|
p = rb_enc_prev_char(beg, end, end, enc);
|
2007-09-06 07:42:12 +04:00
|
|
|
if (!p) return 0;
|
2009-10-27 10:36:54 +03:00
|
|
|
if (p > beg && rb_enc_ascget(p, end, 0, enc) == '\n') {
|
2008-09-13 22:22:04 +04:00
|
|
|
p2 = rb_enc_prev_char(beg, p, end, enc);
|
2009-10-27 10:36:54 +03:00
|
|
|
if (p2 && rb_enc_ascget(p2, end, 0, enc) == '\r') p = p2;
|
2007-09-06 07:42:12 +04:00
|
|
|
}
|
|
|
|
return p - beg;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.chop! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Processes <i>str</i> as for <code>String#chop</code>, returning <i>str</i>,
|
|
|
|
* or <code>nil</code> if <i>str</i> is the empty string. See also
|
|
|
|
* <code>String#chomp!</code>.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_chop_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-06-02 19:24:23 +04:00
|
|
|
str_modify_keep_cr(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > 0) {
|
2007-09-06 07:42:12 +04:00
|
|
|
long len;
|
|
|
|
len = chopped_length(str);
|
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
2009-06-02 19:24:23 +04:00
|
|
|
if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
|
|
|
|
ENC_CODERANGE_CLEAR(str);
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.chop -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a new <code>String</code> with the last character removed. If the
|
|
|
|
* string ends with <code>\r\n</code>, both characters are removed. Applying
|
|
|
|
* <code>chop</code> to an empty string returns an empty
|
|
|
|
* string. <code>String#chomp</code> is often a safer alternative, as it leaves
|
|
|
|
* the string unchanged if it doesn't end in a record separator.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "string\r\n".chop #=> "string"
|
|
|
|
* "string\n\r".chop #=> "string\n"
|
|
|
|
* "string\n".chop #=> "string"
|
|
|
|
* "string".chop #=> "strin"
|
|
|
|
* "x".chop.chop #=> ""
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_chop(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2007-09-06 07:42:12 +04:00
|
|
|
VALUE str2 = rb_str_new5(str, RSTRING_PTR(str), chopped_length(str));
|
2008-02-23 13:29:13 +03:00
|
|
|
rb_enc_cr_str_copy_for_substr(str2, str);
|
2007-09-06 07:42:12 +04:00
|
|
|
OBJ_INFECT(str2, str);
|
|
|
|
return str2;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.chomp!(separator=$/) -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Modifies <i>str</i> in place as described for <code>String#chomp</code>,
|
|
|
|
* returning <i>str</i>, or <code>nil</code> if no modifications were made.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2007-12-19 17:15:05 +03:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rs;
|
|
|
|
int newline;
|
2007-12-19 17:15:05 +03:00
|
|
|
char *p, *pp, *e;
|
2004-10-01 19:56:05 +04:00
|
|
|
long len, rslen;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2009-06-02 19:24:23 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-02-29 16:33:26 +03:00
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
if (len == 0) return Qnil;
|
|
|
|
p = RSTRING_PTR(str);
|
|
|
|
e = p + len;
|
2008-03-05 08:22:17 +03:00
|
|
|
if (argc == 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rs = rb_rs;
|
2001-10-30 11:43:28 +03:00
|
|
|
if (rs == rb_default_rs) {
|
|
|
|
smart_chomp:
|
2008-02-29 16:33:26 +03:00
|
|
|
enc = rb_enc_get(str);
|
|
|
|
if (rb_enc_mbminlen(enc) > 1) {
|
2008-09-13 23:23:52 +04:00
|
|
|
pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
|
2008-02-29 16:33:26 +03:00
|
|
|
if (rb_enc_is_newline(pp, e, enc)) {
|
|
|
|
e = pp;
|
2001-10-30 11:43:28 +03:00
|
|
|
}
|
2008-03-01 04:09:43 +03:00
|
|
|
pp = e - rb_enc_mbminlen(enc);
|
|
|
|
if (pp >= p) {
|
2008-09-13 23:23:52 +04:00
|
|
|
pp = rb_enc_left_char_head(p, pp, e, enc);
|
2008-03-01 04:09:43 +03:00
|
|
|
if (rb_enc_ascget(pp, e, 0, enc) == '\r') {
|
|
|
|
e = pp;
|
2008-02-29 16:33:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e == RSTRING_END(str)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2008-03-01 04:09:43 +03:00
|
|
|
len = e - RSTRING_PTR(str);
|
2008-02-29 16:33:26 +03:00
|
|
|
STR_SET_LEN(str, len);
|
2001-10-30 11:43:28 +03:00
|
|
|
}
|
2001-11-08 09:43:14 +03:00
|
|
|
else {
|
2008-02-29 16:33:26 +03:00
|
|
|
if (RSTRING_PTR(str)[len-1] == '\n') {
|
|
|
|
STR_DEC_LEN(str);
|
|
|
|
if (RSTRING_LEN(str) > 0 &&
|
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)-1] == '\r') {
|
|
|
|
STR_DEC_LEN(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RSTRING_PTR(str)[len-1] == '\r') {
|
|
|
|
STR_DEC_LEN(str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-11-08 09:43:14 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
2001-11-08 09:43:14 +03:00
|
|
|
return str;
|
2001-10-30 11:43:28 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2008-03-05 08:22:17 +03:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &rs);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(rs)) return Qnil;
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(rs);
|
2006-08-31 14:47:44 +04:00
|
|
|
rslen = RSTRING_LEN(rs);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rslen == 0) {
|
|
|
|
while (len>0 && p[len-1] == '\n') {
|
|
|
|
len--;
|
2001-10-30 11:43:28 +03:00
|
|
|
if (len>0 && p[len-1] == '\r')
|
|
|
|
len--;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len < RSTRING_LEN(str)) {
|
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
1999-01-20 07:59:39 +03:00
|
|
|
return str;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
if (rslen > len) return Qnil;
|
2006-08-31 14:47:44 +04:00
|
|
|
newline = RSTRING_PTR(rs)[rslen-1];
|
2001-10-30 11:43:28 +03:00
|
|
|
if (rslen == 1 && newline == '\n')
|
|
|
|
goto smart_chomp;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-02-29 17:57:04 +03:00
|
|
|
enc = rb_enc_check(str, rs);
|
2007-12-19 17:15:05 +03:00
|
|
|
if (is_broken_string(rs)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2007-12-19 17:38:46 +03:00
|
|
|
pp = e - rslen;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (p[len-1] == newline &&
|
|
|
|
(rslen <= 1 ||
|
2007-12-19 17:15:05 +03:00
|
|
|
memcmp(RSTRING_PTR(rs), pp, rslen) == 0)) {
|
2008-09-13 23:23:52 +04:00
|
|
|
if (rb_enc_left_char_head(p, pp, e, enc) != pp)
|
2007-12-21 07:03:19 +03:00
|
|
|
return Qnil;
|
2009-03-05 04:24:57 +03:00
|
|
|
if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
|
|
|
|
ENC_CODERANGE_CLEAR(str);
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
STR_SET_LEN(str, RSTRING_LEN(str) - rslen);
|
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
2000-02-08 11:54:01 +03:00
|
|
|
return str;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.chomp(separator=$/) -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a new <code>String</code> with the given record separator removed
|
|
|
|
* from the end of <i>str</i> (if present). If <code>$/</code> has not been
|
|
|
|
* changed from the default Ruby record separator, then <code>chomp</code> also
|
|
|
|
* removes carriage return characters (that is it will remove <code>\n</code>,
|
|
|
|
* <code>\r</code>, and <code>\r\n</code>).
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "hello".chomp #=> "hello"
|
|
|
|
* "hello\n".chomp #=> "hello"
|
|
|
|
* "hello\r\n".chomp #=> "hello"
|
|
|
|
* "hello\n\r".chomp #=> "hello\n"
|
|
|
|
* "hello\r".chomp #=> "hello"
|
|
|
|
* "hello \n there".chomp #=> "hello \n there"
|
|
|
|
* "hello".chomp("llo") #=> "he"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_chomp(int argc, VALUE *argv, VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_chomp_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.lstrip! -> self or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
|
|
|
|
* change was made. See also <code>String#rstrip!</code> and
|
|
|
|
* <code>String#strip!</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " hello ".lstrip #=> "hello "
|
|
|
|
* "hello".lstrip! #=> nil
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_lstrip_bang(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *t, *e;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
if (!s || RSTRING_LEN(str) == 0) return Qnil;
|
* 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-25 07:29:39 +04:00
|
|
|
e = t = RSTRING_END(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
/* remove spaces at head */
|
* 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-25 07:29:39 +04:00
|
|
|
while (s < e) {
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
|
|
|
unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc);
|
2009-02-22 17:26:27 +03:00
|
|
|
|
2009-09-15 09:27:29 +04:00
|
|
|
if (!rb_isspace(cc)) break;
|
* 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 20:59:22 +04:00
|
|
|
s += n;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
if (s > RSTRING_PTR(str)) {
|
|
|
|
STR_SET_LEN(str, t-s);
|
|
|
|
memmove(RSTRING_PTR(str), s, RSTRING_LEN(str));
|
|
|
|
RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
|
2001-08-06 07:05:23 +04:00
|
|
|
return str;
|
|
|
|
}
|
2001-08-14 12:13:31 +04:00
|
|
|
return Qnil;
|
2001-08-06 07:05:23 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.lstrip -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with leading whitespace removed. See also
|
|
|
|
* <code>String#rstrip</code> and <code>String#strip</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " hello ".lstrip #=> "hello "
|
|
|
|
* "hello".lstrip #=> "hello"
|
|
|
|
*/
|
|
|
|
|
2001-08-06 07:05:23 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_lstrip(VALUE str)
|
2001-08-06 07:05:23 +04:00
|
|
|
{
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_lstrip_bang(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.rstrip! -> self or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Removes trailing whitespace from <i>str</i>, returning <code>nil</code> if
|
|
|
|
* no change was made. See also <code>String#lstrip!</code> and
|
|
|
|
* <code>String#strip!</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " hello ".rstrip #=> " hello"
|
|
|
|
* "hello".rstrip! #=> nil
|
|
|
|
*/
|
|
|
|
|
2001-08-06 07:05:23 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_rstrip_bang(VALUE str)
|
2001-08-06 07:05:23 +04:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
2001-08-06 07:05:23 +04:00
|
|
|
char *s, *t, *e;
|
|
|
|
|
2009-06-02 19:24:23 +04:00
|
|
|
str_modify_keep_cr(str);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2008-10-29 19:44:24 +03:00
|
|
|
rb_str_check_dummy_enc(enc);
|
2007-08-27 18:17:28 +04:00
|
|
|
s = RSTRING_PTR(str);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (!s || RSTRING_LEN(str) == 0) return Qnil;
|
2007-08-27 18:17:28 +04:00
|
|
|
t = e = RSTRING_END(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-24 18:01:41 +04:00
|
|
|
/* remove trailing spaces or '\0's */
|
2008-09-15 18:40:00 +04:00
|
|
|
if (single_byte_optimizable(str)) {
|
2009-04-08 15:08:18 +04:00
|
|
|
unsigned char c;
|
2009-09-14 22:10:00 +04:00
|
|
|
while (s < t && ((c = *(t-1)) == '\0' || ascii_isspace(c))) t--;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *tp;
|
2008-09-15 18:40:00 +04:00
|
|
|
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
|
2008-09-24 18:01:41 +04:00
|
|
|
unsigned int c = rb_enc_codepoint(tp, e, enc);
|
2009-09-15 09:27:29 +04:00
|
|
|
if (c && !rb_isspace(c)) break;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
t = tp;
|
* 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-25 07:29:39 +04:00
|
|
|
}
|
|
|
|
}
|
2001-08-06 07:05:23 +04:00
|
|
|
if (t < e) {
|
2009-09-18 19:17:17 +04:00
|
|
|
long len = t-RSTRING_PTR(str);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
|
|
|
|
STR_SET_LEN(str, len);
|
|
|
|
RSTRING_PTR(str)[len] = '\0';
|
2001-08-06 07:05:23 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-08-06 07:05:23 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.rstrip -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with trailing whitespace removed. See also
|
|
|
|
* <code>String#lstrip</code> and <code>String#strip</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " hello ".rstrip #=> " hello"
|
|
|
|
* "hello".rstrip #=> "hello"
|
|
|
|
*/
|
|
|
|
|
2001-08-06 07:05:23 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_rstrip(VALUE str)
|
2001-08-06 07:05:23 +04:00
|
|
|
{
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_rstrip_bang(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.strip! -> str or nil
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Removes leading and trailing whitespace from <i>str</i>. Returns
|
|
|
|
* <code>nil</code> if <i>str</i> was not altered.
|
|
|
|
*/
|
|
|
|
|
2001-08-06 07:05:23 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_strip_bang(VALUE str)
|
2001-08-06 07:05:23 +04:00
|
|
|
{
|
|
|
|
VALUE l = rb_str_lstrip_bang(str);
|
|
|
|
VALUE r = rb_str_rstrip_bang(str);
|
|
|
|
|
|
|
|
if (NIL_P(l) && NIL_P(r)) return Qnil;
|
1999-10-20 11:10:23 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.strip -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a copy of <i>str</i> with leading and trailing whitespace removed.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* " hello ".strip #=> "hello"
|
|
|
|
* "\tgoodbye\r\n".strip #=> "goodbye"
|
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_strip(VALUE str)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2000-02-02 11:10:32 +03:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_strip_bang(str);
|
|
|
|
return str;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
scan_once(VALUE str, VALUE pat, long *start)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE result, match;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct re_registers *regs;
|
2009-09-18 19:17:17 +04:00
|
|
|
int i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_reg_search(pat, str, *start, 0) >= 0) {
|
|
|
|
match = rb_backref_get();
|
2008-02-16 23:08:35 +03:00
|
|
|
regs = RMATCH_REGS(match);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (BEG(0) == END(0)) {
|
2008-06-19 12:39:03 +04:00
|
|
|
rb_encoding *enc = STR_ENC_GET(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
/*
|
|
|
|
* Always consume at least one character of the input string
|
|
|
|
*/
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > END(0))
|
2009-05-20 08:44:36 +04:00
|
|
|
*start = END(0)+rb_enc_fast_mbclen(RSTRING_PTR(str)+END(0),
|
|
|
|
RSTRING_END(str), enc);
|
2003-06-23 10:52:39 +04:00
|
|
|
else
|
|
|
|
*start = END(0)+1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*start = END(0);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (regs->num_regs == 1) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_nth_match(0, match);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
result = rb_ary_new2(regs->num_regs);
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=1; i < regs->num_regs; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(result, rb_reg_nth_match(i, match));
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.scan(pattern) -> array
|
|
|
|
* str.scan(pattern) {|match, ...| block } -> str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Both forms iterate through <i>str</i>, matching the pattern (which may be a
|
|
|
|
* <code>Regexp</code> or a <code>String</code>). For each match, a result is
|
|
|
|
* generated and either added to the result array or passed to the block. If
|
|
|
|
* the pattern contains no groups, each individual result consists of the
|
|
|
|
* matched string, <code>$&</code>. If the pattern contains groups, each
|
|
|
|
* individual result is itself an array containing one entry per group.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a = "cruel world"
|
|
|
|
* a.scan(/\w+/) #=> ["cruel", "world"]
|
|
|
|
* a.scan(/.../) #=> ["cru", "el ", "wor"]
|
|
|
|
* a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
|
|
|
|
* a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* And the block form:
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* a.scan(/\w+/) {|w| print "<<#{w}>> " }
|
|
|
|
* print "\n"
|
2006-10-07 19:55:17 +04:00
|
|
|
* a.scan(/(.)(.)/) {|x,y| print y, x }
|
2003-12-25 00:14:46 +03:00
|
|
|
* print "\n"
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* <<cruel>> <<world>>
|
|
|
|
* rceu lowlr
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_scan(VALUE str, VALUE pat)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE result;
|
1999-08-13 09:45:20 +04:00
|
|
|
long start = 0;
|
2008-06-19 12:25:50 +04:00
|
|
|
long last = -1, prev = 0;
|
2006-08-31 14:47:44 +04:00
|
|
|
char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-09-11 08:05:36 +04:00
|
|
|
pat = get_pat(pat, 1);
|
2000-05-24 08:34:26 +04:00
|
|
|
if (!rb_block_given_p()) {
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE ary = rb_ary_new();
|
1998-01-16 15:19:22 +03:00
|
|
|
|
|
|
|
while (!NIL_P(result = scan_once(str, pat, &start))) {
|
2008-06-19 12:25:50 +04:00
|
|
|
last = prev;
|
|
|
|
prev = start;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, result);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2008-06-19 12:25:50 +04:00
|
|
|
if (last >= 0) rb_reg_search(pat, str, last, 0);
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2004-10-19 03:47:32 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (!NIL_P(result = scan_once(str, pat, &start))) {
|
2008-06-19 12:25:50 +04:00
|
|
|
last = prev;
|
|
|
|
prev = start;
|
2003-08-27 23:43:50 +04:00
|
|
|
rb_yield(result);
|
2006-07-26 11:43:16 +04:00
|
|
|
str_mod_check(str, p, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-06-19 12:25:50 +04:00
|
|
|
if (last >= 0) rb_reg_search(pat, str, last, 0);
|
1999-08-13 09:45:20 +04:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.hex -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Treats leading characters from <i>str</i> as a string of hexadecimal digits
|
|
|
|
* (with an optional sign and an optional <code>0x</code>) and returns the
|
|
|
|
* corresponding number. Zero is returned on error.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "0x0a".hex #=> 10
|
|
|
|
* "-1234".hex #=> -4660
|
|
|
|
* "0".hex #=> 0
|
|
|
|
* "wombat".hex #=> 0
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_hex(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-09-18 19:17:17 +04:00
|
|
|
return rb_str_to_inum(str, 16, FALSE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.oct -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Treats leading characters of <i>str</i> as a string of octal digits (with an
|
|
|
|
* optional sign) and returns the corresponding number. Returns 0 if the
|
|
|
|
* conversion fails.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "123".oct #=> 83
|
|
|
|
* "-377".oct #=> -255
|
|
|
|
* "bad".oct #=> 0
|
|
|
|
* "0377bad".oct #=> 255
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_oct(VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-09-18 19:17:17 +04:00
|
|
|
return rb_str_to_inum(str, -8, FALSE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-02-27 12:07:24 +04:00
|
|
|
* str.crypt(salt_str) -> new_str
|
|
|
|
*
|
|
|
|
* Applies a one-way cryptographic hash to <i>str</i> by invoking the
|
|
|
|
* standard library function <code>crypt(3)</code> with the given
|
|
|
|
* salt string. While the format and the result are system and
|
|
|
|
* implementation dependent, using a salt matching the regular
|
2012-02-27 19:11:00 +04:00
|
|
|
* expression <code>\A[a-zA-Z0-9./]{2}</code> should be valid and
|
|
|
|
* safe on any platform, in which only the first two characters are
|
2012-02-27 12:07:24 +04:00
|
|
|
* significant.
|
|
|
|
*
|
|
|
|
* This method is for use in system specific scripts, so if you want
|
|
|
|
* a cross-platform hash function consider using Digest or OpenSSL
|
|
|
|
* instead.
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_crypt(VALUE str, VALUE salt)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2005-09-13 09:35:27 +04:00
|
|
|
extern char *crypt(const char *, const char *);
|
2002-06-18 10:29:07 +04:00
|
|
|
VALUE result;
|
2008-09-16 16:33:40 +04:00
|
|
|
const char *s, *saltp;
|
2008-09-07 12:57:53 +04:00
|
|
|
#ifdef BROKEN_CRYPT
|
2008-09-16 16:33:40 +04:00
|
|
|
char salt_8bit_clean[3];
|
2008-09-07 12:57:53 +04:00
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(salt);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(salt) < 2)
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
rb_raise(rb_eArgError, "salt too short (need >=2 bytes)");
|
2002-06-18 10:29:07 +04:00
|
|
|
|
2008-09-16 16:33:40 +04:00
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
if (!s) s = "";
|
|
|
|
saltp = RSTRING_PTR(salt);
|
2008-09-07 12:57:53 +04:00
|
|
|
#ifdef BROKEN_CRYPT
|
2008-09-16 16:33:40 +04:00
|
|
|
if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) {
|
|
|
|
salt_8bit_clean[0] = saltp[0] & 0x7f;
|
|
|
|
salt_8bit_clean[1] = saltp[1] & 0x7f;
|
|
|
|
salt_8bit_clean[2] = '\0';
|
|
|
|
saltp = salt_8bit_clean;
|
|
|
|
}
|
2008-09-07 12:57:53 +04:00
|
|
|
#endif
|
2008-09-16 16:33:40 +04:00
|
|
|
result = rb_str_new2(crypt(s, saltp));
|
2002-06-18 10:29:07 +04:00
|
|
|
OBJ_INFECT(result, str);
|
|
|
|
OBJ_INFECT(result, salt);
|
|
|
|
return result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.intern -> symbol
|
|
|
|
* str.to_sym -> symbol
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns the <code>Symbol</code> corresponding to <i>str</i>, creating the
|
|
|
|
* symbol if it did not previously exist. See <code>Symbol#id2name</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* "Koala".intern #=> :Koala
|
|
|
|
* s = 'cat'.to_sym #=> :cat
|
|
|
|
* s == :cat #=> true
|
|
|
|
* s = '@cat'.to_sym #=> :@cat
|
|
|
|
* s == :@cat #=> true
|
|
|
|
*
|
|
|
|
* This can also be used to create symbols that cannot be represented using the
|
|
|
|
* <code>:xxx</code> notation.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* 'cat and dog'.to_sym #=> :"cat and dog"
|
|
|
|
*/
|
|
|
|
|
2002-10-23 14:17:30 +04: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 14:44:21 +04:00
|
|
|
rb_str_intern(VALUE s)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-07-16 13:29:35 +04:00
|
|
|
VALUE str = RB_GC_GUARD(s);
|
2009-10-15 11:57:40 +04:00
|
|
|
ID id;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* 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-25 07:29:39 +04:00
|
|
|
id = rb_intern_str(str);
|
2009-10-15 11:57:40 +04:00
|
|
|
return ID2SYM(id);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2006-06-10 20:09:12 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.ord -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-06-10 20:09:12 +04:00
|
|
|
* Return the <code>Integer</code> ordinal of a one-character string.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-06-10 20:09:12 +04:00
|
|
|
* "a".ord #=> 97
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_ord(VALUE s)
|
|
|
|
{
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int c;
|
2006-06-10 20:09:12 +04:00
|
|
|
|
2008-01-07 19:28:43 +03:00
|
|
|
c = rb_enc_codepoint(RSTRING_PTR(s), RSTRING_END(s), STR_ENC_GET(s));
|
2008-09-11 14:34:59 +04:00
|
|
|
return UINT2NUM(c);
|
2006-06-10 20:09:12 +04:00
|
|
|
}
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.sum(n=16) -> integer
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
|
|
|
|
* where <em>n</em> is the optional <code>Fixnum</code> parameter, defaulting
|
|
|
|
* to 16. The result is simply the sum of the binary value of each character in
|
2010-03-18 23:35:18 +03:00
|
|
|
* <i>str</i> modulo <code>2**n - 1</code>. This is not a particularly good
|
2003-12-25 00:14:46 +03:00
|
|
|
* checksum.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_sum(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE vbits;
|
2002-08-28 12:05:23 +04:00
|
|
|
int bits;
|
2004-10-02 07:49:15 +04:00
|
|
|
char *ptr, *p, *pend;
|
2004-10-01 19:56:05 +04:00
|
|
|
long len;
|
2010-03-18 23:35:18 +03:00
|
|
|
VALUE sum = INT2FIX(0);
|
|
|
|
unsigned long sum0 = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-03-05 08:22:17 +03:00
|
|
|
if (argc == 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
bits = 16;
|
|
|
|
}
|
2008-03-05 08:22:17 +03:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &vbits);
|
|
|
|
bits = NUM2INT(vbits);
|
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
ptr = p = RSTRING_PTR(str);
|
|
|
|
len = RSTRING_LEN(str);
|
2004-10-01 19:56:05 +04:00
|
|
|
pend = p + len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2010-03-18 23:35:18 +03:00
|
|
|
while (p < pend) {
|
2010-03-20 16:52:34 +03:00
|
|
|
if (FIXNUM_MAX - UCHAR_MAX < sum0) {
|
2010-03-18 23:35:18 +03:00
|
|
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
|
|
|
str_mod_check(str, ptr, len);
|
|
|
|
sum0 = 0;
|
|
|
|
}
|
|
|
|
sum0 += (unsigned char)*p;
|
|
|
|
p++;
|
|
|
|
}
|
2004-10-02 11:57:32 +04:00
|
|
|
|
2010-03-18 23:35:18 +03:00
|
|
|
if (bits == 0) {
|
|
|
|
if (sum0) {
|
|
|
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-03-18 23:35:18 +03:00
|
|
|
if (sum == INT2FIX(0)) {
|
|
|
|
if (bits < (int)sizeof(long)*CHAR_BIT) {
|
|
|
|
sum0 &= (((unsigned long)1)<<bits)-1;
|
|
|
|
}
|
|
|
|
sum = LONG2FIX(sum0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE mod;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2010-03-18 23:35:18 +03:00
|
|
|
if (sum0) {
|
|
|
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
|
|
|
}
|
|
|
|
|
|
|
|
mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits));
|
|
|
|
mod = rb_funcall(mod, '-', 1, INT2FIX(1));
|
|
|
|
sum = rb_funcall(sum, '&', 1, mod);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2010-03-18 23:35:18 +03:00
|
|
|
return sum;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* 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-25 07:29:39 +04:00
|
|
|
rb_encoding *enc;
|
2003-05-02 10:41:33 +04:00
|
|
|
VALUE w;
|
* 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-25 07:29:39 +04:00
|
|
|
long width, len, flen = 1, fclen = 1;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE res;
|
2008-05-31 13:28:20 +04:00
|
|
|
char *p;
|
|
|
|
const char *f = " ";
|
2009-12-07 07:11:26 +03:00
|
|
|
long n, size, llen, rlen, llen2 = 0, rlen2 = 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-25 07:29:39 +04:00
|
|
|
volatile VALUE pad;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
int singlebyte = 1, cr;
|
2003-05-02 10:41:33 +04:00
|
|
|
|
2004-10-06 11:40:06 +04:00
|
|
|
rb_scan_args(argc, argv, "11", &w, &pad);
|
2008-01-07 19:28:43 +03:00
|
|
|
enc = STR_ENC_GET(str);
|
2003-05-02 10:41:33 +04:00
|
|
|
width = NUM2LONG(w);
|
2004-10-06 11:40:06 +04:00
|
|
|
if (argc == 2) {
|
|
|
|
StringValue(pad);
|
2007-12-19 09:15:35 +03:00
|
|
|
enc = rb_enc_check(str, pad);
|
2006-08-31 14:47:44 +04:00
|
|
|
f = RSTRING_PTR(pad);
|
|
|
|
flen = RSTRING_LEN(pad);
|
* 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-25 07:29:39 +04:00
|
|
|
fclen = str_strlen(pad, enc);
|
2007-12-31 13:39:20 +03:00
|
|
|
singlebyte = single_byte_optimizable(pad);
|
2008-02-22 09:52:54 +03:00
|
|
|
if (flen == 0 || fclen == 0) {
|
2004-11-15 19:45:03 +03:00
|
|
|
rb_raise(rb_eArgError, "zero width padding");
|
2004-10-06 11:40:06 +04:00
|
|
|
}
|
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
len = str_strlen(str, enc);
|
|
|
|
if (width < 0 || len >= width) return rb_str_dup(str);
|
|
|
|
n = width - len;
|
|
|
|
llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2);
|
|
|
|
rlen = n - llen;
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
cr = ENC_CODERANGE(str);
|
2009-12-07 07:11:26 +03:00
|
|
|
if (flen > 1) {
|
|
|
|
llen2 = str_offset(f, f + flen, llen % fclen, enc, singlebyte);
|
|
|
|
rlen2 = str_offset(f, f + flen, rlen % fclen, enc, singlebyte);
|
|
|
|
}
|
|
|
|
size = RSTRING_LEN(str);
|
|
|
|
if ((len = llen / fclen + rlen / fclen) >= LONG_MAX / flen ||
|
|
|
|
(len *= flen) >= LONG_MAX - llen2 - rlen2 ||
|
|
|
|
(len += llen2 + rlen2) >= LONG_MAX - size) {
|
|
|
|
rb_raise(rb_eArgError, "argument too big");
|
|
|
|
}
|
|
|
|
len += size;
|
|
|
|
res = rb_str_new5(str, 0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
p = RSTRING_PTR(res);
|
2009-12-07 07:11:26 +03:00
|
|
|
if (flen <= 1) {
|
|
|
|
memset(p, *f, llen);
|
|
|
|
p += llen;
|
|
|
|
}
|
|
|
|
else {
|
2009-12-09 03:50:37 +03:00
|
|
|
while (llen >= fclen) {
|
* 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-25 07:29:39 +04:00
|
|
|
memcpy(p,f,flen);
|
|
|
|
p += flen;
|
|
|
|
llen -= fclen;
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
2009-12-07 07:11:26 +03:00
|
|
|
if (llen > 0) {
|
|
|
|
memcpy(p, f, llen2);
|
|
|
|
p += llen2;
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
2003-05-02 10:41:33 +04:00
|
|
|
}
|
2009-12-07 07:11:26 +03:00
|
|
|
memcpy(p, RSTRING_PTR(str), size);
|
|
|
|
p += size;
|
|
|
|
if (flen <= 1) {
|
|
|
|
memset(p, *f, rlen);
|
|
|
|
p += rlen;
|
|
|
|
}
|
|
|
|
else {
|
2009-12-09 03:50:37 +03:00
|
|
|
while (rlen >= fclen) {
|
* 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-25 07:29:39 +04:00
|
|
|
memcpy(p,f,flen);
|
|
|
|
p += flen;
|
|
|
|
rlen -= fclen;
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
2009-12-07 07:11:26 +03:00
|
|
|
if (rlen > 0) {
|
|
|
|
memcpy(p, f, rlen2);
|
|
|
|
p += rlen2;
|
2004-10-19 04:03:01 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
* 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-25 07:29:39 +04:00
|
|
|
*p = '\0';
|
|
|
|
STR_SET_LEN(res, p-RSTRING_PTR(res));
|
2001-05-30 13:12:34 +04:00
|
|
|
OBJ_INFECT(res, 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-25 07:29:39 +04:00
|
|
|
if (!NIL_P(pad)) OBJ_INFECT(res, pad);
|
2007-12-13 11:28:40 +03:00
|
|
|
rb_enc_associate(res, enc);
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-16 04:47:20 +04:00
|
|
|
if (argc == 2)
|
|
|
|
cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(pad));
|
|
|
|
if (cr != ENC_CODERANGE_BROKEN)
|
|
|
|
ENC_CODERANGE_SET(res, cr);
|
1998-01-16 15:19:22 +03:00
|
|
|
return res;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.ljust(integer, padstr=' ') -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If <i>integer</i> is greater than the length of <i>str</i>, returns a new
|
|
|
|
* <code>String</code> of length <i>integer</i> with <i>str</i> left justified
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
* and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
* "hello".ljust(4) #=> "hello"
|
|
|
|
* "hello".ljust(20) #=> "hello "
|
|
|
|
* "hello".ljust(20, '1234') #=> "hello123412341234123"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_ljust(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-05-02 10:41:33 +04:00
|
|
|
return rb_str_justify(argc, argv, str, 'l');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.rjust(integer, padstr=' ') -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* If <i>integer</i> is greater than the length of <i>str</i>, returns a new
|
|
|
|
* <code>String</code> of length <i>integer</i> with <i>str</i> right justified
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
* and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
* "hello".rjust(4) #=> "hello"
|
|
|
|
* "hello".rjust(20) #=> " hello"
|
|
|
|
* "hello".rjust(20, '1234') #=> "123412341234123hello"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_rjust(int argc, VALUE *argv, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-05-02 10:41:33 +04:00
|
|
|
return rb_str_justify(argc, argv, str, 'r');
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-07-06 00:39:15 +04:00
|
|
|
* str.center(width, padstr=' ') -> new_str
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-07-06 00:39:15 +04:00
|
|
|
* Centers +str+ in +width+. If +width+ is greater than the length of +str+,
|
|
|
|
* returns a new String of length +width+ with +str+ centered and padded with
|
|
|
|
* +padstr+; otherwise, returns +str+.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
* "hello".center(4) #=> "hello"
|
|
|
|
* "hello".center(20) #=> " hello "
|
|
|
|
* "hello".center(20, '123') #=> "1231231hello12312312"
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
2003-05-02 10:41:33 +04:00
|
|
|
static 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 14:44:21 +04:00
|
|
|
rb_str_center(int argc, VALUE *argv, VALUE str)
|
2003-05-02 10:41:33 +04:00
|
|
|
{
|
|
|
|
return rb_str_justify(argc, argv, str, 'c');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2006-09-20 20:41:56 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.partition(sep) -> [head, sep, tail]
|
|
|
|
* str.partition(regexp) -> [head, match, tail]
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2009-07-02 01:20:44 +04:00
|
|
|
* Searches <i>sep</i> or pattern (<i>regexp</i>) in the string
|
|
|
|
* and returns the part before it, the match, and the part
|
|
|
|
* after it.
|
|
|
|
* If it is not found, returns two empty strings and <i>str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-20 20:41:56 +04:00
|
|
|
* "hello".partition("l") #=> ["he", "l", "lo"]
|
2006-09-21 05:10:25 +04:00
|
|
|
* "hello".partition("x") #=> ["hello", "", ""]
|
2009-07-02 01:20:44 +04:00
|
|
|
* "hello".partition(/.l/) #=> ["h", "el", "lo"]
|
2006-09-20 20:41:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2006-09-22 13:08:58 +04:00
|
|
|
rb_str_partition(VALUE str, VALUE sep)
|
2006-09-20 20:41:56 +04:00
|
|
|
{
|
|
|
|
long pos;
|
2009-09-18 19:17:17 +04:00
|
|
|
int regex = FALSE;
|
2006-09-20 20:41:56 +04:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(sep, T_REGEXP)) {
|
2006-09-20 20:41:56 +04:00
|
|
|
pos = rb_reg_search(sep, str, 0, 0);
|
2009-09-18 19:17:17 +04:00
|
|
|
regex = TRUE;
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_check_string_type(sep);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
|
|
|
rb_obj_classname(sep));
|
|
|
|
}
|
2009-05-25 02:50:11 +04:00
|
|
|
sep = tmp;
|
2006-09-20 20:41:56 +04:00
|
|
|
pos = rb_str_index(str, sep, 0);
|
|
|
|
}
|
|
|
|
if (pos < 0) {
|
|
|
|
failed:
|
2010-02-23 18:41:34 +03:00
|
|
|
return rb_ary_new3(3, str, str_new_empty(str), str_new_empty(str));
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
if (regex) {
|
2009-03-14 21:04:21 +03:00
|
|
|
sep = rb_str_subpat(str, sep, INT2FIX(0));
|
2006-09-20 20:41:56 +04:00
|
|
|
if (pos == 0 && RSTRING_LEN(sep) == 0) goto failed;
|
|
|
|
}
|
2007-12-10 07:50:35 +03:00
|
|
|
return rb_ary_new3(3, rb_str_subseq(str, 0, pos),
|
2006-09-20 20:41:56 +04:00
|
|
|
sep,
|
2007-12-10 07:50:35 +03:00
|
|
|
rb_str_subseq(str, pos+RSTRING_LEN(sep),
|
2006-09-20 20:41:56 +04:00
|
|
|
RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.rpartition(sep) -> [head, sep, tail]
|
|
|
|
* str.rpartition(regexp) -> [head, match, tail]
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2009-07-02 01:20:44 +04:00
|
|
|
* Searches <i>sep</i> or pattern (<i>regexp</i>) in the string from the end
|
|
|
|
* of the string, and returns the part before it, the match, and the part
|
|
|
|
* after it.
|
|
|
|
* If it is not found, returns two empty strings and <i>str</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-21 05:10:25 +04:00
|
|
|
* "hello".rpartition("l") #=> ["hel", "l", "o"]
|
|
|
|
* "hello".rpartition("x") #=> ["", "", "hello"]
|
2009-07-02 01:20:44 +04:00
|
|
|
* "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
|
2006-09-20 20:41:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_rpartition(VALUE str, VALUE sep)
|
|
|
|
{
|
|
|
|
long pos = RSTRING_LEN(str);
|
2009-09-18 19:17:17 +04:00
|
|
|
int regex = FALSE;
|
2006-09-20 20:41:56 +04:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(sep, T_REGEXP)) {
|
2006-09-20 20:41:56 +04:00
|
|
|
pos = rb_reg_search(sep, str, pos, 1);
|
2009-09-18 19:17:17 +04:00
|
|
|
regex = TRUE;
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_check_string_type(sep);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
|
|
|
rb_obj_classname(sep));
|
|
|
|
}
|
2009-05-25 03:06:57 +04:00
|
|
|
sep = 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-25 07:29:39 +04:00
|
|
|
pos = rb_str_sublen(str, pos);
|
2006-09-21 05:10:25 +04:00
|
|
|
pos = rb_str_rindex(str, sep, pos);
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
if (pos < 0) {
|
2010-02-23 18:41:34 +03:00
|
|
|
return rb_ary_new3(3, str_new_empty(str), str_new_empty(str), str);
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
if (regex) {
|
|
|
|
sep = rb_reg_nth_match(0, rb_backref_get());
|
|
|
|
}
|
2008-02-29 09:19:21 +03:00
|
|
|
return rb_ary_new3(3, rb_str_substr(str, 0, pos),
|
2006-09-20 20:41:56 +04:00
|
|
|
sep,
|
2008-02-29 09:19:21 +03:00
|
|
|
rb_str_substr(str,pos+str_strlen(sep,STR_ENC_GET(sep)),RSTRING_LEN(str)));
|
2006-09-20 20:41:56 +04:00
|
|
|
}
|
|
|
|
|
2006-09-21 12:47:03 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-06-09 03:09:56 +04:00
|
|
|
* str.start_with?([prefixes]+) -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-06-09 03:09:56 +04:00
|
|
|
* Returns true if +str+ starts with one of the +prefixes+ given.
|
2009-05-09 15:01:47 +04:00
|
|
|
*
|
2012-06-09 03:09:56 +04:00
|
|
|
* "hello".start_with?("hell") #=> true
|
2009-05-09 15:01:47 +04:00
|
|
|
*
|
2011-05-09 13:35:27 +04:00
|
|
|
* # returns true if one of the prefixes matches.
|
2012-06-09 03:09:56 +04:00
|
|
|
* "hello".start_with?("heaven", "hell") #=> true
|
|
|
|
* "hello".start_with?("heaven", "paradise") #=> false
|
2006-09-21 12:47:03 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2006-09-27 16:45:29 +04:00
|
|
|
rb_str_start_with(int argc, VALUE *argv, VALUE str)
|
2006-09-21 12:47:03 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
2012-04-02 05:34:11 +04:00
|
|
|
VALUE tmp = argv[i];
|
|
|
|
StringValue(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-25 07:29:39 +04:00
|
|
|
rb_enc_check(str, tmp);
|
2006-09-21 12:47:03 +04:00
|
|
|
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
|
2007-10-06 09:32:37 +04:00
|
|
|
if (memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
2006-09-21 12:47:03 +04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-06-09 03:09:56 +04:00
|
|
|
* str.end_with?([suffixes]+) -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2012-06-09 03:09:56 +04:00
|
|
|
* Returns true if +str+ ends with one of the +suffixes+ given.
|
2006-09-21 12:47:03 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2006-09-27 16:45:29 +04:00
|
|
|
rb_str_end_with(int argc, VALUE *argv, VALUE str)
|
2006-09-21 12:47:03 +04:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-13 23:23:52 +04:00
|
|
|
char *p, *s, *e;
|
2008-01-22 06:59:53 +03:00
|
|
|
rb_encoding *enc;
|
2006-09-21 12:47:03 +04:00
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
2012-04-02 05:34:11 +04:00
|
|
|
VALUE tmp = argv[i];
|
|
|
|
StringValue(tmp);
|
2008-01-22 06:59:53 +03:00
|
|
|
enc = rb_enc_check(str, tmp);
|
2006-09-21 12:47:03 +04:00
|
|
|
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
|
2008-01-22 06:59:53 +03:00
|
|
|
p = RSTRING_PTR(str);
|
2008-09-13 23:23:52 +04:00
|
|
|
e = p + RSTRING_LEN(str);
|
|
|
|
s = e - RSTRING_LEN(tmp);
|
|
|
|
if (rb_enc_left_char_head(p, s, e, enc) != s)
|
2008-01-22 06:59:53 +03:00
|
|
|
continue;
|
2008-02-07 09:42:44 +03:00
|
|
|
if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
2006-09-21 12:47:03 +04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2000-08-25 12:26:06 +04:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
rb_str_setter(VALUE val, ID id, VALUE *var)
|
2000-08-25 12:26:06 +04:00
|
|
|
{
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING)) {
|
2000-08-25 12:26:06 +04:00
|
|
|
rb_raise(rb_eTypeError, "value of %s must be String", rb_id2name(id));
|
|
|
|
}
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
|
2007-09-28 23:27:10 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.force_encoding(encoding) -> str
|
2007-09-28 23:27:10 +04:00
|
|
|
*
|
|
|
|
* Changes the encoding to +encoding+ and returns self.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_str_force_encoding(VALUE str, VALUE enc)
|
2007-09-28 23:27:10 +04:00
|
|
|
{
|
|
|
|
str_modifiable(str);
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_enc_associate(str, rb_to_encoding(enc));
|
2009-02-25 06:28:00 +03:00
|
|
|
ENC_CODERANGE_CLEAR(str);
|
2007-09-28 23:27:10 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-12-22 11:38:53 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.valid_encoding? -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-12-22 11:38:53 +03:00
|
|
|
* Returns true for a string which encoded correctly.
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* "\xc2\xa1".force_encoding("UTF-8").valid_encoding? #=> true
|
|
|
|
* "\xc2".force_encoding("UTF-8").valid_encoding? #=> false
|
|
|
|
* "\x80".force_encoding("UTF-8").valid_encoding? #=> false
|
2007-12-22 11:38:53 +03:00
|
|
|
*/
|
|
|
|
|
2007-12-06 12:28:26 +03:00
|
|
|
static VALUE
|
|
|
|
rb_str_valid_encoding_p(VALUE str)
|
|
|
|
{
|
2007-12-17 10:41:21 +03:00
|
|
|
int cr = rb_enc_str_coderange(str);
|
2007-12-06 12:28:26 +03:00
|
|
|
|
2007-12-17 10:41:21 +03:00
|
|
|
return cr == ENC_CODERANGE_BROKEN ? Qfalse : Qtrue;
|
2007-12-06 12:28:26 +03:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:38:53 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* str.ascii_only? -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2007-12-22 11:38:53 +03:00
|
|
|
* Returns true for a string which has only ASCII characters.
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* "abc".force_encoding("UTF-8").ascii_only? #=> true
|
|
|
|
* "abc\u{6666}".force_encoding("UTF-8").ascii_only? #=> false
|
2007-12-22 11:38:53 +03:00
|
|
|
*/
|
|
|
|
|
2007-12-21 06:18:37 +03:00
|
|
|
static VALUE
|
|
|
|
rb_str_is_ascii_only_p(VALUE str)
|
|
|
|
{
|
|
|
|
int cr = rb_enc_str_coderange(str);
|
|
|
|
|
|
|
|
return cr == ENC_CODERANGE_7BIT ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2011-01-30 07:01:58 +03:00
|
|
|
/**
|
|
|
|
* Shortens _str_ and adds three dots, an ellipsis, if it is longer
|
|
|
|
* than _len_ characters.
|
|
|
|
*
|
|
|
|
* \param str the string to ellipsize.
|
|
|
|
* \param len the maximum string length.
|
|
|
|
* \return the ellipsized string.
|
|
|
|
* \pre _len_ must not be negative.
|
|
|
|
* \post the length of the returned string in characters is less than or equal to _len_.
|
|
|
|
* \post If the length of _str_ is less than or equal _len_, returns _str_ itself.
|
|
|
|
* \post the encoded of returned string is equal to the encoded of _str_.
|
|
|
|
* \post the class of returned string is equal to the class of _str_.
|
|
|
|
* \note the length is counted in characters.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_str_ellipsize(VALUE str, long len)
|
|
|
|
{
|
|
|
|
static const char ellipsis[] = "...";
|
|
|
|
const long ellipsislen = sizeof(ellipsis) - 1;
|
|
|
|
rb_encoding *const enc = rb_enc_get(str);
|
|
|
|
const long blen = RSTRING_LEN(str);
|
|
|
|
const char *const p = RSTRING_PTR(str), *e = p + blen;
|
|
|
|
VALUE estr, ret = 0;
|
|
|
|
|
|
|
|
if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
|
|
|
|
if (len * rb_enc_mbminlen(enc) >= blen ||
|
|
|
|
(e = rb_enc_nth(p, e, len, enc)) - p == blen) {
|
|
|
|
ret = str;
|
|
|
|
}
|
|
|
|
else if (len <= ellipsislen ||
|
|
|
|
!(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
|
|
|
|
if (rb_enc_asciicompat(enc)) {
|
|
|
|
ret = rb_str_new_with_class(str, ellipsis, len);
|
|
|
|
rb_enc_associate(ret, enc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
estr = rb_usascii_str_new(ellipsis, len);
|
|
|
|
ret = rb_str_encode(estr, rb_enc_from_encoding(enc), 0, Qnil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret = rb_str_subseq(str, 0, e - p), rb_enc_asciicompat(enc)) {
|
|
|
|
rb_str_cat(ret, ellipsis, ellipsislen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
estr = rb_str_encode(rb_usascii_str_new(ellipsis, ellipsislen),
|
|
|
|
rb_enc_from_encoding(enc), 0, Qnil);
|
|
|
|
rb_str_append(ret, estr);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-02 19:05:27 +04:00
|
|
|
/**********************************************************************
|
|
|
|
* Document-class: Symbol
|
|
|
|
*
|
|
|
|
* <code>Symbol</code> objects represent names and some strings
|
|
|
|
* inside the Ruby
|
|
|
|
* interpreter. They are generated using the <code>:name</code> and
|
|
|
|
* <code>:"string"</code> literals
|
|
|
|
* syntax, and by the various <code>to_sym</code> methods. The same
|
|
|
|
* <code>Symbol</code> object will be created for a given name or string
|
|
|
|
* for the duration of a program's execution, regardless of the context
|
|
|
|
* or meaning of that name. Thus if <code>Fred</code> is a constant in
|
|
|
|
* one context, a method in another, and a class in a third, the
|
|
|
|
* <code>Symbol</code> <code>:Fred</code> will be the same object in
|
|
|
|
* all three contexts.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
* module One
|
|
|
|
* class Fred
|
|
|
|
* end
|
|
|
|
* $f1 = :Fred
|
|
|
|
* end
|
|
|
|
* module Two
|
|
|
|
* Fred = 1
|
|
|
|
* $f2 = :Fred
|
|
|
|
* end
|
|
|
|
* def Fred()
|
|
|
|
* end
|
|
|
|
* $f3 = :Fred
|
2008-03-09 04:04:46 +03:00
|
|
|
* $f1.object_id #=> 2514190
|
|
|
|
* $f2.object_id #=> 2514190
|
|
|
|
* $f3.object_id #=> 2514190
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-09-06 20:59:10 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym == obj -> true or false
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-06 20:59:10 +04:00
|
|
|
* Equality---If <i>sym</i> and <i>obj</i> are exactly the same
|
2009-02-22 17:26:27 +03:00
|
|
|
* symbol, returns <code>true</code>.
|
2006-09-06 20:59:10 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sym_equal(VALUE sym1, VALUE sym2)
|
|
|
|
{
|
|
|
|
if (sym1 == sym2) return Qtrue;
|
2007-02-02 16:19:44 +03:00
|
|
|
return Qfalse;
|
2006-09-20 08:18:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-13 11:09:23 +03:00
|
|
|
static int
|
2008-12-13 13:35:06 +03:00
|
|
|
sym_printable(const char *s, const char *send, rb_encoding *enc)
|
2008-12-13 11:09:23 +03:00
|
|
|
{
|
2008-12-13 13:35:06 +03:00
|
|
|
while (s < send) {
|
* 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 20:59:22 +04:00
|
|
|
int n;
|
|
|
|
int c = rb_enc_codepoint_len(s, send, &n, enc);
|
|
|
|
|
2009-09-18 19:17:17 +04:00
|
|
|
if (!rb_enc_isprint(c, enc)) return FALSE;
|
2008-12-13 11:09:23 +03:00
|
|
|
s += n;
|
|
|
|
}
|
2009-09-18 19:17:17 +04:00
|
|
|
return TRUE;
|
2008-12-13 11:09:23 +03:00
|
|
|
}
|
|
|
|
|
2012-06-09 18:36:17 +04:00
|
|
|
int
|
|
|
|
rb_str_symname_p(VALUE sym)
|
|
|
|
{
|
|
|
|
rb_encoding *enc;
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
rb_encoding *resenc = rb_default_internal_encoding();
|
|
|
|
|
|
|
|
if (resenc == NULL) resenc = rb_default_external_encoding();
|
|
|
|
enc = STR_ENC_GET(sym);
|
|
|
|
ptr = RSTRING_PTR(sym);
|
|
|
|
len = RSTRING_LEN(sym);
|
|
|
|
if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
|
|
|
|
!rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-09-02 19:05:27 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.inspect -> string
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
* Returns the representation of <i>sym</i> as a symbol literal.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
* :fred.inspect #=> ":fred"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sym_inspect(VALUE sym)
|
|
|
|
{
|
2008-07-30 17:07:16 +04:00
|
|
|
VALUE str;
|
2010-05-28 13:39:41 +04:00
|
|
|
const char *ptr;
|
|
|
|
long len;
|
2012-06-09 18:36:17 +04:00
|
|
|
ID id = SYM2ID(sym);
|
2010-05-28 13:39:41 +04:00
|
|
|
char *dest;
|
2006-09-02 19:05:27 +04:00
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
sym = rb_id2str(id);
|
2012-06-09 18:36:17 +04:00
|
|
|
if (!rb_str_symname_p(sym)) {
|
2010-05-28 13:39:41 +04:00
|
|
|
str = rb_str_inspect(sym);
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
rb_str_resize(str, len + 1);
|
|
|
|
dest = RSTRING_PTR(str);
|
|
|
|
memmove(dest + 1, dest, len);
|
|
|
|
dest[0] = ':';
|
|
|
|
}
|
|
|
|
else {
|
2012-06-09 18:36:17 +04:00
|
|
|
rb_encoding *enc = STR_ENC_GET(sym);
|
|
|
|
ptr = RSTRING_PTR(sym);
|
|
|
|
len = RSTRING_LEN(sym);
|
2010-05-28 13:39:41 +04:00
|
|
|
str = rb_enc_str_new(0, len + 1, enc);
|
|
|
|
dest = RSTRING_PTR(str);
|
|
|
|
dest[0] = ':';
|
|
|
|
memcpy(dest + 1, ptr, len);
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.id2name -> string
|
|
|
|
* sym.to_s -> string
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
* Returns the name or string corresponding to <i>sym</i>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2006-09-02 19:05:27 +04:00
|
|
|
* :fred.id2name #=> "fred"
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
VALUE
|
|
|
|
rb_sym_to_s(VALUE sym)
|
2006-09-02 19:05:27 +04:00
|
|
|
{
|
2007-02-02 16:19:44 +03:00
|
|
|
ID id = SYM2ID(sym);
|
|
|
|
|
|
|
|
return str_new3(rb_cString, rb_id2str(id));
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.to_sym -> sym
|
|
|
|
* sym.intern -> sym
|
2006-09-02 19:05:27 +04:00
|
|
|
*
|
|
|
|
* In general, <code>to_sym</code> returns the <code>Symbol</code> corresponding
|
|
|
|
* to an object. As <i>sym</i> is already a symbol, <code>self</code> is returned
|
|
|
|
* in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sym_to_sym(VALUE sym)
|
|
|
|
{
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2007-12-23 05:26:45 +03:00
|
|
|
sym_call(VALUE args, VALUE sym, int argc, VALUE *argv)
|
2006-09-02 19:05:27 +04:00
|
|
|
{
|
|
|
|
VALUE obj;
|
|
|
|
|
2007-12-23 05:26:45 +03:00
|
|
|
if (argc < 1) {
|
2006-09-02 19:05:27 +04:00
|
|
|
rb_raise(rb_eArgError, "no receiver given");
|
|
|
|
}
|
2007-12-23 05:26:45 +03:00
|
|
|
obj = argv[0];
|
2010-09-24 18:45:19 +04:00
|
|
|
return rb_funcall_passing_block(obj, (ID)sym, argc - 1, argv + 1);
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sym.to_proc
|
|
|
|
*
|
|
|
|
* Returns a _Proc_ object which respond to the given method by _sym_.
|
|
|
|
*
|
|
|
|
* (1..3).collect(&:to_s) #=> ["1", "2", "3"]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sym_to_proc(VALUE sym)
|
|
|
|
{
|
2008-12-04 18:17:21 +03:00
|
|
|
static VALUE sym_proc_cache = Qfalse;
|
|
|
|
enum {SYM_PROC_CACHE_SIZE = 67};
|
|
|
|
VALUE proc;
|
|
|
|
long id, index;
|
|
|
|
VALUE *aryp;
|
|
|
|
|
|
|
|
if (!sym_proc_cache) {
|
2008-12-04 19:39:07 +03:00
|
|
|
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
|
|
|
|
rb_gc_register_mark_object(sym_proc_cache);
|
|
|
|
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
|
2008-12-04 18:17:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id = SYM2ID(sym);
|
|
|
|
index = (id % SYM_PROC_CACHE_SIZE) << 1;
|
2006-09-02 19:05:27 +04:00
|
|
|
|
2008-12-04 18:17:21 +03:00
|
|
|
aryp = RARRAY_PTR(sym_proc_cache);
|
|
|
|
if (aryp[index] == sym) {
|
2008-12-04 19:39:07 +03:00
|
|
|
return aryp[index + 1];
|
2008-12-04 18:17:21 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-12-04 19:39:07 +03:00
|
|
|
proc = rb_proc_new(sym_call, (VALUE)id);
|
|
|
|
aryp[index] = sym;
|
|
|
|
aryp[index + 1] = proc;
|
|
|
|
return proc;
|
2008-12-04 18:17:21 +03:00
|
|
|
}
|
|
|
|
}
|
2006-09-02 19:05:27 +04:00
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
|
|
|
* sym.succ
|
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.succ.intern</code>.
|
|
|
|
*/
|
|
|
|
|
2006-11-02 04:27:58 +03:00
|
|
|
static VALUE
|
|
|
|
sym_succ(VALUE sym)
|
|
|
|
{
|
2007-02-02 16:19:44 +03:00
|
|
|
return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* str <=> other -> -1, 0, +1 or nil
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Compares _sym_ with _other_ in string form.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_cmp(VALUE sym, VALUE other)
|
|
|
|
{
|
|
|
|
if (!SYMBOL_P(other)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return rb_str_cmp_m(rb_sym_to_s(sym), rb_sym_to_s(other));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.casecmp(other) -> -1, 0, +1 or nil
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Case-insensitive version of <code>Symbol#<=></code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_casecmp(VALUE sym, VALUE other)
|
|
|
|
{
|
|
|
|
if (!SYMBOL_P(other)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return rb_str_casecmp(rb_sym_to_s(sym), rb_sym_to_s(other));
|
2006-11-02 04:27:58 +03:00
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym =~ obj -> fixnum or nil
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Returns <code>sym.to_s =~ obj</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_match(VALUE sym, VALUE other)
|
2006-09-02 19:05:27 +04:00
|
|
|
{
|
2007-02-02 16:19:44 +03:00
|
|
|
return rb_str_match(rb_sym_to_s(sym), other);
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym[idx] -> char
|
|
|
|
* sym[b, n] -> char
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Returns <code>sym.to_s[]</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_aref(int argc, VALUE *argv, VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_aref_m(argc, argv, rb_sym_to_s(sym));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.length -> integer
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.length</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_length(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_length(rb_id2str(SYM2ID(sym)));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.empty? -> true or false
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Returns that _sym_ is :"" or not.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_empty(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_empty(rb_id2str(SYM2ID(sym)));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.upcase -> symbol
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.upcase.intern</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_upcase(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_intern(rb_str_upcase(rb_id2str(SYM2ID(sym))));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.downcase -> symbol
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.downcase.intern</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_downcase(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_intern(rb_str_downcase(rb_id2str(SYM2ID(sym))));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.capitalize -> symbol
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.capitalize.intern</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_capitalize(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_intern(rb_str_capitalize(rb_id2str(SYM2ID(sym))));
|
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.swapcase -> symbol
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Same as <code>sym.to_s.swapcase.intern</code>.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
static VALUE
|
|
|
|
sym_swapcase(VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_str_intern(rb_str_swapcase(rb_id2str(SYM2ID(sym))));
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 19:42:59 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* sym.encoding -> encoding
|
2009-09-18 19:42:59 +04:00
|
|
|
*
|
|
|
|
* Returns the Encoding object that represents the encoding of _sym_.
|
|
|
|
*/
|
|
|
|
|
2007-08-27 10:56:49 +04:00
|
|
|
static VALUE
|
|
|
|
sym_encoding(VALUE sym)
|
|
|
|
{
|
2007-10-04 10:57:19 +04:00
|
|
|
return rb_obj_encoding(rb_id2str(SYM2ID(sym)));
|
2007-08-27 10:56:49 +04:00
|
|
|
}
|
|
|
|
|
2006-09-02 19:05:27 +04:00
|
|
|
ID
|
|
|
|
rb_to_id(VALUE name)
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
switch (TYPE(name)) {
|
|
|
|
default:
|
|
|
|
tmp = rb_check_string_type(name);
|
2007-02-02 16:19:44 +03:00
|
|
|
if (NIL_P(tmp)) {
|
2008-12-12 04:19:23 +03:00
|
|
|
tmp = rb_inspect(name);
|
2007-02-02 16:19:44 +03:00
|
|
|
rb_raise(rb_eTypeError, "%s is not a symbol",
|
2008-12-12 04:19:23 +03:00
|
|
|
RSTRING_PTR(tmp));
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
2007-02-02 16:19:44 +03:00
|
|
|
name = tmp;
|
2007-07-16 13:29:35 +04:00
|
|
|
/* fall through */
|
2007-02-02 16:19:44 +03:00
|
|
|
case T_STRING:
|
|
|
|
name = rb_str_intern(name);
|
|
|
|
/* fall through */
|
|
|
|
case T_SYMBOL:
|
|
|
|
return SYM2ID(name);
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
2012-04-14 04:36:26 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2006-09-02 19:05:27 +04:00
|
|
|
}
|
|
|
|
|
2003-12-25 00:14:46 +03:00
|
|
|
/*
|
|
|
|
* A <code>String</code> object holds and manipulates an arbitrary sequence of
|
|
|
|
* bytes, typically representing characters. String objects may be created
|
|
|
|
* using <code>String::new</code> or as literals.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
* Because of aliasing issues, users of strings should be aware of the methods
|
|
|
|
* that modify the contents of a <code>String</code> object. Typically,
|
|
|
|
* methods with names ending in ``!'' modify their receiver, while those
|
|
|
|
* without a ``!'' return a new <code>String</code>. However, there are
|
|
|
|
* exceptions, such as <code>String#[]=</code>.
|
2009-02-22 17:26:27 +03:00
|
|
|
*
|
2003-12-25 00:14:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
* 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 14:44:21 +04:00
|
|
|
Init_String(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
#undef rb_intern
|
2008-08-16 04:20:31 +04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2008-06-09 13:25:32 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cString = rb_define_class("String", rb_cObject);
|
|
|
|
rb_include_module(rb_cString, rb_mComparable);
|
2002-12-20 11:33:17 +03:00
|
|
|
rb_define_alloc_func(rb_cString, str_alloc);
|
2007-08-24 21:47:09 +04:00
|
|
|
rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
|
2001-09-19 10:54:11 +04:00
|
|
|
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
|
2004-10-19 03:47:32 +04:00
|
|
|
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "==", rb_str_equal, 1);
|
2009-08-12 09:55:06 +04:00
|
|
|
rb_define_method(rb_cString, "===", rb_str_equal, 1);
|
2001-07-24 13:07:33 +04:00
|
|
|
rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
|
2001-07-24 13:07:33 +04:00
|
|
|
rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "+", rb_str_plus, 1);
|
|
|
|
rb_define_method(rb_cString, "*", rb_str_times, 1);
|
2005-07-23 05:02:18 +04:00
|
|
|
rb_define_method(rb_cString, "%", rb_str_format_m, 1);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cString, "[]", rb_str_aref_m, -1);
|
|
|
|
rb_define_method(rb_cString, "[]=", rb_str_aset_m, -1);
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_define_method(rb_cString, "insert", rb_str_insert, 2);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "length", rb_str_length, 0);
|
1999-12-07 12:25:55 +03:00
|
|
|
rb_define_method(rb_cString, "size", rb_str_length, 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-25 07:29:39 +04:00
|
|
|
rb_define_method(rb_cString, "bytesize", rb_str_bytesize, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "empty?", rb_str_empty, 0);
|
|
|
|
rb_define_method(rb_cString, "=~", rb_str_match, 1);
|
2004-07-17 12:02:20 +04:00
|
|
|
rb_define_method(rb_cString, "match", rb_str_match_m, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "succ", rb_str_succ, 0);
|
|
|
|
rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "next", rb_str_succ, 0);
|
|
|
|
rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0);
|
2007-06-22 20:23:16 +04:00
|
|
|
rb_define_method(rb_cString, "upto", rb_str_upto, -1);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cString, "index", rb_str_index_m, -1);
|
2002-07-26 10:12:39 +04:00
|
|
|
rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_define_method(rb_cString, "replace", rb_str_replace, 1);
|
2004-08-19 11:33:15 +04:00
|
|
|
rb_define_method(rb_cString, "clear", rb_str_clear, 0);
|
* 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-10 01:20:17 +04:00
|
|
|
rb_define_method(rb_cString, "chr", rb_str_chr, 0);
|
2008-02-15 09:35:20 +03:00
|
|
|
rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1);
|
|
|
|
rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2);
|
2011-03-01 04:35:17 +03:00
|
|
|
rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-01-11 12:18:54 +03:00
|
|
|
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
|
|
|
|
rb_define_method(rb_cString, "to_s", rb_str_to_s, 0);
|
|
|
|
rb_define_method(rb_cString, "to_str", rb_str_to_s, 0);
|
|
|
|
rb_define_method(rb_cString, "inspect", rb_str_inspect, 0);
|
|
|
|
rb_define_method(rb_cString, "dump", rb_str_dump, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "upcase", rb_str_upcase, 0);
|
|
|
|
rb_define_method(rb_cString, "downcase", rb_str_downcase, 0);
|
|
|
|
rb_define_method(rb_cString, "capitalize", rb_str_capitalize, 0);
|
|
|
|
rb_define_method(rb_cString, "swapcase", rb_str_swapcase, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "upcase!", rb_str_upcase_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "downcase!", rb_str_downcase_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "capitalize!", rb_str_capitalize_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "swapcase!", rb_str_swapcase_bang, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "hex", rb_str_hex, 0);
|
|
|
|
rb_define_method(rb_cString, "oct", rb_str_oct, 0);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cString, "split", rb_str_split_m, -1);
|
2006-10-18 08:30:40 +04:00
|
|
|
rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
|
|
|
|
rb_define_method(rb_cString, "bytes", rb_str_each_byte, 0);
|
2007-12-26 10:10:20 +03:00
|
|
|
rb_define_method(rb_cString, "chars", rb_str_each_char, 0);
|
2008-09-16 07:14:41 +04:00
|
|
|
rb_define_method(rb_cString, "codepoints", rb_str_each_codepoint, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
|
|
|
|
rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "concat", rb_str_concat, 1);
|
|
|
|
rb_define_method(rb_cString, "<<", rb_str_concat, 1);
|
2010-08-27 11:33:19 +04:00
|
|
|
rb_define_method(rb_cString, "prepend", rb_str_prepend, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
|
|
|
|
rb_define_method(rb_cString, "intern", rb_str_intern, 0);
|
2002-11-03 14:04:35 +03:00
|
|
|
rb_define_method(rb_cString, "to_sym", rb_str_intern, 0);
|
2006-06-10 20:09:12 +04:00
|
|
|
rb_define_method(rb_cString, "ord", rb_str_ord, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "include?", rb_str_include, 1);
|
2006-09-27 16:45:29 +04:00
|
|
|
rb_define_method(rb_cString, "start_with?", rb_str_start_with, -1);
|
|
|
|
rb_define_method(rb_cString, "end_with?", rb_str_end_with, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "scan", rb_str_scan, 1);
|
|
|
|
|
2003-05-02 10:41:33 +04:00
|
|
|
rb_define_method(rb_cString, "ljust", rb_str_ljust, -1);
|
|
|
|
rb_define_method(rb_cString, "rjust", rb_str_rjust, -1);
|
|
|
|
rb_define_method(rb_cString, "center", rb_str_center, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "sub", rb_str_sub, -1);
|
|
|
|
rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);
|
|
|
|
rb_define_method(rb_cString, "chop", rb_str_chop, 0);
|
|
|
|
rb_define_method(rb_cString, "chomp", rb_str_chomp, -1);
|
|
|
|
rb_define_method(rb_cString, "strip", rb_str_strip, 0);
|
2001-08-06 07:05:23 +04:00
|
|
|
rb_define_method(rb_cString, "lstrip", rb_str_lstrip, 0);
|
|
|
|
rb_define_method(rb_cString, "rstrip", rb_str_rstrip, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "sub!", rb_str_sub_bang, -1);
|
|
|
|
rb_define_method(rb_cString, "gsub!", rb_str_gsub_bang, -1);
|
|
|
|
rb_define_method(rb_cString, "chop!", rb_str_chop_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "chomp!", rb_str_chomp_bang, -1);
|
2001-08-06 07:05:23 +04:00
|
|
|
rb_define_method(rb_cString, "strip!", rb_str_strip_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "lstrip!", rb_str_lstrip_bang, 0);
|
|
|
|
rb_define_method(rb_cString, "rstrip!", rb_str_rstrip_bang, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "tr", rb_str_tr, 2);
|
|
|
|
rb_define_method(rb_cString, "tr_s", rb_str_tr_s, 2);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cString, "delete", rb_str_delete, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "squeeze", rb_str_squeeze, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cString, "count", rb_str_count, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "tr!", rb_str_tr_bang, 2);
|
|
|
|
rb_define_method(rb_cString, "tr_s!", rb_str_tr_s_bang, 2);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cString, "delete!", rb_str_delete_bang, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cString, "squeeze!", rb_str_squeeze_bang, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "each_line", rb_str_each_line, -1);
|
|
|
|
rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 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-25 07:29:39 +04:00
|
|
|
rb_define_method(rb_cString, "each_char", rb_str_each_char, 0);
|
2008-09-16 07:14:41 +04:00
|
|
|
rb_define_method(rb_cString, "each_codepoint", rb_str_each_codepoint, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "sum", rb_str_sum, -1);
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
rb_define_method(rb_cString, "slice", rb_str_aref_m, -1);
|
|
|
|
rb_define_method(rb_cString, "slice!", rb_str_slice_bang, -1);
|
2006-09-20 20:41:56 +04:00
|
|
|
|
2006-09-22 13:08:58 +04:00
|
|
|
rb_define_method(rb_cString, "partition", rb_str_partition, 1);
|
2006-09-20 20:41:56 +04:00
|
|
|
rb_define_method(rb_cString, "rpartition", rb_str_rpartition, 1);
|
2000-02-01 06:12:21 +03:00
|
|
|
|
2007-10-04 10:57:19 +04:00
|
|
|
rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */
|
2007-09-28 23:27:10 +04:00
|
|
|
rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1);
|
2007-12-06 12:28:26 +03:00
|
|
|
rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
|
2007-12-21 06:18:37 +03:00
|
|
|
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 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-25 07:29:39 +04:00
|
|
|
|
2001-03-26 12:57:16 +04:00
|
|
|
id_to_s = rb_intern("to_s");
|
2000-01-05 07:41:21 +03:00
|
|
|
|
|
|
|
rb_fs = Qnil;
|
2003-07-30 11:08:56 +04:00
|
|
|
rb_define_variable("$;", &rb_fs);
|
|
|
|
rb_define_variable("$-F", &rb_fs);
|
2006-09-02 19:05:27 +04:00
|
|
|
|
2006-11-02 04:27:58 +03:00
|
|
|
rb_cSymbol = rb_define_class("Symbol", rb_cObject);
|
|
|
|
rb_include_module(rb_cSymbol, rb_mComparable);
|
2006-09-02 19:20:24 +04:00
|
|
|
rb_undef_alloc_func(rb_cSymbol);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cSymbol), "new");
|
2006-09-02 19:05:27 +04:00
|
|
|
rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
|
|
|
|
|
2006-09-06 20:59:10 +04:00
|
|
|
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
|
2009-08-12 09:55:06 +04:00
|
|
|
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
|
2006-09-02 19:05:27 +04:00
|
|
|
rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
|
2007-02-02 16:19:44 +03:00
|
|
|
rb_define_method(rb_cSymbol, "to_s", rb_sym_to_s, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "id2name", rb_sym_to_s, 0);
|
2006-09-02 19:05:27 +04:00
|
|
|
rb_define_method(rb_cSymbol, "intern", sym_to_sym, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "to_sym", sym_to_sym, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "to_proc", sym_to_proc, 0);
|
2006-11-02 04:27:58 +03:00
|
|
|
rb_define_method(rb_cSymbol, "succ", sym_succ, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "next", sym_succ, 0);
|
|
|
|
|
2007-02-02 16:19:44 +03:00
|
|
|
rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1);
|
|
|
|
rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1);
|
|
|
|
rb_define_method(rb_cSymbol, "=~", sym_match, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cSymbol, "[]", sym_aref, -1);
|
|
|
|
rb_define_method(rb_cSymbol, "slice", sym_aref, -1);
|
|
|
|
rb_define_method(rb_cSymbol, "length", sym_length, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "size", sym_length, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "empty?", sym_empty, 0);
|
2008-01-07 13:12:45 +03:00
|
|
|
rb_define_method(rb_cSymbol, "match", sym_match, 1);
|
2007-02-02 16:19:44 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cSymbol, "upcase", sym_upcase, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "downcase", sym_downcase, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, 0);
|
|
|
|
rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, 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-25 07:29:39 +04:00
|
|
|
|
2007-08-27 10:56:49 +04:00
|
|
|
rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|