@@ -253,6 +253,24 @@ cdef class DAWG:
253253
254254 return res
255255
256+ def longest_prefix (self , unicode key ):
257+ cdef BaseType index = self .dct.root()
258+ cdef int pos = 1
259+ cdef int lastpos = 0
260+ cdef CharType ch
261+
262+ for ch in key:
263+ if not self .dct.Follow(ch, & index):
264+ break
265+ if self .dct.has_value(index):
266+ lastpos = pos
267+ pos += 1
268+
269+ if lastpos:
270+ return key[:lastpos]
271+ else :
272+ raise KeyError (" No prefix found" )
273+
256274 def iterprefixes (self , unicode key ):
257275 '''
258276 Return a generator with keys of this DAWG that are prefixes of the ``key``.
@@ -802,7 +820,28 @@ cdef class BytesDAWG(CompletionDAWG):
802820 """
803821 return self ._similar_item_values(0 , key, self .dct.root(), replaces)
804822
823+ def longest_prefix (self , unicode key ):
824+ cdef BaseType index = self .dct.root()
825+ cdef BaseType tmp
826+ cdef BaseType lastindex
827+ cdef int pos = 1
828+ cdef int lastpos = 0
829+ cdef CharType ch
805830
831+ for ch in key:
832+ if not self .dct.Follow(ch, & index):
833+ break
834+
835+ tmp = index
836+ if self .dct.Follow(self ._c_payload_separator, & tmp):
837+ lastpos = pos
838+ lastindex = tmp
839+ pos += 1
840+
841+ if lastpos:
842+ return key[:lastpos], self ._value_for_index(lastindex)
843+ else :
844+ raise KeyError (" No prefix found" )
806845
807846cdef class RecordDAWG(BytesDAWG):
808847 """
@@ -904,6 +943,26 @@ cdef class IntDAWG(DAWG):
904943 cpdef int b_get_value(self , bytes key):
905944 return self .dct.Find(key)
906945
946+ def longest_prefix (self , unicode key ):
947+ cdef BaseType index = self .dct.root()
948+ cdef BaseType lastindex
949+ cdef int pos = 1
950+ cdef int lastpos = 0
951+ cdef CharType ch
952+
953+ for ch in key:
954+ if not self .dct.Follow(ch, & index):
955+ break
956+
957+ if self .dct.has_value(index):
958+ lastpos = pos
959+ lastindex = index
960+ pos += 1
961+
962+ if lastpos:
963+ return key[:lastpos], self .dct.value(lastindex)
964+ else :
965+ raise KeyError (" No prefix found" )
907966
908967# FIXME: code duplication.
909968cdef class IntCompletionDAWG(CompletionDAWG):
0 commit comments