Skip to content

Commit 591d543

Browse files
committed
new realization
1 parent 5b19f01 commit 591d543

File tree

9 files changed

+641
-538
lines changed

9 files changed

+641
-538
lines changed

README.md

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
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
1414
package 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

5662
func 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
7385
friend
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.

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/wmentor/tokens
22

33
go 1.15
4+
5+
require github.com/wmentor/tbuf v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/wmentor/tbuf v1.0.0 h1:KHfiIdOTWor7a/5dSoLovxgGuipLAAU1X4+U3jzdIZ4=
2+
github.com/wmentor/tbuf v1.0.0/go.mod h1:YvYY3BMph/UVPSIMbQoraxgr7+7DCAvYSSJHZk2gsBQ=

runes.go renamed to runes/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tokens
1+
package runes
22

33
const (
44
ASYMP rune = '≈'

0 commit comments

Comments
 (0)