Run-length encoding

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression.

The output can be anything, as long as you can recreate the input with it.



Example

 Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
 Output: 12W1B12W3B24W1B14W


Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.




$ include "seed7_05.s7i";
  include "scanstri.s7i";
 
const func string: letterRleEncode (in string: data) is func
  result
    var string: result is "";
  local
    var char: code is ' ';
    var integer: index is 1;
  begin
    if length(data) <> 0 then
      code := data[1];
      repeat
        incr(index);
      until index > length(data) or code <> data[index];
      result := str(pred(index)) & str(code) & letterRleEncode(data[index ..]);
    end if;
  end func;
 
const func string: letterRleDecode (in var string: data) is func
  result
    var string: result is "";
  local
    var integer: count is 0;
  begin
    if length(data) <> 0 then
      count := integer parse getDigits(data);
      result := data[1 len 1] mult count & letterRleDecode(data[2 ..]);
    end if;
  end func;
 
const proc: main is func
  begin
    writeln(letterRleEncode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"));
    writeln(letterRleDecode("12W1B12W3B24W1B14W"));
  end func;