Updated reference split package to int64

This commit breaks package split's API to take in int64 types instead of
int. This is because CompletionEntry now uses int64 over int for
concreteness.
This commit is contained in:
diamondburned 2020-10-09 09:34:02 -07:00
parent 6140b5a131
commit 1b1e10a8a6
2 changed files with 22 additions and 22 deletions

View File

@ -12,17 +12,17 @@ var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
// SpaceIndexed returns a splitted string with the current index that // SpaceIndexed returns a splitted string with the current index that
// CompleteMessage wants. The text is the entire input string and the offset is // CompleteMessage wants. The text is the entire input string and the offset is
// where the cursor currently is. // where the cursor currently is.
func SpaceIndexed(text string, offset int) ([]string, int) { func SpaceIndexed(text string, offset int64) ([]string, int64) {
// First count the fields. // First count the fields.
// This is an exact count if s is ASCII, otherwise it is an approximation. // This is an exact count if s is ASCII, otherwise it is an approximation.
n := 0 n := int64(0)
wasSpace := 1 wasSpace := int64(1)
// setBits is used to track which bits are set in the bytes of s. // setBits is used to track which bits are set in the bytes of s.
setBits := uint8(0) setBits := uint8(0)
for i := 0; i < len(text); i++ { for i := 0; i < len(text); i++ {
r := text[i] r := text[i]
setBits |= r setBits |= r
isSpace := int(asciiSpace[r]) isSpace := int64(asciiSpace[r])
n += wasSpace & ^isSpace n += wasSpace & ^isSpace
wasSpace = isSpace wasSpace = isSpace
} }
@ -34,19 +34,19 @@ func SpaceIndexed(text string, offset int) ([]string, int) {
// ASCII fast path // ASCII fast path
a := make([]string, n) a := make([]string, n)
na := 0 na := int64(0)
fieldStart := 0 fieldStart := int64(0)
i := 0 i := int64(0)
j := n - 1 // last by default j := n - 1 // last by default
// Skip spaces in the front of the input. // Skip spaces in the front of the input.
for i < len(text) && asciiSpace[text[i]] != 0 { for i < int64(len(text)) && asciiSpace[text[i]] != 0 {
i++ i++
} }
fieldStart = i fieldStart = i
for i < len(text) { for i < int64(len(text)) {
if asciiSpace[text[i]] == 0 { if asciiSpace[text[i]] == 0 {
i++ i++
continue continue
@ -61,37 +61,37 @@ func SpaceIndexed(text string, offset int) ([]string, int) {
i++ i++
// Skip spaces in between fields. // Skip spaces in between fields.
for i < len(text) && asciiSpace[text[i]] != 0 { for i < int64(len(text)) && asciiSpace[text[i]] != 0 {
i++ i++
} }
fieldStart = i fieldStart = i
} }
if fieldStart < len(text) { // Last field might end at EOF. if fieldStart < int64(len(text)) { // Last field might end at EOF.
a[na] = text[fieldStart:] a[na] = text[fieldStart:]
} }
return a, j return a, j
} }
func spaceIndexedRunes(runes []rune, offset int) ([]string, int) { func spaceIndexedRunes(runes []rune, offset int64) ([]string, int64) {
// A span is used to record a slice of s of the form s[start:end]. // A span is used to record a slice of s of the form s[start:end].
// The start index is inclusive and the end index is exclusive. // The start index is inclusive and the end index is exclusive.
type span struct{ start, end int } type span struct{ start, end int64 }
spans := make([]span, 0, 16) spans := make([]span, 0, 16)
// Find the field start and end indices. // Find the field start and end indices.
wasField := false wasField := false
fromIndex := 0 fromIndex := int64(0)
for i, rune := range runes { for i, rune := range runes {
if unicode.IsSpace(rune) { if unicode.IsSpace(rune) {
if wasField { if wasField {
spans = append(spans, span{start: fromIndex, end: i}) spans = append(spans, span{start: fromIndex, end: int64(i)})
wasField = false wasField = false
} }
} else { } else {
if !wasField { if !wasField {
fromIndex = i fromIndex = int64(i)
wasField = true wasField = true
} }
} }
@ -99,18 +99,18 @@ func spaceIndexedRunes(runes []rune, offset int) ([]string, int) {
// Last field might end at EOF. // Last field might end at EOF.
if wasField { if wasField {
spans = append(spans, span{fromIndex, len(runes)}) spans = append(spans, span{fromIndex, int64(len(runes))})
} }
// Create strings from recorded field indices. // Create strings from recorded field indices.
a := make([]string, 0, len(spans)) a := make([]string, 0, len(spans))
j := len(spans) - 1 // assume last j := int64(len(spans)) - 1 // assume last
for i, span := range spans { for i, span := range spans {
a = append(a, string(runes[span.start:span.end])) a = append(a, string(runes[span.start:span.end]))
if span.start <= offset && offset <= span.end { if span.start <= offset && offset <= span.end {
j = i j = int64(i)
} }
} }

View File

@ -5,9 +5,9 @@ import "testing"
func TestSpaceIndexed(t *testing.T) { func TestSpaceIndexed(t *testing.T) {
var tests = []struct { var tests = []struct {
input string input string
offset int offset int64
output []string output []string
index int index int64
}{{ }{{
input: "bruhemus momentus lorem ipsum", input: "bruhemus momentus lorem ipsum",
offset: 13, // ^ offset: 13, // ^
@ -23,7 +23,7 @@ func TestSpaceIndexed(t *testing.T) {
index: 6, index: 6,
}, { }, {
input: "sorry, what were you typing?", input: "sorry, what were you typing?",
offset: len("sorry, what were you typing?") - 1, offset: int64(len("sorry, what were you typing?")) - 1,
output: []string{"sorry,", "what", "were", "you", "typing?"}, output: []string{"sorry,", "what", "were", "you", "typing?"},
index: 4, index: 4,
}, { }, {