Rebol3 Code Examplex


GSTrans string conversion

Convert text using a substitution or transliteration scheme.

Rebol [
    title: "Rosetta code: GSTrans string conversion"
    file: %GSTrans_string_conversion.r3
    url: https://rosettacode.org/wiki/GSTrans_string_conversion
    needs: 3.0.0
]
register-codec [
    name:  'GSTrans
    type:  'text
    encode: function [data [binary! any-string!]][
        o: copy ""
        foreach c to binary! data [
            if c > 127 [append o "|!" c: c - 128]
            case [
                c < 32    [append append o #"|" #"@" + c]
                c = #"^"" [append o {|"}]
                c = #"|"  [append o "||"]
                c = #"<"  [append o "|<"]
                c < 127   [append o to char! c]
                c = 127   [append o "|?"]
            ]
        ]
        o
    ]
    decode: function [data [binary! any-string!]][
        c: none
        o: copy #{}
        parse data [some [
            #"|" [
                #"!" [
                    #"|" [
                        set c [#"^"" | #"|" | #"<"] (append o 128 + c)
                        | #"?" (append o 255)
                        | set c skip (append o 64  + uppercase c)
                    ]
                    | set c skip (append o 128 + c)
                ]
                | #"?" (append o 127)
                | set c [#"|" | #"^""] (append o c)
                | set c skip (append o (uppercase c) - #"@")
            ]
            | copy c to [#"|" | end] (append o c)
            | skip
        ]]
        try [o: to string! o]
        o
    ]
]

assert ["|M|J|@|E|!t|M|!|?" = codecs/gstrans/encode #{0D 0A 00 05 F4 0D FF}]
assert [#{0D 0A 00 05 F4 0D FF} = codecs/gstrans/decode "|m|j|@|e|!t|m|!|?"]

foreach str [
    "ALERT|G"
    "wert↑"
    "@♂aN°$ª7Î"
    "ÙC▼æÔt6¤☻Ì"
    {"@)Ð♠qhýÌÿ}
    "+☻#o9$u♠©A"
    "♣àlæi6Ú.é"
    "ÏÔ♀È♥@ë"
    "Rç÷%◄MZûhZ"
    "ç>¾AôVâ♫↓P"

][
    print ["Source: " mold str]
    print ["Encoded:" mold enc: encode 'GSTrans str]
    print ["Decoded:" mold dec: decode 'GSTrans enc]
    if not equal? str dec [print as-red "FAILED!"]
    print "---"
]

Output:

Source:  "ALERT|G"
Encoded: "ALERT||G"
Decoded: "ALERT|G"
---
Source:  "wert↑"
Encoded: "wert|!b|!|F|!|Q"
Decoded: "wert↑"
---
Source:  "@♂aN°$ª7Î"
Encoded: "@|!b|!|Y|!|BaN|!B|!0$|!B|!*7|!C|!|N"
Decoded: "@♂aN°$ª7Î"
---
Source:  "ÙC▼æÔt6¤☻Ì"
Encoded: {|!C|!|YC|!b|!|V|!|<|!C|!&|!C|!|Tt6|!B|!$|!b|!|X|!;|!C|!|L}
Decoded: "ÙC▼æÔt6¤☻Ì"
---
Source:  {"@)Ð♠qhýÌÿ}
Encoded: {|"@)|!C|!|P|!b|!|Y|! qh|!C|!=|!C|!|L|!C|!?}
Decoded: {"@)Ð♠qhýÌÿ}
---
Source:  "+☻#o9$u♠©A"
Encoded: "+|!b|!|X|!;#o9$u|!b|!|Y|! |!B|!)A"
Decoded: "+☻#o9$u♠©A"
---
Source:  "♣àlæi6Ú.é"
Encoded: "|!b|!|Y|!#|!C|! l|!C|!&i6|!C|!|Z.|!C|!)"
Decoded: "♣àlæi6Ú.é"
---
Source:  "ÏÔ♀È♥@ë"
Encoded: "|!C|!|O|!C|!|T|!b|!|Y|!|@|!C|!|H|!b|!|Y|!%@|!C|!+"
Decoded: "ÏÔ♀È♥@ë"
---
Source:  "Rç÷%◄MZûhZ"
Encoded: "R|!C|!'|!C|!7%|!b|!|W|!|DMZ|!C|!;hZ"
Decoded: "Rç÷%◄MZûhZ"
---
Source:  "ç>¾AôVâ♫↓P"
Encoded: {|!C|!'>|!B|!>A|!C|!4V|!C|!|"|!b|!|Y|!+|!b|!|F|!|SP}
Decoded: "ç>¾AôVâ♫↓P"
---