aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes_example_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2018-09-15 20:44:29 +0700
committerShulhan <ms@kilabit.info>2018-09-17 22:51:17 +0700
commit0f68f37ce159c96d56b4748eee6a9a88c6b7f801 (patch)
treeaad89a072ed73ca6669818c60596c0ab0c3f6333 /lib/bytes/bytes_example_test.go
parent05799acac0c977ea79c62df40de67bb3bef30db5 (diff)
downloadpakakeh.go-0f68f37ce159c96d56b4748eee6a9a88c6b7f801.tar.xz
Merge package "github.com/shuLhan/tekstus", part 1/1
Diffstat (limited to 'lib/bytes/bytes_example_test.go')
-rw-r--r--lib/bytes/bytes_example_test.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
new file mode 100644
index 00000000..141d7f17
--- /dev/null
+++ b/lib/bytes/bytes_example_test.go
@@ -0,0 +1,106 @@
+// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package bytes
+
+import (
+ "fmt"
+)
+
+func ExampleCutUntilToken() {
+ line := []byte(`abc \def ghi`)
+
+ cut, p, found := CutUntilToken(line, []byte("def"), 0, false)
+ fmt.Printf("'%s' %d %t\n", cut, p, found)
+
+ cut, p, found = CutUntilToken(line, []byte("def"), 0, true)
+ fmt.Printf("'%s' %d %t\n", cut, p, found)
+
+ cut, p, found = CutUntilToken(line, []byte("ef"), 0, true)
+ fmt.Printf("'%s' %d %t\n", cut, p, found)
+
+ cut, p, found = CutUntilToken(line, []byte("hi"), 0, true)
+ fmt.Printf("'%s' %d %t\n", cut, p, found)
+
+ // Output:
+ // 'abc \' 8 true
+ // 'abc def ghi' 12 false
+ // 'abc \d' 8 true
+ // 'abc \def g' 12 true
+}
+
+func ExampleEncloseRemove() {
+ line := []byte(`[[ ABC ]] DEF`)
+ leftcap := []byte(`[[`)
+ rightcap := []byte(`]]`)
+
+ got, changed := EncloseRemove(line, leftcap, rightcap)
+
+ fmt.Printf("'%s' %t\n", got, changed)
+ // Output: ' DEF' true
+}
+
+func ExampleEncloseToken() {
+ line := []byte(`// Copyright 2016-2018 "Shulhan <ms@kilabit.info>". All rights reserved.`)
+ token := []byte(`"`)
+ leftcap := []byte(`\`)
+ rightcap := []byte(`_`)
+
+ got, changed := EncloseToken(line, token, leftcap, rightcap)
+
+ fmt.Printf("'%s' %t\n", got, changed)
+ // Output:
+ // '// Copyright 2016-2018 \"_Shulhan <ms@kilabit.info>\"_. All rights reserved.' true
+}
+
+func ExampleIsTokenAt() {
+ line := []byte("Hello, world")
+ token := []byte("world")
+ token2 := []byte("worlds")
+ tokenEmpty := []byte{}
+
+ fmt.Printf("%t\n", IsTokenAt(line, tokenEmpty, 6))
+ fmt.Printf("%t\n", IsTokenAt(line, token, 6))
+ fmt.Printf("%t\n", IsTokenAt(line, token, 7))
+ fmt.Printf("%t\n", IsTokenAt(line, token, 8))
+ fmt.Printf("%t\n", IsTokenAt(line, token2, 8))
+ // Output:
+ // false
+ // false
+ // true
+ // false
+ // false
+}
+
+func ExampleSkipAfterToken() {
+ line := []byte(`abc \def ghi`)
+
+ p, found := SkipAfterToken(line, []byte("def"), 0, false)
+ fmt.Printf("%d %t\n", p, found)
+
+ p, found = SkipAfterToken(line, []byte("def"), 0, true)
+ fmt.Printf("%d %t\n", p, found)
+
+ p, found = SkipAfterToken(line, []byte("ef"), 0, true)
+ fmt.Printf("%d %t\n", p, found)
+
+ p, found = SkipAfterToken(line, []byte("hi"), 0, true)
+ fmt.Printf("%d %t\n", p, found)
+
+ // Output:
+ // 8 true
+ // 12 false
+ // 8 true
+ // 12 true
+}
+
+func ExampleTokenFind() {
+ line := []byte("// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.")
+ token := []byte("right")
+
+ at := TokenFind(line, token, 0)
+
+ fmt.Printf("%d\n", at)
+ // Output: 7
+}