mirror of
https://github.com/cemu-project/idapython.git
synced 2025-04-11 21:51:19 +02:00
- added NearestName class
This commit is contained in:
parent
3ea31d70c7
commit
910f88f6b7
41
swig/name.i
41
swig/name.i
@ -52,5 +52,46 @@ PyObject *py_get_debug_names(ea_t ea1, ea_t ea2)
|
||||
}
|
||||
%}
|
||||
|
||||
%pythoncode %{
|
||||
|
||||
import bisect
|
||||
|
||||
class NearestName:
|
||||
"""
|
||||
Utility class to help find the nearest name in a given ea/name dictionary
|
||||
"""
|
||||
def __init__(self, ea_names):
|
||||
self.update(ea_names)
|
||||
|
||||
def update(self, ea_names):
|
||||
"""Updates the ea/names map"""
|
||||
self._names = ea_names
|
||||
self._addrs = ea_names.keys()
|
||||
self._addrs.sort()
|
||||
|
||||
def find(self, ea):
|
||||
"""
|
||||
Returns a tupple (ea, name, pos) that is the nearest to the passed ea
|
||||
If no name is matched then None is returned
|
||||
"""
|
||||
pos = bisect.bisect_left(self._addrs, ea)
|
||||
# no match
|
||||
if pos >= len(self._addrs):
|
||||
return None
|
||||
# exact match?
|
||||
if self._addrs[pos] != ea:
|
||||
pos -= 1 # go to previous element
|
||||
if pos < 0:
|
||||
return None
|
||||
return self[pos]
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""Returns the tupple (ea, name, index)"""
|
||||
if index > len(self._addrs):
|
||||
raise StopIteration
|
||||
ea = self._addrs[index]
|
||||
return (ea, self._names[ea], index)
|
||||
|
||||
%}
|
||||
%include "name.hpp"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user