diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-07-23 00:46:42 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-07-23 00:46:42 -0600 |
commit | c57cc3d4739ffa0346350c63fb4ec59a63b56e46 (patch) | |
tree | 8d22d276e869e5b816e3113def16b3defcbd538c /lib/jsonutil/hex_string.go | |
parent | 77628ce11ce3693d8ac06f1a404a1005ba05f190 (diff) | |
parent | 6e104326f81ec59ece1817988af41b70e4f4cd15 (diff) |
Merge branch 'lukeshu/json'
Diffstat (limited to 'lib/jsonutil/hex_string.go')
-rw-r--r-- | lib/jsonutil/hex_string.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/jsonutil/hex_string.go b/lib/jsonutil/hex_string.go new file mode 100644 index 0000000..3e0b154 --- /dev/null +++ b/lib/jsonutil/hex_string.go @@ -0,0 +1,86 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +// Package jsonutil provides utilities for implementing the interfaces +// consumed by the "git.lukeshu.com/go/lowmemjson" package. +package jsonutil + +import ( + "io" + + "git.lukeshu.com/go/lowmemjson" +) + +func EncodeHexString[T ~[]byte | ~string](w io.Writer, str T) error { + const hextable = "0123456789abcdef" + var buf [2]byte + buf[0] = '"' + if _, err := w.Write(buf[:1]); err != nil { + return err + } + for i := 0; i < len(str); i++ { + buf[0] = hextable[str[i]>>4] + buf[1] = hextable[str[i]&0x0f] + if _, err := w.Write(buf[:]); err != nil { + return err + } + } + buf[0] = '"' + if _, err := w.Write(buf[:1]); err != nil { + return err + } + return nil +} + +func DecodeHexString(r io.RuneScanner, dst io.ByteWriter) error { + dec := &hexDecoder{dst: dst} + if err := lowmemjson.DecodeString(r, dec); err != nil { + return err + } + return dec.Close() +} + +func EncodeSplitHexString[T ~[]byte | ~string](w io.Writer, str T, maxStrLen int) error { + if maxStrLen <= 0 || len(str) <= maxStrLen/2 { + return EncodeHexString(w, str) + } + var buf [1]byte + buf[0] = '[' + if _, err := w.Write(buf[:]); err != nil { + return err + } + for len(str) > maxStrLen/2 { + if err := EncodeHexString(w, str[:maxStrLen/2]); err != nil { + return err + } + str = str[maxStrLen/2:] + if len(str) > 0 { + buf[0] = ',' + if _, err := w.Write(buf[:]); err != nil { + return err + } + } + } + if len(str) > 0 { + if err := EncodeHexString(w, str); err != nil { + return err + } + } + buf[0] = ']' + if _, err := w.Write(buf[:]); err != nil { + return err + } + return nil +} + +func DecodeSplitHexString(r io.RuneScanner, dst io.ByteWriter) error { + c, _, _ := r.ReadRune() + _ = r.UnreadRune() + if c == '"' { + return DecodeHexString(r, dst) + } + return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error { + return DecodeHexString(r, dst) + }) +} |