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 // Start run-length counter
while fn StringIsEqual( mid( string, i + rl, 1), ch )
rl ++ // Same char, so increase counter
wend
if rl == 1 then t = @"" else t = fn StringWithFormat( @"%d", rl ) // IgnoreCounter countas of 1string
t = fn StringWithFormatStringByAppendingString( @"%d"t, rlch ) // Counter asAdd stringcharacter
ts = fn StringByAppendingString( ts, cht ) // Add characterto encoded string
i += rl - 1
s = fn StringByAppendingString( s, t ) // Add to output string
i += rl - 1 // Move counter
next
end fn =print s
end fn = s
 
 
local fn decode( string as CFStringRef ) as CFStringRef
CFStringRef ch, s, t // character, output stringouputstring, temporary string
Short i, rl // index, run- length
s = @"" // InitializeInitalize 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 outputdecoded string
next // character
print s
end fn
 
 
Line 2,921 ⟶ 2,924:
001
111
 
 
 
</pre>
60

edits