DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rpatch XOR Diff/patch Script

01.13.2006
| 2128 views |
  • submit to reddit
        
REBOL []


do-by-type: func [
    value
    actions [block!]
    /default
        def-action [block!]
    /local action
][
    either action: select actions type?/word value [
        ;print mold action
        do bind/copy action 'value
    ][
        if default [do bind/copy def-action 'value]
    ]
]

to-binary-ex: func [value] [
    do-by-type value [
        binary! [value]
        string! [to-binary value]
        file!   [read/binary value]
    ]
]

xor-diff: func [
    a [binary! string! file!]
    b [binary! string! file!]
    /local bin-a bin-b
][
    bin-a: to-binary-ex a
    bin-b: to-binary-ex b
    bin-a xor bin-b
]

make-patch: func [
    a [binary! string! file!]
    b [binary! string! file!]
][
    compress xor-diff a b
]

apply-patch: func [
    data  [binary! string! file!]
    patch [binary! string!]
    /local result
][
    result: xor-diff data decompress patch
    ; If they gave us a string as the source, return a string.
    ;?? If they give us a file, should we write back out to it?
    either string? data [to-string result][result]
]


a: "Now is the time for all good men to come to the aid of their country..."
b: {Now is the time for all good men to come to the aid of their country...
whether 'tis nolber to suffer the slings and arrows...}
;d1: xor-diff a b
p1: make-patch a b
bp: apply-patch a p1



change-dir %./test-files

files: sort read %.
print mold files
print [files/1 files/2]
fp-1-2: make-patch files/1 files/2
fp-2-3: make-patch files/2 files/3
fp-3-4: make-patch files/3 files/4
fp-a-b: make-patch files/5 files/6

pp-a-b: apply-patch files/5 fp-a-b

change-dir %..


halt