#nop vim: set filetype=tt:; /* 本文件属于 PaoTin++ 的一部分。 PaoTin++ © 2020~2023 的所有版权均由担子炮(dzp ) 享有并保留一切法律权利 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。 */ #nop 本文件是 xtintin 的一部分,实现了一些常用算法; ///=== { // #@ algo.Diff <字符串1> <字符串2> // 如果两个字符串参数中,其中一个是另一个插入了一个子串的结果,则本函数可计算并返回该子串。 // }; #func {algo.Diff} { #local str1 {%1}; #local str2 {%2}; #list str1 {tokenize} {$str1}; #list str2 {tokenize} {$str2}; #local size1 {&str1[]}; #local size2 {&str2[]}; #if { $size1 < $size2 } { #local tmp {$str1}; #local str1 {$str2}; #local str2 {$tmp}; #local tmp {$size1}; #local size1 {$size2}; #local size2 {$tmp}; }; #local idx {}; #loop {1} {$size2} {idx} { #local ch1 {$str1[$idx]}; #local ch2 {$str2[$idx]}; #if { "$ch1" != "$ch2" } { #break; }; }; #local begin {$idx}; #local end {}; #math end {$size1 - $size2 + $begin - 1}; #local diff {}; #loop {$begin} {$end} {idx} { #local ch {$str1[$idx]}; #cat diff {$ch}; }; #return {$diff}; }; ///=== { // #@ algo.ParseVertLayout <竖排文字串> // 将竖排文字重新排版成横排,方便做匹配。 // 结果是一个列表,其中的每个元素代表一行转换后的文本。 // 为方便计算对齐,其中竖排文字串要求用竖线定出每行的左右边界,且一个汉字必须对应两个空格。 // }; #func {algo.ParseVertLayout} { #local text {%0}; #replace text {| |} {|}; #replace text {^|} {}; #replace text {|$} {}; #local ch {}; #local lines {}; #local state {left}; #local colNo {1}; #local newline {|}; #parse {$text} {ch} { #switch {"$state/$ch"} { #case {"left/ "} { #local state {right}; }; #case {"right/ "} { #local state {left}; #local lines[$colNo] {$lines[$colNo] }; #math colNo {$colNo + 1}; }; #case {"%*/$newline"} { #math colNo {1}; #local state {left}; }; #case {"left/%*"} { #local lines[$colNo] {$lines[$colNo]$ch}; #math colNo {$colNo + 1}; }; #default { errLog find BUG; #return {}; }; }; }; #local output {}; #loop {&lines[]} {1} {colNo} { #replace {lines[$colNo]} {%+1..s$} {}; #if { {$lines[$colNo]} == {%*%+1..S%*} } { #list output add {{$lines[$colNo]}}; }; }; #return {$output}; };