Skip to content

Commit d12fac6

Browse files
committed
Add search methods for Symbols.
1 parent 7b8fce7 commit d12fac6

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/Abstract/Symbol.jl

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# Export Symbols API
66
export Symbols,
77
getindex, length, iterate, lastindex, eltype,
8+
findall, findfirst,
89
handle, header
9-
10+
1011
# Export SymtabEntry API
1112
export SymtabEntry,
1213
deref, symbol_name, symbol_value, isundef, isglobal, islocal, isweak
@@ -15,8 +16,8 @@ export SymtabEntry,
1516
export SymbolRef,
1617
symbol_number
1718

18-
# Import iteration protocol
19-
import Base: length, iterate, lastindex
19+
# Import Base methods for extension
20+
import Base: length, iterate, lastindex, findall, findfirst
2021

2122
"""
2223
Symbols
@@ -38,6 +39,10 @@ in emphasis:
3839
- iterate()
3940
- eltype()
4041
42+
### Search
43+
- findall()
44+
- findfirst()
45+
4146
### Misc.
4247
- *handle()*
4348
"""
@@ -62,6 +67,46 @@ function getindex(syms::Symbols{H}, idx) where {H <: ObjectHandle}
6267
)
6368
end
6469

70+
"""
71+
findall(symbols::Symbols, name::String)
72+
73+
Return a list of symbols that match the given `name`.
74+
"""
75+
function findall(symbols::Symbols, name::AbstractString)
76+
return findall(symbols, [name])
77+
end
78+
79+
"""
80+
findall(symbols::Symbols, name::String)
81+
82+
Return a list of symbols that match one of the given `names`.
83+
"""
84+
function findall(symbols::Symbols, names::Vector{S}) where {S <: AbstractString}
85+
return [s for s in symbols if symbol_name(s) in names]
86+
end
87+
88+
"""
89+
findfirst(symbols::Symbols, name::String)
90+
91+
Return the first section that matches the given `name`.
92+
"""
93+
function findfirst(symbols::Symbols, name::AbstractString)
94+
return findfirst(symbols, [name])
95+
end
96+
97+
"""
98+
findfirst(symbols::Symbols, names::Vector{String})
99+
100+
Return the first section that matches on of the given `names`.
101+
"""
102+
function findfirst(symbols::Symbols, names::Vector{String})
103+
results = findall(symbols, names)
104+
if isempty(results)
105+
error("Could not find any symbols that match $(names)")
106+
end
107+
return first(results)
108+
end
109+
65110

66111

67112

0 commit comments

Comments
 (0)