From 4a3f2a7bc5de8e0b3c1cb18a689d21a66e289f70 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 22 Oct 2008 05:55:22 +0000 Subject: * string.c (rb_external_str_new_with_enc): no implicit strlen call. [ruby-dev:36854] * string.c (rb_external_str_new_cstr): new function to create string from external NUL terminated C string. * string.c (rb_locale_str_new_cstr): ditto. * ext/readline/readline.c: now use rb_locale_str_new_cstr(). * test/sdbm/test_sdbm.rb (TestSDBM#test_delete_with_block): deleted key to the block may be a copy of specified key. * test/dbm/test_dbm.rb (TestDBM#test_delete_with_block): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 17 +++++++++++++++++ ext/readline/readline.c | 32 ++++++++++++++++---------------- include/ruby/intern.h | 2 ++ string.c | 13 ++++++++++++- test/dbm/test_dbm.rb | 7 ++----- test/sdbm/test_sdbm.rb | 7 ++----- 6 files changed, 51 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index b5e669d285..e75cb5549f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Wed Oct 22 14:52:17 2008 Yukihiro Matsumoto + + * string.c (rb_external_str_new_with_enc): no implicit strlen call. + [ruby-dev:36854] + + * string.c (rb_external_str_new_cstr): new function to create + string from external NUL terminated C string. + + * string.c (rb_locale_str_new_cstr): ditto. + + * ext/readline/readline.c: now use rb_locale_str_new_cstr(). + + * test/sdbm/test_sdbm.rb (TestSDBM#test_delete_with_block): + deleted key to the block may be a copy of specified key. + + * test/dbm/test_dbm.rb (TestDBM#test_delete_with_block): ditto. + Wed Oct 22 13:16:47 2008 Yukihiro Matsumoto * re.c (unescape_escaped_nonascii): back out the last change on diff --git a/ext/readline/readline.c b/ext/readline/readline.c index 6cc720f504..5e30aa3046 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -246,7 +246,7 @@ readline_readline(int argc, VALUE *argv, VALUE self) add_history(buff); } if (buff) { - result = rb_locale_str_new(buff, strlen(buff)); + result = rb_locale_str_new_cstr(buff); } else result = Qnil; @@ -385,7 +385,7 @@ readline_attempted_completion_function(const char *text, int start, int end) rl_attempted_completion_over = 1; #endif case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold)); - ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new(text, strlen(text))); + ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new_cstr(text)); if (TYPE(ary) != T_ARRAY) ary = rb_Array(ary); matches = RARRAY_LEN(ary); @@ -608,7 +608,7 @@ readline_s_get_completion_append_character(VALUE self) if (rl_completion_append_character == '\0') return Qnil; - str = rb_str_new("", 1); + str = rb_str_new(0, 1); RSTRING_PTR(str)[0] = rl_completion_append_character; return str; #else @@ -673,7 +673,7 @@ readline_s_get_basic_word_break_characters(VALUE self, VALUE str) rb_secure(4); if (rl_basic_word_break_characters == NULL) return Qnil; - return rb_locale_str_new(rl_basic_word_break_characters, 0); + return rb_locale_str_new_cstr(rl_basic_word_break_characters); #else rb_notimplement(); return Qnil; /* not reached */ @@ -736,7 +736,7 @@ readline_s_get_completer_word_break_characters(VALUE self, VALUE str) rb_secure(4); if (rl_completer_word_break_characters == NULL) return Qnil; - return rb_locale_str_new(rl_completer_word_break_characters, 0); + return rb_locale_str_new_cstr(rl_completer_word_break_characters); #else rb_notimplement(); return Qnil; /* not reached */ @@ -797,7 +797,7 @@ readline_s_get_basic_quote_characters(VALUE self, VALUE str) rb_secure(4); if (rl_basic_quote_characters == NULL) return Qnil; - return rb_locale_str_new(rl_basic_quote_characters, 0); + return rb_locale_str_new_cstr(rl_basic_quote_characters); #else rb_notimplement(); return Qnil; /* not reached */ @@ -861,7 +861,7 @@ readline_s_get_completer_quote_characters(VALUE self, VALUE str) rb_secure(4); if (rl_completer_quote_characters == NULL) return Qnil; - return rb_locale_str_new(rl_completer_quote_characters, 0); + return rb_locale_str_new_cstr(rl_completer_quote_characters); #else rb_notimplement(); return Qnil; /* not reached */ @@ -923,7 +923,7 @@ readline_s_get_filename_quote_characters(VALUE self, VALUE str) rb_secure(4); if (rl_filename_quote_characters == NULL) return Qnil; - return rb_locale_str_new(rl_filename_quote_characters, 0); + return rb_locale_str_new_cstr(rl_filename_quote_characters); #else rb_notimplement(); return Qnil; /* not reached */ @@ -933,7 +933,7 @@ readline_s_get_filename_quote_characters(VALUE self, VALUE str) static VALUE hist_to_s(VALUE self) { - return rb_str_new2("HISTORY"); + return rb_str_new_cstr("HISTORY"); } static int @@ -965,7 +965,7 @@ hist_get(VALUE self, VALUE index) if (entry == NULL) { rb_raise(rb_eIndexError, "invalid index"); } - return rb_locale_str_new(entry->line, 0); + return rb_locale_str_new_cstr(entry->line); } static VALUE @@ -1027,7 +1027,7 @@ rb_remove_history(int index) rb_secure(4); entry = remove_history(index); if (entry) { - val = rb_locale_str_new(entry->line, 0); + val = rb_locale_str_new_cstr(entry->line); free((void *) entry->line); free(entry); return val; @@ -1074,7 +1074,7 @@ hist_each(VALUE self) entry = history_get(history_get_offset_func(i)); if (entry == NULL) break; - rb_yield(rb_locale_str_new(entry->line, 0)); + rb_yield(rb_locale_str_new_cstr(entry->line)); } return self; } @@ -1133,7 +1133,7 @@ filename_completion_proc_call(VALUE self, VALUE str) if (matches) { result = rb_ary_new(); for (i = 0; matches[i]; i++) { - rb_ary_push(result, rb_locale_str_new(matches[i], 0)); + rb_ary_push(result, rb_locale_str_new_cstr(matches[i])); free(matches[i]); } free(matches); @@ -1158,7 +1158,7 @@ username_completion_proc_call(VALUE self, VALUE str) if (matches) { result = rb_ary_new(); for (i = 0; matches[i]; i++) { - rb_ary_push(result, rb_locale_str_new(matches[i], 0)); + rb_ary_push(result, rb_locale_str_new_cstr(matches[i])); free(matches[i]); } free(matches); @@ -1275,7 +1275,7 @@ Init_readline() rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp); history_get_offset_func = history_get_offset_history_base; #if defined HAVE_RL_LIBRARY_VERSION - version = rb_str_new2(rl_library_version); + version = rb_str_new_cstr(rl_library_version); #if defined HAVE_CLEAR_HISTORY || defined HAVE_REMOVE_HISTORY if (strncmp(rl_library_version, EDIT_LINE_LIBRARY_VERSION, strlen(EDIT_LINE_LIBRARY_VERSION)) == 0) { @@ -1295,7 +1295,7 @@ Init_readline() } #endif #else - version = rb_str_new2("2.0 or prior version"); + version = rb_str_new_cstr("2.0 or prior version"); #endif /* Version string of GNU Readline or libedit. */ rb_define_const(mReadline, "VERSION", version); diff --git a/include/ruby/intern.h b/include/ruby/intern.h index ce6221cc23..ada1ec0944 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -548,7 +548,9 @@ VALUE rb_tainted_str_new_cstr(const char*); VALUE rb_tainted_str_new(const char*, long); VALUE rb_tainted_str_new2(const char*); VALUE rb_external_str_new(const char*, long); +VALUE rb_external_str_new_cstr(const char*); VALUE rb_locale_str_new(const char*, long); +VALUE rb_locale_str_new_cstr(const char*); VALUE rb_str_buf_new(long); VALUE rb_str_buf_new_cstr(const char*); VALUE rb_str_buf_new2(const char*); diff --git a/string.c b/string.c index b637d38fb4..16df6021d7 100644 --- a/string.c +++ b/string.c @@ -529,7 +529,6 @@ rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *eenc) { VALUE str; - if (len == 0 && ptr) len = strlen(ptr); str = rb_tainted_str_new(ptr, len); rb_enc_associate(str, eenc); return rb_str_conv_enc(str, eenc, rb_default_internal_encoding()); @@ -541,12 +540,24 @@ rb_external_str_new(const char *ptr, long len) return rb_external_str_new_with_enc(ptr, len, rb_default_external_encoding()); } +VALUE +rb_external_str_new_cstr(const char *ptr) +{ + return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_default_external_encoding()); +} + VALUE rb_locale_str_new(const char *ptr, long len) { return rb_external_str_new_with_enc(ptr, len, rb_locale_encoding()); } +VALUE +rb_locale_str_new_cstr(const char *ptr) +{ + return rb_external_str_new_with_enc(ptr, strlen(ptr), rb_locale_encoding()); +} + VALUE rb_str_export(VALUE str) { diff --git a/test/dbm/test_dbm.rb b/test/dbm/test_dbm.rb index dc02d30c76..546e969e58 100644 --- a/test/dbm/test_dbm.rb +++ b/test/dbm/test_dbm.rb @@ -334,14 +334,11 @@ if defined? DBM def test_delete_with_block key = 'no called block' @dbm[key] = 'foo' - assert_equal('foo', @dbm.delete(key) {|k| k.replace 'called block'}) - assert_equal('no called block', key) + assert_equal('foo', @dbm.delete(key) {|k| k.replace 'called block'; :blockval}) assert_equal(0, @dbm.size) key = 'no called block' - assert_equal(:blockval, - @dbm.delete(key) {|k| k.replace 'called block'; :blockval}) - assert_equal('called block', key) + assert_equal(:blockval, @dbm.delete(key) {|k| k.replace 'called block'; :blockval}) assert_equal(0, @dbm.size) end diff --git a/test/sdbm/test_sdbm.rb b/test/sdbm/test_sdbm.rb index 0c6df66fb2..b7e8498300 100644 --- a/test/sdbm/test_sdbm.rb +++ b/test/sdbm/test_sdbm.rb @@ -365,14 +365,11 @@ class TestSDBM < Test::Unit::TestCase def test_delete_with_block key = 'no called block' @sdbm[key] = 'foo' - assert_equal('foo', @sdbm.delete(key) {|k| k.replace 'called block'}) - assert_equal('no called block', key) + assert_equal('foo', @sdbm.delete(key) {|k| k.replace 'called block'; :blockval}) assert_equal(0, @sdbm.size) key = 'no called block' - assert_equal(:blockval, - @sdbm.delete(key) {|k| k.replace 'called block'; :blockval}) - assert_equal('called block', key) + assert_equal(:blockval, @sdbm.delete(key) {|k| k.replace 'called block'; :blockval}) assert_equal(0, @sdbm.size) end -- cgit v1.2.3