Skip to content

there is probably a simplification in the logic in this method AllBetweenPattern... #86

Description

@github-actions

there is probably a simplification in the logic in this method AllBetweenPattern. We unfortunately can't use a regular expression since go does not support before text matching (?=re) https://github.com/google/re2/wiki/Syntax https://github.com/google/re2/wiki/WhyRE2

// TODO: there is probably a simplification in the logic in this method AllBetweenPattern. We unfortunately can't use a regular expression since go does not support before text matching (?=re) https://github.com/google/re2/wiki/Syntax https://github.com/google/re2/wiki/WhyRE2

package strs

// TODO: there is probably a simplification in the logic in this method AllBetweenPattern. We unfortunately can't use a regular expression since go does not support before text matching (?=re) https://github.com/google/re2/wiki/Syntax https://github.com/google/re2/wiki/WhyRE2
func AllBetweenPattern(str, pattern string) []string {
	stringsMatched := []string{}

	matchingPatternIdx := 0
	patternMatched := false

	currentMatch := ""

	for _, char := range str {
		if byte(char) == pattern[matchingPatternIdx] {
			matchingPatternIdx++

			if !patternMatched && matchingPatternIdx == len(pattern) {
				patternMatched = true
				matchingPatternIdx = 0
				continue
			}
		}

		if patternMatched {
			currentMatch += string(char)

			if matchingPatternIdx == len(pattern) {
				patternMatched = false
				matchingPatternIdx = 0

				matchedStr := currentMatch[:len(currentMatch)-len(pattern)]
				stringsMatched = append(stringsMatched, matchedStr)
				currentMatch = ""
			}
		}

	}

	return stringsMatched
}

2097e500072990b5a74babce8c96da15b89a04e0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions