From ca274e51a3da1e24095a12cbc79b8f6a715fad19 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 24 Oct 2014 02:05:23 +0200 Subject: [PATCH] scanner.Terminal: A literal (utf8) string within a document Signed-off-by: Alejandro Mery --- scanner/scanner.go | 1 + scanner/terminal.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 scanner/scanner.go create mode 100644 scanner/terminal.go diff --git a/scanner/scanner.go b/scanner/scanner.go new file mode 100644 index 0000000..95a12d4 --- /dev/null +++ b/scanner/scanner.go @@ -0,0 +1 @@ +package scanner diff --git a/scanner/terminal.go b/scanner/terminal.go new file mode 100644 index 0000000..7b5ffd7 --- /dev/null +++ b/scanner/terminal.go @@ -0,0 +1,41 @@ +package scanner + +import ( + "unicode/utf8" +) + +// A literal element within a document +type Terminal struct { + val string + bytes, runes uint + line, col uint +} + +// Creates a new Terminal +func NewTerminalFull(val string, bytes, runes, line, col uint) *Terminal { + return &Terminal{ + val: val, + bytes: bytes, + runes: runes, + line: line, + col: col, + } +} + +// Creates a new Terminal without knowing it's length +func NewTerminal(val string, line, col uint) *Terminal { + bytes := uint(len(val)) + runes := uint(utf8.RuneCountInString(val)) + + return NewTerminalFull(val, bytes, runes, line, col) +} + +// Position of the terminal in the document +func (t *Terminal) Position() (uint, uint) { + return t.line, t.col +} + +// Value and size of the Terminal +func (t *Terminal) Value() (string, uint, uint) { + return t.val, t.bytes, t.runes +}