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

ruby-changes:8925

From: knu <ko1@a...>
Date: Wed, 3 Dec 2008 16:23:28 +0900 (JST)
Subject: [ruby-changes:8925] Ruby:r20461 (ruby_1_8): * string.c (rb_str_getbyte, rb_str_setbyte): Add String#getbyte

knu	2008-12-03 16:23:09 +0900 (Wed, 03 Dec 2008)

  New Revision: 20461

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20461

  Log:
    * string.c (rb_str_getbyte, rb_str_setbyte): Add String#getbyte
      and String#setbyte for the forward compatibility with 1.9, where
      the behavior of String#[] and String#[]= have changed; based on
      a patch from Shinichiro Hamaji in  [ruby-dev:37247].

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/string.c
    branches/ruby_1_8/test/ruby/test_string.rb

Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 20460)
+++ ruby_1_8/NEWS	(revision 20461)
@@ -43,6 +43,12 @@
     New alias to #include? for the forward compatibility with 1.9, in
     which the behavior of Range#include? has changed.
 
+  * String#getbyte
+  * String#setbyte
+
+    New methods for the forward compatibility with 1.9, in which the
+    behavior of String#[] and String#[]= have changed.
+
 * dbm
 
   DBM#key
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 20460)
+++ ruby_1_8/ChangeLog	(revision 20461)
@@ -1,3 +1,10 @@
+Wed Dec  3 16:17:36 2008  Akinori MUSHA  <knu@i...>
+
+	* string.c (rb_str_getbyte, rb_str_setbyte): Add String#getbyte
+	  and String#setbyte for the forward compatibility with 1.9, where
+	  the behavior of String#[] and String#[]= have changed; based on
+	  a patch from Shinichiro Hamaji in  [ruby-dev:37247].
+
 Wed Dec  3 09:26:29 2008  Yukihiro Matsumoto  <matz@r...>
 
 	* lib/rexml/xpath.rb (REXML::XPath.first): apply documentation
Index: ruby_1_8/string.c
===================================================================
--- ruby_1_8/string.c	(revision 20460)
+++ ruby_1_8/string.c	(revision 20461)
@@ -2473,6 +2473,55 @@
 
 /*
  *  call-seq:
+ *     str.getbyte(index)          => 0 .. 255
+ *
+ *  returns the <i>index</i>th byte as an integer.
+ */
+static VALUE
+rb_str_getbyte(str, index)
+    VALUE str, index;
+{
+    long pos = NUM2LONG(index);
+    long len = RSTRING(str)->len;
+
+    if (pos < -len || len <= pos)
+	return Qnil;
+    if (pos < 0)
+	pos += len;
+
+    return INT2FIX((unsigned char)RSTRING(str)->ptr[pos]);
+}
+
+
+/*
+ *  call-seq:
+ *     str.setbyte(index, int) => int
+ *
+ *  modifies the <i>index</i>th byte as <i>int</i>.
+ */
+static VALUE
+rb_str_setbyte(str, index, value)
+    VALUE str, index, value;
+{
+    long pos = NUM2LONG(index);
+    long len = RSTRING(str)->len;
+    int byte = NUM2INT(value);
+
+    rb_str_modify(str);
+
+    if (pos < -len || len <= pos)
+	rb_raise(rb_eIndexError, "index %ld out of string", pos);
+    if (pos < 0)
+	pos += len;
+
+    RSTRING(str)->ptr[pos] = byte;
+
+    return value;
+}
+
+
+/*
+ *  call-seq:
  *     str.reverse   => new_str
  *  
  *  Returns a new string with the characters from <i>str</i> in reverse order.
@@ -4959,6 +5008,8 @@
     rb_define_method(rb_cString, "index", rb_str_index_m, -1);
     rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
     rb_define_method(rb_cString, "replace", rb_str_replace, 1);
+    rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1);
+    rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2);
 
     rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
     rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
Index: ruby_1_8/test/ruby/test_string.rb
===================================================================
--- ruby_1_8/test/ruby/test_string.rb	(revision 20460)
+++ ruby_1_8/test/ruby/test_string.rb	(revision 20461)
@@ -77,4 +77,23 @@
     assert_equal("aaaaaaaaaaaa", "zzzzzzzzzzz".succ!)
     assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzz".succ!)
   end
+
+  def test_getbyte
+    assert_equal(0x82, "\xE3\x81\x82\xE3\x81\x84".getbyte(2))
+    assert_equal(0x82, "\xE3\x81\x82\xE3\x81\x84".getbyte(-4))
+    assert_nil("\xE3\x81\x82\xE3\x81\x84".getbyte(100))
+  end
+
+  def test_setbyte
+    s = "\xE3\x81\x82\xE3\x81\x84"
+    s.setbyte(2, 0x84)
+    assert_equal("\xE3\x81\x84\xE3\x81\x84", s)
+
+    s = "\xE3\x81\x82\xE3\x81\x84"
+    assert_raise(IndexError) { s.setbyte(100, 0) }
+
+    s = "\xE3\x81\x82\xE3\x81\x84"
+    s.setbyte(-4, 0x84)
+    assert_equal("\xE3\x81\x84\xE3\x81\x84", s)
+  end
 end

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

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