Replaced rune-slice parameters with string parameters in functions; avoids unnecessary conversion from strings to rune-slices

master
Aadhavan Srinivasan 2 months ago
parent 723be527fb
commit dca81c1796

@ -97,7 +97,7 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
// Increment until we hit a character matching the start state (assuming not 0-state)
if start.isEmpty == false {
for i < len(str) && !start.contentContains([]rune(str), i) {
for i < len(str) && !start.contentContains(str, i) {
i++
}
startIdx = i
@ -136,7 +136,7 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
numStatesMatched := 0 // The number of states which had at least 1 match for this round
assertionFailed := false // Whether or not an assertion failed for this round
for _, state := range currentStates {
matches, numMatches := state.matchesFor([]rune(str), i)
matches, numMatches := state.matchesFor(str, i)
if numMatches > 0 {
numStatesMatched++
tempStates = append(tempStates, matches...)
@ -211,7 +211,7 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset
// Only add the match if the start index is in bounds. If the state has an assertion,
// make sure the assertion checks out.
if state.isLast && startIdx < len(str) {
if state.assert == NONE || state.checkAssertion([]rune(str), len(str)) {
if state.assert == NONE || state.checkAssertion(str, len(str)) {
endIdx = i
tempIndices, _ = unique_append(tempIndices, matchIndex{startIdx, endIdx})
}

@ -22,11 +22,12 @@ func dotChars() []rune { // Returns all possible characters represented by the d
}
// Returns true if str[idx] and str[idx-1] are separated by a word boundary.
func isWordBoundary(str []rune, idx int) bool {
func isWordBoundary(str string, idx int) bool {
str_runes := []rune(str)
wbounded := idx == 0 ||
idx >= len(str)-1 ||
(!slices.Contains(wordChars, str[idx-1]) && slices.Contains(wordChars, str[idx])) ||
(slices.Contains(wordChars, str[idx-1]) && !slices.Contains(wordChars, str[idx]))
(!slices.Contains(wordChars, str_runes[idx-1]) && slices.Contains(wordChars, str_runes[idx])) ||
(slices.Contains(wordChars, str_runes[idx-1]) && !slices.Contains(wordChars, str_runes[idx]))
return wbounded
}

@ -27,7 +27,7 @@ type State struct {
// Checks if the given state's assertion is true. Returns true if the given
// state doesn't have an assertion.
func (s State) checkAssertion(str []rune, idx int) bool {
func (s State) checkAssertion(str string, idx int) bool {
if s.assert == SOS {
return idx == 0
}
@ -44,7 +44,7 @@ func (s State) checkAssertion(str []rune, idx int) bool {
}
// Returns true if the contents of 's' contain the value at the given index of the given string
func (s State) contentContains(str []rune, idx int) bool {
func (s State) contentContains(str string, idx int) bool {
if s.assert != NONE {
return s.checkAssertion(str, idx)
}
@ -54,7 +54,7 @@ func (s State) contentContains(str []rune, idx int) bool {
// Returns the matches for the character at the given index of the given string.
// Also returns the number of matches. Returns -1 if an assertion failed.
func (s State) matchesFor(str []rune, idx int) ([]*State, int) {
func (s State) matchesFor(str string, idx int) ([]*State, int) {
// Assertions can be viewed as 'checks'. If the check fails, we return
// an empty array and 0.
// If it passes, we treat it like any other state, and return all the transitions.

Loading…
Cancel
Save