Skip to content

Commit

Permalink
Merge pull request #1 from bgentry/escape-set-map
Browse files Browse the repository at this point in the history
use a map to check for escape bytes
  • Loading branch information
pauleyj authored Feb 20, 2017
2 parents c7e7a62 + dbebe5b commit e98ecc2
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ const xoff byte = 0x13
const ESCChar byte = 0x20

var (
escapeSet = [...]byte{FrameDelimiter, ESC, xon, xoff}
escapeSet = map[byte]struct{}{
FrameDelimiter: {},
ESC: {},
xon: {},
xoff: {},
}
// ErrChecksumValidation frame failed checksum validation
ErrChecksumValidation = errors.New("Frame failed checksum validation")
// ErrFrameDelimiter expecting frame start delimiter
Expand Down Expand Up @@ -56,25 +61,11 @@ const (

// ShouldEscape should this byte be escaped
func ShouldEscape(c byte) bool {
return include(escapeSet[:], c)
_, ok := escapeSet[c]
return ok
}

// Escape escape this byte
func Escape(c byte) byte {
return c ^ ESCChar
}

// index returns the first index of the target byte t, or -1 if no match is found
func index(vc []byte, c byte) int {
for i, v := range vc {
if v == c {
return i
}
}
return -1
}

// include returns true if the target byte t is in the slice.
func include(vc []byte, c byte) bool {
return index(vc, c) >= 0
}

0 comments on commit e98ecc2

Please sign in to comment.