RosettaCodeData/Task/Boyer-Moore-string-search/FutureBasic/boyer-moore-string-search.b...

56 lines
1.7 KiB
Plaintext

include "NSLog.incl"
NSInteger local fn String_Search_Single( haystack as CFStringRef, needle as CFStringRef )
CFRange range = fn StringRangeOfString( haystack, needle )
return range.location
end fn = 0
CFArrayRef local fn String_Search( haystack as CFStringRef, needle as CFStringRef )
CFMutableArrayRef result = fn MutableArrayNew
NSUInteger start = 0
NSInteger index = 0
while ( index >= 0 && start < len(haystack) )
CFStringRef haystackReduced = fn StringSubstringFromIndex( haystack, start )
index = fn String_Search_Single( haystackReduced, needle )
if ( index != NSNotFound )
MutableArrayAddObject( result, @(start + index) )
start += index + len(needle)
else
break
end if
wend
return result
end fn = 0
void local fn FindNeedlesInHaystacks
NSUInteger i
CFArrayRef texts = @[
@"GCTAGCTCTACGAGTCTA",
@"GGCTATAATGCGTA",
@"there would have been a time for such a word",
@"needle need noodle needle",
@"DKnuthusesandprogramsanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguages",
@"Nearby farms grew an acre of alfalfa on the dairy's behalf, with bales of that alfalfa exchanged for milk."
]
CFArrayRef patterns = @[@"TCTA", @"TAATAAA", @"word", @"needle", @"and", @"alfalfa"]
for i = 0 to fn ArrayCount(texts) - 1
NSLog( @"text%lu = %@", (unsigned long)(i + 1), texts[i] )
next
NSLog( @"" )
for i = 0 to fn ArrayCount(texts) - 1
CFArrayRef indexes = fn String_Search( texts[i], patterns[i] )
NSLog( @"Found \"%@\" in 'text%lu' at indexes \b", patterns[i], (unsigned long)(i + 1) )
NSLog( @"[%@]", fn ArrayComponentsJoinedByString( indexes, @", " ) )
next
end fn
fn FindNeedlesInHaystacks
HandleEvents