-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
node_set.cr
59 lines (45 loc) · 852 Bytes
/
node_set.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct XML::NodeSet
include Enumerable(Node)
def initialize(@doc : Node, @set : LibXML::NodeSet*)
end
def self.new(doc : Node)
new doc, LibXML.xmlXPathNodeSetCreate(nil)
end
def [](index : Int)
index += size if index < 0
unless 0 <= index < size
raise IndexError.new
end
internal_at(index)
end
def each : Nil
size.times do |i|
yield internal_at(i)
end
end
def empty?
size == 0
end
# See `Object#hash(hasher)`
def_hash object_id
def inspect(io : IO) : Nil
io << '['
join ", ", io, &.inspect(io)
io << ']'
end
def size
@set.value.node_nr
end
def object_id
@set.address
end
def to_s(io : IO) : Nil
join '\n', io
end
def to_unsafe
@set
end
private def internal_at(index)
Node.new(@set.value.node_tab[index])
end
end