Skip to content

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

@github-actions

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

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