2016-06-07 18:43:28 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-29 21:44:30 +03:00
|
|
|
"bufio"
|
2016-06-07 18:43:28 +03:00
|
|
|
"bytes"
|
2016-11-29 21:44:30 +03:00
|
|
|
"io"
|
2016-06-07 18:43:28 +03:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
func runeToByteIndex(n int, txt []byte) int {
|
|
|
|
if n == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
i := 0
|
|
|
|
for len(txt) > 0 {
|
|
|
|
_, size := utf8.DecodeRune(txt)
|
|
|
|
|
|
|
|
txt = txt[size:]
|
|
|
|
count += size
|
|
|
|
i++
|
|
|
|
|
|
|
|
if i == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// A LineArray simply stores and array of lines and makes it easy to insert
|
|
|
|
// and delete in it
|
2016-06-07 18:43:28 +03:00
|
|
|
type LineArray struct {
|
|
|
|
lines [][]byte
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// NewLineArray returns a new line array from an array of bytes
|
2016-11-29 21:44:30 +03:00
|
|
|
func NewLineArray(reader io.Reader) *LineArray {
|
2016-06-07 18:43:28 +03:00
|
|
|
la := new(LineArray)
|
2016-11-29 21:44:30 +03:00
|
|
|
br := bufio.NewReader(reader)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
data, err := br.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2017-02-21 21:07:37 +03:00
|
|
|
la.lines = append(la.lines, data[:])
|
2016-11-29 21:44:30 +03:00
|
|
|
}
|
|
|
|
// Last line was read
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
la.lines = append(la.lines, data[:len(data)-1])
|
|
|
|
}
|
|
|
|
i++
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return la
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// Returns the String representation of the LineArray
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) String() string {
|
|
|
|
return string(bytes.Join(la.lines, []byte("\n")))
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// NewlineBelow adds a newline below the given line number
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) NewlineBelow(y int) {
|
|
|
|
la.lines = append(la.lines, []byte(" "))
|
|
|
|
copy(la.lines[y+2:], la.lines[y+1:])
|
|
|
|
la.lines[y+1] = []byte("")
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// inserts a byte array at a given location
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) insert(pos Loc, value []byte) {
|
|
|
|
x, y := runeToByteIndex(pos.X, la.lines[pos.Y]), pos.Y
|
|
|
|
// x, y := pos.x, pos.y
|
|
|
|
for i := 0; i < len(value); i++ {
|
|
|
|
if value[i] == '\n' {
|
|
|
|
la.Split(Loc{x, y})
|
|
|
|
x = 0
|
|
|
|
y++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
la.insertByte(Loc{x, y}, value[i])
|
|
|
|
x++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// inserts a byte at a given location
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) insertByte(pos Loc, value byte) {
|
|
|
|
la.lines[pos.Y] = append(la.lines[pos.Y], 0)
|
|
|
|
copy(la.lines[pos.Y][pos.X+1:], la.lines[pos.Y][pos.X:])
|
|
|
|
la.lines[pos.Y][pos.X] = value
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// JoinLines joins the two lines a and b
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) JoinLines(a, b int) {
|
|
|
|
la.insert(Loc{len(la.lines[a]), a}, la.lines[b])
|
|
|
|
la.DeleteLine(b)
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// Split splits a line at a given position
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) Split(pos Loc) {
|
|
|
|
la.NewlineBelow(pos.Y)
|
|
|
|
la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y][pos.X:])
|
|
|
|
la.DeleteToEnd(Loc{pos.X, pos.Y})
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// removes from start to end
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) remove(start, end Loc) string {
|
|
|
|
sub := la.Substr(start, end)
|
|
|
|
startX := runeToByteIndex(start.X, la.lines[start.Y])
|
|
|
|
endX := runeToByteIndex(end.X, la.lines[end.Y])
|
|
|
|
if start.Y == end.Y {
|
|
|
|
la.lines[start.Y] = append(la.lines[start.Y][:startX], la.lines[start.Y][endX:]...)
|
|
|
|
} else {
|
|
|
|
for i := start.Y + 1; i <= end.Y-1; i++ {
|
2016-06-08 17:21:27 +03:00
|
|
|
la.DeleteLine(start.Y + 1)
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
la.DeleteToEnd(Loc{startX, start.Y})
|
|
|
|
la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
|
|
|
|
la.JoinLines(start.Y, start.Y+1)
|
|
|
|
}
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// DeleteToEnd deletes from the end of a line to the position
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) DeleteToEnd(pos Loc) {
|
|
|
|
la.lines[pos.Y] = la.lines[pos.Y][:pos.X]
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// DeleteFromStart deletes from the start of a line to the position
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) DeleteFromStart(pos Loc) {
|
|
|
|
la.lines[pos.Y] = la.lines[pos.Y][pos.X+1:]
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// DeleteLine deletes the line number
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) DeleteLine(y int) {
|
|
|
|
la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// DeleteByte deletes the byte at a position
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) DeleteByte(pos Loc) {
|
|
|
|
la.lines[pos.Y] = la.lines[pos.Y][:pos.X+copy(la.lines[pos.Y][pos.X:], la.lines[pos.Y][pos.X+1:])]
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// Substr returns the string representation between two locations
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) Substr(start, end Loc) string {
|
|
|
|
startX := runeToByteIndex(start.X, la.lines[start.Y])
|
|
|
|
endX := runeToByteIndex(end.X, la.lines[end.Y])
|
|
|
|
if start.Y == end.Y {
|
|
|
|
return string(la.lines[start.Y][startX:endX])
|
|
|
|
}
|
|
|
|
var str string
|
|
|
|
str += string(la.lines[start.Y][startX:]) + "\n"
|
|
|
|
for i := start.Y + 1; i <= end.Y-1; i++ {
|
|
|
|
str += string(la.lines[i]) + "\n"
|
|
|
|
}
|
|
|
|
str += string(la.lines[end.Y][:endX])
|
|
|
|
return str
|
|
|
|
}
|