Commits

Bryan O'Sullivan committed ed52dc1

Initial commit

  • Participants

Comments (0)

Files changed (5)

File Data/Text/ICU/Conversion.hs

+{-# LANGUAGE EmptyDataDecls, ForeignFunctionInterface #-}
+module Data.Text.ICU.Conversion
+    (
+    ) where
+
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Internal as B
+import Data.ByteString (ByteString)
+import Data.Text.Foreign
+import Data.Text
+import System.IO.Unsafe (unsafePerformIO)
+import Foreign.Marshal.Alloc (alloca)
+import Foreign.Ptr (Ptr)
+import Foreign.C.String (CString)
+import Data.Int (Int32)
+import Data.Word (Word8, Word16)
+
+newtype Encoding = Encoding ByteString
+
+data Converter
+
+fromUnicode :: Ptr UConverter -> Text -> ByteString
+fromUnicode cnv txt = unsafePerformIO $ do
+  useAsPtr txt $ \ptr len -> do
+    dstLen <- getMaxBytesForString len cnv
+    B.createAndTrim dstLen $ \dst -> do
+      alloca $ \errPtr -> do
+        fromIntegral `fmap` fromUChars cnv dst (fromIntegral dstLen) ptr (fromIntegral len) errPtr
+
+data UConverter
+
+foreign import ccall unsafe "__get_max_bytes_for_string" getMaxBytesForString
+    :: Int -> Ptr UConverter -> IO Int
+
+type UChar = Word16
+type UErrorCode = Int
+
+foreign import ccall unsafe "ucnv_fromUChars" fromUChars
+    :: Ptr UConverter -> Ptr Word8 -> Int32 -> Ptr UChar -> Int32
+    -> Ptr UErrorCode -> IO Int32
+Copyright (c) 2009, Bryan O'Sullivan
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#!/usr/bin/env runhaskell
+> import Distribution.Simple
+> main = defaultMain

File cbits/text_icu.c

+#include "unicode/ucnv.h"
+
+#include <stdint.h>
+
+int __get_max_bytes_for_string(int src_length, UConverter *cnv)
+{
+    return UCNV_GET_MAX_BYTES_FOR_STRING(src_length, ucnv_getMaxCharSize(cnv));
+}
+
+int __u_failure(UErrorCode err)
+{
+    return U_FAILURE(err);
+}

File text-icu.cabal

+Name: text-icu
+Version: 0.0
+License: BSD3
+License-file: LICENSE
+Build-Type: Simple
+Cabal-Version: >= 1.2
+
+library
+  build-depends:     base < 5, bytestring, text
+  if impl(ghc >= 6.10)
+    build-depends:   base >= 4
+
+  exposed-modules:
+      Data.Text.ICU.Conversion
+      Data.Text.ICU.Converter
+  c-sources: cbits/text_icu.c
+  extra-libraries: icui18n icuuc icudata