bytes

package
v0.0.0-...-1dacd80 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlphaNum

func AlphaNum() parser.Func[byte, byte]

AlphaNum parses either an alphabet letter or digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.AlphaNum()

	fmt.Println(p([]byte("a")))
	fmt.Println(p([]byte("9")))
	fmt.Println(p([]byte("!")))

}
Output:

97 [] <nil>
57 [] <nil>
33 [] letter or digit, or, 2 errors occurred:
	* letter, satisfy, actual '0x21', unexpected
	* digit, satisfy, actual '0x21', unexpected

func Any

func Any() parser.Func[byte, byte]

Any parses any byte.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Any()

	fmt.Println(p([]byte("apple")))
	fmt.Println(p(nil))

}
Output:

97 [112 112 108 101] <nil>
0 [] any, unexpected EOF

func Byte

func Byte(b byte) parser.Func[byte, byte]

Byte parses a byte and succeeds if the byte is equal to `b`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Byte(byte('!'))

	fmt.Println(p([]byte("!")))
	fmt.Println(p([]byte("a")))
	fmt.Println(p([]byte("")))

}
Output:

33 [] <nil>
97 [] byte, expected '0x21', actual '0x61', unexpected
0 [] byte, unexpected EOF

func Bytes

func Bytes(s []byte) parser.Func[byte, []byte]

Bytes parses the bytes `s`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Bytes([]byte("golang"))

	fmt.Println(p([]byte("golang")))
	fmt.Println(p([]byte("goto")))

}
Output:

[103 111 108 97 110 103] [] <nil>
[103 111 116] [111] bytes, cmp, expected "golang", actual "got", unexpected

func Cmp

func Cmp(s []byte, cmp func(l, r byte) bool) parser.Func[byte, []byte]

Cmp parses the bytes `s`, using `cmp` to compare each character.

Example
package main

import (
	"fmt"
	"unicode"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Cmp([]byte("golang"), func(l, r byte) bool {
		return unicode.ToLower(rune(l)) == unicode.ToLower(rune(r))
	})

	fmt.Println(p([]byte("Golang")))
	fmt.Println(p([]byte("goto")))

}
Output:

[71 111 108 97 110 103] [] <nil>
[103 111 116] [111] cmp, expected "golang", actual "got", unexpected

func CrLf

func CrLf() parser.Func[byte, []byte]

CrLf parses carriage return and newline (`"\r\n"`), returning the newline character.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.CrLf()

	fmt.Println(p([]byte("\r\n")))
	fmt.Println(p([]byte("\r")))
	fmt.Println(p([]byte("\n")))

}
Output:

[13 10] [] <nil>
[13] [] crlf, and, unexpected EOF
[10] [] crlf, and, expected '0x0d', actual '0x0a', unexpected

func Digit

func Digit() parser.Func[byte, byte]

Digit parses a base-10 digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Digit()

	fmt.Println(p([]byte("9")))
	fmt.Println(p([]byte("A")))

}
Output:

57 [] <nil>
65 [] digit, satisfy, actual '0x41', unexpected

func Fold

func Fold(s []byte) parser.Func[byte, []byte]

Fold parses the bytes `s`, are equal under Unicode case-folding.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Fold([]byte("golang"))

	fmt.Println(p([]byte("Golang")))
	fmt.Println(p([]byte("goto")))

}
Output:

[71 111 108 97 110 103] [] <nil>
[103 111 116] [111] bytes fold, cmp, expected "golang", actual "got", unexpected

func HexDigit

func HexDigit() parser.Func[byte, byte]

HexDigit parses a hexdecimal digit with uppercase and lowercase.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.HexDigit()

	fmt.Println(p([]byte("7")))
	fmt.Println(p([]byte("F")))
	fmt.Println(p([]byte("h")))

}
Output:

55 [] <nil>
70 [] <nil>
104 [] hex digit, satisfy, actual '0x68', unexpected

func Letter

func Letter() parser.Func[byte, byte]

