Run-length encoding: Difference between revisions

no edit summary
No edit summary
Line 2,831:
{{out}}
La salida es similar a la de [[#BASIC|BASIC]], mostrada arriba.
 
=={{header|FutureBasic}}==
<syntaxhighlight lang=FutureBasic>
local fn decode( string as CFStringRef ) as CFStringRef
CFStringRef ch, s, t // character, ouputstring, temporary string
Short i, rl // index, run length
s = @"" // Initalize 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
s = fn StringByAppendingString( s, t ) // Add to decoded string
next // character
end fn = s
 
 
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 ... // Uncomment to ignore 1's
t = fn StringWithFormat( @"%d", rl ) // Counter as string
t = fn StringByAppendingString( t, ch ) // Add character
s = fn StringByAppendingString( s, t ) // Add to output string
i += rl - 1
next
end fn = s
 
 
print fn encode( @"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" )
print fn encode( fn decode( @"12W1B12W3B24W1B14W" ) )
 
handleevents // Join Mac event loop
 
</syntaxhighlight>
<pre>
12W1B12W3B24W1B14W
12W1B12W3B24W1B14W
</pre>
 
=={{header|Gambas}}==
60

edits