Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@TIHan
Last active September 20, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TIHan/8816444 to your computer and use it in GitHub Desktop.
Save TIHan/8816444 to your computer and use it in GitHub Desktop.
module FParsec.Binary
open System
open System.Text
open System.Text.RegularExpressions
open FParsec
[<RequireQualifiedAccess>]
module File =
let readAllString path =
let data =
System.IO.File.ReadAllBytes path
|> Array.map char
String data
let (|Match|_|) (m: string) (str: string) =
if Regex.IsMatch (str, m) then
Some str
else
None
let anyByte : Parser<byte, unit> = anyChar |>> byte
let anyBytes n : Parser<byte array, unit> =
anyString n |>> (fun x -> Array.map byte <| x.ToCharArray ())
let anyInt16 : Parser<int16, unit> =
anyBytes 2
|>> (fun x -> BitConverter.ToInt16 (x, 0))
let anyInt32 : Parser<int, unit> =
anyBytes 4
|>> (fun x -> BitConverter.ToInt32 (x, 0))
let skipAnyByte : Parser<unit, _> =
fun stream ->
stream.Skip (1)
Reply (())
let skipAnyBytes (n: int) : Parser<unit, _> =
fun stream ->
stream.Skip (n)
Reply (())
@allykzam
Copy link

Mind if I fork this and make use of it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment