[#26266] pragma on ripper — nobuyoshi nakada <nobuyoshi.nakada@...>

なかだです。

15 messages 2005/06/02

[#26312] rb_gc_mark_threads spin — Tanaka Akira <akr@...17n.org>

最近、とあるプログラム(五月雨)が、無限ループに陥ることが何回かありました。

32 messages 2005/06/09
[#26323] Re: rb_gc_mark_threads spin — Tanaka Akira <akr@...17n.org> 2005/06/10

In article <TYOMLEM04Rqf69aZbLA0000002d@tyomlvem02.e2k.ad.ge.com>,

[#26329] Re: rb_gc_mark_threads spin — nobu@... 2005/06/10

なかだです。

[#26331] Re: rb_gc_mark_threads spin — Tanaka Akira <akr@...17n.org> 2005/06/11

In article <200506101543.j5AFhToG009328@sharui.nakada.niregi.kanuma.tochigi.jp>,

[#26333] Re: rb_gc_mark_threads spin — Tanaka Akira <akr@...17n.org> 2005/06/11

In article <8764wlil9l.fsf@m17n.org>,

[#26334] Re: rb_gc_mark_threads spin — nobu@... 2005/06/11

なかだです。

[#26337] Re: rb_gc_mark_threads spin — Tanaka Akira <akr@...17n.org> 2005/06/11

In article <200506111335.j5BDZkoG019423@sharui.nakada.niregi.kanuma.tochigi.jp>,

[#26405] WEBrick DoS vulnerability — Tanaka Akira <akr@...17n.org>

NetBSD 2.0 で WEBrick を使って HTTP サーバを動かした場合、クライアント

24 messages 2005/06/29
[#26477] Re: WEBrick DoS vulnerability — GOTOU Yuuzou <gotoyuzo@...> 2005/07/08

ごとうゆうぞうです。

[#26480] Re: WEBrick DoS vulnerability — Tanaka Akira <akr@...17n.org> 2005/07/08

In article <20050708.175802.957830318.gotoyuzo@sawara.does.notwork.org>,

[#26481] Re: WEBrick DoS vulnerability — GOTOU Yuuzou <gotoyuzo@...> 2005/07/08

In message <87fyupzgcq.fsf@m17n.org>,

[#26421] Subversion — Shugo Maeda <shugo@...>

前田です。

24 messages 2005/06/30
[#26422] Re: Subversion — Yukihiro Matsumoto <matz@...> 2005/06/30

まつもと ゆきひろです

[#26423] Re: Subversion — "U.Nakamura" <usa@...> 2005/06/30

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

[ruby-dev:26279] match index

From: SASADA Koichi <ko1@...>
Date: 2005-06-05 00:46:05 UTC
List: ruby-dev #26279
 ささだです。

 正規表現で複数の括弧があったとき、「最後に n 番目($n)の括弧にマッ
チした」という情報を得ることが出来ないでしょうか。

 つまり、/(A)|(B)|(C)/ =~ 'B' とあったとき、2 という情報が欲しい、と
いう意味です。

 Ruby での文字列のトークン化はよくやる話だと思うのですが、これを正規
表現とあわせて効率よくやる方法を考えていたんですが、素直に書くとパター
ンとアクション(もしくはそのトークンの種別を表すシンボル)の組を作って
ループで回して、みたいな処理になるかと思うんですが、これが楽にならない
かな、と。

 case/when でヒットしたらアクションをその場で記述、という場合には今の
VMではあんまり変わらないと思うんですが。

case 'B'
when /A/
  ...
when /B/
  ...
when /C/
  ...
end

 正規表現などは全然よく知らないんですが、こういうのは駄目でしょうか。



ベンチマーク結果:
      user     system      total        real
  0.969000   0.000000   0.969000 (  0.969000) # /A/, /B/, /C/
  0.265000   0.000000   0.265000 (  0.265000) # /(A)|(B)|(C)/
  0.157000   0.000000   0.157000 (  0.156000) # /A|B|C/


ベンチマークプログラム:

def test_data
  open('sample.rb').read * 10
end

# いい加減な Ruby token
$pattens = [
  #
  [/__LINE__/,  :__LINE__],
  [/__FILE__/,  :__FILE__],
  [/BEGIN/,  :BEGIN],
  [/END/,  :END],
  [/alias/,  :alias],
  [/and/,  :and],
  [/begin/,  :begin],
  [/break/,  :break],
  [/case/,  :case],
  [/class/,  :class],
  [/def/,  :def],
  [/defined?/,  :defined?],
  [/do/,  :do],
  [/else/,  :else],
  [/elsif/,  :elsif],
  [/end/,  :end],
  [/ensure/,  :ensure],
  [/false/,  :false],
  [/for/,  :for],
  [/if/,  :if],
  [/in/,  :in],
  [/module/,  :module],
  [/next/,  :next],
  [/nil/,  :nil],
  [/not/,  :not],
  [/or/,  :or],
  [/redo/,  :redo],
  [/rescue/,  :rescue],
  [/retry/,  :retry],
  [/return/,  :return],
  [/self/,  :self],
  [/super/,  :super],
  [/then/,  :then],
  [/true/,  :true],
  [/undef/,  :undef],
  [/unless/,  :unless],
  [/until/,  :until],
  [/when/,  :when],
  [/while/,  :while],
  [/yield/,  :yield],

  [/[\+\-\*\/\<\>]/, :_operator],
  [/\{/, :_openbrace],
  [/\}/, :_closebrace],
  [/\(/, :_openparen],
  [/\)/, :_closeparen],
  [/\[/, :_openbracket],
  [/\]/, :_closebracket],
  [/\|/, :_bar],
  [/\./, :_dot],
  [/\,/, :_comma],
  [/\===/, :_eqq],
  [/\==/, :_eq],
  [/\=/, :_asign],
  [/::/, :_colon2],
  [/:/, :_colon],
  [/\&/, :_ampersand],
  [/\?/, :_question],
  [/\!\=/, :_neq],
  [/\!/, :_not],

  [/\s+/, :_space],
  [/\#.*$/, :_comment],

  [/".*"/, :_quotedstring],
  [/'.*'/, :_singlequotedstring],

  [/\d+\.\d+/, :_floating],
  [/\d+/, :_integer],
  [/\$[\!\?\"]+/, :_global_id_special],
  [/\$[a-zA-z_]+/, :_global_id],
  [/\:[a-zA-z_]+/, :_symbol],
  [/\@[a-zA-z_]+/, :_attribute],
  [/[A-Z][a-zA-z_]+/, :_constant],
  [/[a-zA-z_]+/, :id],
]

$pattens = $pattens.map{|pat|
  [/\A#{pat[0]}/, pat[1]]
}
$pattens_only   = $pattens.map{|pat| pat[0]}
$pattens_total  =
  Regexp.compile($pattens.map{|pat| /(#{pat[0]})/}.join('|'))
$pattens_total2 =
  Regexp.compile($pattens.map{|pat| pat[0]}.join('|'))

$token_types   = $pattens.map{|pat| pat[1]}


def tokenize1 str
  tokens = []

  while str.size > 0
    case $pattens_only.each{|patten|
      if patten =~ str
        type = :token # patten[1]
        token = [type, $~[0]]
        tokens << token
        str = $~.post_match
        break :next
      end
    }
    when :next
    when :break
      break
    else
      raise "unknown: #{str}"
    end
  end
  tokens
end

def tokenize2 str
  patterns = $pattens_total
  tokens   = []
  while str.size > 0
    if patterns =~ str
      type = :token
      tokens << [type, $~[0]]
      str = $~.post_match
    else

    end
  end

  tokens
end

def tokenize3 str
  patterns = $pattens_total2
  tokens   = []
  while str.size > 0
    if patterns =~ str
      type = :token
      tokens << [type, $~[0]]
      str = $~.post_match
    else

    end
  end

  tokens
end

if false # ||true # test
  t1 = tokenize1(test_data)
  t2 = tokenize2(test_data)
  unless t1 == t2
    p t1, t2
  end
else

  require 'benchmark'
  include Benchmark

  bm{|x|
    x.report{
      tokenize1(test_data)
    }
    x.report{
      tokenize2(test_data)
    }
    x.report{
      tokenize3(test_data)
    }
  }
end

In This Thread

Prev Next