Skip to content

Instantly share code, notes, and snippets.

@frohoff
Created May 15, 2014 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frohoff/52cb94c95e79640d746f to your computer and use it in GitHub Desktop.
Save frohoff/52cb94c95e79640d746f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"bufio"
"regexp"
"io"
)
func main() {
restr := os.Args[1]
re, _ := regexp.Compile(restr)
fmt.Println(re)
n := 4
iobuf := make([]byte, n)
buf := make([]byte, n*2)
in := bufio.NewReader(os.Stdin)
chunk := -1
lastFound := make(map[int]string)
curFound := make(map[int]string)
for {
b, e := in.Read(iobuf)
if e != nil && e != io.EOF { panic(e) }
if b == 0 { break }
copy(buf[0:n],buf[n:n*2])
copy(buf[n:n*2],iobuf)
if chunk == -1 {
chunk++
continue
}
fmt.Println(buf)
matches := re.FindAllIndex(buf[0:n+b],-1)
for _,m := range matches {
fmt.Println(m)
rel := m[0]+chunk*n
_,ok := lastFound[rel]
if ! ok {
str := string(buf[m[0]:m[1]])
curFound[rel] = str
fmt.Println(rel,`:`,str)
}
}
chunk += 1
lastFound = curFound
curFound = make(map[int]string) // reset map
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment