File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,8 @@ import Control.DeepSeq ( NFData )
5757import Control.Monad ( void , zipWithM_ )
5858import Data.Int (Int32 )
5959import Data.Complex (Complex )
60+ import Data.Text (Text )
61+ import qualified Data.Text as T
6062import Foreign ( FunPtr , castPtr )
6163import Foreign.C.String ( withCString )
6264import Foreign.Storable ( Storable , pokeElemOff )
@@ -171,6 +173,17 @@ instance Literal [String] 'R.String where
171173 fromSEXP _ =
172174 failure " fromSEXP" " String expected where some other expression appeared."
173175
176+ instance Literal Text 'R.String where
177+ mkSEXPIO s =
178+ mkSEXPVectorIO sing
179+ [GHC. withCString utf8 (T. unpack s) (R. mkCharCE R. CE_UTF8 )]
180+ fromSEXP (hexp -> String v) =
181+ case map (\ (hexp -> Char xs) -> SVector. toString xs) (SVector. toList v) of
182+ [x] -> T. pack x
183+ _ -> failure " fromSEXP" " Not a singleton vector"
184+ fromSEXP _ =
185+ failure " fromSEXP" " String expected where some other expression appeared."
186+
174187-- | Create a pairlist from an association list. Result is either a pairlist or
175188-- @nilValue@ if the input is the null list. These are two distinct forms. Hence
176189-- why the type of this function is not more precise.
Original file line number Diff line number Diff line change 99{-# LANGUAGE ScopedTypeVariables #-}
1010{-# LANGUAGE TemplateHaskell #-}
1111{-# LANGUAGE ViewPatterns #-}
12+ {-# LANGUAGE OverloadedStrings #-}
1213
1314module Main where
1415
@@ -17,6 +18,7 @@ import H.Prelude as H
1718import qualified Data.Vector.SEXP as SVector
1819import qualified Data.Vector.SEXP.Mutable as SMVector
1920import Control.Memory.Region
21+ import Data.Text (Text )
2022
2123import Control.Applicative
2224import Control.Monad.Trans (liftIO )
@@ -108,9 +110,11 @@ main = H.withEmbeddedR H.defaultConfig $ H.runRegion $ do
108110 (" c(7, 2, 3)" @=? ) =<< [r | v = v2_hs; v[1] <- 7; v |]
109111 io . assertEqual " " " fromList [1,2,3]" . Prelude. show =<< SVector. unsafeFreeze v1
110112
111- let utf8string = " abcd çéõßø"
113+ let utf8string = " abcd çéõßø" :: String
112114 io . assertEqual " " utf8string =<< fromSEXP <$> R. cast (sing :: R. SSEXPTYPE 'R.String ) <$> [r | utf8string_hs |]
113115
116+ let utf8string1 = " abcd çéõßø" :: Text
117+ io . assertEqual " " utf8string1 =<< fromSEXP <$> R. cast (sing :: R. SSEXPTYPE 'R.String ) <$> [r | utf8string1_hs |]
114118
115119 -- Disable gctorture, otherwise test takes too long to execute.
116120 _ <- [r | gctorture2(0) |]
You can’t perform that action at this time.
0 commit comments