osnovno iskanje
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*grep2electricbogaloo*
|
||||
3
go.mod
Normal file
3
go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module git.govekar.net/grep2electricbogaloo
|
||||
|
||||
go 1.25.5
|
||||
31
main.go
Normal file
31
main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func matchEngine(pattern string, line string) {
|
||||
// sliding window approach for simple full pattern matching
|
||||
for i := 0; i < len(line)-len(pattern); i++ {
|
||||
slice := line[i : i+len(pattern)]
|
||||
if slice == pattern {
|
||||
println(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func processReader(src string, r *os.File, pattern string) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
line := 0
|
||||
for scanner.Scan() {
|
||||
line++
|
||||
text := scanner.Text()
|
||||
matchEngine(pattern, text)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
pattern := os.Args[1]
|
||||
processReader("stdin", os.Stdin, pattern)
|
||||
}
|
||||
Reference in New Issue
Block a user