-
Notifications
You must be signed in to change notification settings - Fork 0
/
rune-buffer.go
46 lines (39 loc) · 848 Bytes
/
rune-buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package linebreak
type runeBuffer struct {
runes []rune
length int
}
func newRuneBuffer(capacity int) runeBuffer {
return runeBuffer{runes: make([]rune, capacity)}
}
func (rb *runeBuffer) add(runes ...rune) bool {
n := len(runes)
if rb.length+n > len(rb.runes) {
return false
}
for i, r := range runes {
rb.runes[rb.length+i] = r
}
rb.length += n
return true
}
func (rb *runeBuffer) cr(start int) {
if start < 0 {
return
}
if start >= rb.length {
rb.length = 0
return
}
n := rb.length - start
for i := 0; i < n; i++ {
rb.runes[i] = rb.runes[i+start]
}
rb.length = n
}
func (rb runeBuffer) full() []rune {
return rb.runes[0:rb.length]
}