Run-length encoding: Difference between revisions
m
no edit summary
mNo edit summary |
mNo edit summary |
||
Line 2,835:
This gives RLE encoding for strings and RLE decoding for strings and arrays, e.g., for [[Conway's_Game_of_Life|Conway's Game of Life]]
<syntaxhighlight lang=FutureBasic>
local fn decode( string as CFStringRef ) as CFStringRef▼
CFStringRef ch, s, t // character, output string, temporary string▼
Short i, rl // index, run-length▼
s = @"" // Initialize the output string▼
for i = 0 to len( string ) - 1 // Decode input string char by char▼
ch = mid( string, i, 1 ) // Read character at index▼
if intval( ch ) == 0 // Not a digit▼
rl = 1▼
else▼
rl = intval( mid( string, i ) ) // Read run-length counter▼
i += fix( log10( rl ) + 1 ) // Move index past digits▼
ch = mid( string, i, 1 ) // Read character after run-length▼
end if▼
t = fn StringByPaddingToLength( ch, rl, ch, 0 ) // Assemble string of chars▼
s = fn StringByAppendingString( s, t ) // Add to output string▼
next // character▼
end fn = s▼
Dynamic a(1, 1) as Short // Self-fulfilling array
local fn encode( string as CFStringRef) as CFStringRef
CFStringRef ch, s, t
Short i, rl
s = @"" // Initalize the output string
for i = 0 to len( string ) - 1 // Encode string char by char
ch = mid( string, i, 1) // Read character at index
rl = 1
while fn StringIsEqual( mid( string, i + rl, 1), ch )
rl ++
wend
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) //
t = fn
i += rl - 1
next
▲ rl = 1
▲ else
▲ end if
▲ next // character
print s
end fn
Line 2,921 ⟶ 2,924:
001
111
</pre>
|