[#32945] Shift_JIS variants and UTF-16 support — "U.Nakamura" <usa@...>

こんにちは、なかむら(う)です。

22 messages 2008/01/07
[#32953] Re: Shift_JIS variants and UTF-16 support — Martin Duerst <duerst@...> 2008/01/07

中村さん、こんにちは。

[#32955] Re: Shift_JIS variants and UTF-16 support — Yukihiro Matsumoto <matz@...> 2008/01/07

まつもと ゆきひろです

[#32959] Re: Shift_JIS variants and UTF-16 support — "NARUSE, Yui" <naruse@...> 2008/01/07

成瀬です。

[#32960] Re: Shift_JIS variants and UTF-16 support — Yukihiro Matsumoto <matz@...> 2008/01/07

まつもと ゆきひろです

[#32992] ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — "NARUSE, Yui" <naruse@...>

成瀬です。

18 messages 2008/01/08
[#32994] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — Yukihiro Matsumoto <matz@...> 2008/01/09

まつもと ゆきひろです

[#32995] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — Martin Duerst <duerst@...> 2008/01/09

At 18:13 08/01/09, Yukihiro Matsumoto wrote:

[#33011] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — "NARUSE, Yui" <naruse@...> 2008/01/11

成瀬です。

[#33012] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — Yukihiro Matsumoto <matz@...> 2008/01/11

まつもと ゆきひろです

[#33014] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — "NARUSE, Yui" <naruse@...> 2008/01/11

成瀬です。

[#33015] Re: ASCII is alias of US-ASCII; replica of dummy encoding is not a dummy — Yukihiro Matsumoto <matz@...> 2008/01/11

まつもと ゆきひろです

[#33239] Re: [ruby-cvs:22386] Ruby:r15149 (trunk): * string.c (rb_str_each_char): move forward. — Tanaka Akira <akr@...>

In article <200801210259.m0L2x3CW017171@ci.ruby-lang.org>,

11 messages 2008/01/21
[#33240] Re: [ruby-cvs:22386] Ruby:r15149 (trunk): * string.c (rb_str_each_char): move forward. — Nobuyoshi Nakada <nobu@...> 2008/01/21

なかだです。

[#33303] Time#strftimeのエンコーディング — rubikitch@...

るびきちです。

13 messages 2008/01/23
[#33305] Re: Time#strftimeのエンコーディング — Yukihiro Matsumoto <matz@...> 2008/01/23

まつもと ゆきひろです

[#33368] summary of script encoding — "U.Nakamura" <usa@...>

こんにちは、なかむら(う)です。

22 messages 2008/01/25
[#33375] Re: summary of script encoding — Yukihiro Matsumoto <matz@...> 2008/01/25

まつもと ゆきひろです

[#33376] Re: summary of script encoding — "U.Nakamura" <usa@...> 2008/01/25

こんにちは、なかむら(う)です。

[#33387] HashからStructを作る — rubikitch@...

るびきちです。

19 messages 2008/01/25
[#33455] Re: HashからStructを作る — Yukihiro Matsumoto <matz@...> 2008/01/28

まつもと ゆきひろです

[#33505] Re: HashからStructを作る — rubikitch@... 2008/01/29

From: Yukihiro Matsumoto <matz@ruby-lang.org>

[#33507] Re: HashからStructを作る — Yukihiro Matsumoto <matz@...> 2008/01/29

まつもと ゆきひろです

[#33508] Re: HashからStructを作る — rubikitch@... 2008/01/29

From: Yukihiro Matsumoto <matz@ruby-lang.org>

[#33433] Win32OLE: set encoding to OLE string — "U.Nakamura" <usa@...>

こんにちは、なかむら(う)です。

16 messages 2008/01/28

[#33461] Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — MORITA Hideyuki <h-morita@...>

=1B$B?9ED$H?=3D$7$^$9!#=1B(B

19 messages 2008/01/28
[#33473] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — Nobuyoshi Nakada <nobu@...> 2008/01/28

なかだです。

[#33503] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — MORITA Hideyuki <h-morita@...> 2008/01/29

森田です。

[#33514] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — Nobuyoshi Nakada <nobu@...> 2008/01/29

なかだです。

[#33518] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — MORITA Hideyuki <h-morita@...> 2008/01/30

森田です。

[#33545] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — Ryutaro Amano <wn9r-amn@...> 2008/01/31

天野竜太郎と申します。

[#33546] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — MORITA Hideyuki <h-morita@...> 2008/01/31

森田です。

[#33547] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — Ryutaro Amano <wn9r-amn@...> 2008/01/31

天野です。

[#33551] Re: Failed to make ruby-1.8.6-p111 on MacOSX 10.5(Leopard) — MORITA Hideyuki <h-morita@...> 2008/01/31

森田です。

[#33488] 現在の script encoding の値を得る方法は? — Hidetoshi NAGAI <nagai@...>

永井@知能.九工大です.

20 messages 2008/01/29
[#33491] Re: 現在の script encoding の値を得る方法は? — Yukihiro Matsumoto <matz@...> 2008/01/29

まつもと ゆきひろです

[#33500] Re: 現在の script encoding の値を得る方法は? — Hidetoshi NAGAI <nagai@...> 2008/01/29

永井@知能.九工大です.

[#33501] Re: 現在の script encoding の値を得る方法は? — "NARUSE, Yui" <naruse@...> 2008/01/29

成瀬です。

[#33515] Re: 現在の script encoding の値を得る方法は? — Hidetoshi NAGAI <nagai@...> 2008/01/30

永井@知能.九工大です.

[#33516] Re: 現在の script encoding の値を得る方法は? — "NARUSE, Yui" <naruse@...> 2008/01/30

成瀬です。

[#33519] Re: 現在の script encoding の値を得る方法は? — Hidetoshi NAGAI <nagai@...> 2008/01/30

永井@知能.九工大です.

[#33522] Re: 現在の script encoding の値を得る方法は? — "NARUSE, Yui" <naruse@...> 2008/01/30

成瀬です。

[ruby-dev:33443] Re: Ruby1.9 String バイト列へのインデックス アクセス

From: Nobuyoshi Nakada <nobu@...>
Date: 2008-01-28 06:18:41 UTC
List: ruby-dev #33443
なかだです。

At Sun, 20 Jan 2008 03:58:32 +0900,
Yukihiro Matsumoto wrote in [ruby-dev:33196]:
> |s = "abcde"
> |s.byte(3)       # => 100
> |s.byte(3, 101)
> |s               # => "abcee"
> 
> 難点は「s.byte(3,101)」がいまいち「代入感」がないところでしょ
> うか。文法を拡張して「s.byte(3)=101」を許すようにするかな。

これはだいぶ前から考えているんですが、かなり難しいというか面倒そ
うです。

> |あるいはBytesクラスを用意してString#byteで作成できるといいかもしれません。
> 
> この場合はBytesクラスはメモリ領域をStringと共有するのでしょ
> うか。それはそれで面白いと思いますが。
> 
> |s = "abcde"
> |s.byte          # "abcde"のバイト列オブジェクト
> |s.byte[3]       # => 100
> |s.byte[3] = 101 # 破壊的更新
> |s               # => "abcee"
> |
> |インデックスにはRangeも指定できるといいです。
> 
> その場合は右辺に文字列(またはBytes)を指定するわけ?
> 
> そういえば、自分で言い出しといてなんだけど複数形な名前のクラ
> スってのはいかがなものか。

String::ByteIndexerとか。


Index: string.c
===================================================================
--- string.c	(revision 15284)
+++ string.c	(working copy)
@@ -2405,27 +2405,33 @@ rb_str_aref_m(int argc, VALUE *argv, VAL
 
 static void
-rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
+rb_str_splice_ptr(VALUE str, long beg, long len, const char *vptr, long vlen)
 {
     rb_str_modify(str);
-    if (len < RSTRING_LEN(val)) {
+    if (len < vlen) {
 	/* expand string */
-	RESIZE_CAPA(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len + 1);
+	RESIZE_CAPA(str, RSTRING_LEN(str) + vlen - len + 1);
     }
 
-    if (RSTRING_LEN(val) != len) {
-	memmove(RSTRING_PTR(str) + beg + RSTRING_LEN(val),
+    if (vlen != len) {
+	memmove(RSTRING_PTR(str) + beg + vlen,
 		RSTRING_PTR(str) + beg + len,
 		RSTRING_LEN(str) - (beg + len));
     }
-    if (RSTRING_LEN(val) < beg && len < 0) {
+    if (vlen < beg && len < 0) {
 	MEMZERO(RSTRING_PTR(str) + RSTRING_LEN(str), char, -len);
     }
-    if (RSTRING_LEN(val) > 0) {
-	memmove(RSTRING_PTR(str)+beg, RSTRING_PTR(val), RSTRING_LEN(val));
+    if (vlen > 0) {
+	memmove(RSTRING_PTR(str)+beg, vptr, vlen);
     }
-    STR_SET_LEN(str, RSTRING_LEN(str) + RSTRING_LEN(val) - len);
+    STR_SET_LEN(str, RSTRING_LEN(str) + vlen - len);
     if (RSTRING_PTR(str)) {
 	RSTRING_PTR(str)[RSTRING_LEN(str)] = '\0';
     }
+}
+
+static void
+rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
+{
+    rb_str_splice_ptr(str, beg, len, RSTRING_PTR(val), RSTRING_LEN(val));
     OBJ_INFECT(str, val);
 }
@@ -4666,16 +4672,195 @@ rb_str_each_line(int argc, VALUE *argv, 
 
 
+static VALUE rb_str_each_byte(VALUE str);
+
+static VALUE rb_cByteIndexer;
+
+struct byte_indexer {
+    VALUE str;
+    long offset, length;
+};
+
+static void
+bidx_mark(void *ptr)
+{
+    struct byte_indexer *bp = ptr;
+    rb_gc_mark(bp->str);
+}
+
+static struct byte_indexer *
+bidx_ptr(VALUE self)
+{
+    struct byte_indexer *ptr;
+
+    Data_Get_Struct(self, struct byte_indexer, ptr);
+    if (RDATA(self)->dmark != bidx_mark) {
+	rb_raise(rb_eArgError, "ByteIndexer expected");
+    }
+    return ptr;
+}
+
+static VALUE
+rb_byte_indexer(VALUE str, long offset, long length)
+{
+    struct byte_indexer *ptr;
+    VALUE indexer = Data_Make_Struct(rb_cByteIndexer, struct byte_indexer,
+				     bidx_mark, -1, ptr);
+
+    ptr->str = str;
+    ptr->offset = offset;
+    ptr->length = length;
+    return indexer;
+}
+
+static VALUE
+bidx_aref(int argc, VALUE *argv, VALUE self)
+{
+    struct byte_indexer *ptr = bidx_ptr(self);
+    VALUE str = ptr->str;
+    long beg, len, offset = ptr->offset, length = ptr->length;
+
+    if (length < 0) {
+	length += RSTRING_LEN(str) + 1;
+    }
+    if (offset + length > RSTRING_LEN(str)) {
+	length = RSTRING_LEN(str) - offset;
+    }
+    if (argc == 2) {
+	beg = NUM2LONG(argv[0]) + offset;
+	len = NUM2LONG(argv[1]);
+      range:
+	if (len < 0) return Qnil;
+	if (!length) {
+	    len = 0;
+	}
+	if (beg < 0) {
+	    if (len > -beg) len = -beg;
+	    beg += length;
+	    if (beg < 0) return Qnil;
+	}
+	else if (beg > length) {
+	    return Qnil;
+	}
+	return rb_byte_indexer(str, beg, len);
+    }
+    else if (argc != 1) {
+	rb_raise(rb_eArgError, "wrong number arguments (%d for 1)", argc);
+    }
+    if (!FIXNUM_P(argv[0])) {
+	switch (rb_range_beg_len(argv[0], &beg, &len, length, 0)) {
+	  case Qfalse:
+	    break;
+	  case Qnil:
+	    return Qnil;
+	  default:
+	    goto range;
+	}
+    }
+    beg = FIX2INT(argv[0]);
+    if (beg >= length) return Qnil;
+    return INT2FIX(RSTRING_PTR(str)[beg + offset]);
+}
+
+static VALUE
+byte_array(VALUE ary)
+{
+    long i;
+    VALUE str = rb_str_tmp_new(RARRAY_LEN(ary));
+
+    for (i = 0; i < RARRAY_LEN(ary); ++i) {
+	VALUE n = RARRAY_PTR(ary)[i];
+	RSTRING_PTR(str)[i] = NUM2CHR(n);
+    }
+    return str;
+}
+
+static VALUE
+bidx_aset(int argc, VALUE *argv, VALUE self)
+{
+    struct byte_indexer *ptr = bidx_ptr(self);
+    VALUE str = ptr->str, val, ary;
+    long beg, len, slen;
+
+    rb_str_modify(str);
+    slen = RSTRING_LEN(str);
+    switch (argc) {
+      case 3:
+	beg = NUM2LONG(argv[0]);
+	len = NUM2LONG(argv[1]);
+	if (len < 0) rb_raise(rb_eIndexError, "negative length %ld", len);
+	if (beg < 0 ? (beg += slen) < 0 : beg > slen) {
+	    rb_raise(rb_eIndexError, "index %ld out of string", beg);
+	}
+	val = argv[2];
+	break;
+      case 2:
+	if (!rb_range_beg_len(argv[0], &beg, &len, slen, 0)) {
+	    beg = NUM2LONG(argv[0]);
+	    len = 1;
+	}
+	val = argv[1];
+	break;
+      default:
+	rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
+    }
+    if (!NIL_P(ary = rb_check_array_type(val))) {
+	rb_str_splice_0(str, beg, len, byte_array(ary));
+    }
+    else {
+	char byte = NUM2CHR(val);
+	rb_str_splice_ptr(str, beg, len, &byte, 1);
+    }
+    return argv[argc - 1];
+}
+
+static VALUE
+bidx_each(VALUE self)
+{
+    struct byte_indexer *ptr = bidx_ptr(self);
+    VALUE str = ptr->str;
+    long i, l;
+
+    if (!rb_block_given_p()) return self;
+    for (l = (i=ptr->offset) + ptr->length; i<l && i<RSTRING_LEN(str); i++) {
+	rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
+    }
+    return str;
+}
+
+static void
+Init_ByteIndexer(void)
+{
+    rb_cByteIndexer = rb_define_class_under(rb_cString, "ByteIndexer", rb_cData);
+    rb_include_module(rb_cByteIndexer, rb_mEnumerable);
+    rb_undef_alloc_func(rb_cByteIndexer);
+    rb_define_method(rb_cByteIndexer, "[]", bidx_aref, -1);
+    rb_define_method(rb_cByteIndexer, "[]=", bidx_aset, -1);
+    rb_define_method(rb_cByteIndexer, "each", bidx_each, 0);
+}
+
+
 /*
  *  Document-method: bytes
  *  call-seq:
- *     str.bytes   => anEnumerator
+ *     str.bytes   => anIndexer
  *     str.bytes {|fixnum| block }    => str
- *  
+ *
  *  Returns an enumerator that gives each byte in the string.  If a block is
  *  given, it iterates over each byte in the string.
- *     
+ *
  *     "hello".bytes.to_a        #=> [104, 101, 108, 108, 111]
+ *
+ *     str = "hello"
+ *     str.bytes[0] ^= 0x20
+ *     str			 #=> "Hello"
  */
 
+static VALUE
+rb_str_bytes(VALUE str)
+{
+    if (!rb_block_given_p()) return rb_byte_indexer(str, 0, -1);
+    return rb_str_each_byte(str);
+}
+
 /*
  *  Document-method: each_byte
@@ -6077,5 +6262,5 @@ Init_String(void)
     rb_define_method(rb_cString, "split", rb_str_split_m, -1);
     rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
-    rb_define_method(rb_cString, "bytes", rb_str_each_byte, 0);
+    rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
     rb_define_method(rb_cString, "chars", rb_str_each_char, 0);
     rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
@@ -6183,3 +6368,5 @@ Init_String(void)
 
     rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0);
+
+    Init_ByteIndexer();
 }


-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread