Skip to content

Commit

Permalink
refactor(byteseq): move byteseq definition into byteseq package
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 24, 2023
1 parent 797756e commit 644d74e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
13 changes: 0 additions & 13 deletions byteseq.go

This file was deleted.

14 changes: 14 additions & 0 deletions internal/byteseq/byteseq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package byteseq

import "unicode/utf8"

type Byteseq interface {
string | []byte
}

// DecodeRuneInByteseq decodes the first UTF-8 encoded rune in val and returns the rune and its size in bytes.
func DecodeRuneInByteseq[T Byteseq](val T) (r rune, size int) {
var tmp [4]byte
n := copy(tmp[:], val)
return utf8.DecodeRune(tmp[:n])
}
6 changes: 3 additions & 3 deletions byteseq_test.go → internal/byteseq/byteseq_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jx
package byteseq

import (
"testing"
"unicode/utf8"
)

func Benchmark_decodeRuneInByteseq(b *testing.B) {
func BenchmarkDecodeRuneInByteseq(b *testing.B) {
var (
buf [4]byte
result rune
Expand All @@ -16,7 +16,7 @@ func Benchmark_decodeRuneInByteseq(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
result, _ = decodeRuneInByteseq(buf[:])
result, _ = DecodeRuneInByteseq(buf[:])
}

if result != 'ж' {
Expand Down
16 changes: 10 additions & 6 deletions w_str_escape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package jx

import "unicode/utf8"
import (
"unicode/utf8"

"github.com/go-faster/jx/internal/byteseq"
)

// Str encodes string without html escaping.
//
Expand All @@ -18,7 +22,7 @@ func (w *Writer) ByteStr(v []byte) {
writeStr(w, v)
}

func writeStr[T byteseq](w *Writer, v T) {
func writeStr[T byteseq.Byteseq](w *Writer, v T) {
w.Buf = append(w.Buf, '"')

// Fast path, without utf8 and escape support.
Expand All @@ -42,7 +46,7 @@ slow:
strSlow[T](w, v[i:])
}

func strSlow[T byteseq](w *Writer, v T) {
func strSlow[T byteseq.Byteseq](w *Writer, v T) {
var i, start int
// for the remaining parts, we process them char by char
for i < len(v) {
Expand Down Expand Up @@ -92,7 +96,7 @@ func (w *Writer) ByteStrEscape(v []byte) {
strEscape(w, v)
}

func strEscape[T byteseq](w *Writer, v T) {
func strEscape[T byteseq.Byteseq](w *Writer, v T) {
length := len(v)
w.Buf = append(w.Buf, '"')
// Fast path, probably does not require escaping.
Expand All @@ -111,7 +115,7 @@ func strEscape[T byteseq](w *Writer, v T) {
strEscapeSlow[T](w, i, v, length)
}

func strEscapeSlow[T byteseq](w *Writer, i int, v T, valLen int) {
func strEscapeSlow[T byteseq.Byteseq](w *Writer, i int, v T, valLen int) {
start := i
// for the remaining parts, we process them char by char
for i < valLen {
Expand Down Expand Up @@ -145,7 +149,7 @@ func strEscapeSlow[T byteseq](w *Writer, i int, v T, valLen int) {
start = i
continue
}
c, size := decodeRuneInByteseq(v[i:])
c, size := byteseq.DecodeRuneInByteseq(v[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
w.Buf = append(w.Buf, v[start:i]...)
Expand Down

0 comments on commit 644d74e

Please sign in to comment.