Skip to content

Commit 5943278

Browse files
committed
Upgrade iteration protocol
1 parent f4cc793 commit 5943278

20 files changed

+60
-84
lines changed

src/Abstract/DynamicLink.jl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export DynamicLinks API
22
export DynamicLinks,
3-
getindex, endof, length, start, next, done, eltype,
3+
getindex, endof, length, iterate, lastindex, eltype,
44
handle, header
55

66
# Export Dynamic Link API
@@ -11,6 +11,8 @@ export DynamicLink,
1111
export RPath,
1212
handle, rpaths, canonical_rpaths, find_library
1313

14+
# Import iteration protocol
15+
import Base: iterate, length, lastindex
1416

1517
"""
1618
DynamicLinks
@@ -24,23 +26,18 @@ given below, with methods that subclasses must implement marked in emphasis:
2426
2527
### Iteration
2628
- *getindex()*
27-
- *endof()*
28-
- length()
29-
- start()
30-
- next()
31-
- done()
29+
- *lastindex()*
30+
- iterate()
3231
- eltype()
3332
3433
### Misc.
3534
- *handle()*
3635
"""
3736
abstract type DynamicLinks{H <: ObjectHandle} end
3837

39-
@mustimplement endof(dls::DynamicLinks)
40-
start(dls::DynamicLinks) = 1
41-
done(dls::DynamicLinks, idx) = idx > length(dls)
42-
next(dls::DynamicLinks, idx) = (dls[idx], idx+1)
43-
length(dls::DynamicLinks) = endof(dls)
38+
@mustimplement lastindex(dls::DynamicLinks)
39+
iterate(dls::DynamicLinks, idx=1) = idx > length(dls) ? nothing : (dls[idx], idx+1)
40+
length(dls::DynamicLinks) = lastindex(dls)
4441
eltype(::Type{D}) where {D <: DynamicLinks} = DynamicLink
4542
@mustimplement getindex(dls::DynamicLinks, idx)
4643

@@ -83,6 +80,8 @@ marked in emphasis:
8380
- *rpaths()*
8481
- canonical_rpaths()
8582
- find_library()
83+
- lastindex()
84+
- iterate()
8685
"""
8786
abstract type RPath{H <: ObjectHandle} end
8887

@@ -107,11 +106,9 @@ Return the list of paths that will be searched for shared libraries.
107106
"""
108107
@mustimplement rpaths(rpath::RPath)
109108

110-
endof(rpath::RPath) = lastindex(rpaths(rpath))
111-
start(rpath::RPath) = 1
112-
done(rpath::RPath, idx) = idx > length(rpath)
113-
next(rpath::RPath, idx) = (rpath[idx], idx+1)
114-
length(rpath::RPath) = endof(rpath)
109+
lastindex(rpath::RPath) = lastindex(rpaths(rpath))
110+
iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
111+
length(rpath::RPath) = lastindex(rpath)
115112
eltype(::Type{D}) where {D <: RPath} = String
116113
getindex(rpath::RPath, idx) = rpaths(rpath)[idx]
117114

src/Abstract/Section.jl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export Sections API
22
export Sections,
3-
getindex, endof, length, start, next, done, eltype,
3+
getindex, endof, length, lastindex, iterate, eltype,
44
find, findfirst,
55
handle, header, format_string
66

@@ -14,8 +14,8 @@ export SectionRef,
1414
read, seekstart, seek, eof, section_number
1515

1616
# Import Base methods for extension
17-
import Base: read, seek, seekstart, eof, start, length, done, next, endof,
18-
eltype, find, findfirst
17+
import Base: read, seek, seekstart, eof, length, eltype, find,
18+
findfirst, iterate, lastindex
1919

2020
"""
2121
Sections
@@ -32,11 +32,9 @@ in emphasis:
3232
3333
### Iteration
3434
- getindex()
35-
- *endof()*
35+
- *lastindex()*
3636
- length()
37-
- start()
38-
- next()
39-
- done()
37+
- iterate()
4038
- eltype()
4139
4240
### Search
@@ -49,11 +47,9 @@ in emphasis:
4947
abstract type Sections{H<:ObjectHandle} end
5048

5149
# Fairly simple iteration interface specification
52-
@mustimplement endof(sections::Sections)
53-
start(sections::Sections) = 1
54-
done(sections::Sections, idx) = idx > length(sections)
55-
next(sections::Sections, idx) = (sections[idx], idx+1)
56-
length(sections::Sections) = endof(sections)
50+
@mustimplement lastindex(sections::Sections)
51+
length(sections::Sections) = lastindex(sections)
52+
iterate(sections::Sections, idx=1) = idx > length(sections) ? nothing : (sections[idx], idx+1)
5753
eltype(::Type{S}) where {S <: Sections} = SectionRef
5854

5955
function getindex(sections::Sections, idx)
@@ -228,7 +224,6 @@ underlying `Section` API calls for ease of use.
228224
- seek()
229225
- eof()
230226
231-
232227
### Format-specific properties:
233228
- section_name()
234229
- *section_number()*

src/Abstract/Segment.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export Segment, deref, segment_name, segment_offset, segment_file_size,
88
export SegmentRef, segment_number
99

1010
# Import Base stuff for extension
11-
import Base: getindex, endof, length, start, next, done, eltype, find,
12-
findfirst
11+
import Base: getindex, length, eltype, find, findfirst, iterate, lastindex
1312

1413
"""
1514
Segments
@@ -26,11 +25,9 @@ in emphasis:
2625
2726
### Iteration
2827
- *getindex()*
29-
- *endof()*
28+
- *lastindex()*
3029
- length()
31-
- start()
32-
- next()
33-
- done()
30+
- iterate()
3431
- eltype()
3532
3633
### Search
@@ -43,11 +40,9 @@ in emphasis:
4340
abstract type Segments{H<:ObjectHandle} end
4441

