Added MatchString
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
package bytes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -87,3 +88,22 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
||||
return gigaparsec.Succeed(true, string(dst), next, gigaparsec.MessageOK(input.Pos())), nil
|
||||
}
|
||||
}
|
||||
|
||||
func MatchString(s string) gigaparsec.Parser[byte, string] {
|
||||
expected := fmt.Sprintf("%q", s)
|
||||
b := []byte(s)
|
||||
return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) {
|
||||
dst := make([]byte, len(s))
|
||||
_, next, err := input.Read(dst)
|
||||
if errors.Is(err, io.EOF) {
|
||||
return gigaparsec.Fail[byte, string](false, gigaparsec.MessageEnd(input.Pos(), expected)), nil
|
||||
}
|
||||
if err != nil {
|
||||
return gigaparsec.Result[byte, string]{}, fmt.Errorf("MatchString: %w", err)
|
||||
}
|
||||
if !bytes.Equal(dst, b) {
|
||||
return gigaparsec.Fail[byte, string](false, gigaparsec.MakeMessage(input.Pos(), string(dst), expected)), nil
|
||||
}
|
||||
return gigaparsec.Succeed(true, s, next, gigaparsec.MessageOK(input.Pos())), nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user