- Introduced comprehensive benchmark tests for NIP-44 and NIP-4 encryption/decryption, including various message sizes and round-trip operations. - Implemented optimizations to reduce memory allocations and CPU processing time in encryption functions, focusing on pre-allocating buffers and minimizing reallocations. - Enhanced error handling in encryption and decryption processes to ensure robustness. - Documented performance improvements in the new PERFORMANCE_REPORT.md file, highlighting significant reductions in execution time and memory usage.
98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package text
|
|
|
|
// AppendBytesClosure is a function type for appending data from a source to a destination and
|
|
// returning the appended-to slice.
|
|
type AppendBytesClosure func(dst, src []byte) []byte
|
|
|
|
// AppendClosure is a simple append where the caller appends to the destination and returns the
|
|
// appended-to slice.
|
|
type AppendClosure func(dst []byte) []byte
|
|
|
|
// Unquote removes the quotes around a slice of bytes.
|
|
func Unquote(b []byte) []byte { return b[1 : len(b)-1] }
|
|
|
|
// Noop simply appends the source to the destination slice and returns it.
|
|
func Noop(dst, src []byte) []byte { return append(dst, src...) }
|
|
|
|
// AppendQuote appends a source of bytes, that have been processed by an AppendBytesClosure and
|
|
// returns the appended-to slice.
|
|
func AppendQuote(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '"')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, '"')
|
|
return dst
|
|
}
|
|
|
|
// Quote simply quotes a provided source and attaches it to the provided destination slice.
|
|
func Quote(dst, src []byte) []byte { return AppendQuote(dst, src, Noop) }
|
|
|
|
// AppendSingleQuote appends a provided AppendBytesClosure's output from a given source of
|
|
// bytes, wrapped in single quotes ”.
|
|
func AppendSingleQuote(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '\'')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, '\'')
|
|
return dst
|
|
}
|
|
|
|
// AppendBackticks appends a provided AppendBytesClosure's output from a given source of
|
|
// bytes, wrapped in backticks “.
|
|
func AppendBackticks(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '`')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, '`')
|
|
return dst
|
|
}
|
|
|
|
// AppendBrace appends a provided AppendBytesClosure's output from a given source of
|
|
// bytes, wrapped in braces ().
|
|
func AppendBrace(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '(')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, ')')
|
|
return dst
|
|
}
|
|
|
|
// AppendParenthesis appends a provided AppendBytesClosure's output from a given source of
|
|
// bytes, wrapped in parentheses {}.
|
|
func AppendParenthesis(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '{')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, '}')
|
|
return dst
|
|
}
|
|
|
|
// AppendBracket appends a provided AppendBytesClosure's output from a given source of
|
|
// bytes, wrapped in brackets [].
|
|
func AppendBracket(dst, src []byte, ac AppendBytesClosure) []byte {
|
|
dst = append(dst, '[')
|
|
dst = ac(dst, src)
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendList appends an input source bytes processed by an AppendBytesClosure and separates
|
|
// elements with the given separator byte.
|
|
func AppendList(
|
|
dst []byte, src [][]byte, separator byte,
|
|
ac AppendBytesClosure,
|
|
) []byte {
|
|
// Pre-allocate buffer if nil to reduce reallocations
|
|
// Estimate: sum of all source sizes + separators
|
|
if dst == nil && len(src) > 0 {
|
|
estimatedSize := len(src) - 1 // separators
|
|
for i := range src {
|
|
estimatedSize += len(src[i]) * 2 // worst case with escaping
|
|
}
|
|
dst = make([]byte, 0, estimatedSize)
|
|
}
|
|
last := len(src) - 1
|
|
for i := range src {
|
|
dst = ac(dst, src[i])
|
|
if i < last {
|
|
dst = append(dst, separator)
|
|
}
|
|
}
|
|
return dst
|
|
}
|