summaryrefslogtreecommitdiff
path: root/ext/readline
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-17 11:52:11 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-17 11:52:11 (GMT)
commitd3d9903dc2bda6c012d67083f28f66772035b6b4 (patch)
tree75e81336d7ba617ba6cd1f5835b5ceb95ee6fd04 /ext/readline
parent998d1c6c30db09fa6898e6c72a4c874a7fc6ef5c (diff)
readline.c: str_subpos
* ext/readline/readline.c (str_subpos): make string instance only if necessary. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56820 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/readline')
-rw-r--r--ext/readline/readline.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/ext/readline/readline.c b/ext/readline/readline.c
index 6ed558e..7356b7f 100644
--- a/ext/readline/readline.c
+++ b/ext/readline/readline.c
@@ -686,6 +686,16 @@ readline_s_insert_text(VALUE self, VALUE str)
#endif
#if defined(HAVE_RL_DELETE_TEXT)
+static const char *
+str_subpos(const char *ptr, const char *end, long beg, long *sublen, rb_encoding *enc)
+{
+ VALUE str = rb_enc_str_new_static(ptr, end-ptr, enc);
+ OBJ_FREEZE(str);
+ ptr = rb_str_subpos(str, beg, sublen);
+ rb_gc_force_recycle(str);
+ return ptr;
+}
+
/*
* call-seq:
* Readline.delete_text([start[, length]]) -> self
@@ -703,20 +713,20 @@ readline_s_delete_text(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 2);
if (rl_line_buffer) {
- char *p, *ptr = rl_line_buffer;
- long beg = 0, len = strlen(rl_line_buffer);
- VALUE str = rb_enc_str_new_static(ptr, len, rb_locale_encoding());
- OBJ_FREEZE(str);
+ const char *p, *ptr = rl_line_buffer;
+ long beg = 0, len = strlen(ptr);
+ const char *end = ptr + len;
+ rb_encoding *enc = rb_locale_encoding();
if (argc == 2) {
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
num_pos:
- p = rb_str_subpos(str, beg, &len);
+ p = str_subpos(ptr, end, beg, &len, enc);
if (!p) rb_raise(rb_eArgError, "invalid index");
beg = p - ptr;
}
else if (argc == 1) {
- len = rb_str_strlen(str);
+ len = rb_enc_strlen(ptr, ptr + len, enc);
if (!rb_range_beg_len(argv[0], &beg, &len, len, 1)) {
beg = NUM2LONG(argv[0]);
goto num_pos;
@@ -2081,3 +2091,9 @@ Init_readline(void)
rb_gc_register_address(&readline_instream);
rb_gc_register_address(&readline_outstream);
}
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * end:
+ */