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.




:RL_ENCODE:
BOF
While (!At_EOF) {
    if (At_EOL) { Line(1) Continue }    // skip newlines
    #1 = Cur_Char                       // #1 = character
    Match("(.)\1*", REGEXP)             // count run length
    #2 = Chars_Matched                  // #2 = run length
    if (#2 > 127) { #2 = 127 }          // can be max 127
    if (#2 > 1 || #1 > 127) {
        Del_Char(#2)
        Ins_Char(#2 | 128)              // run length (high bit set)
        Ins_Char(#1)                    // character
    } else {                            // single ASCII char
        Char                            // skip
    }
}
Return
 
:RL_DECODE:
BOF
While (!At_EOF) {
    #2 = Cur_Char
    if (#2 > 127) {                     // is this run length?
        #1 = Cur_Char(1)                // #1 = character value
        Del_Char(2)
        Ins_Char(#1, COUNT, #2 & 127)
    } else {                            // single ASCII char
        Char
    }
}
Return