Skip to content

Commit

Permalink
* string.c (rb_str_chomp_bang): defer rb_str_modify() to actual
Browse files Browse the repository at this point in the history
  modify point.  other methods, replace, tr, delete, squeeze,
  lstrip, and rstrip as well.

* string.c (rb_str_rstrip_bang): remove trailing '\0' at the end
  of string.

* string.c (rb_str_lstrip_bang): do not strip '\0' from the left.


git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@4184 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Jul 27, 2003
1 parent 4e44bd0 commit cd565c1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Mon Jul 28 01:35:32 2003 Yukihiro Matsumoto <matz@ruby-lang.org>

* string.c (rb_str_chomp_bang): defer rb_str_modify() to actual
modify point. other methods, replace, tr, delete, squeeze,
lstrip, and rstrip as well.

* string.c (rb_str_rstrip_bang): remove trailing '\0' at the end
of string.

* string.c (rb_str_lstrip_bang): do not strip '\0' from the left.

Sun Jul 27 21:16:30 2003 WATANABE Hirofumi <eban@ruby-lang.org>

* ext/openssl/extconf.rb: better support MinGW. add
Expand Down
2 changes: 1 addition & 1 deletion lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ def content_range

def range_length
r = self.content_range
r and r.length
r and r.end - r.begin
end

def basic_auth( account, password )
Expand Down
2 changes: 1 addition & 1 deletion st.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define ST_INCLUDED

typedef long st_data_t;
typedef unsigned long st_data_t;
#define ST_DATA_T_DEFINED

typedef struct st_table st_table;
Expand Down
27 changes: 15 additions & 12 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ rb_str_shared_replace(str, str2)
VALUE str, str2;
{
if (str == str2) return;
rb_str_modify(str);
if (!FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr);
if (NIL_P(str2)) {
RSTRING(str)->ptr = 0;
Expand Down Expand Up @@ -1162,7 +1163,6 @@ static VALUE
rb_str_succ_bang(str)
VALUE str;
{
rb_str_modify(str);
rb_str_shared_replace(str, rb_str_succ(str));

return str;
Expand Down Expand Up @@ -2246,7 +2246,6 @@ tr_trans(str, src, repl, sflag)
int i, c, modify = 0;
char *s, *send;

rb_str_modify(str);
StringValue(src);
StringValue(repl);
if (RSTRING(str)->len == 0 || !RSTRING(str)->ptr) return Qnil;
Expand Down Expand Up @@ -2292,6 +2291,7 @@ tr_trans(str, src, repl, sflag)
}
}

rb_str_modify(str);
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
if (sflag) {
char *t = s;
Expand Down Expand Up @@ -2395,7 +2395,6 @@ rb_str_delete_bang(argc, argv, str)
if (argc < 1) {
rb_raise(rb_eArgError, "wrong number of arguments");
}
rb_str_modify(str);
for (i=0; i<argc; i++) {
VALUE s = argv[i];

Expand All @@ -2404,6 +2403,7 @@ rb_str_delete_bang(argc, argv, str)
init = 0;
}

rb_str_modify(str);
s = t = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
send = s + RSTRING(str)->len;
Expand Down Expand Up @@ -2444,7 +2444,6 @@ rb_str_squeeze_bang(argc, argv, str)
int init = 1;
int i;

rb_str_modify(str);
if (argc == 0) {
for (i=0; i<256; i++) {
squeez[i] = 1;
Expand All @@ -2460,6 +2459,7 @@ rb_str_squeeze_bang(argc, argv, str)
}
}

rb_str_modify(str);
s = t = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
send = s + RSTRING(str)->len;
Expand Down Expand Up @@ -2828,15 +2828,16 @@ rb_str_chomp_bang(argc, argv, str)
rs = rb_rs;
if (rs == rb_default_rs) {
smart_chomp:
rb_str_modify(str);
if (RSTRING(str)->ptr[len-1] == '\n') {
rb_str_modify(str);
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') {
rb_str_modify(str);
RSTRING(str)->len--;
}
else {
Expand All @@ -2850,7 +2851,6 @@ rb_str_chomp_bang(argc, argv, str)
if (len == 0) return Qnil;

StringValue(rs);
rb_str_modify(str);
rslen = RSTRING(rs)->len;
if (rslen == 0) {
while (len>0 && p[len-1] == '\n') {
Expand Down Expand Up @@ -2921,15 +2921,15 @@ rb_str_lstrip_bang(str)
{
char *s, *t, *e;

rb_str_modify(str);
s = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
e = t = s + RSTRING(str)->len;
/* remove spaces at head */
while (s < t && (ISSPACE(*s) || !*s)) s++;
while (s < t && ISSPACE(*s)) s++;

RSTRING(str)->len = t-s;
if (s > RSTRING(str)->ptr) {
rb_str_modify(str);
RSTRING(str)->len = t-s;
memmove(RSTRING(str)->ptr, s, RSTRING(str)->len);
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
return str;
Expand All @@ -2952,16 +2952,19 @@ rb_str_rstrip_bang(str)
{
char *s, *t, *e;

rb_str_modify(str);
s = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
e = t = s + RSTRING(str)->len;

/* remove trailing '\0's */
while (s < t && t[-1] == '\0') t--;

/* remove trailing spaces */
while (s < t && (ISSPACE(*(t-1)) || !*(t-1))) t--;
while (s < t && ISSPACE(*(t-1))) t--;

RSTRING(str)->len = t-s;
if (t < e) {
rb_str_modify(str);
RSTRING(str)->len = t-s;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
return str;
}
Expand Down

0 comments on commit cd565c1

Please sign in to comment.