perf: optimized string viewing logic

This commit is contained in:
tiny-craft 2023-10-05 23:09:35 +08:00
parent ec7a7f18e9
commit 7937b5b0f8
3 changed files with 69 additions and 9 deletions

View File

@ -10,7 +10,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log" "strconv"
"strings" "strings"
"tinyrdm/backend/types" "tinyrdm/backend/types"
) )
@ -54,7 +54,6 @@ func ConvertTo(str, targetType string) (value, resultType string) {
case types.HEX: case types.HEX:
if hexStr, ok := decodeHex(str); ok { if hexStr, ok := decodeHex(str); ok {
log.Print(hexStr)
value = hexStr value = hexStr
} else { } else {
value = str value = str
@ -63,11 +62,11 @@ func ConvertTo(str, targetType string) (value, resultType string) {
return return
case types.BINARY: case types.BINARY:
var binary strings.Builder if binStr, ok := decodeBinary(str); ok {
for _, char := range str { value = binStr
binary.WriteString(fmt.Sprintf("%08b", int(char))) } else {
value = str
} }
value = binary.String()
resultType = targetType resultType = targetType
return return
@ -114,6 +113,10 @@ func autoToType(str string) (value, resultType string) {
} }
if value, ok = decodeGZip(str); ok { if value, ok = decodeGZip(str); ok {
if value, ok = decodeJson(value); ok {
resultType = types.GZIP_JSON
return
}
resultType = types.GZIP resultType = types.GZIP
return return
} }
@ -122,6 +125,13 @@ func autoToType(str string) (value, resultType string) {
resultType = types.DEFLATE resultType = types.DEFLATE
return return
} }
if isBinary(str) {
if value, ok = decodeHex(str); ok {
resultType = types.HEX
return
}
}
} }
value = str value = str
@ -129,6 +139,15 @@ func autoToType(str string) (value, resultType string) {
return return
} }
func isBinary(str string) bool {
for _, s := range str {
if s < 0x20 || s > 0x7E {
return true
}
}
return false
}
func decodeJson(str string) (string, bool) { func decodeJson(str string) (string, bool) {
var data any var data any
if (strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}")) || if (strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}")) ||
@ -150,6 +169,14 @@ func decodeBase64(str string) (string, bool) {
return str, false return str, false
} }
func decodeBinary(str string) (string, bool) {
var binary strings.Builder
for _, char := range str {
binary.WriteString(fmt.Sprintf("%08b", int(char)))
}
return binary.String(), true
}
func decodeHex(str string) (string, bool) { func decodeHex(str string) (string, bool) {
encodeStr := hex.EncodeToString([]byte(str)) encodeStr := hex.EncodeToString([]byte(str))
var resultStr strings.Builder var resultStr strings.Builder
@ -189,12 +216,24 @@ func SaveAs(str, targetType string) (value string, err error) {
base64Str, _ := encodeBase64(str) base64Str, _ := encodeBase64(str)
return base64Str, nil return base64Str, nil
case types.HEX:
hexStr, _ := encodeHex(str)
return hexStr, nil
case types.BINARY:
binStr, _ := encodeBinary(str)
return binStr, nil
case types.JSON, types.BASE64_JSON, types.GZIP_JSON: case types.JSON, types.BASE64_JSON, types.GZIP_JSON:
if jsonStr, ok := encodeJson(str); ok { if jsonStr, ok := encodeJson(str); ok {
if targetType == types.BASE64_JSON { switch targetType {
case types.BASE64_JSON:
base64Str, _ := encodeBase64(jsonStr) base64Str, _ := encodeBase64(jsonStr)
return base64Str, nil return base64Str, nil
} else { case types.GZIP_JSON:
gzipStr, _ := encodeGZip(jsonStr)
return gzipStr, nil
default:
return jsonStr, nil return jsonStr, nil
} }
} else { } else {
@ -236,6 +275,26 @@ func encodeBase64(str string) (string, bool) {
return base64.StdEncoding.EncodeToString([]byte(str)), true return base64.StdEncoding.EncodeToString([]byte(str)), true
} }
func encodeBinary(str string) (string, bool) {
var result strings.Builder
total := len(str)
for i := 0; i < total; i += 8 {
b, _ := strconv.ParseInt(str[i:i+8], 2, 8)
result.WriteByte(byte(b))
}
return result.String(), true
}
func encodeHex(str string) (string, bool) {
hexStrArr := strings.Split(str, "\\x")
hexStr := strings.Join(hexStrArr, "")
if decodeStr, err := hex.DecodeString(hexStr); err == nil {
return string(decodeStr), true
}
return str, false
}
func encodeGZip(str string) (string, bool) { func encodeGZip(str string) (string, bool) {
var compress = func(b []byte) (string, error) { var compress = func(b []byte) (string, error) {
var buf bytes.Buffer var buf bytes.Buffer

View File

@ -66,6 +66,7 @@ const viewLanguage = computed(() => {
switch (props.viewAs) { switch (props.viewAs) {
case types.JSON: case types.JSON:
case types.BASE64_JSON: case types.BASE64_JSON:
case types.GZIP_JSON:
return 'json' return 'json'
default: default:
return 'plaintext' return 'plaintext'

View File

@ -232,7 +232,7 @@ const usePreferencesStore = defineStore('preferences', {
}, },
setAsideWidth(width) { setAsideWidth(width) {
this.behavior.asideWidth = width this.behavior.asideWidth = Math.max(width, 300)
}, },
async checkForUpdate(manual = false) { async checkForUpdate(manual = false) {