Paradigm Shift Design

ISHITOYA Kentaro's blog.

ActionScriptでDiff/Patch, google-diff-match-patchのポート

本当はこんなことやってる暇ないはずなのに・・・
ポートしてしまった.



google-diff-match-patch -


Diff, Match and Patch libraries for Plain Text - Google Project Hosting
ActionScriptへのポート.
ECMAだからコピペするだけでほぼ動く.
ただし警告を消すのが大変だった・・・


http://www.guarana.cc/kent/dmp/DiffMatchPatch.as
http://www.guarana.cc/kent/dmp/PatchObj.as
使い方は,元のやつと同じ.
例えば,次のように

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
    <![CDATA[
        import com.semcode.commons.lang.DiffMatchPatch;
        public function onButtonDown():void{
            var diff:DiffMatchPatch = new DiffMatchPatch();
            var diffs:Array = diff.diff_main(e1.text, e2.text);
            diff.diff_cleanupSemantic(diffs);
            var patches:Array = 
                diff.patch_make(e1.text, e2.text, diffs);
            trace(diff.patch_toText(patches));
            var ret:Array = diff.patch_apply(patches, e1.text);
            result.text = ret[0];
        }
    ]]>
</mx:Script>
    <mx:RichTextEditor id="e1" x="47" y="48">
    </mx:RichTextEditor>
    <mx:RichTextEditor id="e2" x="47" y="378">
    </mx:RichTextEditor>
    <mx:Button x="408" y="47" label="ボタン" click="onButtonDown()"/>
    <mx:RichTextEditor id="result" x="408" y="87">
    </mx:RichTextEditor>
</mx:Application>

関数が汚いからwrapしたいけど,まいいや.
普通にDiff/Patchができました.


ポートしたあとに,Javascriptでdiffる ( with 形態素解析 ) (nakatani @ cybozu labs)に気がつく.まいいか.なんとなく動いてるし.
あ,LGPLです.


I've ported google-diff-match-patch to ActionScript3 based on javascript version.
To use it, just download .as file above and place it somewhere in your project.Then use it like the sample code above.
Licence is LGPL.