[#11357] [PATCH] an analogue of `long long' — "Nobuyoshi.Nakada" <nobu.nakada@...>

なかだです。

18 messages 2000/11/01
[#11358] Re: [PATCH] an analogue of `long long' — matz@... (Yukihiro Matsumoto) 2000/11/01

まつもと ゆきひろです

[#11364] Re: [PATCH] an analogue of `long long' — EGUCHI Osamu <eguchi@...> 2000/11/02

えぐち@エスアンドイー です。

[#11440] class Character (was: Ruby I18N) — Yasushi Shoji <yashi@...>

[ruby-dev:11428] からの続きですが、threadは切りました。

14 messages 2000/11/08
[#11442] Re: class Character (was: Ruby I18N) — TAKAHASHI Masayoshi <maki@...> 2000/11/08

高橋征義です。用語について。

[#11443] Re: class Character (was: Ruby I18N) — Yasushi Shoji <yashi@...> 2000/11/08

At Wed, 8 Nov 2000 20:44:55 +0900,

[#11520] A problem of Socket methods on Windows — OKA Toshiyuki <oka@...>

岡と申します。

22 messages 2000/11/15
[#11523] Re: A problem of Socket methods on Windows — "Nobuyoshi.Nakada" <nobu.nakada@...> 2000/11/15

なかだです。

[#11528] Re: A problem of Socket methods on Windows — matz@... (Yukihiro Matsumoto) 2000/11/15

まつもと ゆきひろです

[#11532] Re: A problem of Socket methods on Windows — "Nobuyoshi.Nakada" <nobu.nakada@...> 2000/11/15

なかだです。

[#11534] Re: A problem of Socket methods on Windows — OKA Toshiyuki <oka@...> 2000/11/15

岡です。

[#11535] Re: A problem of Socket methods on Windows — "Nobuyoshi.Nakada" <nobu.nakada@...> 2000/11/15

なかだです。

[#11538] Re: A problem of Socket methods on Windows — "Nobuyoshi.Nakada" <nobu.nakada@...> 2000/11/15

なかだです。

[#11662] IO (Re: fork problem?) — Tanaka Akira <akr@...17n.org>

In article <E140cR3-0002ls-00@ev.netlab.zetabits.co.jp>,

22 messages 2000/11/28
[#11663] Re: IO (Re: fork problem?) — matz@... (Yukihiro Matsumoto) 2000/11/28

まつもと ゆきひろです

[#11664] Re: IO (Re: fork problem?) — Tanaka Akira <akr@...17n.org> 2000/11/28

In article <E140fxW-0002u9-00@ev.netlab.zetabits.co.jp>,

[#11665] Re: IO (Re: fork problem?) — Tanaka Akira <akr@...17n.org> 2000/11/28

In article <hvor93w5wb8.fsf@coulee.m17n.org>,

[#11669] Re: IO (Re: fork problem?) — Tanaka Akira <akr@...17n.org> 2000/11/29

In article <hvoofz05uwz.fsf@coulee.m17n.org>,

[#11672] Re: IO (Re: fork problem?) — matz@... (Yukihiro Matsumoto) 2000/11/29

まつもと ゆきひろです

[#11675] Re: IO (Re: fork problem?) — Koji Arai <JCA02266@...> 2000/11/30

新井です。

[#11677] Re: IO (Re: fork problem?) — matz@... (Yukihiro Matsumoto) 2000/12/01

まつもと ゆきひろです

[ruby-dev:11445] Re: Ruby I18N

From: とみたまさひろ <tommy@...>
Date: 2000-11-08 15:31:55 UTC
List: ruby-dev #11445
とみたです。

[matz@zetabits.com (Yukihiro Matsumoto)さんが]
["[ruby-dev:11420] Re: Ruby I18N" で曰く]

> が、それは「String の下位クラスを作る」ことと矛盾しそうで、
> 「String の下位クラスを作る」ならば「string.cのほとんどコピー
> が各文字コード系毎に発生することになりそう」という意図だった
> のですが。

ええと、String の下位クラスを作っても、string.c のコピーは不要じゃ
ないかというのが私の意図です。

MySQL と似たようなやり方で、ちょいとサンプルモジュールを作って
みましたんで、添付します。

	require 'ctype-euc-jp'
	s = EucJpString::new("あaいbうcえdおe")
	puts s[2,3]                             # => "いbう"
	puts s.index(EucJpString::new "え")     # => 6

例えば、こういうのと同じような動きをするモジュールを C で作成したと
すると、文字コード系毎には ctype-***.c を作ればいいだけで、mbstring.c 
は一つでいいんではないかと…。

# 何か勘違いしてるかな…?

---
とみたまさひろ <tommy@tmtm.org> http://www.tmtm.org
日本MySQLユーザ会 http://www.mysql.gr.jp

Attachments (3)

mbstring.rb (1.72 KB, text/x-ruby)
class MBString < String

  C = {}

  def initialize(str, ctype=nil)
    @ctype = ctype
    super str
  end

  attr_reader :ctype
  attr_writer :ctype

  def clone()
    MBString::new(self, ctype)
  end

  def dup()
    MBString::new(self, ctype)
  end

  alias :byte_eql? :eql?

  def ==(s)
    s.ctype == ctype and super(self)
  end

  def ===(s)
    self.==(s)
  end

  def eql?(s)
    self.==(s)
  end

  def +(s)
    raise 'different ctype' if s.ctype != ctype
    MBString::new super(s), ctype
  end

  def *(s)
    MBString::new super(s), ctype
  end

  alias :byte_length :length
  alias :byte_at :[]

  def pos(n)
    i = 0
    c = 0
    while c < n and i < byte_length
      i += C[ctype].mblength(byte_at i)
      c += 1
    end
    if i < byte_length then i else nil end
  end

  def length()
    i = 0
    c = 0
    while i < byte_length
      i += C[ctype].mblength(byte_at i)
      c += 1
    end
    c
  end

  alias :size :length

  def [](*arg)
    if arg.length == 1 and arg[0].type == Fixnum then
      i = pos arg[0]
      if i then
	l = C[ctype].mblength(super i)
	r = 0
	l.times do
	  r = r * 256 + super(i)
	  i += 1
	end
	r
      else
	nil
      end
    elsif arg.length == 2 then
      i = pos arg[0]
      j = pos arg[1]
      MBString::new super(i, j), ctype
    elsif arg.length == 1 and arg[0].type == Range then
      i = pos arg[0].first
      j = pos arg[0].end
      MBString::new super(i, j), ctype
    else
      raise 'invalid argument'
    end
  end

  def index(s, p=nil)
    raise 'different ctype' if s.ctype != ctype
    i = p ? pos(p) : 0
    p ||= 0
    while i < byte_length
      if s.byte_eql? byte_at(i, s.byte_length) then return p end
      i += C[ctype].mblength(byte_at i)
      p += 1
    end
    nil
  end

end
ctype-euc-jp.rb (303 Bytes, text/x-ruby)
require 'mbstring'

module EucJpCType
  def mblength(c)
    n = c < 0x80 ? 1 : 0xa1 <= c && c <= 0xfe ? 2 : c == 0x8e ? 2 : c == 0x8f ? 3 : 1
  end
  module_function :mblength
end

MBString::C['euc-jp'] = EucJpCType

class EucJpString < MBString
  def initialize(str)
    super(str, 'euc-jp')
  end
end
ctype-shift_jis.rb (269 Bytes, text/x-ruby)
require 'mbstring'

module ShiftJisCType
  def mblength(c)
    n = 0x81 < c && c < 0x9f ? 2 : 1
  end
  module_function :mblength
end

MBString::C['shift_jis'] = ShiftJisCType

class ShiftJisString < MBString
  def initialize(str)
    super(str, 'shift_jis')
  end
end

In This Thread