1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
matz 2008-10-22 05:55:22 +00:00
parent 1d38a821ea
commit 4a3f2a7bc5
6 changed files with 51 additions and 27 deletions

View file

@ -1,3 +1,20 @@
Wed Oct 22 14:52:17 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* 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 <matz@ruby-lang.org>
* re.c (unescape_escaped_nonascii): back out the last change on

View file

@ -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);

View file

@ -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*);

View file

@ -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)
{

View file

@ -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

View file

@ -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