RosettaCodeData/Task/Word-wrap/FutureBasic/word-wrap.basic

60 lines
2.3 KiB
Plaintext

CFArrayRef local fn WrapStringAtWordBoundaries( txtStr as CFStringRef, maxLineLength as NSUInteger )
CFMutableArrayRef lines = fn MutableArrayNew
ScannerRef scanner = fn ScannerWithString( txtStr )
CFMutableStringref currentLine = fn MutableStringNew
CFStringRef wordStr
ScannerSetCharactersToBeSkipped( scanner, NULL )
while ( fn ScannerIsAtEnd( scanner ) == NO )
fn ScannerScanUpToCharactersFromSet( scanner, fn CharacterSetWhitespaceAndNewlineSet, @wordStr )
if ( wordStr )
if ( ( len( currentLine ) + len( wordStr ) + 1 ) > maxLineLength )
if ( len( currentLine ) > 0 )
MutableArrayAddObject( lines, fn StringWithString( currentLine ) )
MutableStringSetString( currentLine, @"" ) // Reset the line
end if
end if
if ( len( currentLine ) > 0 )
MutableStringAppendString( currentLine, @" " )
end if
MutableStringAppendString( currentLine, wordStr )
end if
fn ScannerScanCharactersFromSet( scanner, fn CharacterSetWhitespaceAndNewlineSet, NULL ) // Skip over whitespace between words
wend
if ( len( currentLine ) > 0 )
MutableArrayAddObject( lines, fn StringWithString( currentLine ) )
end if
return lines
end fn = NULL
CFStringRef txtStr
txtStr = @"In olden times when wishing still helped one, there lived a king ¬
whose daughters were all beautiful, but the youngest was so beautiful ¬
that the sun itself, which has seen so much, was astonished whenever ¬
it shone-in-her-face. Close by the king's castle lay a great dark ¬
forest, and under an old lime-tree in the forest was a well, and when ¬
the day was very warm, the king's child went out into the forest and ¬
sat down by the side of the cool-fountain, and when she was bored she ¬
took a golden ball, and threw it up on high and caught it, and this ¬
ball was her favorite plaything."
CFStringRef columnizedStr
// Create wrapping lines respecting word boundaries
print @"Wrapped at 20 characters:"
columnizedStr = fn ArrayComponentsJoinedByString( fn WrapStringAtWordBoundaries( txtStr, 20 ), @"\n" )
print columnizedStr
print
print @"Wrapped at 80 characters:"
columnizedStr = fn ArrayComponentsJoinedByString( fn WrapStringAtWordBoundaries( txtStr, 80 ), @"\n" )
print columnizedStr
HandleEvents