修正 JSON 位元組相容與 xref Unicode 邊界(審核意見)

依 #12 審核意見修正四項偏差,並補一項同源問題:

1. iterXrefs 前一字元檢查改 Unicode:isWordChar 改用
   unicode.IsLetter/IsDigit,且以 utf8 正確解碼前一 rune
   (原版只認 ASCII 又取單一位元組)。另補齊 Python finditer
   的重試語義:起點被 lookbehind 擋下時從下一 rune 重試,
   否則「修復bear-cli#9」會整段丟失(Python 匹配後半 cli#9);
   lookbehind 字元集含 #(##5 不匹配)。
2. writeJSON 改 json.Encoder + SetEscapeHTML(false):
   Python ensure_ascii=False 不轉義 <、>、&,json.Marshal 預設
   會,標題含這些字元即失去位元組相容。
3. stalled_hours 改 strconv.FormatFloat(v,'f',1,64):整數值輸出
   48.0(原 %g 輸出 48,極大值還會變 1e+06)。
4. Child.last_activity_at 無可解析時刻時輸出 null(原輸出 ""),
   型別改 *string;格式對齊 FormatMoment(UTC 記 +00:00 非 Z)。
5. cmd/xcheck 輸出同樣停用 HTML 轉義(Marshal* 對自訂
   MarshalJSON 的輸出仍會轉義)。

驗證:go build/vet/test 全綠(16 測試函式,新增 CJK、##5、
全形數字、HTML 字元、48.0、null 等案例);cmd/xcheck 與
gitea.py stalled 對真實 API 並行掃描輸出位元組相同(1749B,
含 bear-cli#9 父追蹤項與 5 子項摘要);--xrefs 對 CJK
邊界輸入與 Python 一致。
This commit is contained in:
ceo
2026-09-10 09:47:46 +08:00
parent a86803d336
commit b2dd2f3aac
3 changed files with 109 additions and 41 deletions
+21 -4
View File
@@ -6,6 +6,7 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
@@ -211,8 +212,17 @@ func main() {
fmt.Fprintln(os.Stderr, "ScanStalled:", err)
os.Exit(1)
}
out, _ := json.MarshalIndent(items, "", " ")
fmt.Println(string(out))
// json.Marshal* 會把 <、>、& 轉義成 \u003c 等(含自訂 MarshalJSON 的輸出),
// 與 gitea.py(ensure_ascii=False)位元組不相容;必須停用 HTML 轉義。
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(items); err != nil {
fmt.Fprintln(os.Stderr, "Encode:", err)
os.Exit(1)
}
fmt.Print(buf.String())
}
// xrefsMain 對 stdin 的每行文字解析引用目標並輸出 JSON。
@@ -251,6 +261,13 @@ func xrefsMain() {
fmt.Fprintln(os.Stderr, "ResolveXrefTargets:", err)
os.Exit(1)
}
out, _ := json.MarshalIndent(targets, "", " ")
fmt.Println(string(out))
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(targets); err != nil {
fmt.Fprintln(os.Stderr, "Encode:", err)
os.Exit(1)
}
fmt.Print(buf.String())
}