140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package xlsxtranslate
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// verify is the hard gate from the plan: residual Chinese must be zero,
|
|
// numeric/part tokens must be byte-identical to the input, and the package
|
|
// entry count must not change. Everything is reported; nothing here mutates
|
|
// the output.
|
|
func verify(outData, inData []byte, report *Report) {
|
|
outReader, err := zip.NewReader(bytes.NewReader(outData), int64(len(outData)))
|
|
if err != nil {
|
|
report.Warnings = append(report.Warnings, "输出包无法读回校验:"+err.Error())
|
|
return
|
|
}
|
|
inReader, err := zip.NewReader(bytes.NewReader(inData), int64(len(inData)))
|
|
if err != nil {
|
|
report.Warnings = append(report.Warnings, "输入包无法读回校验:"+err.Error())
|
|
return
|
|
}
|
|
if len(outReader.File) != len(inReader.File) {
|
|
report.Warnings = append(report.Warnings, fmt.Sprintf(
|
|
"zip 条目数变化:输入 %d,输出 %d", len(inReader.File), len(outReader.File)))
|
|
}
|
|
report.ZipEntries = len(outReader.File)
|
|
|
|
outTexts := collectPackageTexts(outReader)
|
|
inTexts := collectPackageTexts(inReader)
|
|
|
|
// Residual CJK ideographs in output text nodes.
|
|
for _, t := range outTexts {
|
|
if hasCJK(t) {
|
|
report.ResidualCJKCount++
|
|
if len(report.ResidualCJKSamples) < 20 {
|
|
report.ResidualCJKSamples = append(report.ResidualCJKSamples, truncate(t, 80))
|
|
}
|
|
} else if cjkAny.MatchString(t) {
|
|
report.FullWidthLeftovers++
|
|
}
|
|
}
|
|
|
|
// Numeric and part-token equality between input and output.
|
|
if diff := multisetDiff(numberToken, inTexts, outTexts); len(diff) > 0 {
|
|
report.NumberMismatches = diff
|
|
}
|
|
if diff := multisetDiff(partToken, inTexts, outTexts); len(diff) > 0 {
|
|
report.PartMismatches = diff
|
|
}
|
|
}
|
|
|
|
// collectPackageTexts gathers the logical text units of a package: shared
|
|
// strings and inline strings joined per block, drawing/chart text joined per
|
|
// paragraph — the same aggregation the write-back uses. Token-level checks
|
|
// (numbers, part numbers) must aggregate identically on both sides, because
|
|
// rich-text runs may split a number such as "1.0" into "1" + ".0" and the
|
|
// merge changes bare token counts without any data change.
|
|
func collectPackageTexts(reader *zip.Reader) []string {
|
|
var texts []string
|
|
appendBlockText := func(block string) {
|
|
var b strings.Builder
|
|
for _, m := range tTagRe.FindAllStringSubmatchIndex(block, -1) {
|
|
b.WriteString(unescapeXMLText(block[m[2]:m[3]]))
|
|
}
|
|
if b.Len() > 0 {
|
|
texts = append(texts, b.String())
|
|
}
|
|
}
|
|
for _, f := range reader.File {
|
|
raw, err := readZip(f)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch {
|
|
case f.Name == sharedStringsEntry || isSheetXML(f.Name):
|
|
re := siBlockRe
|
|
if f.Name != sharedStringsEntry {
|
|
re = isBlockRe
|
|
}
|
|
for _, loc := range re.FindAllStringIndex(string(raw), -1) {
|
|
appendBlockText(string(raw)[loc[0]:loc[1]])
|
|
}
|
|
case isDrawingXML(f.Name):
|
|
src := string(raw)
|
|
for _, bodyLoc := range txBodyRe.FindAllStringIndex(src, -1) {
|
|
body := src[bodyLoc[0]:bodyLoc[1]]
|
|
for _, paraLoc := range paraRe.FindAllStringIndex(body, -1) {
|
|
para := body[paraLoc[0]:paraLoc[1]]
|
|
var b strings.Builder
|
|
for _, m := range aTTagRe.FindAllStringSubmatchIndex(para, -1) {
|
|
b.WriteString(unescapeXMLText(para[m[2]:m[3]]))
|
|
}
|
|
if b.Len() > 0 {
|
|
texts = append(texts, b.String())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return texts
|
|
}
|
|
|
|
// multisetDiff compares token multisets extracted from input versus output
|
|
// texts and returns a stable list of "token (in->out)" descriptions for every
|
|
// token whose count changed.
|
|
func multisetDiff(re *regexp.Regexp, inTexts, outTexts []string) []string {
|
|
in := map[string]int{}
|
|
out := map[string]int{}
|
|
tokens := map[string]struct{}{}
|
|
for _, t := range inTexts {
|
|
for _, tok := range re.FindAllString(t, -1) {
|
|
in[tok]++
|
|
tokens[tok] = struct{}{}
|
|
}
|
|
}
|
|
for _, t := range outTexts {
|
|
for _, tok := range re.FindAllString(t, -1) {
|
|
out[tok]++
|
|
tokens[tok] = struct{}{}
|
|
}
|
|
}
|
|
var diffs []string
|
|
for tok := range tokens {
|
|
a, b := in[tok], out[tok]
|
|
if a != b {
|
|
diffs = append(diffs, fmt.Sprintf("%s (%d->%d)", tok, a, b))
|
|
}
|
|
}
|
|
sort.Strings(diffs)
|
|
if len(diffs) > 20 {
|
|
diffs = append(diffs[:20], fmt.Sprintf("… 共 %d 项", len(diffs)))
|
|
}
|
|
return diffs
|
|
}
|