From dca81c179629a3c877874fe54b005c695993aee7 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 1 Nov 2024 01:53:50 -0400 Subject: [PATCH] Replaced rune-slice parameters with string parameters in functions; avoids unnecessary conversion from strings to rune-slices --- matching.go | 6 +++--- misc.go | 7 ++++--- nfa.go | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/matching.go b/matching.go index 11ddaf3..e32ff6e 100644 --- a/matching.go +++ b/matching.go @@ -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}) } diff --git a/misc.go b/misc.go index f8a6953..bf6304e 100644 --- a/misc.go +++ b/misc.go @@ -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 } diff --git a/nfa.go b/nfa.go index 3457fd9..65b30fc 100644 --- a/nfa.go +++ b/nfa.go @@ -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.