Created
August 24, 2016 23:19
-
-
Save noahlt/9c24fc030045659bb27df7c8804fdd84 to your computer and use it in GitHub Desktop.
Print context of string around a point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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