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) // Increment until we hit a character matching the start state (assuming not 0-state)
if start.isEmpty == false { if start.isEmpty == false {
for i < len(str) && !start.contentContains([]rune(str), i) { for i < len(str) && !start.contentContains(str, i) {
i++ i++
} }
startIdx = 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 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 assertionFailed := false // Whether or not an assertion failed for this round
for _, state := range currentStates { for _, state := range currentStates {
matches, numMatches := state.matchesFor([]rune(str), i) matches, numMatches := state.matchesFor(str, i)
if numMatches > 0 { if numMatches > 0 {
numStatesMatched++ numStatesMatched++
tempStates = append(tempStates, matches...) 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, // Only add the match if the start index is in bounds. If the state has an assertion,
// make sure the assertion checks out. // make sure the assertion checks out.
if state.isLast && startIdx < len(str) { 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 endIdx = i
tempIndices, _ = unique_append(tempIndices, matchIndex{startIdx, endIdx}) 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. // 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 || wbounded := idx == 0 ||
idx >= len(str)-1 || idx >= len(str)-1 ||
(!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[idx-1]) && !slices.Contains(wordChars, str[idx])) (slices.Contains(wordChars, str_runes[idx-1]) && !slices.Contains(wordChars, str_runes[idx]))
return wbounded return wbounded
} }

@ -27,7 +27,7 @@ type State struct {
// Checks if the given state's assertion is true. Returns true if the given // Checks if the given state's assertion is true. Returns true if the given
// state doesn't have an assertion. // 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 { if s.assert == SOS {
return idx == 0 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 // 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 { if s.assert != NONE {
return s.checkAssertion(str, idx) 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. // 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. // 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 // Assertions can be viewed as 'checks'. If the check fails, we return
// an empty array and 0. // an empty array and 0.
// If it passes, we treat it like any other state, and return all the transitions. // If it passes, we treat it like any other state, and return all the transitions.

Loading…
Cancel
Save