]> git.lizzy.rs Git - micro.git/blobdiff - internal/buffer/loc.go
Merge pull request #1406 from LeapofAzzam/LeapofAzzam-patch-1
[micro.git] / internal / buffer / loc.go
index 0fa682b1b53aa5069e0d91e41032167c7b31b246..89db7b891c0dd3d91d83284bf3f15e4ceffaa866 100644 (file)
@@ -123,3 +123,15 @@ func (l Loc) Diff(a, b Loc, buf *Buffer) int {
 func (l Loc) Move(n int, buf *Buffer) Loc {
        return l.MoveLA(n, buf.LineArray)
 }
+
+// ByteOffset is just like ToCharPos except it counts bytes instead of runes
+func ByteOffset(pos Loc, buf *Buffer) int {
+       x, y := pos.X, pos.Y
+       loc := 0
+       for i := 0; i < y; i++ {
+               // + 1 for the newline
+               loc += len(buf.Line(i)) + 1
+       }
+       loc += len(buf.Line(y)[:x])
+       return loc
+}