Skip to content

Commit c30b3db

Browse files
committed
Add Literal instance for Text.
We add a simple instance for text that goes via String representation.
1 parent d7a87e0 commit c30b3db

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

inline-r/src/Language/R/Literal.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import Control.DeepSeq ( NFData )
5757
import Control.Monad ( void, zipWithM_ )
5858
import Data.Int (Int32)
5959
import Data.Complex (Complex)
60+
import Data.Text (Text)
61+
import qualified Data.Text as T
6062
import Foreign ( FunPtr, castPtr )
6163
import Foreign.C.String ( withCString )
6264
import 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.

inline-r/tests/test-qq.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{-# LANGUAGE ScopedTypeVariables #-}
1010
{-# LANGUAGE TemplateHaskell #-}
1111
{-# LANGUAGE ViewPatterns #-}
12+
{-# LANGUAGE OverloadedStrings #-}
1213

1314
module Main where
1415

@@ -17,6 +18,7 @@ import H.Prelude as H
1718
import qualified Data.Vector.SEXP as SVector
1819
import qualified Data.Vector.SEXP.Mutable as SMVector
1920
import Control.Memory.Region
21+
import Data.Text (Text)
2022

2123
import Control.Applicative
2224
import 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) |]

0 commit comments

Comments
 (0)