fx/ring.go

50 lines
705 B
Go
Raw Normal View History

2023-09-07 23:53:51 +03:00
package main
2023-11-11 00:35:44 +03:00
import (
"strings"
)
2023-09-07 23:53:51 +03:00
type ring struct {
2023-11-11 00:35:44 +03:00
buf [70]byte
2023-09-07 23:53:51 +03:00
start, end int
}
func (r *ring) writeByte(b byte) {
r.buf[r.end] = b
2023-11-11 00:35:44 +03:00
r.end = (r.end + 1) % len(r.buf)
2023-09-07 23:53:51 +03:00
if r.end == r.start {
2023-11-11 00:35:44 +03:00
r.start = (r.start + 1) % len(r.buf)
2023-09-07 23:53:51 +03:00
}
}
func (r *ring) string() string {
2023-11-11 00:35:44 +03:00
var lines []byte
newlineCount := 0
for i := r.end - 1; ; i-- {
if i < 0 {
i = len(r.buf) - 1
}
if r.buf[i] == '\n' {
newlineCount++
if newlineCount == 2 {
break
}
}
lines = append(lines, r.buf[i])
if i == r.start {
break
}
2023-09-07 23:53:51 +03:00
}
2023-11-11 00:35:44 +03:00
for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 {
lines[i], lines[j] = lines[j], lines[i]
}
return strings.TrimRight(string(lines), "\t \n")
2023-09-07 23:53:51 +03:00
}