penguin/golifehk

@golifehk bot in Telegram

commit a396a381b59afb350c1ce2f3ea7ed1cf0759df95

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-03-01T20:17:00Z
subjectFixed incorrect humanumarts links
commit a396a381b59afb350c1ce2f3ea7ed1cf0759df95
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-03-01T20:17:00Z

    Fixed incorrect humanumarts links
---
 datasources/cjlookup/humanumarts.go      | 15 +++++++--------
 datasources/cjlookup/humanumarts_test.go | 18 ++++++++++++++++--
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/datasources/cjlookup/humanumarts.go b/datasources/cjlookup/humanumarts.go
index dc484c8..2d3563c 100644
--- a/datasources/cjlookup/humanumarts.go
+++ b/datasources/cjlookup/humanumarts.go
@@ -27,10 +27,11 @@ func getCUHARTSUrlForChar(c string) string {
 
 func SParams(p string) string {
 	s1 := "-"
+	bestMatch := 0
 	for _, x := range SING_MOU_S1 {
-		if strings.HasPrefix(p, x) {
+		if bestMatch < len(x) && strings.HasPrefix(p, x) {
 			s1 = x
-			break
+			bestMatch = len(x)
 		}
 	}
 
@@ -43,13 +44,11 @@ func SParams(p string) string {
 	}
 
 	s2 := ""
-	bestMatch := 0
+	bestMatch = 0
 	for _, x := range WAN_MOU_S2 {
-		if strings.HasSuffix(strings.TrimSuffix(strings.TrimPrefix(p, s1), s3), x) {
-			if bestMatch < len(x) {
-				s2 = x
-				bestMatch = len(x)
-			}
+		if bestMatch < len(x) && strings.HasSuffix(strings.TrimSuffix(strings.TrimPrefix(p, s1), s3), x) {
+			s2 = x
+			bestMatch = len(x)
 		}
 	}
 	return fmt.Sprintf("s1=%s&s2=%s&s3=%s", s1, s2, s3)
diff --git a/datasources/cjlookup/humanumarts_test.go b/datasources/cjlookup/humanumarts_test.go
index 4cc6f48..76ad467 100644
--- a/datasources/cjlookup/humanumarts_test.go
+++ b/datasources/cjlookup/humanumarts_test.go
@@ -10,7 +10,21 @@ func TestCUHARTLinks(t *testing.T) {
 		t.Errorf("Faild to encode Big5 string: %A7r", err)
 	}
 
-	if SParams("caang4") != "s1=c&s2=aang&s3=4" {
-		t.Error("Faild to parse s params for: caang4")
+	expect := "s1=c&s2=aang&s3=4"
+	actual := SParams("caang4")
+	if actual != expect {
+		t.Errorf("SParams failed: expect %s, go %s", expect, actual)
+	}
+
+	expect = "s1=ng&s2=aau&s3=4"
+	actual = SParams("ngaau4")
+	if actual != expect {
+		t.Errorf("SParams failed: expect %s, go %s", expect, actual)
+	}
+
+	expect = "s1=gw&s2=ong&s3=1"
+	actual = SParams("gwong1")
+	if actual != expect {
+		t.Errorf("SParams failed: expect %s, go %s", expect, actual)
 	}
 }