module Language.Phonetic.DoubleMetaphone
(
doubleMetaphoneBounded,
doubleMetaphone
) where
import Foreign.C.String (CString)
import Foreign.C.Types (CInt (..))
import Foreign.Marshal.Array (allocaArray, peekArray)
import Foreign.Ptr (Ptr)
import Data.ByteString (ByteString, useAsCString)
import Data.ByteString.Unsafe (unsafePackMallocCString)
import System.IO.Unsafe (unsafePerformIO)
doubleMetaphoneBounded
:: Int
-> ByteString
-> (ByteString, ByteString)
doubleMetaphoneBounded max_len input = unsafePerformIO $
useAsCString input $ \c_str ->
allocaArray 2 $ \results_p -> do
c_double_metaphone c_str results_p (fromIntegral max_len)
rs <- peekArray 2 results_p >>= traverse unsafePackMallocCString
case rs of
[r1, r2] -> return (r1, r2)
_ -> error "doubleMetaphone: |array| != 2, impossible?"
doubleMetaphone
:: ByteString
-> (ByteString, ByteString)
doubleMetaphone = doubleMetaphoneBounded maxBound
foreign import ccall unsafe "double-metaphones.h DoubleMetaphone"
c_double_metaphone
:: CString
-> Ptr CString
-> CInt
-> IO ()