forked from cchalmers/inline-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegex.hs
47 lines (36 loc) · 1.08 KB
/
Regex.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
module Main where
import Language.Rust.Inline
import Foreign.Marshal.Unsafe
import Foreign.Ptr
import Foreign.Marshal.Utils
import Data.ByteString
import Data.Word ( Word64 )
import Data.Int ( Int64 )
extendContext basic
extendContext pointers
setCrateRoot
[ ("regex", "0.2")
]
[rust|
extern crate regex;
use regex::bytes::Regex;
use std::slice::from_raw_parts_mut;
|]
main = do
print $ multipleOfThree "1111100010101110010101" -- 4074389 = 2 (mod 3)
print $ multipleOfThree "100101" -- 37 = 1 (mod 3)
print $ multipleOfThree "101100101001010101111" -- 1462959 = 0 (mod 3)
-- | Check if a bytestring fits a regular expression
multipleOfThree :: ByteString -> Bool
multipleOfThree input = unsafeLocalState $ withByteString input $ \ptr len -> do
out <- [rustIO|
bool {
let input: &mut [u8] = unsafe {
from_raw_parts_mut( $(ptr: *mut u8), $(len: usize) )
};
let regex = Regex::new(r"^(1(01*0)*1|0)*$").unwrap();
regex.is_match(input)
}
|]
pure (toBool out)