src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

edit.go (758B)


      1 package line
      2 
      3 import "strings"
      4 
      5 func (l *Line) Append(s string) {
      6 	l.data = l.data + s
      7 }
      8 
      9 func (l *Line) DeleteRune(index int) (deleted rune) {
     10 	if index < 0 || index >= len(l.data) {
     11 		return
     12 	}
     13 
     14 	var str strings.Builder
     15 	for i, r := range l.data {
     16 		if i == index {
     17 			deleted = r
     18 			continue
     19 		}
     20 		str.WriteRune(r)
     21 	}
     22 
     23 	l.data = str.String()
     24 
     25 	return deleted
     26 }
     27 
     28 func (l *Line) Insert(index int, nr rune) {
     29 	if index < 0 || index > len(l.data) {
     30 		return
     31 	}
     32 
     33 	var str strings.Builder
     34 	switch {
     35 
     36 	// Handle empty lines and the end of the line.
     37 	case index == len(l.data):
     38 		str.WriteString(l.data)
     39 		str.WriteRune(nr)
     40 
     41 	default:
     42 		for i, r := range l.data {
     43 			if i == index {
     44 				str.WriteRune(nr)
     45 			}
     46 			str.WriteRune(r)
     47 		}
     48 	}
     49 
     50 	l.data = str.String()
     51 }