Add Log method to scanner and its cursor

pull/53/head
rwxrob 3 years ago
parent 2b9195ec9d
commit fb9eec2d5b
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -5,6 +5,7 @@ package scan
import ( import (
"fmt" "fmt"
"log"
"github.com/rwxrob/bonzai/scan/tk" "github.com/rwxrob/bonzai/scan/tk"
) )
@ -70,3 +71,6 @@ func (c *Cur) String() string {
// Print prints the cursor itself in String form. See String. // Print prints the cursor itself in String form. See String.
func (c *Cur) Print() { fmt.Println(c.String()) } func (c *Cur) Print() { fmt.Println(c.String()) }
// Log calls log.Println on the cursor itself in String form. See String.
func (c *Cur) Log() { log.Println(c.String()) }

@ -1,13 +1,24 @@
package scan_test package scan_test
import "github.com/rwxrob/bonzai/scan" import (
"log"
"os"
"github.com/rwxrob/bonzai/scan"
)
func ExampleCur() { func ExampleCur() {
defer log.SetOutput(os.Stderr)
defer log.SetFlags(log.Flags())
log.SetOutput(os.Stdout)
log.SetFlags(0)
m := new(scan.Cur) m := new(scan.Cur)
m.Print() m.Print()
m.NewLine() m.NewLine()
m.Print() m.Print()
m.Log()
//Output: //Output:
// U+0000 '\x00' 0,0-0 (0-1) // U+0000 '\x00' 0,0-0 (0-1)
// U+0000 '\x00' 1,1-1 (0-1) // U+0000 '\x00' 1,1-1 (0-1)
// U+0000 '\x00' 1,1-1 (0-1)
} }

@ -62,3 +62,8 @@ type Rng struct {
First rune First rune
Last rune Last rune
} }
// ---------------------------- composites ----------------------------
// (keep most common to the left)
var WS = In{' ', '\n', '\t', '\r'}

@ -156,6 +156,9 @@ func (s *Scanner) String() string { return s.Cur.String() }
// Print delegates to internal cursor Print. // Print delegates to internal cursor Print.
func (s *Scanner) Print() { s.Cur.Print() } func (s *Scanner) Print() { s.Cur.Print() }
// Log delegates to internal cursor Log.
func (s *Scanner) Log() { s.Cur.Log() }
// Mark returns a copy of the current scanner cursor to preserve like // Mark returns a copy of the current scanner cursor to preserve like
// a bookmark into the buffer data. See Cur, Look, LookSlice. // a bookmark into the buffer data. See Cur, Look, LookSlice.
func (s *Scanner) Mark() *Cur { func (s *Scanner) Mark() *Cur {

@ -2,6 +2,8 @@ package scan_test
import ( import (
"fmt" "fmt"
"log"
"os"
"strings" "strings"
"github.com/rwxrob/bonzai/scan" "github.com/rwxrob/bonzai/scan"
@ -347,12 +349,18 @@ func ExampleSnap() {
} }
func ExampleScan() { func ExampleScan() {
defer log.SetOutput(os.Stderr)
defer log.SetFlags(log.Flags())
log.SetOutput(os.Stdout)
log.SetFlags(0)
s, _ := scan.New(`s😈me thing`) s, _ := scan.New(`s😈me thing`)
s.Scan() s.Scan()
s.Print() s.Print()
s.Scan() s.Scan()
s.Print() s.Print()
s.Log()
// Output: // Output:
// U+1F608 '😈' 1,2-2 (2-2) // U+1F608 '😈' 1,2-2 (2-2)
// U+006D 'm' 1,3-6 (3-6) // U+006D 'm' 1,3-6 (3-6)
// U+006D 'm' 1,3-6 (3-6)
} }

Loading…
Cancel
Save