Created
June 22, 2022 20:27
-
-
Save pgaskin/97eb51ad67c22cf73abc4b95903b1892 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config | |
import "bytes" | |
// bsutil implements efficient zero-allocation operations on a byte slice. | |
type bsutil []byte | |
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1} | |
// Clone returns a clone of b (but not the underlying data). | |
func (b *bsutil) Clone() *bsutil { | |
n := *b | |
return &n | |
} | |
// DeepClone returns a clone of b and the underlying data. | |
func (b *bsutil) DeepClone() *bsutil { | |
n := make(bsutil, len(*b)) | |
copy(n, *b) | |
return &n | |
} | |
// IsAscii checks if b contains only valid ASCII. | |
func (b bsutil) IsAscii() bool { | |
for _, c := range b { | |
if c > 127 { | |
return false | |
} | |
} | |
return true | |
} | |
// TrimSpace trims whitespace from the beinning and end of the string, returning | |
// whether spaces were trimmed from either end. | |
func (b *bsutil) TrimSpace() (bool, bool) { | |
i, j, n := 0, len(*b), len(*b) | |
for ; i < n; i++ { | |
if asciiSpace[(*b)[i]] == 0 { | |
break | |
} | |
} | |
for ; j > i; j-- { | |
if asciiSpace[(*b)[j-1]] == 0 { | |
break | |
} | |
} | |
*b = (*b)[i:j] | |
return i != 0, n-j > 0 | |
} | |
// PopStart pops a byte from the start, returning a zero byte if there are no | |
// more bytes to pop. | |
func (b *bsutil) PopStartZero() byte { | |
if len(*b) != 0 { | |
t := (*b)[0] | |
*b = (*b)[1:] | |
return t | |
} | |
return 0 | |
} | |
// HasPrefixByte checks if b begins with the specified byte. | |
func (b bsutil) HasPrefixByte(x byte) bool { | |
return len(b) != 0 && b[0] == x | |
} | |
// HasPrefixString checks if b begins with the provided string. | |
func (b bsutil) HasPrefixString(s string) bool { | |
return bytes.HasPrefix(b, []byte(s)) | |
} | |
// FieldCount counts the number of whitespace-delimited fields in b, ignoring | |
// leading/trailing/consecutive whitespace. | |
func (b bsutil) FieldCount() int { | |
var i, n int | |
for i < len(b) { | |
for ; i < len(b); i++ { | |
if asciiSpace[(b)[i]] == 0 { | |
n++ | |
break | |
} | |
} | |
for ; i < len(b); i++ { | |
if asciiSpace[(b)[i]] == 1 { | |
break | |
} | |
} | |
} | |
return n | |
} | |
// FieldStart trims whitespace from the beginning of b, returns the byte slice | |
// up to the next whitespace character or the end, and updates b to point at the | |
// next character. | |
func (b *bsutil) FieldStart() bsutil { | |
var i, j int | |
for i = 0; i < len(*b); i++ { | |
if asciiSpace[(*b)[i]] == 0 { | |
break | |
} | |
} | |
for j = i; j < len(*b); j++ { | |
if asciiSpace[(*b)[j]] == 1 { | |
break | |
} | |
} | |
r := (*b)[i:j] | |
*b = (*b)[j:] | |
return r | |
} | |
// FieldEnd trims whitespace from the end of b, returns the byte slice up to the | |
// next whitespace character or the beginning, and updates b to point at the | |
// next character. | |
func (b *bsutil) FieldEnd() bsutil { | |
var i, j int | |
for i = len(*b); i > 0; i-- { | |
if asciiSpace[(*b)[i-1]] == 0 { | |
break | |
} | |
} | |
for j = i; j > 0; j-- { | |
if asciiSpace[(*b)[j-1]] == 1 { | |
break | |
} | |
} | |
r := (*b)[j:i] | |
*b = (*b)[:j] | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment