Permalink
Browse files

Add Text serialization function txt.

It's similar to the existing str, except that we're using strict
ByteStrings (since text-icu does) and we need a Converter value to do
proper unicode conversion.

str still uses Char8.pack, which isn't ideal.
  • Loading branch information...
1 parent 230bcb1 commit f6fbc8842cba5f738905a3364a03693793f7ebe6 @adimit committed May 1, 2012
Showing with 9 additions and 0 deletions.
  1. +9 −0 Text/Search/Sphinx/Put.hs
@@ -8,6 +8,10 @@ import Data.ByteString.Lazy.Char8 (pack)
import qualified Data.ByteString.Lazy as BS
import qualified Text.Search.Sphinx.Types as T
+import Data.Text (Text)
+import qualified Data.Text.ICU.Convert as ICU
+import qualified Data.ByteString as Strict (length)
+
num = putWord32be . fromIntegral
num64 i = putWord64be $ fromIntegral i
@@ -39,3 +43,8 @@ foldPuts :: [Put] -> Put
foldPuts [] = return ()
foldPuts [p] = p
foldPuts (p:ps) = p >> foldPuts ps
+
+txt :: ICU.Converter -> Text -> Put
+txt conv t = do let bs = ICU.fromUnicode conv t
+ num (fromEnum $ Strict.length bs)
+ putByteString bs

0 comments on commit f6fbc88

Please sign in to comment.