cyucelen / marker

🖍️ Marker is the easiest way to match and mark strings for colorful terminal outputs!
MIT License
47 stars 13 forks source link

Fixed the incorrect behavior in findPatternMatchIndexes. It was prev… #31

Closed dapryor closed 3 years ago

dapryor commented 3 years ago

…iously removing chunks of the strings which was effect subsequent match index locations.

It also now supports regexp patterns to better match the name. Added a few test as well.

Please mark this as a hacktoberfest-accepted PR ;)

codecov-io commented 3 years ago

Codecov Report

Merging #31 into master will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #31   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          121        84   -37     
=========================================
- Hits           121        84   -37     
Impacted Files Coverage Δ
matcher.go 100.00% <100.00%> (ø)
log.go 100.00% <0.00%> (ø)
marker.go 100.00% <0.00%> (ø)
builder.go 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 78e46f9...1e542a7. Read the comment docs.

dapryor commented 3 years ago
func findPatternMatchIndexes(str string, patternsToMatch []string) map[int]string {
    patternMatchIndexes := make(map[int]string)
    for _, patternToMatch := range patternsToMatch {
        for strings.Contains(str, patternToMatch) {
            matchIndex := strings.Index(str, patternToMatch)
            str = strings.Replace(str, patternToMatch, "", 1)
            patternMatchIndexes[matchIndex] = patternToMatch
        }
    }
    return patternMatchIndexes
}

this was the original block of code. It incorrectly found indexes. i.e.

input: "I scream, you all scream, we all scream for ice cream."
patterns: "scream", "ice"
Output:
2 scream
12 scream
21 scream
26 ice

It also did not support patterns like the name suggest, only strings due to this line str = strings.Replace(str, patternToMatch, "", 1) You would replace the match with an empty string so the first instance of scream would be correct, the second would be 6 spaces off, the third would be 12 spaces off This is the new block of code:

func findPatternMatchIndexes(str string, patternsToMatch []string) map[int]string {
    patternMatchIndexes := make(map[int]string)
    pattern := strings.Join(patternsToMatch , "|")
    patternRegex := regexp.MustCompile(pattern)
    indices := patternRegex.FindAllStringIndex(str, -1)
    for _, v := range(indices) {
        start, end := v[0], v[1]
        patternMatchIndexes[start] = str[start:end]
    }
    return patternMatchIndexes
}

It correctly finds the indexes: i.e.

input: "I scream, you all scream, we all scream for ice cream."
patterns: "scream", "ice"
Output:
2 scream
18 scream
33 scream
44 ice

and it also supports patterns since the new block uses regex