cursor 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # $Id: position.rb,v 1.3 2005/07/18 14:46:28 eric_mahurin Exp $
1
+ # $Id: position.rb,v 1.4 2005/08/31 14:41:16 eric_mahurin Exp $
2
2
 
3
3
  require 'cursor'
4
4
 
@@ -15,8 +15,10 @@ class Position < Cursor::Position # :nodoc:
15
15
  end
16
16
  prop(nil,@parent.prop)
17
17
  end
18
- def pos(reverse=false)
19
- if reverse.nil? ? @anchor_after : reverse
18
+ def pos(reverse=false,&code)
19
+ if code
20
+ super
21
+ elsif reverse.nil? ? @anchor_after : reverse
20
22
  @pos && (@pos.to_i-@parent.size).nonzero? || -0.0
21
23
  else
22
24
  @pos || @parent.size
data/duck.rb CHANGED
@@ -1,28 +1,43 @@
1
- # $Id: duck.rb,v 1.3 2005/07/21 15:15:38 eric_mahurin Exp $
1
+ # $Id: duck.rb,v 1.4 2005/10/14 00:22:58 eric_mahurin Exp $
2
2
 
3
3
  class Object
4
- # Return a new object that maps methods the original object to different
4
+ # Return a new object (starts with a #clone) that maps methods the original
5
+ # object to different
5
6
  # names. The arguments are a list alternating new and old method names
6
7
  # (strings or symbols). If an odd number (usually one) of arguments is given,
7
8
  # the last old method is assumed to be the original object itself -
8
9
  # hopefully a Proc or Method and it is used to map to the new name in the
9
10
  # new object.
10
11
  def duck(*new_old)
11
- obj = Object.new
12
+ obj = clone
13
+ 0.step(new_old.size-1,2) { |i|
14
+ old = new_old[i+1]
15
+ new_old[i+1] = old ? begin
16
+ self.method(old)
17
+ rescue
18
+ old # assume old is a Proc/Method
19
+ end : lambda(&self)
20
+ }
12
21
  klass = (class << obj;self;end)
13
- until new_old.empty?
14
- new,old = new_old.slice!(0,2)
15
- klass.__send__(:define_method,new,&(old ? self.method(old) : self))
16
- end
22
+ 0.step(new_old.size-1,2) { |i|
23
+ klass.__send__(:define_method,new_old[i],&new_old[i+1])
24
+ }
17
25
  obj
18
26
  end
19
- # Return a new object that maps a hash of name=>proc to the methods of the
20
- # object.
21
- def self.duck(methods)
22
- obj = self.new
27
+ # Same as #duck except it modifies and returns self rather than a #clone.
28
+ def duck!(*new_old)
29
+ obj = self
30
+ 0.step(new_old.size-1,2) { |i|
31
+ old = new_old[i+1]
32
+ new_old[i+1] = old ? begin
33
+ self.method(old)
34
+ rescue
35
+ old # assume old is a Proc/Method
36
+ end : lambda(&self)
37
+ }
23
38
  klass = (class << obj;self;end)
24
- methods.each { |name,proc|
25
- klass.__send__(:define_method,name,&proc)
39
+ 0.step(new_old.size-1,2) { |i|
40
+ klass.__send__(:define_method,new_old[i],&new_old[i+1])
26
41
  }
27
42
  obj
28
43
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: cursor
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.8"
7
- date: 2005-07-21
6
+ version: "0.9"
7
+ date: 2005-10-13
8
8
  summary: external iterator API
9
9
  require_paths:
10
10
  - "."
@@ -28,7 +28,6 @@ authors:
28
28
  - Eric Mahurin
29
29
  files:
30
30
  - cursor.rb
31
- - regexp_cursor.rb
32
31
  - weakrefset.rb
33
32
  - duck.rb
34
33
  - cursor/position.rb
@@ -1,50 +0,0 @@
1
- # $Id: regexp_cursor.rb,v 1.2 2005/07/21 15:20:48 eric_mahurin Exp $
2
-
3
- require 'strscan'
4
-
5
- class Regexp
6
- # Scan the cursor for the regexp and return what was matched or +nil+ for
7
- # a mismatch. The regexp will be implicitly anchored at the cursor location.
8
- # Unless +hold+ or a mismatch, the cursor will be advanced to a point right
9
- # after the match. +len+ should be the max length that the regexp can match or
10
- # zero to look at everything. A negative +len+ (including -0.0) will scan
11
- # the cursor in reverse.
12
- def scan_cursor(cursor,len=0,hold=false)
13
- reverse = (len.nonzero?||1.0/len)<0
14
- buffer = len.zero? ? cursor.read!(reverse,true,"") : cursor.read(len,true,"")
15
- buffer or return
16
- scanner = StringScanner.new(buffer)
17
- ret = scanner.scan(self)
18
- if ret
19
- len = scanner.pos
20
- cursor.skip(reverse ? -len : +len) if !hold
21
- end
22
- ret
23
- end
24
- # Scan the cursor until it matches the regexp and what was scanned up to and
25
- # including the match or +nil+ if it never matched.
26
- # Unless +hold+ or a mismatch, the cursor will be advanced to a point right
27
- # after the match. +len+ should be the start buffer size of what to look at
28
- # from the cursor or zero to look at everything at once. A negative +len+
29
- # (including -0.0) will scan the cursor in reverse.
30
- def scan_cursor_until(cursor,len=1,hold=false)
31
- reverse = (len.nonzero?||1.0/len)<0
32
- buffer = ""
33
- scanner = StringScanner.new(buffer)
34
- if len.zero?
35
- buffer = cursor.read!(reverse,true,buffer) or return
36
- ret = scanner.scan_until(self)
37
- else
38
- while cursor.read(len,false,buffer)
39
- ret = scanner.scan_until(self) and break(ret)
40
- len *= 2
41
- end
42
- len = buffer.length
43
- end
44
- len = (hold ? 0 : scanner.pos)-len
45
- cursor.skip(reverse ? -len : +len) if len.nonzero?
46
- ret
47
- end
48
- end
49
-
50
-