diff --git a/Sources/DictionaryKit/TTTDictionary.m b/Sources/DictionaryKit/TTTDictionary.m index 2e4a54c..a3ae3e2 100644 --- a/Sources/DictionaryKit/TTTDictionary.m +++ b/Sources/DictionaryKit/TTTDictionary.m @@ -21,6 +21,8 @@ // THE SOFTWARE. #import "TTTDictionary.h" +#include +#include #import @@ -180,6 +182,24 @@ - (NSArray *)entriesForSearchTerm:(NSString *)term { return [NSArray arrayWithArray:mutableEntries]; } +- (BOOL)containsSearchTerm:(nonnull NSString *)term { + CFRange termRange = DCSGetTermRangeInString(self.dictionary, (__bridge CFStringRef)term, 0); + if (termRange.location == kCFNotFound) { + return NO; + } + + term = [term substringWithRange:NSMakeRange(termRange.location, termRange.length)]; + + CFArrayRef records = DCSCopyRecordsForSearchString(self.dictionary, (__bridge CFStringRef)term, NULL, NULL); + BOOL hasRecords = records && CFArrayGetCount(records) > 0; + + if (records) { + CFRelease(records); + } + + return hasRecords; +} + #pragma mark - NSObject - (BOOL)isEqual:(id)object { diff --git a/Sources/DictionaryKit/include/TTTDictionary.h b/Sources/DictionaryKit/include/TTTDictionary.h index a400f1f..54c06df 100644 --- a/Sources/DictionaryKit/include/TTTDictionary.h +++ b/Sources/DictionaryKit/include/TTTDictionary.h @@ -42,6 +42,7 @@ + (nonnull NSSet *)availableDictionaries; + (nullable instancetype)dictionaryNamed:(nonnull NSString *)name; - (nonnull NSArray *)entriesForSearchTerm:(nonnull NSString *)term; +- (BOOL)containsSearchTerm:(nonnull NSString *)term NS_SWIFT_NAME(contains(searchTerm:)); @end