[#37248] [Feature:1.9] Enumerator#inspect — "Yusuke ENDOH" <mame@...>

遠藤です。

12 messages 2008/12/02

[#37337] [Feature #841] Object#self — "rubikitch ." <redmine@...>

Feature #841: Object#self

13 messages 2008/12/09

[#37513] Current status of 1.9.1 RC1's issues — "Yugui (Yuki Sonoda)" <yugui@...>

Hi, folks

14 messages 2008/12/20
[#37516] Re: Current status of 1.9.1 RC1's issues — Masatoshi SEKI <m_seki@...> 2008/12/20

咳といいます。

[#37576] [BUG:trunk] encoding for stdio's — "Yugui (Yuki Sonoda)" <yugui@...>

Yuguiです。

11 messages 2008/12/24

[ruby-dev:37247] [Backport #809] String#ord and String#[gs]etbyte for 1.8

From: Shinichiro Hamaji <redmine@...>
Date: 2008-12-02 13:38:50 UTC
List: ruby-dev #37247
Backport #809: String#ord and String#[gs]etbyte for 1.8
http://redmine.ruby-lang.org/issues/show/809

起票者: Shinichiro Hamaji
ステータス: Open, 優先度: Normal

Ruby1.9 でコードを書いている時に、 1.8 でも動くようにしとくかなぁ…
と思う時に最も困るのが個人的には String#[] の挙動の変化なのですが、
1.9 の ord, getbyte, setbyte を backport しておくわけにはいけないもんでしょうか。

Index: string.c
===================================================================
--- string.c	(revision 20448)
+++ string.c	(working copy)
@@ -2473,6 +2473,51 @@
 
 /*
  *  call-seq:
+ *     str.getbyte(index)          => 0 .. 255
+ *
+ *  returns the <i>index</i>th byte as an integer.
+ */
+static VALUE
+rb_str_getbyte(VALUE str, VALUE index)
+{
+    long pos = NUM2LONG(index);
+
+    if (pos < 0)
+	pos += RSTRING_LEN(str);
+    if (pos < 0 ||  RSTRING_LEN(str) <= pos)
+	return Qnil;
+
+    return INT2FIX((unsigned char)RSTRING_PTR(str)[pos]);
+}
+
+
+/*
+ *  call-seq:
+ *     str.setbyte(index, int) => int
+ *
+ *  modifies the <i>index</i>th byte as <i>int</i>.
+ */
+static VALUE
+rb_str_setbyte(VALUE str, VALUE index, VALUE value)
+{
+    long pos = NUM2LONG(index);
+    int byte = NUM2INT(value);
+
+    rb_str_modify(str);
+
+    if (pos < -RSTRING_LEN(str) || RSTRING_LEN(str) <= pos)
+	rb_raise(rb_eIndexError, "index %ld out of string", pos);
+    if (pos < 0)
+	pos += RSTRING_LEN(str);
+
+    RSTRING_PTR(str)[pos] = byte;
+
+    return value;
+}
+
+
+/*
+ *  call-seq:
  *     str.reverse   => new_str
  *  
  *  Returns a new string with the characters from <i>str</i> in reverse order.
@@ -4556,6 +4601,24 @@
 
 /*
  *  call-seq:
+ *     str.ord   => integer
+ *  
+ *  Return the <code>Integer</code> ordinal of a one-character string.
+ *     
+ *     "a".ord         #=> 97
+ */
+
+VALUE
+rb_str_ord(VALUE s)
+{
+    if (RSTRING_LEN(s) <= 0)
+	rb_raise(rb_eArgError, "empty string");
+    return INT2FIX(*RSTRING_PTR(s) & 0xff);
+}
+
+
+/*
+ *  call-seq:
  *     str.sum(n=16)   => integer
  *  
  *  Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
@@ -4959,6 +5022,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);
@@ -4987,6 +5052,7 @@
     rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
     rb_define_method(rb_cString, "intern", rb_str_intern, 0);
     rb_define_method(rb_cString, "to_sym", rb_str_intern, 0);
+    rb_define_method(rb_cString, "ord", rb_str_ord, 0);
 
     rb_define_method(rb_cString, "include?", rb_str_include, 1);
     rb_define_method(rb_cString, "start_with?", rb_str_start_with, -1);
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 20448)
+++ test/ruby/test_string.rb	(working copy)
@@ -77,4 +77,28 @@
     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
+
+  def test_ord
+    assert_equal(0xe3, "\xE3\x81\x82\xE3\x81\x84".ord)
+    assert_raise(ArgumentError) { "".ord }
+  end
 end


----------------------------------------
http://redmine.ruby-lang.org

In This Thread

Prev Next