1
1
mirror of https://github.com/walles/moar.git synced 2024-09-11 12:15:43 +03:00

Fix lastChar()

This commit is contained in:
Johan Walles 2023-11-11 08:33:07 +01:00
parent a57a6c4709
commit d5d576b75f
2 changed files with 31 additions and 2 deletions

View File

@ -56,6 +56,10 @@ func (s *styledStringSplitter) nextChar() rune {
// Returns whatever the last call to nextChar() returned
func (s *styledStringSplitter) lastChar() rune {
if s.previousByteIndex >= len(s.input) {
return -1
}
char, _ := utf8.DecodeRuneInString(s.input[s.previousByteIndex:])
return char
}
@ -74,8 +78,6 @@ func (s *styledStringSplitter) run() {
// Somewhere in handleEscape(), we got a character that was
// unexpected. We need to treat everything up to before that
// character as just plain runes.
//
// The character itself, should be retried inside this loop.
for _, char := range s.input[escIndex:s.previousByteIndex] {
s.handleRune(char)
}

View File

@ -0,0 +1,27 @@
package m
import (
"testing"
"gotest.tools/v3/assert"
)
func TestNextCharLastChar_base(t *testing.T) {
s := styledStringSplitter{
input: "a",
}
assert.Equal(t, 'a', s.nextChar())
assert.Equal(t, 'a', s.lastChar())
assert.Equal(t, rune(-1), s.nextChar())
assert.Equal(t, rune(-1), s.lastChar())
}
func TestNextCharLastChar_empty(t *testing.T) {
s := styledStringSplitter{
input: "",
}
assert.Equal(t, rune(-1), s.nextChar())
assert.Equal(t, rune(-1), s.lastChar())
}