Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created August 24, 2016 23:19
Show Gist options
  • Save noahlt/9c24fc030045659bb27df7c8804fdd84 to your computer and use it in GitHub Desktop.
Save noahlt/9c24fc030045659bb27df7c8804fdd84 to your computer and use it in GitHub Desktop.
Print context of string around a point
// Print context of string around a point
func prctx(buf string, point int) {
if point < 0 || point >= len(buf) {
log.Printf("context for point: out of bounds (%d/%d)", point, len(buf))
return
}
min := func(a int, b int) int {
if a < b {
return a
}
return b
}
max := func(a int, b int) int {
if a > b {
return a
}
return b
}
bufL := func(i int) int {
return max(0, i)
}
bufR := func(i int) int {
return min(len(buf), i)
}
log.Printf("context (%d, %d) @%d: %s{%s}%s", point-10, point+10, point,
buf[bufL(point-10):point], buf[point:bufR(point+1)], buf[bufR(point+1):bufR(point+10)])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment