mirror of
https://github.com/walles/moar.git
synced 2024-09-11 12:15:43 +03:00
Fix lastChar()
This commit is contained in:
parent
a57a6c4709
commit
d5d576b75f
@ -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)
|
||||
}
|
||||
|
27
m/styledStringSplitter_test.go
Normal file
27
m/styledStringSplitter_test.go
Normal 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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user