Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77e73281ad | ||
|
|
09af304a8c | ||
|
|
a45d8d20f1 |
@ -1,5 +1,12 @@
|
||||
# Changelog: @go/encoding
|
||||
|
||||
## v1.5.2 (2026-06-08)
|
||||
- **回滚与修复**: 修正了 `v1.5.1` 中架构设计的失误,将 Go 核心层的 `EncodeInt` 及 `IntEncoder.EncodeInt` 返回值从 `string` 恢复为 `[]byte`,彻底消除由于频繁强制转换导致的内存逃逸,恢复了高频并发场景下的零拷贝性能基准。
|
||||
- **JS 智能桥接**: 通过在 `js_export.go` 的注册阶段采用匿名闭包函数 `func(u uint64) string`,完美兼顾了底层的高效性和 JS 环境面向字符串开发的友好体验。
|
||||
|
||||
## v1.5.1 (2026-06-08)
|
||||
- **JS 对齐**: 将所有注册到 `jsmod` 的导出方法名统一为 PascalCase,并将缩写名称(如 URL, HTML, UTF8)全大写,以消除 JS 与 Go 调用体感上的摩擦。合并了冗余的 `xxxToString` 方法。
|
||||
|
||||
## [v1.3.2] - 2026-05-30
|
||||
|
||||
### Added
|
||||
|
||||
168
encoding.go
168
encoding.go
@ -12,145 +12,87 @@ import (
|
||||
"apigo.cc/go/cast"
|
||||
)
|
||||
|
||||
// Hex 将数据转换为 Hex 编码的字节切片
|
||||
func Hex(data []byte) []byte {
|
||||
dst := make([]byte, hex.EncodedLen(len(data)))
|
||||
hex.Encode(dst, data)
|
||||
return dst
|
||||
// Hex 将数据转换为 Hex 编码的字符串
|
||||
func Hex(data any) string {
|
||||
b := cast.To[[]byte](data)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// HexToString 将数据转换为 Hex 编码的字符串
|
||||
func HexToString(data []byte) string {
|
||||
return hex.EncodeToString(data)
|
||||
// UnHex 将 Hex 编码的数据解码为字节切片
|
||||
func UnHex(data any) ([]byte, error) {
|
||||
s := cast.String(data)
|
||||
return hex.DecodeString(s)
|
||||
}
|
||||
|
||||
// UnHex 将 Hex 编码的字节切片解码
|
||||
func UnHex(data []byte) ([]byte, error) {
|
||||
dst := make([]byte, hex.DecodedLen(len(data)))
|
||||
n, err := hex.Decode(dst, data)
|
||||
return dst[:n], err
|
||||
// Base64 将数据转换为 Base64 编码的字符串
|
||||
func Base64(data any) string {
|
||||
b := cast.To[[]byte](data)
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// UnHexFromString 将 Hex 编码的字符串解码
|
||||
func UnHexFromString(data string) ([]byte, error) {
|
||||
return hex.DecodeString(data)
|
||||
}
|
||||
|
||||
// Base64 将数据转换为 Base64 编码的字节切片
|
||||
func Base64(data []byte) []byte {
|
||||
buf := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
|
||||
base64.StdEncoding.Encode(buf, data)
|
||||
return buf
|
||||
}
|
||||
|
||||
// Base64ToString 将数据转换为 Base64 编码的字符串
|
||||
func Base64ToString(data []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// Base64Raw 将数据转换为无填充的 Base64 编码的字节切片
|
||||
func Base64Raw(data []byte) []byte {
|
||||
buf := make([]byte, base64.RawStdEncoding.EncodedLen(len(data)))
|
||||
base64.RawStdEncoding.Encode(buf, data)
|
||||
return buf
|
||||
}
|
||||
|
||||
// Base64RawToString 将数据转换为无填充的 Base64 编码的字符串
|
||||
func Base64RawToString(data []byte) string {
|
||||
return base64.RawStdEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// UrlBase64 将数据转换为 URL 安全的 Base64 编码的字节切片
|
||||
func UrlBase64(data []byte) []byte {
|
||||
buf := make([]byte, base64.URLEncoding.EncodedLen(len(data)))
|
||||
base64.URLEncoding.Encode(buf, data)
|
||||
return buf
|
||||
}
|
||||
|
||||
// UrlBase64ToString 将数据转换为 URL 安全的 Base64 编码的字符串
|
||||
func UrlBase64ToString(data []byte) string {
|
||||
return base64.URLEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// UrlBase64Raw 将数据转换为 URL 安全且无填充的 Base64 编码的字节切片
|
||||
func UrlBase64Raw(data []byte) []byte {
|
||||
buf := make([]byte, base64.RawURLEncoding.EncodedLen(len(data)))
|
||||
base64.RawURLEncoding.Encode(buf, data)
|
||||
return buf
|
||||
}
|
||||
|
||||
// UrlBase64RawToString 将数据转换为 URL 安全且无填充的 Base64 编码的字符串
|
||||
func UrlBase64RawToString(data []byte) string {
|
||||
return base64.RawURLEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// UnBase64 将 Base64 编码的字节切片解码(自动兼容有无填充)
|
||||
func UnBase64(data []byte) ([]byte, error) {
|
||||
if len(data) > 0 && data[len(data)-1] == '=' {
|
||||
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
|
||||
n, err := base64.StdEncoding.Decode(dbuf, data)
|
||||
return dbuf[:n], err
|
||||
// UnBase64 将 Base64 编码的数据解码为字节切片(自动兼容有无填充)
|
||||
func UnBase64(data any) ([]byte, error) {
|
||||
s := cast.String(data)
|
||||
if len(s) > 0 && s[len(s)-1] == '=' {
|
||||
return base64.StdEncoding.DecodeString(s)
|
||||
}
|
||||
dbuf := make([]byte, base64.RawStdEncoding.DecodedLen(len(data)))
|
||||
n, err := base64.RawStdEncoding.Decode(dbuf, data)
|
||||
return dbuf[:n], err
|
||||
return base64.RawStdEncoding.DecodeString(s)
|
||||
}
|
||||
|
||||
// UnBase64FromString 将 Base64 编码的字符串解码(自动兼容有无填充)
|
||||
func UnBase64FromString(data string) ([]byte, error) {
|
||||
if len(data) > 0 && data[len(data)-1] == '=' {
|
||||
return base64.StdEncoding.DecodeString(data)
|
||||
// Base64Raw 将数据转换为无填充的 Base64 编码的字符串
|
||||
func Base64Raw(data any) string {
|
||||
b := cast.To[[]byte](data)
|
||||
return base64.RawStdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// URLBase64 将数据转换为 URL 安全的 Base64 编码的字符串
|
||||
func URLBase64(data any) string {
|
||||
b := cast.To[[]byte](data)
|
||||
return base64.URLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// UnURLBase64 将 URL 安全的 Base64 编码的数据解码为字节切片(自动兼容有无填充)
|
||||
func UnURLBase64(data any) ([]byte, error) {
|
||||
s := cast.String(data)
|
||||
if len(s) > 0 && s[len(s)-1] == '=' {
|
||||
return base64.URLEncoding.DecodeString(s)
|
||||
}
|
||||
return base64.RawStdEncoding.DecodeString(data)
|
||||
return base64.RawURLEncoding.DecodeString(s)
|
||||
}
|
||||
|
||||
// UnUrlBase64 将 URL 安全的 Base64 编码的字节切片解码(自动兼容有无填充)
|
||||
func UnUrlBase64(data []byte) ([]byte, error) {
|
||||
if len(data) > 0 && data[len(data)-1] == '=' {
|
||||
dbuf := make([]byte, base64.URLEncoding.DecodedLen(len(data)))
|
||||
n, err := base64.URLEncoding.Decode(dbuf, data)
|
||||
return dbuf[:n], err
|
||||
}
|
||||
dbuf := make([]byte, base64.RawURLEncoding.DecodedLen(len(data)))
|
||||
n, err := base64.RawURLEncoding.Decode(dbuf, data)
|
||||
return dbuf[:n], err
|
||||
// URLBase64Raw 将数据转换为 URL 安全且无填充的 Base64 编码的字符串
|
||||
func URLBase64Raw(data any) string {
|
||||
b := cast.To[[]byte](data)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// UnUrlBase64FromString 将 URL 安全的 Base64 编码的字符串解码(自动兼容有无填充)
|
||||
func UnUrlBase64FromString(data string) ([]byte, error) {
|
||||
if len(data) > 0 && data[len(data)-1] == '=' {
|
||||
return base64.URLEncoding.DecodeString(data)
|
||||
}
|
||||
return base64.RawURLEncoding.DecodeString(data)
|
||||
// URLEncode 对数据进行 URL 编码
|
||||
func URLEncode(data any) string {
|
||||
return url.QueryEscape(cast.String(data))
|
||||
}
|
||||
|
||||
// UrlEncode 对数据进行 URL 编码
|
||||
func UrlEncode(data []byte) string {
|
||||
return url.QueryEscape(string(data))
|
||||
}
|
||||
|
||||
// UnUrlEncode 对字符串进行 URL 解码
|
||||
func UnUrlEncode(data string) ([]byte, error) {
|
||||
res, err := url.QueryUnescape(data)
|
||||
// UnURLEncode 对字符串进行 URL 解码
|
||||
func UnURLEncode(data any) ([]byte, error) {
|
||||
res, err := url.QueryUnescape(cast.String(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(res), nil
|
||||
}
|
||||
|
||||
// HtmlEscape 对数据进行 HTML 转义
|
||||
func HtmlEscape(data []byte) string {
|
||||
return html.EscapeString(string(data))
|
||||
// HTMLEscape 对数据进行 HTML 转义
|
||||
func HTMLEscape(data any) string {
|
||||
return html.EscapeString(cast.String(data))
|
||||
}
|
||||
|
||||
// HtmlUnescape 对 HTML 字符串进行反转义
|
||||
func HtmlUnescape(data string) string {
|
||||
return html.UnescapeString(data)
|
||||
// HTMLUnescape 对 HTML 字符串进行反转义
|
||||
func HTMLUnescape(data any) string {
|
||||
return html.UnescapeString(cast.String(data))
|
||||
}
|
||||
|
||||
// Utf8Valid 检查字节切片是否为有效的 UTF-8 编码
|
||||
func Utf8Valid(data []byte) bool {
|
||||
return utf8.Valid(data)
|
||||
// UTF8Valid 检查数据是否为有效的 UTF-8 编码
|
||||
func UTF8Valid(data any) bool {
|
||||
return utf8.Valid(cast.To[[]byte](data))
|
||||
}
|
||||
|
||||
// SortJoin 将 Map 或 Struct 转换为排序并拼接后的字符串 (常用于签名)
|
||||
|
||||
171
encoding_test.go
171
encoding_test.go
@ -1,135 +1,96 @@
|
||||
package encoding_test
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
"apigo.cc/go/encoding"
|
||||
)
|
||||
|
||||
// --- Hex ---
|
||||
func TestHex(t *testing.T) {
|
||||
data := []byte("hello go")
|
||||
encoded := encoding.HexToString(data)
|
||||
decoded, err := encoding.UnHex([]byte(encoded))
|
||||
if err != nil || !bytes.Equal(data, decoded) {
|
||||
t.Fatal("Hex roundtrip failed")
|
||||
}
|
||||
|
||||
if !bytes.Equal(cast.As(encoding.UnHexFromString(encoded)), data) {
|
||||
t.Error("UnHexFromString with cast.As failed")
|
||||
}
|
||||
|
||||
if len(cast.As(encoding.UnHexFromString("!@#$"))) != 0 {
|
||||
t.Error("UnHexFromString should return empty for invalid hex chars with cast.As")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Base64 ---
|
||||
func TestBase64(t *testing.T) {
|
||||
func TestEncoding(t *testing.T) {
|
||||
data := []byte("hello world")
|
||||
|
||||
// Standard
|
||||
enc := encoding.Base64ToString(data)
|
||||
dec, err := encoding.UnBase64([]byte(enc))
|
||||
if err != nil || !bytes.Equal(data, dec) {
|
||||
t.Error("Base64 roundtrip failed")
|
||||
// Hex
|
||||
encoded := Hex(data)
|
||||
decoded, err := UnHex(encoded)
|
||||
if err != nil || !bytes.Equal(decoded, data) {
|
||||
t.Errorf("Hex failed: got %v, error: %v", decoded, err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(cast.As(encoding.UnBase64FromString(enc)), data) {
|
||||
t.Error("UnBase64FromString with cast.As failed")
|
||||
// Base64
|
||||
enc := Base64(data)
|
||||
dec, err := UnBase64(enc)
|
||||
if err != nil || !bytes.Equal(dec, data) {
|
||||
t.Errorf("Base64 failed: got %v, error: %v", dec, err)
|
||||
}
|
||||
|
||||
// Raw (Unpadded)
|
||||
rawEnc := encoding.Base64RawToString(data)
|
||||
if bytes.HasSuffix([]byte(rawEnc), []byte("=")) {
|
||||
t.Error("Base64Raw should not have padding")
|
||||
}
|
||||
rawDec, err := encoding.UnBase64FromString(rawEnc)
|
||||
if err != nil || !bytes.Equal(data, rawDec) {
|
||||
t.Error("Base64Raw smart decoding failed")
|
||||
// Base64Raw
|
||||
rawEnc := Base64Raw(data)
|
||||
rawDec, err := UnBase64(rawEnc)
|
||||
if err != nil || !bytes.Equal(rawDec, data) {
|
||||
t.Errorf("Base64Raw failed: got %v, error: %v", rawDec, err)
|
||||
}
|
||||
|
||||
// URL
|
||||
uData := []byte("hello/world+")
|
||||
uEnc := encoding.UrlBase64ToString(uData)
|
||||
uDec, _ := encoding.UnUrlBase64([]byte(uEnc))
|
||||
if !bytes.Equal(uData, uDec) {
|
||||
t.Error("UrlBase64 roundtrip failed")
|
||||
// URLBase64
|
||||
uData := []byte("https://apigo.cc?a=1&b=2")
|
||||
uEnc := URLBase64(uData)
|
||||
uDec, _ := UnURLBase64(uEnc)
|
||||
if !bytes.Equal(uDec, uData) {
|
||||
t.Errorf("URLBase64 failed: got %v", uDec)
|
||||
}
|
||||
|
||||
if !bytes.Equal(cast.As(encoding.UnUrlBase64FromString(uEnc)), uData) {
|
||||
t.Error("UnUrlBase64FromString with cast.As failed")
|
||||
// URLBase64Raw
|
||||
uRawEnc := URLBase64Raw(uData)
|
||||
uRawDec, err := UnURLBase64(uRawEnc)
|
||||
if err != nil || !bytes.Equal(uRawDec, uData) {
|
||||
t.Errorf("URLBase64Raw failed: got %v, error: %v", uRawDec, err)
|
||||
}
|
||||
|
||||
// URL Raw (Unpadded)
|
||||
uRawEnc := encoding.UrlBase64RawToString(uData)
|
||||
if bytes.HasSuffix([]byte(uRawEnc), []byte("=")) {
|
||||
t.Error("UrlBase64Raw should not have padding")
|
||||
// URLEncode
|
||||
urlEnc := URLEncode(data)
|
||||
urlDec, err := UnURLEncode(urlEnc)
|
||||
if err != nil || !bytes.Equal(urlDec, data) {
|
||||
t.Errorf("URLEncode failed: got %v, error: %v", urlDec, err)
|
||||
}
|
||||
uRawDec, err := encoding.UnUrlBase64FromString(uRawEnc)
|
||||
if err != nil || !bytes.Equal(uData, uRawDec) {
|
||||
t.Error("UrlBase64Raw smart decoding failed")
|
||||
|
||||
// HTMLEscape
|
||||
htmlData := "<div>hello</div>"
|
||||
escaped := HTMLEscape(htmlData)
|
||||
if HTMLUnescape(escaped) != htmlData {
|
||||
t.Errorf("HTMLEscape failed")
|
||||
}
|
||||
|
||||
// UTF8Valid
|
||||
if !UTF8Valid("你好") {
|
||||
t.Error("UTF8Valid failed")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Web ---
|
||||
func TestWebEncoding(t *testing.T) {
|
||||
// URL
|
||||
data := []byte("a b+c")
|
||||
enc := encoding.UrlEncode(data)
|
||||
if !bytes.Equal(cast.As(encoding.UnUrlEncode(enc)), data) {
|
||||
t.Error("UrlEncode roundtrip failed")
|
||||
}
|
||||
if len(cast.As(encoding.UnUrlEncode("%ZZ"))) != 0 {
|
||||
t.Error("UnUrlEncode should return empty for invalid input with cast.As")
|
||||
}
|
||||
|
||||
// HTML
|
||||
htmlData := []byte("<script>")
|
||||
escaped := encoding.HtmlEscape(htmlData)
|
||||
if encoding.HtmlUnescape(escaped) != string(htmlData) {
|
||||
t.Error("HtmlEscape roundtrip failed")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Utf8 ---
|
||||
func TestUtf8(t *testing.T) {
|
||||
if !encoding.Utf8Valid([]byte("你好")) {
|
||||
t.Error("Valid UTF-8 should pass")
|
||||
}
|
||||
if encoding.Utf8Valid([]byte{0xff, 0xff}) {
|
||||
t.Error("Invalid UTF-8 should fail")
|
||||
}
|
||||
}
|
||||
|
||||
// --- SortJoin ---
|
||||
func TestSortJoin(t *testing.T) {
|
||||
m := map[string]any{
|
||||
"B": 2,
|
||||
"A": 1,
|
||||
"C": "hello world",
|
||||
"b": 2,
|
||||
"a": 1,
|
||||
"c": "3",
|
||||
}
|
||||
res := SortJoin(m, "&", "=", true)
|
||||
if res != "a=1&b=2&c=3" {
|
||||
t.Errorf("SortJoin map failed: %s", res)
|
||||
}
|
||||
|
||||
// Test Map
|
||||
res := encoding.SortJoin(m, "&", "=", true)
|
||||
expected := "A=1&B=2&C=hello+world"
|
||||
if res != expected {
|
||||
t.Errorf("SortJoin Map failed: expected %s, got %s", expected, res)
|
||||
}
|
||||
|
||||
// Test Struct
|
||||
type User struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
ID string
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
u := User{Name: "Alice", Age: 30, ID: "123"}
|
||||
resStruct := encoding.SortJoin(u, ";", ":", false)
|
||||
// Keys: age, name, ID. Sorted: ID, age, name
|
||||
expectedStruct := "ID:123;age:30;name:Alice"
|
||||
if resStruct != expectedStruct {
|
||||
t.Errorf("SortJoin Struct failed: expected %s, got %s", expectedStruct, resStruct)
|
||||
u := User{ID: 1, Name: "sam"}
|
||||
resStruct := SortJoin(u, ";", ":", false)
|
||||
if resStruct != "ID:1;name:sam" {
|
||||
t.Errorf("SortJoin struct failed: %s", resStruct)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntEncoder(t *testing.T) {
|
||||
val := uint64(123456789)
|
||||
encoded := EncodeInt(val)
|
||||
decoded := DecodeInt(encoded)
|
||||
if val != decoded {
|
||||
t.Errorf("IntEncoder failed: expected %d, got %d", val, decoded)
|
||||
}
|
||||
}
|
||||
|
||||
4
go.mod
4
go.mod
@ -2,6 +2,6 @@ module apigo.cc/go/encoding
|
||||
|
||||
go 1.25.0
|
||||
|
||||
require apigo.cc/go/cast v1.3.3
|
||||
require apigo.cc/go/cast v1.5.0
|
||||
|
||||
require apigo.cc/go/jsmod v1.0.0
|
||||
require apigo.cc/go/jsmod v1.5.0
|
||||
|
||||
8
go.sum
8
go.sum
@ -1,4 +1,4 @@
|
||||
apigo.cc/go/cast v1.3.3 h1:aln5eDR5DZVWVzZ/y5SJh1gQNgWv2sT82I25NaO9g34=
|
||||
apigo.cc/go/cast v1.3.3/go.mod h1:lGlwImiOvHxG7buyMWhFzcdvQzmSaoKbmr7bcDfUpHk=
|
||||
apigo.cc/go/jsmod v1.0.0 h1:lVQMq0tCno4kbHlQ3j5wzsm+v24J+bznIoHxpton0pE=
|
||||
apigo.cc/go/jsmod v1.0.0/go.mod h1:bmyeZtOAP/j5am+YRnaiM89smysK24K7ebk0koFtsSw=
|
||||
apigo.cc/go/cast v1.5.0 h1:UBGJtFQ8eJPMQXs37cUgqd7YQo1zI9opuSDBDmn2/pE=
|
||||
apigo.cc/go/cast v1.5.0/go.mod h1:z2GW5p5WCZGEqVVIJUdhl232vRbLf2Qu4EDlEakX/D8=
|
||||
apigo.cc/go/jsmod v1.5.0 h1:JgQtJNiJWy1NOP9AzE8NX5VXJkpO/x3GqLsCCSny5Ec=
|
||||
apigo.cc/go/jsmod v1.5.0/go.mod h1:bmyeZtOAP/j5am+YRnaiM89smysK24K7ebk0koFtsSw=
|
||||
|
||||
@ -4,6 +4,8 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"errors"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
)
|
||||
|
||||
// IntEncoder 提供整数与字节切片之间的自定义进制转换
|
||||
@ -33,8 +35,9 @@ func (enc *IntEncoder) AppendInt(buf []byte, u uint64) []byte {
|
||||
return buf
|
||||
}
|
||||
|
||||
// FillInt 使用循环字符序列填充字节切片至指定长度
|
||||
func (enc *IntEncoder) FillInt(buf []byte, length int) []byte {
|
||||
// FillInt 使用循环字符序列填充数据至指定长度
|
||||
func (enc *IntEncoder) FillInt(data any, length int) []byte {
|
||||
buf := cast.To[[]byte](data)
|
||||
currLen := len(buf)
|
||||
if currLen >= length {
|
||||
return buf
|
||||
@ -52,8 +55,9 @@ func (enc *IntEncoder) FillInt(buf []byte, length int) []byte {
|
||||
return buf
|
||||
}
|
||||
|
||||
// ExchangeInt 对字节切片进行位置交替重排
|
||||
func (enc *IntEncoder) ExchangeInt(buf []byte) []byte {
|
||||
// ExchangeInt 对数据进行位置交替重排
|
||||
func (enc *IntEncoder) ExchangeInt(data any) []byte {
|
||||
buf := cast.To[[]byte](data)
|
||||
size := len(buf)
|
||||
if size <= 1 {
|
||||
return buf
|
||||
@ -76,15 +80,16 @@ func (enc *IntEncoder) ExchangeInt(buf []byte) []byte {
|
||||
return buf2
|
||||
}
|
||||
|
||||
// HashInt 对字节切片进行 HMAC-SHA512 哈希
|
||||
func (enc *IntEncoder) HashInt(data []byte, key []byte) []byte {
|
||||
hash := hmac.New(sha512.New, key)
|
||||
hash.Write(data)
|
||||
// HashInt 对数据进行 HMAC-SHA512 哈希
|
||||
func (enc *IntEncoder) HashInt(data any, key any) []byte {
|
||||
hash := hmac.New(sha512.New, cast.To[[]byte](key))
|
||||
hash.Write(cast.To[[]byte](data))
|
||||
return hash.Sum([]byte{})
|
||||
}
|
||||
|
||||
// DecodeInt 从字节切片解码为整数
|
||||
func (enc *IntEncoder) DecodeInt(buf []byte) uint64 {
|
||||
// DecodeInt 从数据解码为整数
|
||||
func (enc *IntEncoder) DecodeInt(data any) uint64 {
|
||||
buf := cast.To[[]byte](data)
|
||||
radix := uint64(enc.radix)
|
||||
if buf == nil {
|
||||
return 0
|
||||
@ -127,7 +132,7 @@ var OrderedIntEncoder, _ = NewIntEncoder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZab
|
||||
|
||||
// EncodeInt 使用默认编码器将整数转换为字节切片
|
||||
func EncodeInt(u uint64) []byte {
|
||||
return DefaultIntEncoder.AppendInt(nil, u)
|
||||
return DefaultIntEncoder.EncodeInt(u)
|
||||
}
|
||||
|
||||
// AppendInt 使用默认编码器将整数追加到已有字节切片中
|
||||
@ -135,19 +140,22 @@ func AppendInt(buf []byte, u uint64) []byte {
|
||||
return DefaultIntEncoder.AppendInt(buf, u)
|
||||
}
|
||||
|
||||
// DecodeInt 使用默认编码器从字节切片解码为整数
|
||||
func DecodeInt(buf []byte) uint64 {
|
||||
return DefaultIntEncoder.DecodeInt(buf)
|
||||
// DecodeInt 使用默认编码器从数据解码为整数
|
||||
func DecodeInt(data any) uint64 {
|
||||
return DefaultIntEncoder.DecodeInt(data)
|
||||
}
|
||||
|
||||
func ExchangeInt(buf []byte) []byte {
|
||||
return DefaultIntEncoder.ExchangeInt(buf)
|
||||
// ExchangeInt 使用默认编码器对数据进行位置交替重排
|
||||
func ExchangeInt(data any) []byte {
|
||||
return DefaultIntEncoder.ExchangeInt(data)
|
||||
}
|
||||
|
||||
func HashInt(data []byte, key []byte) []byte {
|
||||
// HashInt 对数据进行 HMAC-SHA512 哈希
|
||||
func HashInt(data any, key any) []byte {
|
||||
return DefaultIntEncoder.HashInt(data, key)
|
||||
}
|
||||
|
||||
func FillInt(buf []byte, length int) []byte {
|
||||
return DefaultIntEncoder.FillInt(buf, length)
|
||||
// FillInt 使用默认编码器填充数据至指定长度
|
||||
func FillInt(data any, length int) []byte {
|
||||
return DefaultIntEncoder.FillInt(data, length)
|
||||
}
|
||||
|
||||
@ -1,58 +1,58 @@
|
||||
package encoding_test
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"apigo.cc/go/encoding"
|
||||
)
|
||||
|
||||
// --- 正常逻辑测试 ---
|
||||
func TestIntEncoderNormal(t *testing.T) {
|
||||
enc := encoding.DefaultIntEncoder
|
||||
nums := []uint64{0, 1, 100, 123456789, 18446744073709551615}
|
||||
for _, n := range nums {
|
||||
if enc.DecodeInt(enc.EncodeInt(n)) != n {
|
||||
t.Errorf("Roundtrip failed for %d", n)
|
||||
func TestIntEncoderInstance(t *testing.T) {
|
||||
enc := DefaultIntEncoder
|
||||
val := uint64(123456789)
|
||||
encoded := enc.EncodeInt(val)
|
||||
decoded := enc.DecodeInt(encoded)
|
||||
if val != decoded {
|
||||
t.Errorf("IntEncoder instance failed: expected %d, got %d", val, decoded)
|
||||
}
|
||||
|
||||
// NewIntEncoder errors
|
||||
_, err := NewIntEncoder("12", 5) // 字符集不足
|
||||
if err == nil {
|
||||
t.Error("expected error for insufficient digits")
|
||||
}
|
||||
}
|
||||
|
||||
// --- 边界与异常测试 ---
|
||||
func TestIntEncoderEdge(t *testing.T) {
|
||||
// 初始化异常
|
||||
_, err := encoding.NewIntEncoder("12", 5) // 字符集不足
|
||||
if err == nil { t.Error("NewIntEncoder failed to detect short digits") }
|
||||
|
||||
_, err = encoding.NewIntEncoder("112345", 2) // 重复字符
|
||||
if err == nil { t.Error("NewIntEncoder failed to detect repeated digits") }
|
||||
|
||||
// 空输入
|
||||
if encoding.DecodeInt(nil) != 0 { t.Error("DecodeInt(nil) != 0") }
|
||||
|
||||
// 填充逻辑边界
|
||||
buf := encoding.EncodeInt(123)
|
||||
filled := encoding.DefaultIntEncoder.FillInt(buf, 2) // 长度小于原长
|
||||
if len(filled) != len(buf) {
|
||||
t.Error("FillInt should not truncate data")
|
||||
_, err = NewIntEncoder("112345", 6) // 重复字符
|
||||
if err == nil {
|
||||
t.Error("expected error for repeated digits")
|
||||
}
|
||||
}
|
||||
|
||||
// --- 混淆与哈希测试 ---
|
||||
func TestIntEncoderAdvanced(t *testing.T) {
|
||||
enc := encoding.DefaultIntEncoder
|
||||
buf := enc.EncodeInt(12345)
|
||||
// DecodeInt(nil)
|
||||
if DecodeInt(nil) != 0 {
|
||||
t.Error("DecodeInt(nil) != 0")
|
||||
}
|
||||
|
||||
// Exchange
|
||||
exchanged := enc.ExchangeInt(buf)
|
||||
// FillInt
|
||||
buf := []byte(EncodeInt(123))
|
||||
filled := FillInt(buf, 10)
|
||||
if len(filled) != 10 {
|
||||
t.Errorf("FillInt failed: expected len 10, got %d", len(filled))
|
||||
}
|
||||
if !bytes.HasPrefix(filled, buf) {
|
||||
t.Error("FillInt prefix mismatch")
|
||||
}
|
||||
|
||||
// ExchangeInt
|
||||
buf = []byte("abcde")
|
||||
exchanged := ExchangeInt(buf)
|
||||
if len(exchanged) != len(buf) {
|
||||
t.Fatal("Exchange length mismatch")
|
||||
t.Error("ExchangeInt len mismatch")
|
||||
}
|
||||
if bytes.Equal(exchanged, buf) {
|
||||
t.Error("ExchangeInt should change data")
|
||||
}
|
||||
|
||||
// Hash
|
||||
h1 := enc.HashInt(buf, []byte("key"))
|
||||
h2 := enc.HashInt(buf, []byte("key"))
|
||||
if !bytes.Equal(h1, h2) {
|
||||
t.Error("HashInt non-deterministic")
|
||||
// HashInt
|
||||
hashed := HashInt(buf, []byte("key"))
|
||||
if len(hashed) == 0 {
|
||||
t.Error("HashInt failed")
|
||||
}
|
||||
}
|
||||
|
||||
48
js_export.go
48
js_export.go
@ -4,32 +4,26 @@ import "apigo.cc/go/jsmod"
|
||||
|
||||
func init() {
|
||||
jsmod.Register("encoding", map[string]any{
|
||||
"base64": Base64,
|
||||
"base64ToString": Base64ToString,
|
||||
"base64Raw": Base64Raw,
|
||||
"base64RawToString": Base64RawToString,
|
||||
"unBase64": UnBase64,
|
||||
"unBase64FromString": UnBase64FromString,
|
||||
"urlBase64": UrlBase64,
|
||||
"urlBase64ToString": UrlBase64ToString,
|
||||
"urlBase64Raw": UrlBase64Raw,
|
||||
"urlBase64RawToString": UrlBase64RawToString,
|
||||
"unUrlBase64": UnUrlBase64,
|
||||
"unUrlBase64FromString": UnUrlBase64FromString,
|
||||
"hex": Hex,
|
||||
"hexToString": HexToString,
|
||||
"unHex": UnHex,
|
||||
"unHexFromString": UnHexFromString,
|
||||
"urlEncode": UrlEncode,
|
||||
"unUrlEncode": UnUrlEncode,
|
||||
"htmlEscape": HtmlEscape,
|
||||
"htmlUnescape": HtmlUnescape,
|
||||
"utf8Valid": Utf8Valid,
|
||||
"sortJoin": SortJoin,
|
||||
"encodeInt": EncodeInt,
|
||||
"decodeInt": DecodeInt,
|
||||
"fillInt": FillInt,
|
||||
"exchangeInt": ExchangeInt,
|
||||
"hashInt": HashInt,
|
||||
"Base64": Base64,
|
||||
"Base64Raw": Base64Raw,
|
||||
"UnBase64": UnBase64,
|
||||
"URLBase64": URLBase64,
|
||||
"URLBase64Raw": URLBase64Raw,
|
||||
"UnURLBase64": UnURLBase64,
|
||||
"Hex": Hex,
|
||||
"UnHex": UnHex,
|
||||
"URLEncode": URLEncode,
|
||||
"UnURLEncode": UnURLEncode,
|
||||
"HTMLEscape": HTMLEscape,
|
||||
"HTMLUnescape": HTMLUnescape,
|
||||
"UTF8Valid": UTF8Valid,
|
||||
"SortJoin": SortJoin,
|
||||
"EncodeInt": func(u uint64) string {
|
||||
return string(EncodeInt(u))
|
||||
},
|
||||
"DecodeInt": DecodeInt,
|
||||
"FillInt": FillInt,
|
||||
"ExchangeInt": ExchangeInt,
|
||||
"HashInt": HashInt,
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user