Skip to content

Commit

Permalink
优化Unicode解码,可以处理部分Unicode编码的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
yhy0 committed May 23, 2023
1 parent fda360c commit 785a56b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestDecode(t *testing.T) {
fmt.Println(decoder.DecodeURL("Hello%2C%20%E4%B8%96%E7%95%8C%2B%21"))
fmt.Println(decoder.DecodeBase64("SGVsbG8sIOS4lueVjCE="))
fmt.Println(decoder.DecodeHex("48656c6c6f2c20e4b896e7958c21"))
fmt.Println(decoder.DecodeUnicode(`@\u006fgnl.OgnlC\u006fntext`))
}

func TestEncode(t *testing.T) {
Expand Down
13 changes: 9 additions & 4 deletions tools/decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ func DecodeUnicode(str string) string {
if r == "" {
continue
}
code, err := strconv.ParseInt(r, 16, 32)
if err != nil {
result.WriteString(r)
if len(r) >= 4 {
code, err := strconv.ParseInt(r[:4], 16, 32)
if err != nil {
result.WriteString(r)
} else {
result.WriteRune(rune(code))
result.WriteString(r[4:])
}
} else {
result.WriteRune(rune(code))
result.WriteString(r)
}
}
return result.String()
Expand Down

0 comments on commit 785a56b

Please sign in to comment.