Letter parses an alphabet letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Letter()

	fmt.Println(p([]byte("a")))
	fmt.Println(p([]byte("A")))
	fmt.Println(p([]byte("9")))

}
Output:

97 [] <nil>
65 [] <nil>
57 [] letter, satisfy, actual '0x39', unexpected

func Lower

func Lower() parser.Func[byte, byte]

Lower parses a lowercase letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Lower()

	fmt.Println(p([]byte("a")))
	fmt.Println(p([]byte("A")))

}
Output:

97 [] <nil>
65 [] lowercase letter, satisfy, actual '0x41', unexpected

func NewLine

func NewLine() parser.Func[byte, byte]

NewLine parses a newline character (`'\n'`).

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.NewLine()

	fmt.Println(p([]byte("\r")))
	fmt.Println(p([]byte("\n")))

}
Output:

13 [] newline, expected '0x0a', actual '0x0d', unexpected
10 [] <nil>

func OctDigit

func OctDigit() parser.Func[byte, byte]

OctDigit parses an octal digit.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.OctDigit()

	fmt.Println(p([]byte("7")))
	fmt.Println(p([]byte("8")))

}
Output:

55 [] <nil>
56 [] octal digit, satisfy, actual '0x38', unexpected

func Space

func Space() parser.Func[byte, byte]

Space parse a single whitespace.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Space()

	fmt.Println(p([]byte(" ")))
	fmt.Println(p([]byte("  ")))
	fmt.Println(p([]byte("!")))
	fmt.Println(p([]byte("")))

}
Output:

32 [] <nil>
32 [32] <nil>
33 [] whitespace, satisfy, actual '0x21', unexpected
0 [] whitespace, satisfy, unexpected EOF

func Spaces

func Spaces() parser.Func[byte, []byte]

Spaces parse zero or more spaces.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Spaces()

	fmt.Println(p([]byte(" ")))
	fmt.Println(p([]byte("  ")))
	fmt.Println(p([]byte("!")))
	fmt.Println(p([]byte("")))

}
Output:

[32] [] <nil>
[32 32] [] <nil>
[] [33] <nil>
[] [] <nil>

func Tab

func Tab() parser.Func[byte, byte]

Tab parses a tab character (`'\t'`).

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Tab()

	fmt.Println(p([]byte("\t")))
	fmt.Println(p([]byte(" ")))

}
Output:

9 [] <nil>
32 [] tab, expected '0x09', actual '0x20', unexpected

func Take

func Take(n int) parser.Func[byte, []byte]

Take reads a bytes of length `n`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Take(2)

	fmt.Println(p([]byte("let")))
	fmt.Println(p([]byte("1")))

}
Output:

[108 101] [116] <nil>
[] [49] take, unexpected EOF

func TakeOf

func TakeOf[T any]() parser.Func[byte, []byte]

Take reads a bytes of length `T`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.TakeOf[uint16]()

	fmt.Println(p([]byte{0x01, 0x02, 0x03, 0x04}))
	fmt.Println(p([]byte{0x01}))

}
Output:

[1 2] [3 4] <nil>
[] [1] take of, take, unexpected EOF

func TakeUntil

func TakeUntil(b ...byte) parser.Func[byte, []byte]

TakeUntil reads a range of 0 or more tokens until `b` is found.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.TakeUntil('\r')

	fmt.Println(p([]byte("To: user@example.com\r\n")))
	fmt.Println(p([]byte("foobar")))

}
Output:

[84 111 58 32 117 115 101 114 64 101 120 97 109 112 108 101 46 99 111 109] [13 10] <nil>
[] [] take until, expected [0x0d], actual "foobar", unexpected

func Upper

func Upper() parser.Func[byte, byte]

Upper parses an uppercase letter.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/bytes"
)

func main() {
	p := bytes.Upper()

	fmt.Println(p([]byte("a")))
	fmt.Println(p([]byte("A")))

}
Output:

97 [] uppercase letter, satisfy, actual '0x61', unexpected
65 [] <nil>

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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