Skip to content

Commit 9804c15

Browse files
authored
new: added runeBuffer (#5)
1 parent 280d023 commit 9804c15

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

rune-buffer.go

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

rune-buffer_test.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cliargs
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNewRuneBuffer_empty(t *testing.T) {
9+
rb := newRuneBuffer(0)
10+
assert.Equal(t, rb.runes, []rune{})
11+
assert.Equal(t, rb.length, 0)
12+
assert.Equal(t, len(rb.runes), 0)
13+
assert.Equal(t, cap(rb.runes), 0)
14+
assert.Equal(t, rb.slice(), []rune{})
15+
}
16+
17+
func TestRuneBuffer_add(t *testing.T) {
18+
rb := newRuneBuffer(5)
19+
assert.Equal(t, rb.runes, []rune{0, 0, 0, 0, 0})
20+
assert.Equal(t, rb.length, 0)
21+
assert.Equal(t, len(rb.runes), 5)
22+
assert.Equal(t, cap(rb.runes), 5)
23+
assert.Equal(t, rb.slice(), []rune{})
24+
25+
assert.True(t, rb.add('1'))
26+
assert.Equal(t, rb.runes, []rune{'1', 0, 0, 0, 0})
27+
assert.Equal(t, rb.length, 1)
28+
assert.Equal(t, len(rb.runes), 5)
29+
assert.Equal(t, cap(rb.runes), 5)
30+
assert.Equal(t, rb.slice(), []rune{'1'})
31+
32+
assert.True(t, rb.add('2', '3'))
33+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', 0, 0})
34+
assert.Equal(t, rb.length, 3)
35+
assert.Equal(t, len(rb.runes), 5)
36+
assert.Equal(t, cap(rb.runes), 5)
37+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3'})
38+
39+
assert.False(t, rb.add('x', 'y', 'z'))
40+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', 0, 0})
41+
assert.Equal(t, rb.length, 3)
42+
assert.Equal(t, len(rb.runes), 5)
43+
assert.Equal(t, cap(rb.runes), 5)
44+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3'})
45+
46+
assert.True(t, rb.add('4', '5'))
47+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
48+
assert.Equal(t, rb.length, 5)
49+
assert.Equal(t, len(rb.runes), 5)
50+
assert.Equal(t, cap(rb.runes), 5)
51+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
52+
53+
assert.False(t, rb.add('6'))
54+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
55+
assert.Equal(t, rb.length, 5)
56+
assert.Equal(t, len(rb.runes), 5)
57+
assert.Equal(t, cap(rb.runes), 5)
58+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
59+
}
60+
61+
func TestRuneBuffer_cr(t *testing.T) {
62+
rb := newRuneBuffer(5)
63+
assert.Equal(t, rb.runes, []rune{0, 0, 0, 0, 0})
64+
assert.Equal(t, rb.length, 0)
65+
assert.Equal(t, len(rb.runes), 5)
66+
assert.Equal(t, cap(rb.runes), 5)
67+
assert.Equal(t, rb.slice(), []rune{})
68+
69+
assert.True(t, rb.add('1', '2', '3', '4', '5'))
70+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
71+
assert.Equal(t, rb.length, 5)
72+
assert.Equal(t, len(rb.runes), 5)
73+
assert.Equal(t, cap(rb.runes), 5)
74+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
75+
76+
rb.cr(3)
77+
assert.Equal(t, rb.runes, []rune{'4', '5', '3', '4', '5'})
78+
assert.Equal(t, rb.length, 2)
79+
assert.Equal(t, len(rb.runes), 5)
80+
assert.Equal(t, cap(rb.runes), 5)
81+
assert.Equal(t, rb.slice(), []rune{'4', '5'})
82+
83+
assert.True(t, rb.add('6'))
84+
assert.Equal(t, rb.runes, []rune{'4', '5', '6', '4', '5'})
85+
assert.Equal(t, rb.length, 3)
86+
assert.Equal(t, len(rb.runes), 5)
87+
assert.Equal(t, cap(rb.runes), 5)
88+
assert.Equal(t, rb.slice(), []rune{'4', '5', '6'})
89+
90+
rb.cr(3)
91+
assert.Equal(t, rb.runes, []rune{'4', '5', '6', '4', '5'})
92+
assert.Equal(t, rb.length, 0)
93+
assert.Equal(t, len(rb.runes), 5)
94+
assert.Equal(t, cap(rb.runes), 5)
95+
assert.Equal(t, rb.slice(), []rune{})
96+
97+
assert.True(t, rb.add('1', '2', '3', '4', '5'))
98+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
99+
assert.Equal(t, rb.length, 5)
100+
assert.Equal(t, len(rb.runes), 5)
101+
assert.Equal(t, cap(rb.runes), 5)
102+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
103+
104+
rb.cr(0)
105+
assert.False(t, rb.add('1', '2', '3', '4', '5'))
106+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
107+
assert.Equal(t, rb.length, 5)
108+
assert.Equal(t, len(rb.runes), 5)
109+
assert.Equal(t, cap(rb.runes), 5)
110+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
111+
112+
rb.cr(-1)
113+
assert.False(t, rb.add('1', '2', '3', '4', '5'))
114+
assert.Equal(t, rb.runes, []rune{'1', '2', '3', '4', '5'})
115+
assert.Equal(t, rb.length, 5)
116+
assert.Equal(t, len(rb.runes), 5)
117+
assert.Equal(t, cap(rb.runes), 5)
118+
assert.Equal(t, rb.slice(), []rune{'1', '2', '3', '4', '5'})
119+
}

0 commit comments

Comments
 (0)