2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
string.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Mon Aug 9 17:12:58 JST 1993
|
|
|
|
|
2003-01-16 02:34:03 -05:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "re.h"
|
2003-05-19 03:11:48 -04:00
|
|
|
#include "version.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#define BEG(no) regs->beg[no]
|
|
|
|
#define END(no) regs->end[no]
|
|
|
|
|
2000-09-26 05:11:47 -04:00
|
|
|
#include <math.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <ctype.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_cString;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-30 05:12:34 -04:00
|
|
|
#define STR_ASSOC FL_USER3
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-12-19 02:48:32 -05:00
|
|
|
#define RESIZE_CAPA(str,capacity) do {\
|
|
|
|
REALLOC_N(RSTRING(str)->ptr, char, (capacity)+1);\
|
|
|
|
RSTRING(str)->aux.capa = (capacity);\
|
|
|
|
} while (0)
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
VALUE rb_fs;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
* array.c (ary_alloc), dir.c (dir_s_alloc), eval.c (thgroup_s_alloc),
file.c (rb_stat_s_alloc), hash.c (hash_alloc), io.c (io_alloc),
object.c (rb_module_s_alloc, rb_class_allocate_instance),
re.c (match_alloc, rb_reg_s_alloc), string.c (str_alloc),
time.c (time_s_alloc), ext/digest/digest.c (rb_digest_base_alloc),
ext/tcltklib/tcltklib.c (ip_alloc),
ext/win32ole/win32ole.c (fole_s_allocate, fev_s_allocate)
: add prototype to get rid of VC++ warnings.
* ext/sdbm/init.c (fsdbm_alloc): allocator takes only one argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-21 13:02:01 -05:00
|
|
|
static VALUE str_alloc _((VALUE));
|
2001-10-03 03:19:19 -04:00
|
|
|
static VALUE
|
2002-12-20 03:33:17 -05:00
|
|
|
str_alloc(klass)
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
NEWOBJ(str, struct RString);
|
2001-10-02 00:31:23 -04:00
|
|
|
OBJSETUP(str, klass, T_STRING);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
str->ptr = 0;
|
2001-10-03 03:19:19 -04:00
|
|
|
str->len = 0;
|
2001-12-10 02:18:16 -05:00
|
|
|
str->aux.capa = 0;
|
2001-10-03 03:19:19 -04:00
|
|
|
|
|
|
|
return (VALUE)str;
|
|
|
|
}
|
|
|
|
|
2001-12-10 02:18:16 -05:00
|
|
|
static VALUE
|
|
|
|
str_new(klass, ptr, len)
|
2001-10-03 03:19:19 -04:00
|
|
|
VALUE klass;
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
{
|
2002-07-11 04:22:18 -04:00
|
|
|
VALUE str;
|
2001-10-03 03:19:19 -04:00
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
|
2002-12-20 03:33:17 -05:00
|
|
|
str = str_alloc(klass);
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str)->len = len;
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str)->aux.capa = len;
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str)->ptr = ALLOC_N(char,len+1);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (ptr) {
|
2001-10-03 03:19:19 -04:00
|
|
|
memcpy(RSTRING(str)->ptr, ptr, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str)->ptr[len] = '\0';
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE
|
|
|
|
rb_str_new(ptr, len)
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
{
|
2001-12-10 02:18:16 -05:00
|
|
|
return str_new(rb_cString, ptr, len);
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_new2(ptr)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *ptr;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2002-01-07 00:27:01 -05:00
|
|
|
if (!ptr) {
|
|
|
|
rb_raise(rb_eArgError, "NULL pointer given");
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_new(ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_tainted_str_new(ptr, len)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *ptr;
|
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE str = rb_str_new(ptr, len);
|
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
OBJ_TAINT(str);
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_tainted_str_new2(ptr)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *ptr;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE str = rb_str_new2(ptr);
|
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
OBJ_TAINT(str);
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2001-12-10 02:18:16 -05:00
|
|
|
static VALUE
|
|
|
|
str_new3(klass, str)
|
|
|
|
VALUE klass, str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE str2 = str_alloc(klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str2)->len = RSTRING(str)->len;
|
|
|
|
RSTRING(str2)->ptr = RSTRING(str)->ptr;
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str2)->aux.shared = str;
|
|
|
|
FL_SET(str2, ELTS_SHARED);
|
2000-06-14 01:30:29 -04:00
|
|
|
OBJ_INFECT(str2, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
return str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-12-10 02:18:16 -05:00
|
|
|
VALUE
|
|
|
|
rb_str_new3(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
return str_new3(rb_obj_class(str), str);
|
|
|
|
}
|
|
|
|
|
2002-12-17 13:20:46 -05:00
|
|
|
static VALUE
|
|
|
|
str_new4(klass, str)
|
|
|
|
VALUE klass, str;
|
|
|
|
{
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE str2 = str_alloc(klass);
|
2002-12-17 13:20:46 -05:00
|
|
|
|
|
|
|
RSTRING(str2)->len = RSTRING(str)->len;
|
|
|
|
RSTRING(str2)->ptr = RSTRING(str)->ptr;
|
2003-08-05 05:27:21 -04:00
|
|
|
if (FL_TEST(str, ELTS_SHARED) && !RSTRING(str)->aux.shared) {
|
|
|
|
/* ptr should be null_str */
|
|
|
|
FL_SET(str2, ELTS_SHARED);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FL_SET(str, ELTS_SHARED);
|
|
|
|
RSTRING(str)->aux.shared = str2;
|
|
|
|
}
|
2002-12-17 13:20:46 -05:00
|
|
|
|
|
|
|
return str2;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_new4(orig)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE orig;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-12-10 02:18:16 -05:00
|
|
|
VALUE klass, str;
|
2000-12-25 01:29:27 -05:00
|
|
|
|
2003-07-03 07:02:53 -04:00
|
|
|
if (OBJ_FROZEN(orig)) return orig;
|
2001-08-20 00:29:58 -04:00
|
|
|
klass = rb_obj_class(orig);
|
2003-06-23 02:52:39 -04:00
|
|
|
if (FL_TEST(orig, ELTS_SHARED) && RSTRING(orig)->aux.shared) {
|
2002-12-17 13:20:46 -05:00
|
|
|
long ofs;
|
|
|
|
str = RSTRING(orig)->aux.shared;
|
|
|
|
ofs = RSTRING(str)->len - RSTRING(orig)->len;
|
2003-07-03 07:02:53 -04:00
|
|
|
if (ofs > 0) {
|
|
|
|
str = str_new3(klass, str);
|
|
|
|
RSTRING(str)->ptr += ofs;
|
|
|
|
RSTRING(str)->len -= ofs;
|
|
|
|
}
|
2001-12-10 02:18:16 -05:00
|
|
|
}
|
|
|
|
else if (FL_TEST(orig, STR_ASSOC)) {
|
|
|
|
str = str_new(klass, RSTRING(orig)->ptr, RSTRING(orig)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-12-17 13:20:46 -05:00
|
|
|
str = str_new4(klass, orig);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-12-10 02:18:16 -05:00
|
|
|
OBJ_FREEZE(str);
|
|
|
|
return str;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE
|
|
|
|
rb_str_new5(obj, ptr, len)
|
|
|
|
VALUE obj;
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
{
|
2001-12-10 02:18:16 -05:00
|
|
|
return str_new(rb_obj_class(obj), ptr, len);
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
|
|
|
|
2001-05-30 05:12:34 -04:00
|
|
|
#define STR_BUF_MIN_SIZE 128
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_buf_new(capa)
|
|
|
|
long capa;
|
|
|
|
{
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE str = str_alloc(rb_cString);
|
2001-05-30 05:12:34 -04:00
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
if (capa < STR_BUF_MIN_SIZE) {
|
2001-05-30 05:12:34 -04:00
|
|
|
capa = STR_BUF_MIN_SIZE;
|
2002-07-11 04:22:18 -04:00
|
|
|
}
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str)->ptr = 0;
|
|
|
|
RSTRING(str)->len = 0;
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str)->aux.capa = capa;
|
2001-10-03 03:19:19 -04:00
|
|
|
RSTRING(str)->ptr = ALLOC_N(char, capa+1);
|
|
|
|
RSTRING(str)->ptr[0] = '\0';
|
2001-05-30 05:12:34 -04:00
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
return str;
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_buf_new2(ptr)
|
|
|
|
const char *ptr;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
long len = strlen(ptr);
|
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
str = rb_str_buf_new(len);
|
|
|
|
rb_str_buf_cat(str, ptr, len);
|
2001-05-30 05:12:34 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
|
|
|
rb_str_to_str(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
return rb_convert_type(str, T_STRING, "String", "to_str");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_str_shared_replace(str, str2)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE str, str2;
|
|
|
|
{
|
|
|
|
if (str == str2) return;
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
2002-07-11 04:22:18 -04:00
|
|
|
if (!FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(str2)) {
|
|
|
|
RSTRING(str)->ptr = 0;
|
|
|
|
RSTRING(str)->len = 0;
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str)->aux.capa = 0;
|
2002-09-13 04:26:35 -04:00
|
|
|
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
|
1999-01-19 23:59:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
RSTRING(str)->ptr = RSTRING(str2)->ptr;
|
|
|
|
RSTRING(str)->len = RSTRING(str2)->len;
|
2003-07-02 00:27:29 -04:00
|
|
|
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
|
2001-12-10 02:18:16 -05:00
|
|
|
if (FL_TEST(str2, ELTS_SHARED|STR_ASSOC)) {
|
|
|
|
FL_SET(str, RBASIC(str2)->flags & (ELTS_SHARED|STR_ASSOC));
|
|
|
|
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RSTRING(str)->aux.capa = RSTRING(str2)->aux.capa;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
RSTRING(str2)->ptr = 0; /* abandon str2 */
|
|
|
|
RSTRING(str2)->len = 0;
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str2)->aux.capa = 0;
|
2002-09-12 02:27:15 -04:00
|
|
|
FL_UNSET(str2, ELTS_SHARED|STR_ASSOC);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (OBJ_TAINTED(str2)) OBJ_TAINT(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-03-26 03:57:16 -05:00
|
|
|
static ID id_to_s;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_obj_as_string(obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
if (TYPE(obj) == T_STRING) {
|
|
|
|
return obj;
|
|
|
|
}
|
2001-03-26 03:57:16 -05:00
|
|
|
str = rb_funcall(obj, id_to_s, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (TYPE(str) != T_STRING)
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_any_to_s(obj);
|
|
|
|
if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-08-27 04:31:08 -04:00
|
|
|
static VALUE rb_str_replace _((VALUE, VALUE));
|
2000-03-13 02:18:45 -05:00
|
|
|
|
2001-12-10 02:18:16 -05:00
|
|
|
VALUE
|
|
|
|
rb_str_dup(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2003-01-30 23:00:17 -05:00
|
|
|
VALUE dup = str_alloc(rb_obj_class(str));
|
2002-08-27 04:31:08 -04:00
|
|
|
rb_str_replace(dup, str);
|
|
|
|
return dup;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-09-19 02:54:11 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_init(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
VALUE orig;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &orig) == 1)
|
|
|
|
rb_str_replace(str, orig);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_length(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2NUM(RSTRING(str)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_empty(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
if (RSTRING(str)->len == 0)
|
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_plus(str1, str2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str1, str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str3;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(str2);
|
1999-01-19 23:59:39 -05:00
|
|
|
str3 = rb_str_new(0, RSTRING(str1)->len+RSTRING(str2)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
memcpy(RSTRING(str3)->ptr, RSTRING(str1)->ptr, RSTRING(str1)->len);
|
1999-01-19 23:59:39 -05:00
|
|
|
memcpy(RSTRING(str3)->ptr + RSTRING(str1)->len,
|
|
|
|
RSTRING(str2)->ptr, RSTRING(str2)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str3)->ptr[RSTRING(str3)->len] = '\0';
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (OBJ_TAINTED(str1) || OBJ_TAINTED(str2))
|
|
|
|
OBJ_TAINT(str3);
|
|
|
|
return str3;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_times(str, times)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE times;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str2;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
len = NUM2LONG(times);
|
2001-10-02 00:31:23 -04:00
|
|
|
if (len == 0) return rb_str_new5(str,0,0);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (len < 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "negative argument");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-29 03:05:32 -05:00
|
|
|
if (LONG_MAX/len < RSTRING(str)->len) {
|
|
|
|
rb_raise(rb_eArgError, "argument too big");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
str2 = rb_str_new5(str,0, RSTRING(str)->len*len);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
memcpy(RSTRING(str2)->ptr+(i*RSTRING(str)->len),
|
|
|
|
RSTRING(str)->ptr, RSTRING(str)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
RSTRING(str2)->ptr[RSTRING(str2)->len] = '\0';
|
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
OBJ_INFECT(str2, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return str2;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_format(str, arg)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, arg;
|
|
|
|
{
|
|
|
|
VALUE *argv;
|
|
|
|
|
|
|
|
if (TYPE(arg) == T_ARRAY) {
|
|
|
|
argv = ALLOCA_N(VALUE, RARRAY(arg)->len + 1);
|
|
|
|
argv[0] = str;
|
|
|
|
MEMCPY(argv+1, RARRAY(arg)->ptr, VALUE, RARRAY(arg)->len);
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_f_sprintf(RARRAY(arg)->len+1, argv);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
argv = ALLOCA_N(VALUE, 2);
|
|
|
|
argv[0] = str;
|
|
|
|
argv[1] = arg;
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_f_sprintf(2, argv);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-12-18 03:47:06 -05:00
|
|
|
static int
|
|
|
|
str_independent(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
if (OBJ_FROZEN(str)) rb_error_frozen("string");
|
|
|
|
if (!OBJ_TAINTED(str) && rb_safe_level() >= 4)
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify string");
|
|
|
|
if (!FL_TEST(str, ELTS_SHARED)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
str_make_independent(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = ALLOC_N(char, RSTRING(str)->len+1);
|
|
|
|
if (RSTRING(str)->ptr) {
|
|
|
|
memcpy(ptr, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
}
|
|
|
|
ptr[RSTRING(str)->len] = 0;
|
|
|
|
RSTRING(str)->ptr = ptr;
|
|
|
|
RSTRING(str)->aux.capa = RSTRING(str)->len;
|
|
|
|
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_str_modify(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2002-04-18 04:46:18 -04:00
|
|
|
if (!str_independent(str))
|
|
|
|
str_make_independent(str);
|
2001-12-18 03:47:06 -05:00
|
|
|
}
|
|
|
|
|
2002-09-12 02:27:15 -04:00
|
|
|
void
|
|
|
|
rb_str_associate(str, add)
|
|
|
|
VALUE str, add;
|
|
|
|
{
|
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
|
|
|
/* already associated */
|
|
|
|
rb_ary_concat(RSTRING(str)->aux.shared, add);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (FL_TEST(str, ELTS_SHARED)) {
|
|
|
|
str_make_independent(str);
|
|
|
|
}
|
|
|
|
else if (RSTRING(str)->aux.capa != RSTRING(str)->len) {
|
|
|
|
RESIZE_CAPA(str, RSTRING(str)->len);
|
|
|
|
}
|
|
|
|
RSTRING(str)->aux.shared = add;
|
|
|
|
FL_SET(str, STR_ASSOC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_associated(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
|
|
|
return RSTRING(str)->aux.shared;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-06-23 02:52:39 -04:00
|
|
|
static char *null_str = "";
|
|
|
|
|
2001-12-18 03:47:06 -05:00
|
|
|
VALUE
|
|
|
|
rb_string_value(ptr)
|
|
|
|
volatile VALUE *ptr;
|
2002-08-29 05:08:18 -04:00
|
|
|
{
|
|
|
|
VALUE s = *ptr;
|
|
|
|
if (TYPE(s) != T_STRING) {
|
|
|
|
s = rb_str_to_str(s);
|
|
|
|
*ptr = s;
|
|
|
|
}
|
|
|
|
if (!RSTRING(s)->ptr) {
|
2003-06-23 02:52:39 -04:00
|
|
|
FL_SET(s, ELTS_SHARED);
|
|
|
|
RSTRING(s)->ptr = null_str;
|
2002-08-29 05:08:18 -04:00
|
|
|
}
|
2003-06-23 04:41:07 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
rb_string_value_ptr(ptr)
|
|
|
|
volatile VALUE *ptr;
|
|
|
|
{
|
2003-06-23 04:45:59 -04:00
|
|
|
return RSTRING(rb_string_value(ptr))->ptr;
|
2002-08-29 05:08:18 -04:00
|
|
|
}
|
|
|
|
|
2003-07-20 13:17:52 -04:00
|
|
|
char *
|
|
|
|
rb_string_value_cstr(ptr)
|
|
|
|
volatile VALUE *ptr;
|
|
|
|
{
|
|
|
|
VALUE str = rb_string_value(ptr);
|
|
|
|
char *s = RSTRING(str)->ptr;
|
|
|
|
|
|
|
|
if (!s || RSTRING(str)->len != strlen(s)) {
|
|
|
|
rb_raise(rb_eArgError, "string contains null byte");
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
VALUE
|
|
|
|
rb_check_string_type(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2003-06-23 02:52:39 -04:00
|
|
|
str = rb_check_convert_type(str, T_STRING, "String", "to_str");
|
|
|
|
if (!NIL_P(str) && !RSTRING(str)->ptr) {
|
|
|
|
FL_SET(str, ELTS_SHARED);
|
|
|
|
RSTRING(str)->ptr = null_str;
|
|
|
|
}
|
|
|
|
return str;
|
2002-12-19 04:20:20 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_str_substr(str, beg, len)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (len < 0) return Qnil;
|
2000-06-23 03:05:59 -04:00
|
|
|
if (beg > RSTRING(str)->len) return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (beg < 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
beg += RSTRING(str)->len;
|
|
|
|
if (beg < 0) return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (beg + len > RSTRING(str)->len) {
|
|
|
|
len = RSTRING(str)->len - beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
len = 0;
|
|
|
|
}
|
2001-10-02 00:31:23 -04:00
|
|
|
if (len == 0) return rb_str_new5(str,0,0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-17 05:34:30 -05:00
|
|
|
if (len > sizeof(struct RString)/2 &&
|
2003-10-13 09:05:24 -04:00
|
|
|
beg + len == RSTRING(str)->len && !FL_TEST(str, STR_ASSOC)) {
|
|
|
|
str2 = rb_str_new3(rb_str_new4(str));
|
2002-12-30 02:55:17 -05:00
|
|
|
RSTRING(str2)->ptr += RSTRING(str2)->len - len;
|
2002-12-17 05:34:30 -05:00
|
|
|
RSTRING(str2)->len = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = rb_str_new5(str, RSTRING(str)->ptr+beg, len);
|
|
|
|
}
|
2003-10-13 09:05:24 -04:00
|
|
|
OBJ_INFECT(str2, str);
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
return str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-02-17 02:11:22 -05:00
|
|
|
VALUE
|
|
|
|
rb_str_freeze(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
return rb_obj_freeze(str);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_dup_frozen(str)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str;
|
|
|
|
{
|
2003-06-23 02:52:39 -04:00
|
|
|
if (FL_TEST(str, ELTS_SHARED) && RSTRING(str)->aux.shared) {
|
2002-12-18 06:50:49 -05:00
|
|
|
VALUE shared = RSTRING(str)->aux.shared;
|
|
|
|
if (RSTRING(shared)->len == RSTRING(str)->len) {
|
|
|
|
OBJ_FREEZE(shared);
|
|
|
|
return shared;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
if (OBJ_FROZEN(str)) return str;
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
OBJ_FREEZE(str);
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_resize(str, len)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
|
2001-05-16 05:05:54 -04:00
|
|
|
if (len != RSTRING(str)->len) {
|
|
|
|
rb_str_modify(str);
|
2002-04-24 00:54:16 -04:00
|
|
|
if (RSTRING(str)->len < len || RSTRING(str)->len - len > 1024) {
|
2002-09-12 02:27:15 -04:00
|
|
|
REALLOC_N(RSTRING(str)->ptr, char, len+1);
|
2003-07-02 00:27:29 -04:00
|
|
|
if (!FL_TEST(str, STR_ASSOC|ELTS_SHARED)) {
|
2002-09-12 02:27:15 -04:00
|
|
|
RSTRING(str)->aux.capa = len;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
RSTRING(str)->len = len;
|
|
|
|
RSTRING(str)->ptr[len] = '\0'; /* sentinel */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-05-30 05:12:34 -04:00
|
|
|
VALUE
|
|
|
|
rb_str_buf_cat(str, ptr, len)
|
|
|
|
VALUE str;
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
long capa, total;
|
2001-05-30 05:12:34 -04:00
|
|
|
|
2002-09-12 02:27:15 -04:00
|
|
|
if (len == 0) return str;
|
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
rb_str_modify(str);
|
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
|
|
|
FL_UNSET(str, STR_ASSOC);
|
|
|
|
capa = RSTRING(str)->aux.capa = RSTRING(str)->len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
capa = RSTRING(str)->aux.capa;
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
total = RSTRING(str)->len+len;
|
|
|
|
if (capa <= total) {
|
|
|
|
while (total > capa) {
|
|
|
|
capa = (capa + 1) * 2;
|
|
|
|
}
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, capa);
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
memcpy(RSTRING(str)->ptr + RSTRING(str)->len, ptr, len);
|
|
|
|
RSTRING(str)->len = total;
|
|
|
|
RSTRING(str)->ptr[total] = '\0'; /* sentinel */
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_buf_cat2(str, ptr)
|
|
|
|
VALUE str;
|
|
|
|
const char *ptr;
|
|
|
|
{
|
|
|
|
return rb_str_buf_cat(str, ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_cat(str, ptr, len)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *ptr;
|
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-09-12 02:27:15 -04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative string size (or size too big)");
|
|
|
|
}
|
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
|
|
|
rb_str_modify(str);
|
|
|
|
REALLOC_N(RSTRING(str)->ptr, char, RSTRING(str)->len+len);
|
2003-03-07 00:59:42 -05:00
|
|
|
memcpy(RSTRING(str)->ptr + RSTRING(str)->len, ptr, len);
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->len += len;
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
|
2002-09-12 02:27:15 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-05-30 05:12:34 -04:00
|
|
|
|
2002-09-12 02:27:15 -04:00
|
|
|
return rb_str_buf_cat(str, ptr, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-04-10 01:48:43 -04:00
|
|
|
VALUE
|
|
|
|
rb_str_cat2(str, ptr)
|
|
|
|
VALUE str;
|
|
|
|
const char *ptr;
|
|
|
|
{
|
|
|
|
return rb_str_cat(str, ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_append(str, str2)
|
|
|
|
VALUE str, str2;
|
2000-04-10 01:48:43 -04:00
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
long capa, len;
|
2001-05-30 05:12:34 -04:00
|
|
|
|
2002-09-12 02:27:15 -04:00
|
|
|
rb_str_modify(str);
|
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
|
|
|
FL_UNSET(str, STR_ASSOC);
|
|
|
|
capa = RSTRING(str)->aux.capa = RSTRING(str)->len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
capa = RSTRING(str)->aux.capa;
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
len = RSTRING(str)->len+RSTRING(str2)->len;
|
|
|
|
if (capa <= len) {
|
|
|
|
while (len > capa) {
|
|
|
|
capa = (capa + 1) * 2;
|
|
|
|
}
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, capa);
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
memcpy(RSTRING(str)->ptr + RSTRING(str)->len,
|
|
|
|
RSTRING(str2)->ptr, RSTRING(str2)->len);
|
|
|
|
RSTRING(str)->len += RSTRING(str2)->len;
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
|
2002-09-12 02:27:15 -04:00
|
|
|
OBJ_INFECT(str, str2);
|
2001-05-30 05:12:34 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_append(str, str2)
|
|
|
|
VALUE str, str2;
|
|
|
|
{
|
|
|
|
rb_str_modify(str);
|
2002-09-13 04:26:35 -04:00
|
|
|
StringValue(str2);
|
2001-08-12 05:41:39 -04:00
|
|
|
if (RSTRING(str2)->len > 0) {
|
2002-09-12 02:27:15 -04:00
|
|
|
if (FL_TEST(str, STR_ASSOC)) {
|
2002-09-13 04:26:35 -04:00
|
|
|
long len = RSTRING(str)->len+RSTRING(str2)->len;
|
2002-09-12 02:27:15 -04:00
|
|
|
REALLOC_N(RSTRING(str)->ptr, char, len+1);
|
|
|
|
memcpy(RSTRING(str)->ptr + RSTRING(str)->len,
|
|
|
|
RSTRING(str2)->ptr, RSTRING(str2)->len);
|
|
|
|
RSTRING(str)->ptr[len] = '\0'; /* sentinel */
|
|
|
|
RSTRING(str)->len = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_str_buf_append(str, str2);
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
OBJ_INFECT(str, str2);
|
|
|
|
return str;
|
2000-04-10 01:48:43 -04:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
|
|
|
rb_str_concat(str1, str2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str1, str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(str2)) {
|
|
|
|
int i = FIX2INT(str2);
|
|
|
|
if (0 <= i && i <= 0xff) { /* byte */
|
|
|
|
char c = i;
|
|
|
|
return rb_str_cat(str1, &c, 1);
|
|
|
|
}
|
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
str1 = rb_str_append(str1, str2);
|
|
|
|
|
1999-12-06 04:04:03 -05:00
|
|
|
return str1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_hash(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
register long len = RSTRING(str)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
register char *p = RSTRING(str)->ptr;
|
1998-01-16 07:19:22 -05:00
|
|
|
register int key = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-24 22:51:23 -05:00
|
|
|
#ifdef HASH_ELFHASH
|
|
|
|
register unsigned int g;
|
|
|
|
|
|
|
|
while (len--) {
|
|
|
|
key = (key << 4) + *p++;
|
|
|
|
if (g = key & 0xF0000000)
|
|
|
|
key ^= g >> 24;
|
|
|
|
key &= ~g;
|
|
|
|
}
|
|
|
|
#elif HASH_PERL
|
2001-07-24 05:07:33 -04:00
|
|
|
while (len--) {
|
|
|
|
key = key*33 + *p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-24 22:51:23 -05:00
|
|
|
key = key + (key>>5);
|
2000-02-23 00:23:12 -05:00
|
|
|
#else
|
2001-07-24 05:07:33 -04:00
|
|
|
while (len--) {
|
|
|
|
key = key*65599 + *p;
|
|
|
|
p++;
|
2000-02-23 00:23:12 -05:00
|
|
|
}
|
|
|
|
key = key + (key>>5);
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_hash_m(str)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int key = rb_str_hash(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2FIX(key);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#define lesser(a,b) (((a)>(b))?(b):(a))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
int
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_cmp(str1, str2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str1, str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
int retval;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
len = lesser(RSTRING(str1)->len, RSTRING(str2)->len);
|
2000-06-14 01:30:29 -04:00
|
|
|
retval = rb_memcmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (retval == 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RSTRING(str1)->len == RSTRING(str2)->len) return 0;
|
|
|
|
if (RSTRING(str1)->len > RSTRING(str2)->len) return 1;
|
|
|
|
return -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (retval > 0) return 1;
|
|
|
|
return -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_equal(str1, str2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str1, str2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-08-29 02:28:51 -04:00
|
|
|
if (str1 == str2) return Qtrue;
|
2003-02-04 02:27:43 -05:00
|
|
|
if (TYPE(str2) != T_STRING) {
|
|
|
|
if (!rb_respond_to(str2, rb_intern("to_str"))) {
|
2003-07-21 14:37:07 -04:00
|
|
|
return Qnil;
|
2003-02-04 02:27:43 -05:00
|
|
|
}
|
|
|
|
return rb_equal(str2, str1);
|
|
|
|
}
|
2003-02-03 03:45:26 -05:00
|
|
|
if (RSTRING(str1)->len == RSTRING(str2)->len &&
|
|
|
|
rb_str_cmp(str1, str2) == 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-07-24 05:07:33 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_eql(str1, str2)
|
|
|
|
VALUE str1, str2;
|
|
|
|
{
|
|
|
|
if (TYPE(str2) != T_STRING || RSTRING(str1)->len != RSTRING(str2)->len)
|
|
|
|
return Qfalse;
|
|
|
|
|
|
|
|
if (memcmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr,
|
|
|
|
lesser(RSTRING(str1)->len, RSTRING(str2)->len)) == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_cmp_m(str1, str2)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str1, str2;
|
|
|
|
{
|
2003-02-20 15:40:20 -05:00
|
|
|
long result;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
if (TYPE(str2) != T_STRING) {
|
2003-02-20 15:40:20 -05: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-20 22:52:27 -05:00
|
|
|
if (NIL_P(tmp)) return Qnil;
|
2003-02-20 15:40:20 -05:00
|
|
|
if (!FIXNUM_P(tmp)) {
|
2003-02-20 22:52:27 -05:00
|
|
|
return rb_funcall(LONG2FIX(0), '-', 1, tmp);
|
2003-02-20 15:40:20 -05:00
|
|
|
}
|
2003-02-20 22:52:27 -05:00
|
|
|
result = -FIX2LONG(tmp);
|
2003-02-20 15:40:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = rb_str_cmp(str1, str2);
|
2002-12-19 04:20:20 -05:00
|
|
|
}
|
2003-02-21 04:32:18 -05:00
|
|
|
return LONG2NUM(result);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-07-24 05:07:33 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_casecmp(str1, str2)
|
|
|
|
VALUE str1, str2;
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
StringValue(str2);
|
|
|
|
len = lesser(RSTRING(str1)->len, RSTRING(str2)->len);
|
|
|
|
retval = rb_memcicmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr, len);
|
|
|
|
if (retval == 0) {
|
|
|
|
if (RSTRING(str1)->len == RSTRING(str2)->len) return INT2FIX(0);
|
|
|
|
if (RSTRING(str1)->len > RSTRING(str2)->len) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
if (retval == 0) return INT2FIX(0);
|
|
|
|
if (retval > 0) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static long
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_index(str, sub, offset)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, sub;
|
1999-08-13 01:45:20 -04:00
|
|
|
long offset;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-02-07 01:35:26 -05:00
|
|
|
long pos;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (offset < 0) {
|
|
|
|
offset += RSTRING(str)->len;
|
|
|
|
if (offset < 0) return -1;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(str)->len - offset < RSTRING(sub)->len) return -1;
|
2003-02-03 00:34:16 -05:00
|
|
|
if (RSTRING(sub)->len == 0) return offset;
|
2003-02-07 01:35:26 -05:00
|
|
|
pos = rb_memsearch(RSTRING(sub)->ptr, RSTRING(sub)->len,
|
2003-02-07 01:36:53 -05:00
|
|
|
RSTRING(str)->ptr+offset, RSTRING(str)->len-offset);
|
2003-02-07 01:35:26 -05:00
|
|
|
if (pos < 0) return pos;
|
|
|
|
return pos + offset;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_index_m(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE sub;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE initpos;
|
1999-08-13 01:45:20 -04:00
|
|
|
long pos;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
|
1999-08-13 01:45:20 -04:00
|
|
|
pos = NUM2LONG(initpos);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos = 0;
|
|
|
|
}
|
2000-02-17 02:11:22 -05:00
|
|
|
if (pos < 0) {
|
|
|
|
pos += RSTRING(str)->len;
|
2002-03-19 04:03:11 -05:00
|
|
|
if (pos < 0) {
|
|
|
|
if (TYPE(sub) == T_REGEXP) {
|
|
|
|
rb_backref_set(Qnil);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2000-02-17 02:11:22 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(sub)) {
|
|
|
|
case T_REGEXP:
|
1999-12-14 01:50:43 -05:00
|
|
|
pos = rb_reg_adjust_startpos(sub, str, pos, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
pos = rb_reg_search(sub, str, pos, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
{
|
|
|
|
int c = FIX2INT(sub);
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = RSTRING(str)->len;
|
1998-01-16 07:19:22 -05:00
|
|
|
char *p = RSTRING(str)->ptr;
|
|
|
|
|
|
|
|
for (;pos<len;pos++) {
|
2002-08-21 11:47:54 -04:00
|
|
|
if (p[pos] == c) return LONG2NUM(pos);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-01-30 22:15:33 -05:00
|
|
|
default: {
|
|
|
|
VALUE tmp;
|
|
|
|
|
|
|
|
tmp = rb_check_string_type(sub);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(sub));
|
2003-01-30 22:15:33 -05:00
|
|
|
}
|
|
|
|
sub = tmp;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case T_STRING:
|
|
|
|
pos = rb_str_index(str, sub, pos);
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == -1) return Qnil;
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2NUM(pos);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-07-26 02:12:39 -04:00
|
|
|
static long
|
|
|
|
rb_str_rindex(str, sub, pos)
|
|
|
|
VALUE str, sub;
|
|
|
|
long pos;
|
|
|
|
{
|
|
|
|
long len = RSTRING(sub)->len;
|
|
|
|
char *s, *sbeg, *t;
|
|
|
|
|
|
|
|
/* substring longer than string */
|
|
|
|
if (RSTRING(str)->len < len) return -1;
|
|
|
|
if (RSTRING(str)->len - pos < len) {
|
|
|
|
pos = RSTRING(str)->len - len;
|
|
|
|
}
|
|
|
|
sbeg = RSTRING(str)->ptr;
|
|
|
|
s = RSTRING(str)->ptr + pos;
|
|
|
|
t = RSTRING(sub)->ptr;
|
|
|
|
if (len) {
|
|
|
|
while (sbeg <= s) {
|
|
|
|
if (rb_memcmp(s, t, len) == 0) {
|
|
|
|
return s - RSTRING(str)->ptr;
|
|
|
|
}
|
|
|
|
s--;
|
|
|
|
}
|
2002-08-06 02:20:43 -04:00
|
|
|
return -1;
|
2002-07-26 02:12:39 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2002-07-26 02:12:39 -04:00
|
|
|
rb_str_rindex_m(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE sub;
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE position;
|
2002-08-21 11:47:54 -04:00
|
|
|
long pos;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &sub, &position) == 2) {
|
2002-08-21 11:47:54 -04:00
|
|
|
pos = NUM2LONG(position);
|
2000-07-24 03:19:34 -04:00
|
|
|
if (pos < 0) {
|
|
|
|
pos += RSTRING(str)->len;
|
2002-03-19 04:03:11 -05:00
|
|
|
if (pos < 0) {
|
|
|
|
if (TYPE(sub) == T_REGEXP) {
|
|
|
|
rb_backref_set(Qnil);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
2000-07-24 03:19:34 -04:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (pos > RSTRING(str)->len) pos = RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
pos = RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
switch (TYPE(sub)) {
|
|
|
|
case T_REGEXP:
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RREGEXP(sub)->len) {
|
1999-12-14 01:50:43 -05:00
|
|
|
pos = rb_reg_adjust_startpos(sub, str, pos, 1);
|
1999-08-13 01:45:20 -04:00
|
|
|
pos = rb_reg_search(sub, str, pos, 1);
|
|
|
|
}
|
2002-08-21 11:47:54 -04:00
|
|
|
if (pos >= 0) return LONG2NUM(pos);
|
1998-01-16 07:19:22 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
2002-07-26 02:12:39 -04:00
|
|
|
pos = rb_str_rindex(str, sub, pos);
|
2002-08-21 11:47:54 -04:00
|
|
|
if (pos >= 0) return LONG2NUM(pos);
|
1998-01-16 07:19:22 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FIXNUM:
|
|
|
|
{
|
|
|
|
int c = FIX2INT(sub);
|
1999-08-13 01:45:20 -04:00
|
|
|
char *p = RSTRING(str)->ptr + pos;
|
|
|
|
char *pbeg = RSTRING(str)->ptr;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2003-06-26 14:24:58 -04:00
|
|
|
if (pos == RSTRING(str)->len) {
|
|
|
|
if (pos == 0) return Qnil;
|
|
|
|
--p;
|
|
|
|
}
|
|
|
|
while (pbeg <= p) {
|
2002-08-21 11:47:54 -04:00
|
|
|
if (*p == c) return LONG2NUM(p - RSTRING(str)->ptr);
|
2003-06-26 14:24:58 -04:00
|
|
|
p--;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %s given",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(sub));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_match(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2003-05-26 04:22:33 -04:00
|
|
|
VALUE reg;
|
2002-07-11 04:22:18 -04:00
|
|
|
long start;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_REGEXP:
|
|
|
|
return rb_reg_match(y, x);
|
|
|
|
|
|
|
|
case T_STRING:
|
2003-05-26 04:22:33 -04:00
|
|
|
#if RUBY_VERSION_CODE < 181
|
|
|
|
rb_warn("string =~ string will be obsolete; use explicit regexp");
|
|
|
|
#endif
|
|
|
|
reg = rb_reg_regcomp(y);
|
|
|
|
start = rb_reg_search(reg, x, 0, 0);
|
2002-07-11 04:22:18 -04:00
|
|
|
if (start == -1) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2003-05-26 04:22:33 -04:00
|
|
|
return INT2NUM(start);
|
2002-07-11 04:22:18 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return rb_funcall(y, rb_intern("=~"), 1, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_match2(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
StringValue(str);
|
2003-07-19 13:26:04 -04:00
|
|
|
#if RUBY_VERSION_CODE < 181
|
|
|
|
rb_warn("~string will be obsolete; use explicit regexp");
|
|
|
|
#endif
|
2002-07-26 02:12:39 -04:00
|
|
|
return rb_reg_match2(rb_reg_regcomp(rb_reg_quote(str)));
|
2002-07-11 04:22:18 -04:00
|
|
|
}
|
|
|
|
|
2002-09-11 00:05:36 -04:00
|
|
|
static VALUE get_pat _((VALUE, int));
|
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_match_m(str, re)
|
|
|
|
VALUE str, re;
|
|
|
|
{
|
2002-09-11 00:05:36 -04:00
|
|
|
return rb_funcall(get_pat(re, 0), rb_intern("match"), 1, str);
|
2002-07-11 04:22:18 -04:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static char
|
1998-01-16 07:13:05 -05:00
|
|
|
succ_char(s)
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
char c = *s;
|
|
|
|
|
|
|
|
/* numerics */
|
|
|
|
if ('0' <= c && c < '9') (*s)++;
|
|
|
|
else if (c == '9') {
|
|
|
|
*s = '0';
|
|
|
|
return '1';
|
|
|
|
}
|
|
|
|
/* small alphabets */
|
|
|
|
else if ('a' <= c && c < 'z') (*s)++;
|
|
|
|
else if (c == 'z') {
|
|
|
|
return *s = 'a';
|
|
|
|
}
|
|
|
|
/* capital alphabets */
|
|
|
|
else if ('A' <= c && c < 'Z') (*s)++;
|
|
|
|
else if (c == 'Z') {
|
|
|
|
return *s = 'A';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_succ(orig)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE orig;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-05-12 05:07:57 -04:00
|
|
|
VALUE str;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *sbeg, *s;
|
1999-08-13 01:45:20 -04:00
|
|
|
int c = -1;
|
2002-08-28 04:05:23 -04:00
|
|
|
long n = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
str = rb_str_new5(orig,RSTRING(orig)->ptr, RSTRING(orig)->len);
|
2000-05-01 05:42:38 -04:00
|
|
|
OBJ_INFECT(str, orig);
|
|
|
|
if (RSTRING(str)->len == 0) return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
sbeg = RSTRING(str)->ptr; s = sbeg + RSTRING(str)->len - 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (sbeg <= s) {
|
2000-05-01 05:42:38 -04:00
|
|
|
if (ISALNUM(*s)) {
|
|
|
|
if ((c = succ_char(s)) == 0) break;
|
|
|
|
n = s - sbeg;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
s--;
|
|
|
|
}
|
2000-05-01 05:42:38 -04:00
|
|
|
if (c == -1) { /* str contains no alnum */
|
|
|
|
sbeg = RSTRING(str)->ptr; s = sbeg + RSTRING(str)->len - 1;
|
|
|
|
c = '\001';
|
|
|
|
while (sbeg <= s) {
|
2001-10-15 23:27:23 -04:00
|
|
|
if ((*s += 1) != 0) break;
|
|
|
|
s--;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-05-01 05:42:38 -04:00
|
|
|
if (s < sbeg) {
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, RSTRING(str)->len + 1);
|
2000-05-01 05:42:38 -04:00
|
|
|
s = RSTRING(str)->ptr + n;
|
|
|
|
memmove(s+1, s, RSTRING(str)->len - n);
|
|
|
|
*s = c;
|
|
|
|
RSTRING(str)->len += 1;
|
2000-11-17 00:59:45 -05:00
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_succ_bang(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_str_shared_replace(str, rb_str_succ(str));
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_str_upto(beg, end, excl)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE beg, end;
|
1999-08-13 01:45:20 -04:00
|
|
|
int excl;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-06-23 02:52:39 -04:00
|
|
|
VALUE current, after_end;
|
1999-11-04 03:39:57 -05:00
|
|
|
ID succ = rb_intern("succ");
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(end);
|
2003-06-23 02:52:39 -04:00
|
|
|
if (rb_str_cmp(beg, end) > 0) return beg;
|
|
|
|
after_end = rb_funcall(end, succ, 0, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
current = beg;
|
2003-06-23 02:52:39 -04:00
|
|
|
while (!rb_str_equal(current, after_end)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(current);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!excl && rb_str_equal(current, end)) break;
|
1999-11-04 03:39:57 -05:00
|
|
|
current = rb_funcall(current, succ, 0, 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (excl && rb_str_equal(current, end)) break;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (RSTRING(current)->len > RSTRING(end)->len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
return beg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_upto_m(beg, end)
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE beg, end;
|
|
|
|
{
|
2003-06-23 02:52:39 -04:00
|
|
|
return rb_str_upto(beg, end, Qfalse);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
static VALUE
|
2001-10-03 03:19:19 -04:00
|
|
|
rb_str_subpat(str, re, nth)
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE str, re;
|
2001-10-03 03:19:19 -04:00
|
|
|
int nth;
|
2001-10-02 00:31:23 -04:00
|
|
|
{
|
|
|
|
if (rb_reg_search(re, str, 0, 0) >= 0) {
|
2001-10-03 03:19:19 -04:00
|
|
|
return rb_reg_nth_match(nth, rb_backref_get());
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_aref(str, indx)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE indx;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long idx;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(indx)) {
|
|
|
|
case T_FIXNUM:
|
1999-08-13 01:45:20 -04:00
|
|
|
idx = FIX2LONG(indx);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-29 03:05:32 -05:00
|
|
|
num_index:
|
1998-01-16 07:13:05 -05:00
|
|
|
if (idx < 0) {
|
1998-01-16 07:19:22 -05:00
|
|
|
idx = RSTRING(str)->len + idx;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (idx < 0 || RSTRING(str)->len <= idx) {
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return INT2FIX(RSTRING(str)->ptr[idx] & 0xff);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_REGEXP:
|
2001-10-02 00:31:23 -04:00
|
|
|
return rb_str_subpat(str, indx, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
case T_STRING:
|
2001-10-31 01:53:22 -05:00
|
|
|
if (rb_str_index(str, indx, 0) != -1)
|
|
|
|
return rb_str_dup(indx);
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* check if indx is Range */
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
|
|
|
switch (rb_range_beg_len(indx, &beg, &len, RSTRING(str)->len, 0)) {
|
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
return Qnil;
|
|
|
|
default:
|
|
|
|
return rb_str_substr(str, beg, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-02-29 03:05:32 -05:00
|
|
|
idx = NUM2LONG(indx);
|
|
|
|
goto num_index;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_aref_m(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc == 2) {
|
2001-10-02 00:31:23 -04:00
|
|
|
if (TYPE(argv[0]) == T_REGEXP) {
|
|
|
|
return rb_str_subpat(str, argv[0], NUM2INT(argv[1]));
|
|
|
|
}
|
2002-08-21 11:47:54 -04:00
|
|
|
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
|
2000-05-24 00:34:26 -04:00
|
|
|
}
|
|
|
|
if (argc != 1) {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments(%d for 1)", argc);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
return rb_str_aref(str, argv[0]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-05-16 05:05:54 -04:00
|
|
|
void
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_str_update(str, beg, len, val)
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE str;
|
2002-08-21 11:47:54 -04:00
|
|
|
long beg, len;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE val;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-05-28 14:11:07 -04:00
|
|
|
if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
|
2001-10-03 03:19:19 -04:00
|
|
|
if (RSTRING(str)->len < beg) {
|
|
|
|
out_of_range:
|
2002-05-28 14:11:07 -04:00
|
|
|
rb_raise(rb_eIndexError, "index %ld out of string", beg);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2001-10-03 03:19:19 -04:00
|
|
|
if (beg < 0) {
|
|
|
|
if (-beg > RSTRING(str)->len) {
|
|
|
|
goto out_of_range;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2001-10-03 03:19:19 -04:00
|
|
|
beg += RSTRING(str)->len;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2000-03-23 03:37:35 -05:00
|
|
|
if (RSTRING(str)->len < beg + len) {
|
|
|
|
len = RSTRING(str)->len - beg;
|
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(val);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (len < RSTRING(val)->len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
/* expand string */
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, RSTRING(str)->len + RSTRING(val)->len - len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
if (RSTRING(val)->len != len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
memmove(RSTRING(str)->ptr + beg + RSTRING(val)->len,
|
|
|
|
RSTRING(str)->ptr + beg + len,
|
|
|
|
RSTRING(str)->len - (beg + len));
|
|
|
|
}
|
|
|
|
if (RSTRING(str)->len < beg && len < 0) {
|
|
|
|
MEMZERO(RSTRING(str)->ptr + RSTRING(str)->len, char, -len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-03-23 03:37:35 -05:00
|
|
|
if (RSTRING(val)->len > 0) {
|
|
|
|
memmove(RSTRING(str)->ptr+beg, RSTRING(val)->ptr, RSTRING(val)->len);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->len += RSTRING(val)->len - len;
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2001-05-30 05:12:34 -04:00
|
|
|
OBJ_INFECT(str, val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
static void
|
2001-10-03 03:19:19 -04:00
|
|
|
rb_str_subpat_set(str, re, nth, val)
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE str, re;
|
2001-10-03 03:19:19 -04:00
|
|
|
int nth;
|
2001-10-02 00:31:23 -04:00
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
VALUE match;
|
2002-08-28 04:05:23 -04:00
|
|
|
long start, end, len;
|
2001-10-02 00:31:23 -04:00
|
|
|
|
|
|
|
if (rb_reg_search(re, str, 0, 0) < 0) {
|
|
|
|
rb_raise(rb_eIndexError, "regexp not matched");
|
|
|
|
}
|
|
|
|
match = rb_backref_get();
|
2001-10-03 03:19:19 -04:00
|
|
|
if (nth >= RMATCH(match)->regs->num_regs) {
|
|
|
|
out_of_range:
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of regexp", nth);
|
|
|
|
}
|
|
|
|
if (nth < 0) {
|
|
|
|
if (-nth >= RMATCH(match)->regs->num_regs) {
|
|
|
|
goto out_of_range;
|
|
|
|
}
|
|
|
|
nth += RMATCH(match)->regs->num_regs;
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
start = RMATCH(match)->BEG(nth);
|
2001-10-02 00:31:23 -04:00
|
|
|
if (start == -1) {
|
2001-10-03 03:19:19 -04:00
|
|
|
rb_raise(rb_eIndexError, "regexp group %d not matched", nth);
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
2001-10-03 03:19:19 -04:00
|
|
|
end = RMATCH(match)->END(nth);
|
2001-10-02 00:31:23 -04:00
|
|
|
len = end - start;
|
2002-03-13 19:10:49 -05:00
|
|
|
rb_str_modify(str);
|
2001-10-02 00:31:23 -04:00
|
|
|
rb_str_update(str, start, len, val);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_aset(str, indx, val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE indx, val;
|
|
|
|
{
|
2000-02-29 03:05:32 -05:00
|
|
|
long idx, beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(indx)) {
|
|
|
|
case T_FIXNUM:
|
2000-02-29 03:05:32 -05:00
|
|
|
num_index:
|
2002-08-21 11:47:54 -04:00
|
|
|
idx = NUM2LONG(indx);
|
2001-10-03 03:19:19 -04:00
|
|
|
if (RSTRING(str)->len <= idx) {
|
|
|
|
out_of_range:
|
2002-05-28 14:11:07 -04:00
|
|
|
rb_raise(rb_eIndexError, "index %ld out of string", idx);
|
2001-10-03 03:19:19 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (idx < 0) {
|
2001-10-03 03:19:19 -04:00
|
|
|
if (-idx > RSTRING(str)->len)
|
|
|
|
goto out_of_range;
|
1999-08-13 01:45:20 -04:00
|
|
|
idx += RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (FIXNUM_P(val)) {
|
|
|
|
if (RSTRING(str)->len == idx) {
|
|
|
|
RSTRING(str)->len += 1;
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, RSTRING(str)->len);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
RSTRING(str)->ptr[idx] = NUM2INT(val) & 0xff;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_str_update(str, idx, 1, val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
|
|
|
|
case T_REGEXP:
|
2001-10-02 00:31:23 -04:00
|
|
|
rb_str_subpat_set(str, indx, 0, val);
|
1998-01-16 07:13:05 -05:00
|
|
|
return val;
|
|
|
|
|
|
|
|
case T_STRING:
|
1999-01-19 23:59:39 -05:00
|
|
|
beg = rb_str_index(str, indx, 0);
|
2002-06-04 03:34:19 -04:00
|
|
|
if (beg < 0) {
|
|
|
|
rb_raise(rb_eIndexError, "string not matched");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-06-04 03:34:19 -04:00
|
|
|
rb_str_update(str, beg, RSTRING(indx)->len, val);
|
1998-01-16 07:13:05 -05:00
|
|
|
return val;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* check if indx is Range */
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
|
|
|
if (rb_range_beg_len(indx, &beg, &len, RSTRING(str)->len, 2)) {
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_str_update(str, beg, len, val);
|
1998-01-16 07:13:05 -05:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2000-02-29 03:05:32 -05:00
|
|
|
idx = NUM2LONG(indx);
|
|
|
|
goto num_index;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_aset_m(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
2000-02-24 22:51:23 -05:00
|
|
|
if (argc == 3) {
|
2001-10-02 00:31:23 -04:00
|
|
|
if (TYPE(argv[0]) == T_REGEXP) {
|
|
|
|
rb_str_subpat_set(str, argv[0], NUM2INT(argv[1]), argv[2]);
|
|
|
|
}
|
|
|
|
else {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_str_update(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
|
2001-10-02 00:31:23 -04:00
|
|
|
}
|
2000-02-24 22:51:23 -05:00
|
|
|
return argv[2];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc != 2) {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments(%d for 2)", argc);
|
2000-05-24 00:34:26 -04:00
|
|
|
}
|
2000-02-24 22:51:23 -05:00
|
|
|
return rb_str_aset(str, argv[0], argv[1]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_insert(str, idx, str2)
|
|
|
|
VALUE str, idx, str2;
|
|
|
|
{
|
2001-05-06 11:06:00 -04:00
|
|
|
long pos = NUM2LONG(idx);
|
|
|
|
|
2001-07-18 01:56:05 -04:00
|
|
|
rb_str_modify(str);
|
2001-05-06 11:06:00 -04:00
|
|
|
if (pos == -1) {
|
|
|
|
pos = RSTRING(str)->len;
|
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
rb_str_update(str, pos, 0, str2);
|
2001-05-02 00:22:21 -04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_slice_bang(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-05-24 00:34:26 -04:00
|
|
|
VALUE result;
|
|
|
|
VALUE buf[3];
|
|
|
|
int i;
|
2000-01-31 22:12:21 -05:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc < 1 || 2 < argc) {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments(%d for 1)", argc);
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
buf[i] = argv[i];
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
buf[i] = rb_str_new(0,0);
|
2000-06-05 04:46:59 -04:00
|
|
|
result = rb_str_aref_m(argc, buf, str);
|
2002-07-11 04:22:18 -04:00
|
|
|
if (!NIL_P(result)) {
|
|
|
|
rb_str_aset_m(argc+1, buf, str);
|
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
return result;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2002-09-11 00:05:36 -04:00
|
|
|
get_pat(pat, quote)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE pat;
|
2002-09-11 00:05:36 -04:00
|
|
|
int quote;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-07-26 02:12:39 -04:00
|
|
|
VALUE val;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
switch (TYPE(pat)) {
|
|
|
|
case T_REGEXP:
|
2002-07-26 02:12:39 -04:00
|
|
|
return pat;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
default:
|
2002-12-19 04:20:20 -05:00
|
|
|
val = rb_check_string_type(pat);
|
2002-07-26 02:12:39 -04:00
|
|
|
if (NIL_P(val)) {
|
|
|
|
Check_Type(pat, T_REGEXP);
|
|
|
|
}
|
|
|
|
pat = val;
|
|
|
|
}
|
2002-09-11 00:05:36 -04:00
|
|
|
|
|
|
|
if (quote) {
|
|
|
|
val = rb_reg_quote(pat);
|
2003-05-26 04:22:33 -04:00
|
|
|
#if RUBY_VERSION_CODE < 181
|
2002-09-11 00:05:36 -04:00
|
|
|
if (val != pat && rb_str_cmp(val, pat) != 0) {
|
|
|
|
rb_warn("string pattern instead of regexp; metacharacters no longer effective");
|
|
|
|
}
|
2002-07-26 02:12:39 -04:00
|
|
|
#endif
|
2002-09-11 00:05:36 -04:00
|
|
|
pat = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_reg_regcomp(pat);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_sub_bang(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE pat, repl, match;
|
|
|
|
struct re_registers *regs;
|
|
|
|
int iter = 0;
|
2000-06-16 03:44:16 -04:00
|
|
|
int tainted = 0;
|
2000-02-29 03:05:32 -05:00
|
|
|
long plen;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc == 1 && rb_block_given_p()) {
|
1999-01-19 23:59:39 -05:00
|
|
|
iter = 1;
|
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
2001-05-02 00:22:21 -04:00
|
|
|
repl = argv[1];
|
|
|
|
StringValue(repl);
|
2000-06-16 03:44:16 -04:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments(%d for 2)", argc);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-09-11 00:05:36 -04:00
|
|
|
pat = get_pat(argv[0], 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_reg_search(pat, str, 0, 0) >= 0) {
|
2000-03-13 02:18:45 -05:00
|
|
|
rb_str_modify(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
match = rb_backref_get();
|
|
|
|
regs = RMATCH(match)->regs;
|
|
|
|
|
|
|
|
if (iter) {
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_match_busy(match);
|
1999-01-19 23:59:39 -05:00
|
|
|
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_backref_set(match);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
repl = rb_reg_regsub(repl, str, regs);
|
|
|
|
}
|
2000-06-16 03:44:16 -04:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
plen = END(0) - BEG(0);
|
|
|
|
if (RSTRING(repl)->len > plen) {
|
2001-12-19 02:48:32 -05:00
|
|
|
RESIZE_CAPA(str, RSTRING(str)->len + RSTRING(repl)->len - plen);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
if (RSTRING(repl)->len != plen) {
|
|
|
|
memmove(RSTRING(str)->ptr + BEG(0) + RSTRING(repl)->len,
|
|
|
|
RSTRING(str)->ptr + BEG(0) + plen,
|
|
|
|
RSTRING(str)->len - BEG(0) - plen);
|
|
|
|
}
|
|
|
|
memcpy(RSTRING(str)->ptr + BEG(0),
|
|
|
|
RSTRING(repl)->ptr, RSTRING(repl)->len);
|
|
|
|
RSTRING(str)->len += RSTRING(repl)->len - plen;
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2000-06-16 03:44:16 -04:00
|
|
|
if (tainted) OBJ_TAINT(str);
|
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
return str;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_str_sub(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_sub_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-03-13 02:18:45 -05:00
|
|
|
str_gsub(argc, argv, str, bang)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
2000-03-13 02:18:45 -05:00
|
|
|
int bang;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE pat, val, repl, match;
|
|
|
|
struct re_registers *regs;
|
2000-02-29 03:05:32 -05:00
|
|
|
long beg, n;
|
|
|
|
long offset, blen, len;
|
1999-01-19 23:59:39 -05:00
|
|
|
int iter = 0;
|
|
|
|
char *buf, *bp, *cp;
|
2000-01-04 23:41:21 -05:00
|
|
|
int tainted = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc == 1 && rb_block_given_p()) {
|
1999-01-19 23:59:39 -05:00
|
|
|
iter = 1;
|
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
2001-05-02 00:22:21 -04:00
|
|
|
repl = argv[1];
|
|
|
|
StringValue(repl);
|
2000-01-04 23:41:21 -05:00
|
|
|
if (OBJ_TAINTED(repl)) tainted = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments(%d for 2)", argc);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2002-09-11 00:05:36 -04:00
|
|
|
pat = get_pat(argv[0], 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
offset=0; n=0;
|
|
|
|
beg = rb_reg_search(pat, str, 0, 0);
|
2000-03-13 02:18:45 -05:00
|
|
|
if (beg < 0) {
|
|
|
|
if (bang) return Qnil; /* no match, no substitution */
|
|
|
|
return rb_str_dup(str);
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
blen = RSTRING(str)->len + 30; /* len + margin */
|
|
|
|
buf = ALLOC_N(char, blen);
|
|
|
|
bp = buf;
|
|
|
|
cp = RSTRING(str)->ptr;
|
|
|
|
|
|
|
|
while (beg >= 0) {
|
|
|
|
n++;
|
|
|
|
match = rb_backref_get();
|
|
|
|
regs = RMATCH(match)->regs;
|
|
|
|
if (iter) {
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_match_busy(match);
|
1999-01-19 23:59:39 -05:00
|
|
|
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_backref_set(match);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
val = rb_reg_regsub(repl, str, regs);
|
|
|
|
}
|
2000-01-04 23:41:21 -05:00
|
|
|
if (OBJ_TAINTED(val)) tainted = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
len = (bp - buf) + (beg - offset) + RSTRING(val)->len + 3;
|
|
|
|
if (blen < len) {
|
|
|
|
while (blen < len) blen *= 2;
|
|
|
|
len = bp - buf;
|
|
|
|
REALLOC_N(buf, char, blen);
|
|
|
|
bp = buf + len;
|
|
|
|
}
|
|
|
|
len = beg - offset; /* copy pre-match substr */
|
|
|
|
memcpy(bp, cp, len);
|
|
|
|
bp += len;
|
|
|
|
memcpy(bp, RSTRING(val)->ptr, RSTRING(val)->len);
|
|
|
|
bp += RSTRING(val)->len;
|
2003-07-23 02:48:52 -04:00
|
|
|
offset = END(0);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (BEG(0) == END(0)) {
|
|
|
|
/*
|
|
|
|
* Always consume at least one character of the input string
|
|
|
|
* in order to prevent infinite loops.
|
|
|
|
*/
|
2003-06-23 02:52:39 -04:00
|
|
|
if (RSTRING(str)->len <= END(0)) break;
|
1999-11-04 03:39:57 -05:00
|
|
|
len = mbclen2(RSTRING(str)->ptr[END(0)], pat);
|
2003-06-23 02:52:39 -04:00
|
|
|
memcpy(bp, RSTRING(str)->ptr+END(0), len);
|
|
|
|
bp += len;
|
1999-01-19 23:59:39 -05:00
|
|
|
offset = END(0) + len;
|
|
|
|
}
|
|
|
|
cp = RSTRING(str)->ptr + offset;
|
|
|
|
if (offset > RSTRING(str)->len) break;
|
|
|
|
beg = rb_reg_search(pat, str, offset, 0);
|
|
|
|
}
|
|
|
|
if (RSTRING(str)->len > offset) {
|
|
|
|
len = bp - buf;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (blen - len < RSTRING(str)->len - offset + 1) {
|
1999-01-19 23:59:39 -05:00
|
|
|
REALLOC_N(buf, char, len + RSTRING(str)->len - offset + 1);
|
|
|
|
bp = buf + len;
|
|
|
|
}
|
|
|
|
memcpy(bp, cp, RSTRING(str)->len - offset);
|
|
|
|
bp += RSTRING(str)->len - offset;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_backref_set(match);
|
2000-03-13 02:18:45 -05:00
|
|
|
if (bang) {
|
2000-08-07 01:05:04 -04:00
|
|
|
if (str_independent(str)) {
|
|
|
|
free(RSTRING(str)->ptr);
|
|
|
|
}
|
2001-12-10 02:18:16 -05:00
|
|
|
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
|
2000-03-13 02:18:45 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE dup = str_alloc(rb_obj_class(str));
|
2001-10-03 03:19:19 -04:00
|
|
|
|
2000-03-15 00:01:17 -05:00
|
|
|
OBJ_INFECT(dup, str);
|
2001-10-03 03:19:19 -04:00
|
|
|
str = dup;
|
2000-03-13 02:18:45 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
RSTRING(str)->ptr = buf;
|
|
|
|
RSTRING(str)->len = len = bp - buf;
|
|
|
|
RSTRING(str)->ptr[len] = '\0';
|
2001-12-10 02:18:16 -05:00
|
|
|
RSTRING(str)->aux.capa = len;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
if (tainted) OBJ_TAINT(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_gsub_bang(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
return str_gsub(argc, argv, str, 1);
|
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_gsub(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-03-13 02:18:45 -05:00
|
|
|
return str_gsub(argc, argv, str, 0);
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_str_replace(str, str2)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE str, str2;
|
|
|
|
{
|
2000-12-26 03:08:50 -05:00
|
|
|
if (str == str2) return str;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(str2);
|
2001-12-10 02:18:16 -05:00
|
|
|
if (FL_TEST(str2, ELTS_SHARED)) {
|
2000-12-26 03:08:50 -05:00
|
|
|
if (str_independent(str)) {
|
|
|
|
free(RSTRING(str)->ptr);
|
|
|
|
}
|
2000-12-25 01:29:27 -05:00
|
|
|
RSTRING(str)->len = RSTRING(str2)->len;
|
|
|
|
RSTRING(str)->ptr = RSTRING(str2)->ptr;
|
2002-09-13 04:26:35 -04:00
|
|
|
FL_SET(str, ELTS_SHARED);
|
2003-07-02 00:27:29 -04:00
|
|
|
FL_UNSET(str, STR_ASSOC);
|
2002-08-29 05:08:18 -04:00
|
|
|
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
|
2000-12-25 01:29:27 -05:00
|
|
|
}
|
|
|
|
else {
|
2000-12-26 03:08:50 -05:00
|
|
|
rb_str_modify(str);
|
2000-12-25 01:29:27 -05:00
|
|
|
rb_str_resize(str, RSTRING(str2)->len);
|
|
|
|
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
|
2002-08-29 05:08:18 -04:00
|
|
|
if (FL_TEST(str2, STR_ASSOC)) {
|
2002-09-13 04:26:35 -04:00
|
|
|
FL_SET(str, STR_ASSOC);
|
2002-08-29 05:08:18 -04:00
|
|
|
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
|
|
|
|
}
|
2000-12-25 01:29:27 -05:00
|
|
|
}
|
|
|
|
|
2001-12-18 03:47:06 -05:00
|
|
|
OBJ_INFECT(str, str2);
|
1999-01-19 23:59:39 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
uscore_get()
|
|
|
|
{
|
|
|
|
VALUE line;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
line = rb_lastline_get();
|
1998-01-16 07:13:05 -05:00
|
|
|
if (TYPE(line) != T_STRING) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eTypeError, "$_ value need to be String (%s given)",
|
2003-01-30 23:00:17 -05:00
|
|
|
NIL_P(line) ? "nil" : rb_obj_classname(line));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_sub_bang(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_sub_bang(argc, argv, uscore_get());
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_sub(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2000-08-07 01:05:04 -04:00
|
|
|
VALUE str = rb_str_dup(uscore_get());
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
if (NIL_P(rb_str_sub_bang(argc, argv, str)))
|
2000-06-14 04:08:50 -04:00
|
|
|
return str;
|
2000-08-07 01:05:04 -04:00
|
|
|
rb_lastline_set(str);
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_gsub_bang(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_gsub_bang(argc, argv, uscore_get());
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_gsub(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2000-08-07 01:05:04 -04:00
|
|
|
VALUE str = rb_str_dup(uscore_get());
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
if (NIL_P(rb_str_gsub_bang(argc, argv, str)))
|
2000-06-14 04:08:50 -04:00
|
|
|
return str;
|
2000-08-07 01:05:04 -04:00
|
|
|
rb_lastline_set(str);
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_reverse_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-10-10 03:03:36 -04:00
|
|
|
char *s, *e;
|
|
|
|
char c;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-09-05 01:07:54 -04:00
|
|
|
if (RSTRING(str)->len > 1) {
|
|
|
|
rb_str_modify(str);
|
|
|
|
s = RSTRING(str)->ptr;
|
|
|
|
e = s + RSTRING(str)->len - 1;
|
|
|
|
while (s < e) {
|
|
|
|
c = *s;
|
|
|
|
*s++ = *e;
|
|
|
|
*e-- = c;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_reverse(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE obj;
|
|
|
|
char *s, *e, *p;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RSTRING(str)->len <= 1) return rb_str_dup(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
obj = rb_str_new5(str, 0, RSTRING(str)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr; e = s + RSTRING(str)->len - 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
p = RSTRING(obj)->ptr;
|
|
|
|
|
|
|
|
while (e >= s) {
|
|
|
|
*p++ = *e--;
|
|
|
|
}
|
2001-10-02 00:31:23 -04:00
|
|
|
OBJ_INFECT(obj, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_include(str, arg)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, arg;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
|
|
|
if (FIXNUM_P(arg)) {
|
|
|
|
int c = FIX2INT(arg);
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = RSTRING(str)->len;
|
1998-01-16 07:19:22 -05:00
|
|
|
char *p = RSTRING(str)->ptr;
|
|
|
|
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (p[i] == c) {
|
1999-09-22 00:30:11 -04:00
|
|
|
return Qtrue;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(arg);
|
1999-01-19 23:59:39 -05:00
|
|
|
i = rb_str_index(str, arg, 0);
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (i == -1) return Qfalse;
|
1999-09-22 00:30:11 -04:00
|
|
|
return Qtrue;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2002-01-11 04:18:54 -05:00
|
|
|
rb_str_to_i(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-01-11 04:18:54 -05:00
|
|
|
VALUE b;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &b);
|
|
|
|
if (argc == 0) base = 10;
|
|
|
|
else base = NUM2INT(b);
|
|
|
|
|
2003-04-27 10:13:25 -04:00
|
|
|
if (base < 0) {
|
|
|
|
rb_raise(rb_eArgError, "illegal radix %d", base);
|
|
|
|
}
|
2002-02-01 05:23:22 -05:00
|
|
|
return rb_str_to_inum(str, base, Qfalse);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_to_f(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-01 05:23:22 -05:00
|
|
|
return rb_float_new(rb_str_to_dbl(str, Qfalse));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_to_s(str)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str;
|
|
|
|
{
|
2003-02-10 04:40:13 -05:00
|
|
|
if (rb_obj_class(str) != rb_cString) {
|
|
|
|
VALUE dup = str_alloc(rb_cString);
|
|
|
|
rb_str_replace(dup, str);
|
|
|
|
return dup;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_inspect(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *p, *pend;
|
2001-05-30 05:12:34 -04:00
|
|
|
VALUE result = rb_str_buf_new2("\"");
|
2000-12-05 04:36:54 -05:00
|
|
|
char s[5];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
1999-01-19 23:59:39 -05:00
|
|
|
char c = *p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (ismbchar(c) && p < pend) {
|
1999-08-13 01:45:20 -04:00
|
|
|
int len = mbclen(c);
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, p - 1, len);
|
2000-12-26 03:32:35 -05:00
|
|
|
p += len - 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (c == '"'|| c == '\\') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = c;
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (ISPRINT(c)) {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = c;
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == '\n') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'n';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == '\r') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'r';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == '\t') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 't';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == '\f') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'f';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (c == '\013') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'v';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == '\007') {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'a';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (c == 033) {
|
2000-12-05 04:36:54 -05:00
|
|
|
s[0] = '\\'; s[1] = 'e';
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat(result, s, 2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2000-12-05 04:36:54 -05:00
|
|
|
sprintf(s, "\\%03o", c & 0377);
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat2(result, s);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-05-30 05:12:34 -04:00
|
|
|
rb_str_buf_cat2(result, "\"");
|
2000-12-05 04:36:54 -05:00
|
|
|
|
|
|
|
OBJ_INFECT(result, str);
|
|
|
|
return result;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2002-10-23 06:17:30 -04:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_dump(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-29 03:05:32 -05:00
|
|
|
long len;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *p, *pend;
|
|
|
|
char *q, *qend;
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
len = 2; /* "" */
|
|
|
|
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
|
|
|
|
while (p < pend) {
|
|
|
|
char c = *p++;
|
|
|
|
switch (c) {
|
|
|
|
case '"': case '\\':
|
|
|
|
case '\n': case '\r':
|
1999-08-13 01:45:20 -04:00
|
|
|
case '\t': case '\f': case '#':
|
1999-01-19 23:59:39 -05:00
|
|
|
case '\013': case '\007': case '\033':
|
|
|
|
len += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (ISPRINT(c)) {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len += 4; /* \nnn */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-02 00:31:23 -04:00
|
|
|
result = rb_str_new5(str, 0, len);
|
1999-01-19 23:59:39 -05:00
|
|
|
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
|
|
|
|
q = RSTRING(result)->ptr; qend = q + len;
|
|
|
|
|
|
|
|
*q++ = '"';
|
|
|
|
while (p < pend) {
|
|
|
|
char c = *p++;
|
|
|
|
|
|
|
|
if (c == '"' || c == '\\') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = c;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
else if (c == '#') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = '#';
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (ISPRINT(c)) {
|
|
|
|
*q++ = c;
|
|
|
|
}
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
else if (c == '\007') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'a';
|
|
|
|
}
|
|
|
|
else if (c == '\033') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'e';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*q++ = '\\';
|
|
|
|
sprintf(q, "%03o", c&0xff);
|
|
|
|
q += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q++ = '"';
|
|
|
|
|
2000-12-05 04:36:54 -05:00
|
|
|
OBJ_INFECT(result, str);
|
1999-01-19 23:59:39 -05:00
|
|
|
return result;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_upcase_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send;
|
2000-02-08 03:54:01 -05:00
|
|
|
int modify = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (s < send) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ismbchar(*s)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
s+=mbclen(*s) - 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2001-02-08 04:19:27 -05:00
|
|
|
else if (ISLOWER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = toupper(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
|
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_str_upcase(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_upcase_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_downcase_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send;
|
2000-02-08 03:54:01 -05:00
|
|
|
int modify = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (s < send) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ismbchar(*s)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
s+=mbclen(*s) - 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else if (ISUPPER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = tolower(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
|
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_str_downcase(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_downcase_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_capitalize_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send;
|
2000-02-08 03:54:01 -05:00
|
|
|
int modify = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
2003-03-26 13:26:45 -05:00
|
|
|
if (RSTRING(str)->len == 0 || !RSTRING(str)->ptr) return Qnil;
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ISLOWER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = toupper(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
while (++s < send) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ismbchar(*s)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
s+=mbclen(*s) - 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else if (ISUPPER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = tolower(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_str_capitalize(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_capitalize_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_swapcase_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send;
|
2000-02-08 03:54:01 -05:00
|
|
|
int modify = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (s < send) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ismbchar(*s)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
s+=mbclen(*s) - 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else if (ISUPPER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = tolower(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (ISLOWER(*s)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*s = toupper(*s);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_swapcase(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_swapcase_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
typedef unsigned char *USTR;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
struct tr {
|
1998-01-16 07:13:05 -05:00
|
|
|
int gen, now, max;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *p, *pend;
|
|
|
|
};
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
trnext(t)
|
|
|
|
struct tr *t;
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (!t->gen) {
|
|
|
|
if (t->p == t->pend) return -1;
|
2001-03-04 04:46:08 -05:00
|
|
|
if (t->p < t->pend - 1 && *t->p == '\\') {
|
|
|
|
t->p++;
|
|
|
|
}
|
2001-09-08 10:17:53 -04:00
|
|
|
t->now = *(USTR)t->p++;
|
|
|
|
if (t->p < t->pend - 1 && *t->p == '-') {
|
1998-01-16 07:13:05 -05:00
|
|
|
t->p++;
|
|
|
|
if (t->p < t->pend) {
|
|
|
|
if (t->now > *(USTR)t->p) {
|
|
|
|
t->p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
t->gen = 1;
|
|
|
|
t->max = *(USTR)t->p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t->now;
|
|
|
|
}
|
|
|
|
else if (++t->now < t->max) {
|
|
|
|
return t->now;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->gen = 0;
|
|
|
|
return t->max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE rb_str_delete_bang _((int,VALUE*,VALUE));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
tr_trans(str, src, repl, sflag)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, src, repl;
|
1998-01-16 07:13:05 -05:00
|
|
|
int sflag;
|
|
|
|
{
|
|
|
|
struct tr trsrc, trrepl;
|
|
|
|
int cflag = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
int trans[256];
|
2000-02-08 03:54:01 -05:00
|
|
|
int i, c, modify = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(src);
|
|
|
|
StringValue(repl);
|
2002-08-28 02:01:58 -04:00
|
|
|
if (RSTRING(str)->len == 0 || !RSTRING(str)->ptr) return Qnil;
|
1998-01-16 07:19:22 -05:00
|
|
|
trsrc.p = RSTRING(src)->ptr; trsrc.pend = trsrc.p + RSTRING(src)->len;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RSTRING(src)->len >= 2 && RSTRING(src)->ptr[0] == '^') {
|
1998-01-16 07:13:05 -05:00
|
|
|
cflag++;
|
|
|
|
trsrc.p++;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RSTRING(repl)->len == 0) {
|
2000-02-08 03:54:01 -05:00
|
|
|
return rb_str_delete_bang(1, &src, str);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
trrepl.p = RSTRING(repl)->ptr;
|
|
|
|
trrepl.pend = trrepl.p + RSTRING(repl)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
trsrc.gen = trrepl.gen = 0;
|
|
|
|
trsrc.now = trrepl.now = 0;
|
|
|
|
trsrc.max = trrepl.max = 0;
|
|
|
|
|
|
|
|
if (cflag) {
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
trans[i] = 1;
|
|
|
|
}
|
|
|
|
while ((c = trnext(&trsrc)) >= 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
trans[c & 0xff] = -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
while ((c = trnext(&trrepl)) >= 0)
|
|
|
|
/* retrieve last replacer */;
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<256; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (trans[i] >= 0) {
|
|
|
|
trans[i] = trrepl.now;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
int r;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (i=0; i<256; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
trans[i] = -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
while ((c = trnext(&trsrc)) >= 0) {
|
|
|
|
r = trnext(&trrepl);
|
|
|
|
if (r == -1) r = trrepl.now;
|
|
|
|
trans[c & 0xff] = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (sflag) {
|
1999-01-19 23:59:39 -05:00
|
|
|
char *t = s;
|
1999-08-13 01:45:20 -04:00
|
|
|
int c0, last = -1;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
while (s < send) {
|
1999-08-13 01:45:20 -04:00
|
|
|
c0 = *s++;
|
|
|
|
if ((c = trans[c0 & 0xff]) >= 0) {
|
|
|
|
if (last == c) continue;
|
|
|
|
last = c;
|
|
|
|
*t++ = c & 0xff;
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
last = -1;
|
|
|
|
*t++ = c0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RSTRING(str)->len > (t - RSTRING(str)->ptr)) {
|
|
|
|
RSTRING(str)->len = (t - RSTRING(str)->ptr);
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
*t = '\0';
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (s < send) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if ((c = trans[*s & 0xff]) >= 0) {
|
|
|
|
*s = c & 0xff;
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
s++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
|
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_tr_bang(str, src, repl)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str, src, repl;
|
|
|
|
{
|
2000-02-08 03:54:01 -05:00
|
|
|
return tr_trans(str, src, repl, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_tr(str, src, repl)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str, src, repl;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
tr_trans(str, src, repl, 0);
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-08-13 01:45:20 -04:00
|
|
|
tr_setup_table(str, table, init)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1999-01-19 23:59:39 -05:00
|
|
|
char table[256];
|
1999-08-13 01:45:20 -04:00
|
|
|
int init;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
char buf[256];
|
1998-01-16 07:13:05 -05:00
|
|
|
struct tr tr;
|
1999-08-13 01:45:20 -04:00
|
|
|
int i, c;
|
|
|
|
int cflag = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
tr.p = RSTRING(str)->ptr; tr.pend = tr.p + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
tr.gen = tr.now = tr.max = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(str)->len > 1 && RSTRING(str)->ptr[0] == '^') {
|
1999-08-13 01:45:20 -04:00
|
|
|
cflag = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
tr.p++;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (init) {
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
table[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
buf[i] = cflag;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
while ((c = trnext(&tr)) >= 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
buf[c & 0xff] = !cflag;
|
|
|
|
}
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
table[i] = table[i]&&buf[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_delete_bang(argc, argv, str)
|
1999-08-13 01:45:20 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *send, *t;
|
|
|
|
char squeez[256];
|
2000-02-08 03:54:01 -05:00
|
|
|
int modify = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
int init = 1;
|
|
|
|
int i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-02-28 01:30:03 -05:00
|
|
|
if (argc < 1) {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments");
|
2001-02-28 01:30:03 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
VALUE s = argv[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(s);
|
1999-08-13 01:45:20 -04:00
|
|
|
tr_setup_table(s, squeez, init);
|
|
|
|
init = 0;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
1999-08-13 01:45:20 -04:00
|
|
|
s = t = RSTRING(str)->ptr;
|
2002-08-28 02:01:58 -04:00
|
|
|
if (!s || RSTRING(str)->len == 0) return Qnil;
|
1999-08-13 01:45:20 -04:00
|
|
|
send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (s < send) {
|
2000-02-08 03:54:01 -05:00
|
|
|
if (squeez[*s & 0xff])
|
|
|
|
modify = 1;
|
|
|
|
else
|
1998-01-16 07:13:05 -05:00
|
|
|
*t++ = *s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*t = '\0';
|
1999-08-13 01:45:20 -04:00
|
|
|
RSTRING(str)->len = t - RSTRING(str)->ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-02 03:10:32 -05:00
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_delete(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_delete_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_squeeze_bang(argc, argv, str)
|
1999-08-13 01:45:20 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char squeez[256];
|
|
|
|
char *s, *send, *t;
|
2000-02-08 03:54:01 -05:00
|
|
|
int c, save, modify = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
int init = 1;
|
|
|
|
int i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (argc == 0) {
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
squeez[i] = 1;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
VALUE s = argv[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(s);
|
1999-08-13 01:45:20 -04:00
|
|
|
tr_setup_table(s, squeez, init);
|
|
|
|
init = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
1999-08-13 01:45:20 -04:00
|
|
|
s = t = RSTRING(str)->ptr;
|
2002-08-28 02:01:58 -04:00
|
|
|
if (!s || RSTRING(str)->len == 0) return Qnil;
|
1999-08-13 01:45:20 -04:00
|
|
|
send = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
save = -1;
|
|
|
|
while (s < send) {
|
|
|
|
c = *s++ & 0xff;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (c != save || !squeez[c]) {
|
1998-01-16 07:13:05 -05:00
|
|
|
*t++ = save = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t = '\0';
|
1999-08-13 01:45:20 -04:00
|
|
|
if (t - RSTRING(str)->ptr != RSTRING(str)->len) {
|
|
|
|
RSTRING(str)->len = t - RSTRING(str)->ptr;
|
2000-02-08 03:54:01 -05:00
|
|
|
modify = 1;
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-08 03:54:01 -05:00
|
|
|
if (modify) return str;
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_squeeze(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_squeeze_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_tr_s_bang(str, src, repl)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str, src, repl;
|
|
|
|
{
|
2000-02-08 03:54:01 -05:00
|
|
|
return tr_trans(str, src, repl, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_tr_s(str, src, repl)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str, src, repl;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
tr_trans(str, src, repl, 1);
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_count(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
char table[256];
|
|
|
|
char *s, *send;
|
|
|
|
int init = 1;
|
|
|
|
int i;
|
|
|
|
|
2000-07-25 02:03:50 -04:00
|
|
|
if (argc < 1) {
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments");
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
VALUE s = argv[i];
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(s);
|
1999-08-13 01:45:20 -04:00
|
|
|
tr_setup_table(s, table, init);
|
|
|
|
init = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = RSTRING(str)->ptr;
|
2003-02-06 03:25:12 -05:00
|
|
|
if (!s || RSTRING(str)->len == 0) return INT2FIX(0);
|
1999-08-13 01:45:20 -04:00
|
|
|
send = s + RSTRING(str)->len;
|
|
|
|
i = 0;
|
|
|
|
while (s < send) {
|
|
|
|
if (table[*s++ & 0xff]) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INT2NUM(i);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_str_split_m(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE spat;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE limit;
|
2002-07-26 02:12:39 -04:00
|
|
|
int awk_split = Qfalse;
|
2002-05-29 01:20:39 -04:00
|
|
|
long beg, end, i = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
int lim = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE result, tmp;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
|
1998-01-16 07:13:05 -05:00
|
|
|
lim = NUM2INT(limit);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (lim <= 0) limit = Qnil;
|
2003-07-24 01:18:47 -04:00
|
|
|
else if (lim == 1) {
|
|
|
|
if (RSTRING(str)->len == 0)
|
|
|
|
return rb_ary_new2(0);
|
|
|
|
return rb_ary_new3(1, str);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
if (NIL_P(spat)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!NIL_P(rb_fs)) {
|
|
|
|
spat = rb_fs;
|
1998-01-16 07:13:05 -05:00
|
|
|
goto fs_set;
|
|
|
|
}
|
2002-07-26 02:12:39 -04:00
|
|
|
awk_split = Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2000-01-04 23:41:21 -05:00
|
|
|
fs_set:
|
2002-07-26 02:12:39 -04:00
|
|
|
if (TYPE(spat) == T_STRING && RSTRING(spat)->len == 1) {
|
|
|
|
if (RSTRING(spat)->ptr[0] == ' ') {
|
|
|
|
awk_split = Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-07-26 02:12:39 -04:00
|
|
|
spat = rb_reg_regcomp(rb_reg_quote(spat));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-07-26 02:12:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-09-11 00:05:36 -04:00
|
|
|
spat = get_pat(spat, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
result = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
beg = 0;
|
2002-07-26 02:12:39 -04:00
|
|
|
if (awk_split) {
|
1999-01-19 23:59:39 -05:00
|
|
|
char *ptr = RSTRING(str)->ptr;
|
2000-02-29 03:05:32 -05:00
|
|
|
long len = RSTRING(str)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *eptr = ptr + len;
|
2002-07-26 02:12:39 -04:00
|
|
|
int skip = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-07-26 02:12:39 -04:00
|
|
|
for (end = beg = 0; ptr<eptr; ptr++) {
|
|
|
|
if (skip) {
|
|
|
|
if (ISSPACE(*ptr)) {
|
|
|
|
beg++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-07-26 02:12:39 -04:00
|
|
|
end = beg+1;
|
|
|
|
skip = 0;
|
2003-06-27 01:41:28 -04:00
|
|
|
if (!NIL_P(limit) && lim <= i) break;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2002-07-26 02:12:39 -04:00
|
|
|
else {
|
|
|
|
if (ISSPACE(*ptr)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(result, rb_str_substr(str, beg, end-beg));
|
2002-07-26 02:12:39 -04:00
|
|
|
skip = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
beg = end + 1;
|
2003-06-27 01:41:28 -04:00
|
|
|
if (!NIL_P(limit)) ++i;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-07-26 02:12:39 -04:00
|
|
|
else {
|
|
|
|
end++;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-02-29 03:05:32 -05:00
|
|
|
long start = beg;
|
|
|
|
long idx;
|
1998-01-16 07:13:05 -05:00
|
|
|
int last_null = 0;
|
|
|
|
struct re_registers *regs;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
while ((end = rb_reg_search(spat, str, start, 0)) >= 0) {
|
|
|
|
regs = RMATCH(rb_backref_get())->regs;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (start == end && BEG(0) == END(0)) {
|
2002-10-24 13:54:53 -04:00
|
|
|
if (!RSTRING(str)->ptr) {
|
|
|
|
rb_ary_push(result, rb_str_new("", 0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (last_null == 1) {
|
1999-11-04 03:39:57 -05:00
|
|
|
rb_ary_push(result, rb_str_substr(str, beg, mbclen2(RSTRING(str)->ptr[beg],spat)));
|
1998-01-16 07:13:05 -05:00
|
|
|
beg = start;
|
|
|
|
}
|
|
|
|
else {
|
1999-11-04 03:39:57 -05:00
|
|
|
start += mbclen2(RSTRING(str)->ptr[start],spat);
|
1998-01-16 07:13:05 -05:00
|
|
|
last_null = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(result, rb_str_substr(str, beg, end-beg));
|
1998-01-16 07:13:05 -05: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))
|
2001-10-02 00:31:23 -04:00
|
|
|
tmp = rb_str_new5(str, 0, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
tmp = rb_str_substr(str, BEG(idx), END(idx)-BEG(idx));
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(result, tmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
if (!NIL_P(limit) && lim <= ++i) break;
|
|
|
|
}
|
|
|
|
}
|
2003-04-14 05:04:43 -04:00
|
|
|
if (RSTRING(str)->len > 0 && (!NIL_P(limit) || RSTRING(str)->len > beg || lim < 0)) {
|
2000-06-23 03:05:59 -04:00
|
|
|
if (RSTRING(str)->len == beg)
|
2001-10-02 00:31:23 -04:00
|
|
|
tmp = rb_str_new5(str, 0, 0);
|
2000-06-23 03:05:59 -04:00
|
|
|
else
|
|
|
|
tmp = rb_str_substr(str, beg, RSTRING(str)->len-beg);
|
|
|
|
rb_ary_push(result, tmp);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
if (NIL_P(limit) && lim == 0) {
|
|
|
|
while (RARRAY(result)->len > 0 &&
|
|
|
|
RSTRING(RARRAY(result)->ptr[RARRAY(result)->len-1])->len == 0)
|
|
|
|
rb_ary_pop(result);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_split(str, sep0)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *sep0;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE sep;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
sep = rb_str_new2(sep0);
|
2000-01-04 23:41:21 -05:00
|
|
|
return rb_str_split_m(1, &sep, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_split(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
return rb_str_split_m(argc, argv, uscore_get());
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_each_line(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE rs;
|
|
|
|
int newline;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *p = RSTRING(str)->ptr, *pend = p + RSTRING(str)->len, *s;
|
|
|
|
char *ptr = p;
|
2002-08-21 11:47:54 -04:00
|
|
|
long len = RSTRING(str)->len, rslen;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE line;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_scan_args(argc, argv, "01", &rs) == 0) {
|
|
|
|
rs = rb_rs;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(rs)) {
|
|
|
|
rb_yield(str);
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(rs);
|
1998-01-16 07:13:05 -05:00
|
|
|
rslen = RSTRING(rs)->len;
|
|
|
|
if (rslen == 0) {
|
|
|
|
newline = '\n';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newline = RSTRING(rs)->ptr[rslen-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (s = p, p += rslen; p < pend; p++) {
|
|
|
|
if (rslen == 0 && *p == '\n') {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (*++p != '\n') continue;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (*p == '\n') p++;
|
|
|
|
}
|
2002-09-25 03:03:05 -04:00
|
|
|
if (RSTRING(str)->ptr < p && p[-1] == newline &&
|
1998-01-16 07:13:05 -05:00
|
|
|
(rslen <= 1 ||
|
2000-06-14 01:30:29 -04:00
|
|
|
rb_memcmp(RSTRING(rs)->ptr, p-rslen, rslen) == 0)) {
|
2001-10-02 00:31:23 -04:00
|
|
|
line = rb_str_new5(str, s, p - s);
|
2001-10-03 03:19:19 -04:00
|
|
|
OBJ_INFECT(line, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(line);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(str)->ptr != ptr || RSTRING(str)->len != len)
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "string modified");
|
|
|
|
s = p;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s != pend) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (p > pend) p = pend;
|
2001-10-02 00:31:23 -04:00
|
|
|
line = rb_str_new5(str, s, p - s);
|
2000-01-31 22:12:21 -05:00
|
|
|
OBJ_INFECT(line, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(line);
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_each_byte(str)
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-02-29 03:05:32 -05:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RSTRING(str)->len; i++) {
|
|
|
|
rb_yield(INT2FIX(RSTRING(str)->ptr[i] & 0xff));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_chop_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(str)->len > 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_modify(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->len--;
|
|
|
|
if (RSTRING(str)->ptr[RSTRING(str)->len] == '\n') {
|
|
|
|
if (RSTRING(str)->len > 0 &&
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len-1] == '\r') {
|
|
|
|
RSTRING(str)->len--;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2000-02-08 03:54:01 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_chop(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_chop_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_chop_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_chop_bang(uscore_get());
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_chop()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-06-14 04:08:50 -04:00
|
|
|
VALUE str = uscore_get();
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-06-14 04:08:50 -04:00
|
|
|
if (RSTRING(str)->len > 0) {
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_chop_bang(str);
|
|
|
|
rb_lastline_set(str);
|
|
|
|
}
|
2000-02-02 03:10:32 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
2000-02-02 03:10:32 -05:00
|
|
|
rb_str_chomp_bang(argc, argv, str)
|
1999-01-19 23:59:39 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rs;
|
|
|
|
int newline;
|
|
|
|
char *p = RSTRING(str)->ptr;
|
2002-08-21 11:47:54 -04:00
|
|
|
long len = RSTRING(str)->len, rslen;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_scan_args(argc, argv, "01", &rs) == 0) {
|
2001-10-30 03:43:28 -05:00
|
|
|
if (len == 0) return Qnil;
|
1999-01-19 23:59:39 -05:00
|
|
|
rs = rb_rs;
|
2001-10-30 03:43:28 -05:00
|
|
|
if (rs == rb_default_rs) {
|
|
|
|
smart_chomp:
|
|
|
|
if (RSTRING(str)->ptr[len-1] == '\n') {
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
2001-10-30 03:43:28 -05:00
|
|
|
RSTRING(str)->len--;
|
|
|
|
if (RSTRING(str)->len > 0 &&
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len-1] == '\r') {
|
|
|
|
RSTRING(str)->len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RSTRING(str)->ptr[len-1] == '\r') {
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
2001-10-30 03:43:28 -05:00
|
|
|
RSTRING(str)->len--;
|
|
|
|
}
|
2001-11-08 01:43:14 -05:00
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
|
|
|
return str;
|
2001-10-30 03:43:28 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
if (NIL_P(rs)) return Qnil;
|
2001-10-30 03:43:28 -05:00
|
|
|
if (len == 0) return Qnil;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(rs);
|
1999-01-19 23:59:39 -05:00
|
|
|
rslen = RSTRING(rs)->len;
|
|
|
|
if (rslen == 0) {
|
|
|
|
while (len>0 && p[len-1] == '\n') {
|
|
|
|
len--;
|
2001-10-30 03:43:28 -05:00
|
|
|
if (len>0 && p[len-1] == '\r')
|
|
|
|
len--;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
if (len < RSTRING(str)->len) {
|
2000-03-15 00:01:17 -05:00
|
|
|
rb_str_modify(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
RSTRING(str)->len = len;
|
|
|
|
RSTRING(str)->ptr[len] = '\0';
|
|
|
|
return str;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
if (rslen > len) return Qnil;
|
|
|
|
newline = RSTRING(rs)->ptr[rslen-1];
|
2001-10-30 03:43:28 -05:00
|
|
|
if (rslen == 1 && newline == '\n')
|
|
|
|
goto smart_chomp;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (p[len-1] == newline &&
|
|
|
|
(rslen <= 1 ||
|
2000-06-14 01:30:29 -04:00
|
|
|
rb_memcmp(RSTRING(rs)->ptr, p+len-rslen, rslen) == 0)) {
|
2000-03-15 00:01:17 -05:00
|
|
|
rb_str_modify(str);
|
1999-01-19 23:59:39 -05:00
|
|
|
RSTRING(str)->len -= rslen;
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2000-02-08 03:54:01 -05:00
|
|
|
return str;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_chomp(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_chomp_bang(argc, argv, str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_chomp_bang(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_chomp_bang(argc, argv, uscore_get());
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_chomp(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2000-06-14 04:08:50 -04:00
|
|
|
VALUE str = uscore_get();
|
|
|
|
VALUE dup = rb_str_dup(str);
|
2000-01-31 22:12:21 -05:00
|
|
|
|
2000-06-14 04:08:50 -04:00
|
|
|
if (NIL_P(rb_str_chomp_bang(argc, argv, dup)))
|
|
|
|
return str;
|
|
|
|
rb_lastline_set(dup);
|
|
|
|
return dup;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2001-08-05 23:05:23 -04:00
|
|
|
rb_str_lstrip_bang(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, *t, *e;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
s = RSTRING(str)->ptr;
|
2002-08-28 02:01:58 -04:00
|
|
|
if (!s || RSTRING(str)->len == 0) return Qnil;
|
1998-01-16 07:19:22 -05:00
|
|
|
e = t = s + RSTRING(str)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
/* remove spaces at head */
|
2003-07-27 13:20:29 -04:00
|
|
|
while (s < t && ISSPACE(*s)) s++;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-05 23:05:23 -04:00
|
|
|
if (s > RSTRING(str)->ptr) {
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
|
|
|
RSTRING(str)->len = t-s;
|
2001-08-05 23:05:23 -04:00
|
|
|
memmove(RSTRING(str)->ptr, s, RSTRING(str)->len);
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
2001-08-14 04:13:31 -04:00
|
|
|
return Qnil;
|
2001-08-05 23:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_lstrip(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_lstrip_bang(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_rstrip_bang(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
char *s, *t, *e;
|
|
|
|
|
|
|
|
s = RSTRING(str)->ptr;
|
2002-08-28 02:01:58 -04:00
|
|
|
if (!s || RSTRING(str)->len == 0) return Qnil;
|
2001-08-05 23:05:23 -04:00
|
|
|
e = t = s + RSTRING(str)->len;
|
|
|
|
|
2003-07-27 13:20:29 -04:00
|
|
|
/* remove trailing '\0's */
|
|
|
|
while (s < t && t[-1] == '\0') t--;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
/* remove trailing spaces */
|
2003-07-27 13:20:29 -04:00
|
|
|
while (s < t && ISSPACE(*(t-1))) t--;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-05 23:05:23 -04:00
|
|
|
if (t < e) {
|
2003-07-27 13:20:29 -04:00
|
|
|
rb_str_modify(str);
|
|
|
|
RSTRING(str)->len = t-s;
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2001-08-05 23:05:23 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-08-05 23:05:23 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
|
2001-08-05 23:05:23 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_rstrip(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_rstrip_bang(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_str_strip_bang(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
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 03:10:23 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_str_strip(str)
|
|
|
|
VALUE str;
|
|
|
|
{
|
2000-02-02 03:10:32 -05:00
|
|
|
str = rb_str_dup(str);
|
|
|
|
rb_str_strip_bang(str);
|
|
|
|
return str;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
scan_once(str, pat, start)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, pat;
|
1999-08-13 01:45:20 -04:00
|
|
|
long *start;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE result, match;
|
1998-01-16 07:13:05 -05:00
|
|
|
struct re_registers *regs;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_reg_search(pat, str, *start, 0) >= 0) {
|
|
|
|
match = rb_backref_get();
|
|
|
|
regs = RMATCH(match)->regs;
|
|
|
|
if (BEG(0) == END(0)) {
|
|
|
|
/*
|
|
|
|
* Always consume at least one character of the input string
|
|
|
|
*/
|
2003-06-23 02:52:39 -04:00
|
|
|
if (RSTRING(str)->len < END(0))
|
|
|
|
*start = END(0)+mbclen2(RSTRING(str)->ptr[END(0)],pat);
|
|
|
|
else
|
|
|
|
*start = END(0)+1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*start = END(0);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (regs->num_regs == 1) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_reg_nth_match(0, match);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
result = rb_ary_new2(regs->num_regs);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=1; i < regs->num_regs; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(result, rb_reg_nth_match(i, match));
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_scan(str, pat)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, pat;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE result;
|
1999-08-13 01:45:20 -04:00
|
|
|
long start = 0;
|
2000-02-08 03:54:01 -05:00
|
|
|
VALUE match = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-09-11 00:05:36 -04:00
|
|
|
pat = get_pat(pat, 1);
|
2000-05-24 00:34:26 -04:00
|
|
|
if (!rb_block_given_p()) {
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
|
|
|
|
while (!NIL_P(result = scan_once(str, pat, &start))) {
|
2000-02-08 03:54:01 -05:00
|
|
|
match = rb_backref_get();
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, result);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_backref_set(match);
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
while (!NIL_P(result = scan_once(str, pat, &start))) {
|
2000-02-08 03:54:01 -05:00
|
|
|
match = rb_backref_get();
|
|
|
|
rb_match_busy(match);
|
2003-08-27 15:43:50 -04:00
|
|
|
rb_yield(result);
|
2000-09-01 05:18:11 -04:00
|
|
|
rb_backref_set(match); /* restore $~ value */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
rb_backref_set(match);
|
1999-08-13 01:45:20 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-03-08 01:25:19 -05:00
|
|
|
static VALUE
|
|
|
|
rb_f_scan(self, pat)
|
|
|
|
VALUE self, pat;
|
|
|
|
{
|
|
|
|
return rb_str_scan(uscore_get(), pat);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_hex(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-01 05:23:22 -05:00
|
|
|
return rb_str_to_inum(str, 16, Qfalse);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_oct(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-01 05:23:22 -05:00
|
|
|
return rb_str_to_inum(str, -8, Qfalse);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_crypt(str, salt)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str, salt;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
extern char *crypt();
|
2002-06-18 02:29:07 -04:00
|
|
|
VALUE result;
|
2002-08-28 02:01:58 -04:00
|
|
|
char *s;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(salt);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(salt)->len < 2)
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eArgError, "salt too short(need >=2 bytes)");
|
2002-06-18 02:29:07 -04:00
|
|
|
|
2002-08-28 02:01:58 -04:00
|
|
|
if (RSTRING(str)->ptr) s = RSTRING(str)->ptr;
|
|
|
|
else s = "";
|
|
|
|
result = rb_str_new2(crypt(s, RSTRING(salt)->ptr));
|
2002-06-18 02:29:07 -04:00
|
|
|
OBJ_INFECT(result, str);
|
|
|
|
OBJ_INFECT(result, salt);
|
|
|
|
return result;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-10-23 06:17:30 -04:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_intern(str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
ID id;
|
|
|
|
|
2002-08-28 02:01:58 -04:00
|
|
|
if (!RSTRING(str)->ptr || RSTRING(str)->len == 0) {
|
|
|
|
rb_raise(rb_eArgError, "interning empty string");
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (strlen(RSTRING(str)->ptr) != RSTRING(str)->len)
|
2003-03-07 00:59:42 -05:00
|
|
|
rb_raise(rb_eArgError, "symbol string may not contain `\\0'");
|
1998-01-16 07:19:22 -05:00
|
|
|
id = rb_intern(RSTRING(str)->ptr);
|
2000-03-07 03:37:59 -05:00
|
|
|
return ID2SYM(id);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_sum(argc, argv, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE vbits;
|
2002-08-28 04:05:23 -04:00
|
|
|
int bits;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *p, *pend;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_scan_args(argc, argv, "01", &vbits) == 0) {
|
|
|
|
bits = 16;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
else bits = NUM2INT(vbits);
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (bits > sizeof(long)*CHAR_BIT) {
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE res = INT2FIX(0);
|
|
|
|
VALUE mod;
|
|
|
|
|
|
|
|
mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits));
|
|
|
|
mod = rb_funcall(mod, '-', 1, INT2FIX(1));
|
|
|
|
|
|
|
|
while (p < pend) {
|
1999-01-19 23:59:39 -05:00
|
|
|
res = rb_funcall(res, '+', 1, INT2FIX((unsigned int)*p));
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
res = rb_funcall(res, '&', 1, mod);
|
1998-01-16 07:13:05 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
unsigned int res = 0;
|
|
|
|
unsigned int mod = (1<<bits)-1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (mod == 0) {
|
|
|
|
mod = -1;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
1999-01-19 23:59:39 -05:00
|
|
|
res += (unsigned int)*p;
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
res &= mod;
|
|
|
|
return rb_int2inum(res);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-05-02 02:41:33 -04:00
|
|
|
rb_str_justify(argc, argv, str, jflag)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
2003-05-02 02:41:33 -04:00
|
|
|
char jflag;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-05-02 02:41:33 -04:00
|
|
|
VALUE w;
|
|
|
|
long width, flen = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE res;
|
2003-05-02 02:41:33 -04:00
|
|
|
char *p, *pend, *f = " ";
|
|
|
|
long n;
|
|
|
|
VALUE pad;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &w, &pad) == 2) {
|
|
|
|
if (!NIL_P(pad)) {
|
|
|
|
StringValue(pad);
|
|
|
|
if (RSTRING(pad)->len > 0) {
|
|
|
|
f = RSTRING(pad)->ptr;
|
|
|
|
flen = RSTRING(pad)->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
width = NUM2LONG(w);
|
2002-03-11 03:02:04 -05:00
|
|
|
if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
|
2001-10-02 00:31:23 -04:00
|
|
|
res = rb_str_new5(str, 0, width);
|
2003-05-02 02:41:33 -04:00
|
|
|
p = RSTRING(res)->ptr;
|
|
|
|
if (jflag != 'l') {
|
|
|
|
n = width - RSTRING(str)->len;
|
|
|
|
pend = p + ((jflag == 'r') ? n : n/2);
|
|
|
|
if (flen <= 1) {
|
|
|
|
while (p < pend) {
|
|
|
|
*p++ = *f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *q = f;
|
|
|
|
while (p + flen <= pend) {
|
|
|
|
memcpy(p,f,flen);
|
|
|
|
p += flen;
|
|
|
|
}
|
|
|
|
while (p < pend) {
|
|
|
|
*p++ = *q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(p, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
if (jflag != 'r') {
|
|
|
|
p += RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
|
|
|
|
if (flen <= 1) {
|
|
|
|
while (p < pend) {
|
|
|
|
*p++ = *f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (p + flen <= pend) {
|
|
|
|
memcpy(p,f,flen);
|
|
|
|
p += flen;
|
|
|
|
}
|
|
|
|
while (p < pend) {
|
|
|
|
*p++ = *f++;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-05-30 05:12:34 -04:00
|
|
|
OBJ_INFECT(res, str);
|
2003-05-02 02:41:33 -04:00
|
|
|
if (flen > 0) OBJ_INFECT(res, pad);
|
1998-01-16 07:19:22 -05:00
|
|
|
return res;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-05-02 02:41:33 -04:00
|
|
|
rb_str_ljust(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-05-02 02:41:33 -04:00
|
|
|
return rb_str_justify(argc, argv, str, 'l');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-05-02 02:41:33 -04:00
|
|
|
rb_str_rjust(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-05-02 02:41:33 -04:00
|
|
|
return rb_str_justify(argc, argv, str, 'r');
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-05-02 02:41:33 -04:00
|
|
|
static VALUE
|
|
|
|
rb_str_center(argc, argv, str)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
return rb_str_justify(argc, argv, str, 'c');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-08-25 04:26:06 -04:00
|
|
|
void
|
|
|
|
rb_str_setter(val, id, var)
|
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
if (!NIL_P(val) && TYPE(val) != T_STRING) {
|
|
|
|
rb_raise(rb_eTypeError, "value of %s must be String", rb_id2name(id));
|
|
|
|
}
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
|
|
|
Init_String()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_cString = rb_define_class("String", rb_cObject);
|
|
|
|
rb_include_module(rb_cString, rb_mComparable);
|
|
|
|
rb_include_module(rb_cString, rb_mEnumerable);
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_define_alloc_func(rb_cString, str_alloc);
|
2001-09-19 02:54:11 -04:00
|
|
|
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
|
2003-05-19 01:41:08 -04:00
|
|
|
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cString, "==", rb_str_equal, 1);
|
2001-07-24 05:07:33 -04:00
|
|
|
rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
|
2001-07-24 05:07:33 -04:00
|
|
|
rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cString, "+", rb_str_plus, 1);
|
|
|
|
rb_define_method(rb_cString, "*", rb_str_times, 1);
|
|
|
|
rb_define_method(rb_cString, "%", rb_str_format, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cString, "[]", rb_str_aref_m, -1);
|
|
|
|
rb_define_method(rb_cString, "[]=", rb_str_aset_m, -1);
|
2001-05-02 00:22:21 -04:00
|
|
|
rb_define_method(rb_cString, "insert", rb_str_insert, 2);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cString, "length", rb_str_length, 0);
|
1999-12-07 04:25:55 -05:00
|
|
|
rb_define_method(rb_cString, "size", rb_str_length, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cString, "empty?", rb_str_empty, 0);
|
|
|
|
rb_define_method(rb_cString, "=~", rb_str_match, 1);
|
|
|
|
rb_define_method(rb_cString, "~", rb_str_match2, 0);
|
2001-12-10 02:18:16 -05:00
|
|
|
rb_define_method(rb_cString, "match", rb_str_match_m, 1);
|
1999-01-19 23:59:39 -05: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);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cString, "upto", rb_str_upto_m, 1);
|
|
|
|
rb_define_method(rb_cString, "index", rb_str_index_m, -1);
|
2002-07-26 02:12:39 -04:00
|
|
|
rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_define_method(rb_cString, "replace", rb_str_replace, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-01-11 04:18:54 -05:00
|
|
|
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
|
1999-01-19 23:59:39 -05: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-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cString, "split", rb_str_split_m, -1);
|
1999-01-19 23:59:39 -05: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);
|
|
|
|
rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
|
|
|
|
rb_define_method(rb_cString, "intern", rb_str_intern, 0);
|
2002-11-03 06:04:35 -05:00
|
|
|
rb_define_method(rb_cString, "to_sym", rb_str_intern, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cString, "include?", rb_str_include, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "scan", rb_str_scan, 1);
|
|
|
|
|
2003-05-02 02: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-19 23:59:39 -05: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-05 23: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-19 23:59:39 -05: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-05 23: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-19 23:59:39 -05: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 01:45:20 -04:00
|
|
|
rb_define_method(rb_cString, "delete", rb_str_delete, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cString, "squeeze", rb_str_squeeze, -1);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_method(rb_cString, "count", rb_str_count, -1);
|
1999-01-19 23:59:39 -05: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 01:45:20 -04:00
|
|
|
rb_define_method(rb_cString, "delete!", rb_str_delete_bang, -1);
|
1999-01-19 23:59:39 -05: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", rb_str_each_line, -1);
|
|
|
|
rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "sum", rb_str_sum, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("sub", rb_f_sub, -1);
|
|
|
|
rb_define_global_function("gsub", rb_f_gsub, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("sub!", rb_f_sub_bang, -1);
|
|
|
|
rb_define_global_function("gsub!", rb_f_gsub_bang, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("chop", rb_f_chop, 0);
|
|
|
|
rb_define_global_function("chop!", rb_f_chop_bang, 0);
|
|
|
|
|
|
|
|
rb_define_global_function("chomp", rb_f_chomp, -1);
|
|
|
|
rb_define_global_function("chomp!", rb_f_chomp_bang, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("split", rb_f_split, -1);
|
2000-03-08 01:25:19 -05:00
|
|
|
rb_define_global_function("scan", rb_f_scan, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_define_method(rb_cString, "slice", rb_str_aref_m, -1);
|
|
|
|
rb_define_method(rb_cString, "slice!", rb_str_slice_bang, -1);
|
|
|
|
|
2001-03-26 03:57:16 -05:00
|
|
|
id_to_s = rb_intern("to_s");
|
2000-01-04 23:41:21 -05:00
|
|
|
|
|
|
|
rb_fs = Qnil;
|
2003-07-30 03:08:56 -04:00
|
|
|
rb_define_variable("$;", &rb_fs);
|
|
|
|
rb_define_variable("$-F", &rb_fs);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|