bufioscanner

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package bufioscanner provides a wrapper around bufio.Scanner with iteration capabilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scanner

type Scanner struct {
	*bufio.Scanner
}

Scanner wraps a bufio.Scanner and provides methods to iterate over its content.

func New

func New(s *bufio.Scanner) *Scanner

New creates a new Scanner from a bufio.Scanner.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner creates a new Scanner from an io.Reader.

func (*Scanner) Bytes

func (s *Scanner) Bytes() iter.Seq2[int, []byte]

Bytes returns an iterator that yields byte slices from the scanner using bufio.Scanner.Bytes.

Example
package main

import (
	"bufio"
	"bytes"
	"fmt"

	"github.com/goaux/iter/bufioscanner"
)

func main() {
	s := bufioscanner.NewScanner(bytes.NewBufferString(" Where are you going \n for your next vacation? \n"))
	s.Split(bufio.ScanWords)
	for i, word := range s.Bytes() {
		fmt.Printf("[%d] % 02x\n", i, word)
	}
	if err := s.Err(); err != nil {
		fmt.Printf("error: %v\n", err)
	}
}
Output:

[0] 57 68 65 72 65
[1] 61 72 65
[2] 79 6f 75
[3] 67 6f 69 6e 67
[4] 66 6f 72
[5] 79 6f 75 72
[6] 6e 65 78 74
[7] 76 61 63 61 74 69 6f 6e 3f
Example (Break)
package main

import (
	"bufio"
	"bytes"
	"fmt"

	"github.com/goaux/iter/bufioscanner"
)

func main() {
	s := bufioscanner.NewScanner(bytes.NewBufferString(" Where are you going \n for your next vacation? \n"))
	s.Split(bufio.ScanWords)
	for i, word := range s.Bytes() {
		fmt.Printf("[%d] % 02x\n", i, word)
		if i == 3 {
			break
		}
	}
	if err := s.Err(); err != nil {
		fmt.Printf("error: %v\n", err)
	}
}
Output:

[0] 57 68 65 72 65
[1] 61 72 65
[2] 79 6f 75
[3] 67 6f 69 6e 67

func (*Scanner) Text

func (s *Scanner) Text() iter.Seq2[int, string]

Text returns an iterator that yields strings from the scanner using bufio.Scanner.Text.

Example
package main

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
	"testing/iotest"

	"github.com/goaux/iter/bufioscanner"
)

func main() {
	s := bufioscanner.NewScanner(
		io.MultiReader(
			bytes.NewBufferString(" Where are you going \n for your next vacation? \n"),
			iotest.ErrReader(errors.New("io error?")),
		),
	)
	s.Split(bufio.ScanWords)
	for i, word := range s.Text() {
		fmt.Printf("[%d] %q\n", i, word)
	}
	if err := s.Err(); err != nil {
		fmt.Printf("error: %v\n", err)
	}
}
Output:

[0] "Where"
[1] "are"
[2] "you"
[3] "going"
[4] "for"
[5] "your"
[6] "next"
[7] "vacation?"
error: io error?
Example (Break)
package main

import (
	"bufio"
	"bytes"
	"fmt"

	"github.com/goaux/iter/bufioscanner"
)

func main() {
	s := bufioscanner.NewScanner(bytes.NewBufferString(" Where are you going \n for your next vacation? \n"))
	s.Split(bufio.ScanWords)
	for i, word := range s.Text() {
		fmt.Printf("[%d] %q\n", i, word)
		if i == 3 {
			break
		}
	}
	if err := s.Err(); err != nil {
		fmt.Printf("error: %v\n", err)
	}
}
Output:

[0] "Where"
[1] "are"
[2] "you"
[3] "going"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL