-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/cli/term: Add readRune, a much simpler alternative to runeReader.
- Loading branch information
Showing
4 changed files
with
118 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package term | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type byteReaderWithTimeout interface { | ||
// ReadByteWithTimeout reads a single byte with a timeout. A negative | ||
// timeout means no timeout. | ||
ReadByteWithTimeout(timeout time.Duration) (byte, error) | ||
} | ||
|
||
const badRune = '\ufffd' | ||
|
||
var utf8SeqTimeout = 10 * time.Millisecond | ||
|
||
// Reads a rune from the reader. The timeout applies to the first byte; a | ||
// negative value means no timeout. | ||
func readRune(rd byteReaderWithTimeout, timeout time.Duration) (rune, error) { | ||
leader, err := rd.ReadByteWithTimeout(timeout) | ||
if err != nil { | ||
return badRune, err | ||
} | ||
var r rune | ||
pending := 0 | ||
switch { | ||
case leader>>7 == 0: | ||
r = rune(leader) | ||
case leader>>5 == 0x6: | ||
r = rune(leader & 0x1f) | ||
pending = 1 | ||
case leader>>4 == 0xe: | ||
r = rune(leader & 0xf) | ||
pending = 2 | ||
case leader>>3 == 0x1e: | ||
r = rune(leader & 0x7) | ||
pending = 3 | ||
} | ||
for i := 0; i < pending; i++ { | ||
b, err := rd.ReadByteWithTimeout(utf8SeqTimeout) | ||
if err != nil { | ||
return badRune, err | ||
} | ||
r = r<<6 + rune(b&0x3f) | ||
} | ||
return r, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// +build !windows,!plan9 | ||
|
||
package term | ||
|
||
import "testing" | ||
|
||
// TODO(xiaq): Do not depend on Unix for this test. | ||
|
||
var contents = []string{ | ||
"English", | ||
"Ελληνικά", | ||
"你好 こんにちは", | ||
"𐌰𐌱", | ||
} | ||
|
||
func TestReadRune(t *testing.T) { | ||
for _, content := range contents { | ||
t.Run(content, func(t *testing.T) { | ||
rd, w, cleanup := setupFileReader() | ||
defer cleanup() | ||
|
||
w.Write([]byte(content)) | ||
for _, wantRune := range []rune(content) { | ||
r, err := readRune(rd, 0) | ||
if r != wantRune { | ||
t.Errorf("got rune %q, want %q", r, wantRune) | ||
} | ||
if err != nil { | ||
t.Errorf("got err %v, want nil", err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestReadRune_ErrorAtFirstByte(t *testing.T) { | ||
rd, _, cleanup := setupFileReader() | ||
defer cleanup() | ||
|
||
r, err := readRune(rd, 0) | ||
if r != '\ufffd' { | ||
t.Errorf("got rune %q, want %q", r, '\ufffd') | ||
} | ||
if err == nil { | ||
t.Errorf("got err %v, want non-nil", err) | ||
} | ||
} | ||
|
||
func TestReadRune_ErrorAtNonFirstByte(t *testing.T) { | ||
rd, w, cleanup := setupFileReader() | ||
defer cleanup() | ||
|
||
w.Write([]byte{0xe4}) | ||
|
||
r, err := readRune(rd, 0) | ||
if r != '\ufffd' { | ||
t.Errorf("got rune %q, want %q", r, '\ufffd') | ||
} | ||
if err == nil { | ||
t.Errorf("got err %v, want non-nil", err) | ||
} | ||
} |