4542
# Fairly simple iteration interface specification
46-
@mustimplement endof(segs::Segments)
47-
start(segs::Segments) = 1
48-
done(segs::Segments, idx) = idx > length(segs)
49-
next(segs::Segments, idx) = (segs[idx], idx+1)
50-
length(segs::Segments) = endof(segs)
43+
@mustimplement lastindex(segs::Segments)
44+
length(segs::Segments) = lastindex(segs)
45+
iterate(segs::Segments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
5146
eltype(::Type{S}) where {S <: Segments} = SegmentRef
5247

5348
function getindex(segs::Segments, idx)

src/Abstract/Symbol.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Export Symbols API
66
export Symbols,
7-
getindex, endof, length, start, next, done, eltype,
7+
getindex, endof, length, iterate, lastindex, eltype,
88
handle, header
99

1010
# Export SymtabEntry API
@@ -15,6 +15,8 @@ export SymtabEntry,
1515
export SymbolRef,
1616
symbol_number
1717

18+
# Import iteration protocol
19+
import Base: length, iterate, lastindex
1820

1921
"""
2022
Symbols
@@ -31,11 +33,9 @@ in emphasis:
3133
3234
### Iteration
3335
- getindex()
34-
- *endof()*
36+
- *lastindex()*
3537
- length()
36-
- start()
37-
- next()
38-
- done()
38+
- iterate()
3939
- eltype()
4040
4141
### Misc.
@@ -44,11 +44,9 @@ in emphasis:
4444
abstract type Symbols{H<:ObjectHandle} end
4545

4646
# Fairly simple iteration interface specification
47-
@mustimplement endof(syms::Symbols)
48-
start(syms::Symbols) = 1
49-
done(syms::Symbols, idx) = idx > length(syms)
50-
next(syms::Symbols, idx) = (syms[idx], idx+1)
51-
length(syms::Symbols) = endof(syms)
47+
@mustimplement lastindex(syms::Symbols)
48+
length(syms::Symbols) = lastindex(syms)
49+
iterate(syms::Symbols, idx=1) = idx > length(syms) ? nothing : (syms[idx], idx+1)
5250
eltype(::Type{S}) where {S <: Symbols} = SymbolRef
5351

5452
function getindex(syms::Symbols{H}, idx) where {H <: ObjectHandle}

src/COFF/COFF.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, endof, length, start, next, done, eltype, handle, header, path,
9+
getindex, endof, length, lastindex, iterate, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,
1313
format_string, section_header_offset, section_header_size, section_header_type,
14-
segment_header_offset, segment_header_size, segment_header_type,
14+
segment_header_offset, segment_header_size, segment_header_type, start,
1515
symtab_entry_offset, symtab_entry_size, symtab_entry_type, find_libraries,
1616
find, findfirst, deref, contents, section_name, section_size,
1717
section_offset, section_address, section_number, segment_name, segment_offset,

src/COFF/COFFDynamicLink.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949
DynamicLinks(oh::COFFHandle) = COFFDynamicLinks(oh)
5050

5151
handle(dls::COFFDynamicLinks) = dls.handle
52-
endof(dls::COFFDynamicLinks) = lastindex(dls.links)
52+
lastindex(dls::COFFDynamicLinks) = lastindex(dls.links)
5353
getindex(dls::COFFDynamicLinks, idx) = getindex(dls.links, idx)
5454

5555

src/COFF/COFFSection.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Sections(oh::COFFHandle) = COFFSections(oh)
1212

1313
# Implement Sections API
1414
handle(sections::COFFSections) = sections.handle
15-
endof(sections::COFFSections) = num_sections(header(handle(sections)))
15+
lastindex(sections::COFFSections) = num_sections(header(handle(sections)))
1616

1717

1818
"""

src/COFF/COFFSymbol.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ handle(syms::COFFSymbols) = syms.handle
5454
# next_idx = idx + deref(syms[idx]).NumberOfAuxSymbols + 1
5555
# return (syms[idx], next_idx)
5656
# end
57-
endof(syms::COFFSymbols) = num_symbols(header(handle(syms)))
57+
lastindex(syms::COFFSymbols) = num_symbols(header(handle(syms)))
5858

5959
# We override `iteratorsize` so that list comprehensions and collect() calls on
6060
# our Symbols don't have a bunch of #undef entries at the end of the array

src/ELF/ELF.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, endof, length, start, next, done, eltype, handle, header, path,
9+
getindex, endof, length, iterate, lastindex, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,
1313
format_string, section_header_offset, section_header_size, section_header_type,
14-
segment_header_offset, segment_header_size, segment_header_type,
14+
segment_header_offset, segment_header_size, segment_header_type, start,
1515
symtab_entry_offset, symtab_entry_size, symtab_entry_type, find_libraries,
1616
find, findfirst, deref, contents, section_name, section_size,
1717
section_offset, section_address, section_number, segment_name, segment_offset,

src/ELF/ELFDynamicLink.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function DynamicLinks(oh::ELFHandle)
4545
end
4646
Section(dls::ELFDynamicLinks) = dls.section_ref
4747
handle(dls::ELFDynamicLinks) = handle(Section(dls))
48-
endof(dls::ELFDynamicLinks) = lastindex(dls.links)
48+
lastindex(dls::ELFDynamicLinks) = lastindex(dls.links)
4949
getindex(dls::ELFDynamicLinks, idx) = getindex(dls.links, idx)
5050

5151
"""

0 commit comments

Comments
 (0)