2323
2424import codecs
2525import os
26+ from itertools import islice
2627
2728from bpython .translations import _
2829from bpython .filelock import FileLock
@@ -96,16 +97,20 @@ def entries_by_index(self):
9697
9798
9899 def find_match_backward (self , search_term , include_current = False ):
99- for idx , val in enumerate (self .entries_by_index [self .index + (0 if include_current else 1 ):]):
100+ add = 0 if include_current else 1
101+ start = self .index + add
102+ for idx , val in enumerate (islice (self .entries_by_index , start , None )):
100103 if val .startswith (search_term ):
101- return idx + ( 0 if include_current else 1 )
104+ return idx + add
102105 return 0
103106
104107
105108 def find_partial_match_backward (self , search_term , include_current = False ):
106- for idx , val in enumerate (self .entries_by_index [self .index + (0 if include_current else 1 ):]):
109+ add = 0 if include_current else 1
110+ start = self .index + add
111+ for idx , val in enumerate (islice (self .entries_by_index , start , None )):
107112 if search_term in val :
108- return idx + ( 0 if include_current else 1 )
113+ return idx + add
109114 return 0
110115
111116
@@ -116,7 +121,8 @@ def forward(self, start=True, search=False, target=None,
116121 target = self .saved_line
117122 if self .index > 1 :
118123 if search :
119- self .index -= self .find_partial_match_forward (target , include_current )
124+ self .index -= self .find_partial_match_forward (target ,
125+ include_current )
120126 elif start :
121127 self .index -= self .find_match_forward (target , include_current )
122128 else :
@@ -128,17 +134,22 @@ def forward(self, start=True, search=False, target=None,
128134
129135
130136 def find_match_forward (self , search_term , include_current = False ):
131- #TODO these are no longer efficient, because we realize the whole list. Does this matter?
132- for idx , val in enumerate (reversed (self .entries_by_index [:max (0 , self .index - (1 if include_current else 0 ))])):
137+ add = 0 if include_current else 1
138+ end = max (0 , self .index - (1 - add ))
139+ for idx in xrange (end ):
140+ val = self .entries_by_index [end - 1 - idx ]
133141 if val .startswith (search_term ):
134142 return idx + (0 if include_current else 1 )
135143 return self .index
136144
137145
138146 def find_partial_match_forward (self , search_term , include_current = False ):
139- for idx , val in enumerate (reversed (self .entries_by_index [:max (0 , self .index - (1 if include_current else 0 ))])):
147+ add = 0 if include_current else 1
148+ end = max (0 , self .index - (1 - add ))
149+ for idx in xrange (end ):
150+ val = self .entries_by_index [end - 1 - idx ]
140151 if search_term in val :
141- return idx + ( 0 if include_current else 1 )
152+ return idx + add
142153 return self .index
143154
144155
0 commit comments