[前][次][番号順一覧][スレッド一覧]

ruby-changes:44747

From: nobu <ko1@a...>
Date: Thu, 17 Nov 2016 20:52:15 +0900 (JST)
Subject: [ruby-changes:44747] nobu:r56820 (trunk): readline.c: str_subpos

nobu	2016-11-17 20:52:11 +0900 (Thu, 17 Nov 2016)

  New Revision: 56820

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56820

  Log:
    readline.c: str_subpos
    
    * ext/readline/readline.c (str_subpos): make string instance only
      if necessary.

  Modified files:
    trunk/ext/readline/readline.c
Index: ext/readline/readline.c
===================================================================
--- ext/readline/readline.c	(revision 56819)
+++ ext/readline/readline.c	(revision 56820)
@@ -686,6 +686,16 @@ readline_s_insert_text(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L686
 #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 * https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L713
 {
     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) https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L2091
     rb_gc_register_address(&readline_instream);
     rb_gc_register_address(&readline_outstream);
 }
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * end:
+ */

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]