• 2008-01-28

    文本文件选取、处理和比较的js代码(兼容ie&ff) - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/14610798.html

    写了个文本文件选取、处理和比较的代码,其中文本比较部分采用51js的anbert的代码。
    参考链接:http://bbs.51js.com/viewthread.php?tid=73043
    文本比较有小bug,欢迎大家修正。
     
    <script>
    function getTxt(txtid,obj)
    {
    try{
    if(document.all)
    document.getElementById(txtid).value=obj.contentWindow.document.body.innerText;
    else
    document.getElementById(txtid).value=obj.contentWindow.document.body.textContent;
    if(document.getElementById(txtid).value=="")
    if(document.all)
    document.getElementById(txtid).value=obj.contentWindow.document.documentElement.innerText;
    else
    document.getElementById(txtid).value=obj.contentWindow.document.documentElement.textContent;
    if(document.getElementById(txtid).value=="")document.getElementById(txtid).value="文本为空";
    }
    catch(e){
    document.getElementById(txtid).value='无法读取文本';
    }
    }

        function compare_str(str1,str2){
            var res1 = "";
            var res2 = "";

            while(str1.length && str2.length){
                var arr = find_pos(str1,str2);
               
                if (arr && arr.length)
                {
                    if (arr[0])
                    {
                        res2 +=("<font color='green'>" + str2.substr(0,arr[0]) + "</font>");
                        str2 = str2.substr(arr[0]);
                    }
                    res1 +=(str1.substr(0,arr[1]));
                    res2 +=(str2.substr(0,arr[1]));        

                    str1 = str1.substr(arr[1]);
                    str2 = str2.substr(arr[1]);

                }else{
                    arr = find_max_pos(str1,str2);
                    tmp = find_match_pos(str1,str2);

                    if (tmp.length)
                    {              
                        if (arr[0]>tmp[0] && arr[1]>tmp[1])
                        {
                            arr = tmp;
                        }
                    }
     
                    if (arr!=undefined && arr.length)
                    {              
                        res1 +=("<font color='red'>" + str1.substr(0,arr[0]) + "</font>");
                        res2 +=("<font color='red'>" + str2.substr(0,arr[1]) + "</font>");
                        res1 +=(str1.substr(arr[0],arr[2]));
                        res2 +=(str2.substr(arr[1],arr[2]));

                        str1 = str1.substr(arr[0]+arr[2]);
                        str2 = str2.substr(arr[1]+arr[2]);
                    }else{
                        res1 +=("<font color='blue'>" + str1 + "</font>");
                        res2 +=("<font color='green'>" + str2 + "</font>");
                        str1 = '';
                        str2 = '';
                    }
                   
                }
            }
            if (str1.length)
            {
                res1 +=("<font color='blue'>" + str1 + "</font>");
            }
            if (str2.length)
            {
                res2 +=("<font color='green'>" + str2 + "</font>");
            }
            document.getElementById("txt3").innerHTML = (res1);
            document.getElementById("txt4").innerHTML = (res2);
        }
       
        //从串2查找对串1前N个字符的最大匹配
        function find_pos(str1,str2){
            var len = 1, pos = 0;
            var ret;   
           
            while (pos>0){
                var str = str1.substr(0,len);
                pos = str2.indexOf(str);
                if (pos!=-1)
                {
                    ret = [];
                    ret.push(pos,len);
                    len++;
                    if (len>str1.length){
                        break;
                    }
                   
                }else{
                    break;
                }
            }

            return ret;
        }
       
        //从串2查找对串1从第N个字符开始前N个字符的首次匹配
        function find_match_pos(str1,str2,arr){
            var len =1 , pos1 = 0, pos2 = 0, _pos2;
            var ffind = false;
            var ret = [];
           
            if (arr!=undefined)
            {
                pos1 = arr[0];pos2 = arr[1];
            }
            while((pos1+len)<str1.length){
                var str = str1.substr(pos1,len);
                _pos2 = str2.indexOf(str);
                if (_pos2!=-1)
                {
                    ffind = true;len++;pos2 = _pos2;
                }else{
                    if (ffind)
                    {
                        len--;break;
                    }else{
                        pos1 += len;len = 1;
                    }
                }          
            }

            if (ffind)
            {
                ret.push(pos1,pos2,len);
            }else{
                ret = [];
            }
           
            return ret;
        }
       
        //查找下一个最适匹配
        function find_next_pos(str1,str2){

        }
       
        //查找两个字串的字符数目最大匹配
        function find_max_pos(str1,str2){
            var ret,pos1,pos2,arr=null;
            var res, max = 0;
            do
            {
                ret = find_match_pos(str1,str2,arr);
                if (ret.length)
                {
                    if (ret[2]>max)
                    {
                        res = ret;
                        max = ret[2];
                    }
                   
                    arr = new Array(ret[0]+1,ret[1]);
                    
                }
            }
            while (ret.length);

            return res;
        }

        function comp(){
            var str1 = document.getElementById("txt").value;
            var str2 = document.getElementById("txt2").value;

            compare_str(str1,str2);
        }
    </script>
    <table><tr><td>
    <input type=file size=50 value="choose file1" onchange="document.getElementById('txt').value='文本为空';document.getElementById('ifrm').src='file://'+this.value" contenteditable="false" onkeypress="return false"></td><td><input type=file size=50 value="choose file2" onchange="document.getElementById('txt2').value='文本为空';document.getElementById('ifrm2').src='file://'+this.value" contenteditable="false" onkeypress="return false"></td></tr>
    <tr><td>
    <iframe id=ifrm name=ifrm width=450 onload="getTxt('txt',this)"></iframe>
    </td><td>
    <iframe id=ifrm2 name=ifrm2 width=450 onload="getTxt('txt2',this)"></iframe>
    </td></tr>
    <tr><td>
    <textarea id=txt style="width:450" rows=10></textarea>
    </td><td>
    <textarea id=txt2 style="width:450" rows=10></textarea>
    </td></tr>
    <tr><td>
    <div id="txt3" style="width:450;height:300;overflow:auto"></div>
    </td><td>
    <div id="txt4" style="width:450;height:300;overflow:auto"></div>
    </td></tr></table>
    <input type=button value=开始比较 onclick=comp()>

    分享到:

    历史上的今天: