66[ ![ https://pkg.go.dev/github.com/wmentor/tokens ] ( https://pkg.go.dev/badge/github.com/wmentor/tokens.svg )] ( https://pkg.go.dev/github.com/wmentor/tokens )
77[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
88
9- Text to tokens Go library. There are two ways fetch tokens from text. First is token chan and second is token callback.
9+ Text to tokens Go library.
1010
11- # Token chan
11+ # Token get insensitive mode
1212
1313``` go
1414package main
@@ -24,7 +24,13 @@ func main() {
2424
2525 txt := " Hello, my little friend!"
2626
27- for tok := range tokens.Stream (strings.NewReader (txt)) {
27+ tokenizer := tokens.New (strings.NewReader (txt))
28+
29+ for {
30+ tok , err := range tokenizer.Token ()
31+ if err != nil { // io.EOF
32+ break
33+ }
2834 fmt.Println (tok)
2935 }
3036}
@@ -55,9 +61,15 @@ import (
5561
5662func main () {
5763
58- txt := " Hello, my liTTle friend!"
64+ txt := " Hello, my little friend!"
65+
66+ tokenizer := tokens.New (strings.NewReader (txt), tokens.WithCaseSensitive ())
5967
60- for tok := range tokens.Stream (strings.NewReader (txt), tokens.OptCaseSensitive ) {
68+ for {
69+ tok , err := range tokenizer.Token ()
70+ if err != nil { // io.EOF
71+ break
72+ }
6173 fmt.Println (tok)
6274 }
6375}
@@ -73,71 +85,3 @@ liTTle
7385friend
7486!
7587```
76-
77- # Token callback
78-
79- ``` go
80- package main
81-
82- import (
83- " fmt"
84- " strings"
85-
86- " github.com/wmentor/tokens"
87- )
88-
89- func main () {
90-
91- txt := " Hello, my little friend!"
92-
93- tokens.Process (strings.NewReader (txt), func (w string ) {
94- fmt.Println (w)
95- })
96- }
97- ```
98-
99- Result:
100-
101- ```
102- hello
103- ,
104- my
105- little
106- friend
107- !
108- ```
109-
110- Case sensitive mode:
111-
112- ``` go
113- package main
114-
115- import (
116- " fmt"
117- " strings"
118-
119- " github.com/wmentor/tokens"
120- )
121-
122- func main () {
123-
124- txt := " Hello, my liTtLe fRiEnd!"
125-
126- tokens.Process (strings.NewReader (txt), func (w string ) {
127- fmt.Println (w)
128- }, tokens.OptCaseSensitive )
129- }
130- ```
131-
132- Result:
133-
134- ```
135- Hello
136- ,
137- my
138- liTtLe
139- fRiEnd
140- !
141- ```
142-
143- Unlike the first case, we don't create new chan and goroutine. This method is more efficient especially when we process a large number of short lines.
0 commit comments