Abbreviations, automatic: Difference between revisions
no edit summary
No edit summary |
No edit summary |
||
Line 3,164:
</pre>
=={{header|
This assumes a text file named "DaysOfWeek.txt" is in the same folder as the code file.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
include resources "DaysOfWeek.txt"
local fn DaysOfWeek as CFArrayRef
CFURLRef url▼
CFStringRef string▼
▲CFURLRef url
CFArrayRef weeks = NULL▼
▲CFStringRef string
▲CFArrayRef weeks = NULL
url = fn BundleURLForResource( fn BundleMain, @"DaysOfWeek", @"txt", NULL )▼
string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )▼
▲url = fn BundleURLForResource( fn BundleMain, @"DaysOfWeek", @"txt", NULL )
if ( string )▼
▲string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
weeks = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )▼
▲if ( string )
end if▼
▲weeks = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
▲end if
end fn = weeks
local fn MinLengthAbbreviation( week as CFStringRef ) as long
CFArrayRef days▼
CFStringRef day1, day2, abbr▼
▲CFArrayRef days
long count, length, minLen, index1, index2▼
▲CFStringRef day1, day2, abbr
▲long count, length, minLen, index1, index2
minLen = 1▼
days = fn StringComponentsSeparatedByString( week, @" " )▼
▲minLen = 1
▲days = fn StringComponentsSeparatedByString( week, @" " )
for index1 = 0 to count - 1▼
day1 = days[index1]▼
▲for index1 = 0 to count - 1
length = len( day1 )
▲day1 = days[index1]
for index2 = 0 to count - 1▼
abbr = left( day1, minLen )▼
if ( index2 != index1 )▼
▲for index2 = 0 to count - 1
day2 = days[index2]▼
▲if ( index2 != index1 )
while ( fn StringHasPrefix( day2, abbr ) )▼
▲day2 = days[index2]
minLen++
▲while ( fn StringHasPrefix( day2, abbr ) )
if ( minLen <= length )▼
▲ abbr = left( day1, minLen )
▲if ( minLen <= length )
else
break
end if
wend
end if▼
end if
next
end if▼
next
▲ end if
next▼
end if▼
end fn = minLen
void local fn Abbreviations
CFArrayRef weeks▼
CFStringRef week▼
▲CFArrayRef weeks
▲CFStringRef week
weeks = fn DaysOfWeek▼
if ( weeks )▼
▲weeks = fn DaysOfWeek
for week in weeks▼
▲if ( weeks )
if ( len(week) )
▲for week in weeks
NSLog(@"%ld\t%@",fn MinLengthAbbreviation( week ),week)▼
▲if ( len(week) )
else
▲NSLog(@"%ld\t%@",fn MinLengthAbbreviation( week ),week)
NSLog(@"NULL")▼
▲ end if
▲NSLog(@"NULL")
▲ next
▲end if
▲ end if
end fn
|