packages feed

country 0.2.3 → 0.2.3.1

raw patch · 6 files changed

+36/−44 lines, 6 filesdep ~primitivedep ~primitive-unlifteddep ~textPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: primitive, primitive-unlifted, text

API changes (from Hackage documentation)

Files

country.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: country-version: 0.2.3+version: 0.2.3.1 synopsis: Country data type and functions description:   The `country` library provides a data type for dealing with@@ -75,7 +75,7 @@     , primitive >= 0.6.4 && <0.8     , primitive-unlifted >= 0.1.3 && <1.0     , scientific >=0.3 && <0.4-    , text >= 1.2 && <1.3+    , text >= 2.0 && <2.1     , text-short >=0.1.3     , unordered-containers >=0.2 && <0.3   default-language: Haskell2010
src/Continent.hs view
@@ -26,7 +26,7 @@ import Control.Monad (forM_) import Control.Monad.ST (runST) import Country.Unexposed.Continents (continentAList)-import Country.Unexposed.Util (mapTextArray,charToWord16,word16ToInt,timesTwo)+import Country.Unexposed.Util (mapTextArray,charToWord8,word16ToInt,timesTwo) import Country.Unsafe (Country(Country)) import Data.Char (toLower) import Data.Text (Text)@@ -51,8 +51,8 @@   m <- TA.new (timesTwo numberOfContinents)   forM_ continentNameDb $ \(n,_,(a1,a2)) -> do     let ix = timesTwo (fromIntegral n)-    TA.unsafeWrite m ix (charToWord16 a1)-    TA.unsafeWrite m (ix + 1) (charToWord16 a2)+    TA.unsafeWrite m ix (charToWord8 a1)+    TA.unsafeWrite m (ix + 1) (charToWord8 a2)   return m {-# NOINLINE allAlphaUpper #-} 
src/Continent/Unsafe.hs view
@@ -56,4 +56,3 @@   show NorthAmerica = "NorthAmerica"   show Oceania = "Oceania"   show SouthAmerica = "SouthAmerica"-  show (Continent n) = "Continent " ++ show n
src/Country.hs view
@@ -40,7 +40,7 @@ import Country.Unexposed.Names (numberOfPossibleCodes,alphaTwoHashMap,alphaThreeHashMap,decodeMap,decodeMapUtf8,decodeNumeric,encodeEnglish,encodeEnglishShort) import Country.Unexposed.Trie (Trie,trieFromList,trieParser) import Country.Unexposed.TrieByte (TrieByte,trieByteFromList,trieByteParser)-import Country.Unexposed.Util (mapTextArray,charToWord16,word16ToInt,timesTwo,timesThree)+import Country.Unexposed.Util (mapTextArray,charToWord8,word16ToInt,timesTwo,timesThree) import Country.Unsafe (Country(..)) import Data.Bytes.Types (Bytes(Bytes)) import Data.ByteString (ByteString)@@ -114,8 +114,8 @@ --   countries. It strives to handle any source language. Open an --   issue on the issue tracker if there are names that are missing. decode :: Text -> Maybe Country-decode (TI.Text (TA.Array arr) off16 len16) =-  case (BytesHashMap.lookup (Bytes (ByteArray arr) (off16 * 2) (len16 * 2)) hashMapUtf16) of+decode (TI.Text (TA.ByteArray arr) off8 len8) =+  case (BytesHashMap.lookup (Bytes (ByteArray arr) off8 len8) hashMapUtf8) of     Nothing -> Nothing     Just w -> Just (Country (fromIntegral w)) @@ -164,8 +164,8 @@   m <- TA.new (timesTwo numberOfCountries)   forM_ countryNameQuads $ \(n,_,(a1,a2),_) -> do     let ix = timesTwo (indexOfCountry (Country n))-    TA.unsafeWrite m ix (charToWord16 a1)-    TA.unsafeWrite m (ix + 1) (charToWord16 a2)+    TA.unsafeWrite m ix (charToWord8 a1)+    TA.unsafeWrite m (ix + 1) (charToWord8 a2)   return m {-# NOINLINE allAlphaTwoUpper #-} @@ -174,9 +174,9 @@   m <- TA.new (timesThree numberOfCountries)   forM_ countryNameQuads $ \(n,_,_,(a1,a2,a3)) -> do     let ix = timesThree (indexOfCountry (Country n))-    TA.unsafeWrite m ix (charToWord16 a1)-    TA.unsafeWrite m (ix + 1) (charToWord16 a2)-    TA.unsafeWrite m (ix + 2) (charToWord16 a3)+    TA.unsafeWrite m ix (charToWord8 a1)+    TA.unsafeWrite m (ix + 1) (charToWord8 a2)+    TA.unsafeWrite m (ix + 2) (charToWord8 a3)   return m {-# NOINLINE allAlphaThreeUpper #-} 
src/Country/Unexposed/Names.hs view
@@ -30,7 +30,6 @@ import Data.Word  import Control.DeepSeq (NFData)-import Control.Exception (bracket) import Country.Unexposed.Alias (aliases) import Country.Unexposed.Encode.English (countryNameQuads) import Data.Bytes.Types (Bytes(Bytes))@@ -42,34 +41,28 @@ import Data.Primitive (Array,indexArray,newArray,unsafeFreezeArray,writeArray) import Data.Primitive (sizeOf) import Data.Primitive (writeByteArray,indexByteArray,unsafeFreezeByteArray,newByteArray)-import Data.Primitive.Array (Array(..)) import Data.Primitive.ByteArray (ByteArray(..)) import Data.Primitive.Types (Prim) import Data.Text (Text)-import Data.Text.Encoding (encodeUtf8)+import Data.Text.Encoding (encodeUtf8,encodeUtf16BE) import Data.Text.Short (ShortText)-import Data.Word (Word16) import Foreign.Storable (Storable) import GHC.Generics (Generic)-import System.Entropy (openHandle,closeHandle)-import System.IO.Unsafe (unsafePerformIO) -import qualified Data.Text as T import qualified Data.Aeson as AE import qualified Data.Aeson.Types as AET+import qualified Data.Bytes as Bytes+import qualified Data.Bytes.HashMap.Word as BytesHashMap+import qualified Data.ByteString as ByteString import qualified Data.HashMap.Strict as HM import qualified Data.List as L import qualified Data.Primitive.Unlifted.Array as PM-import qualified Data.Text as T import qualified Data.Scientific as SCI-import qualified GHC.Exts as Exts-import qualified System.IO as IO-import qualified Data.Bytes as Bytes-import qualified Data.Bytes.HashMap.Word as BytesHashMap-import qualified Data.Text.Internal as Text+import qualified Data.Text as T import qualified Data.Text.Array as Text+import qualified Data.Text.Internal as Text import qualified Data.Text.Short as TS-import qualified Data.ByteString as ByteString+import qualified GHC.Exts as Exts  -- | The name of a country given in English encodeEnglish :: Country -> Text@@ -140,11 +133,10 @@ hashMapUtf16 :: BytesHashMap.Map hashMapUtf16 = BytesHashMap.fromTrustedList   ( map-    (\(a,Text.Text (Text.Array arr) off16 len16) ->-      (Bytes (ByteArray arr) (off16 * 2) (len16 * 2),fromIntegral a)+    (\(a, str) ->+      (Bytes.fromByteString $ encodeUtf16BE str, fromIntegral a)     ) countryPairs   )- countryPairs :: [(Word16,Text)] {-# NOINLINE countryPairs #-} countryPairs =@@ -212,9 +204,6 @@ actualNumberOfCountries = length countryNameQuads {-# NOINLINE actualNumberOfCountries #-} --codeToEnum :: Word16 -> Int-codeToEnum w = indexByteArray countryCodeToSequentialMapping (word16ToInt w)  -- todo: add support for encoding directly to bytestring. -- Also, add suport for ToJSONKey and FromJSONKey once everything
src/Country/Unexposed/Util.hs view
@@ -3,7 +3,7 @@  module Country.Unexposed.Util   ( mapTextArray-  , charToWord16+  , charToWord8   , word16ToChar   , word16ToInt   , timesTwo@@ -13,7 +13,7 @@  import Data.Bits (unsafeShiftL,unsafeShiftR) import Data.Char (chr,ord)-import Data.Word (Word16)+import Data.Word (Word8,Word16) import GHC.Exts (sizeofByteArray#) import GHC.Int (Int(I#)) @@ -21,26 +21,30 @@   mapTextArray :: (Char -> Char) -> TA.Array -> TA.Array-mapTextArray f a@(TA.Array inner) = TA.run $ do-  let len = half (I# (sizeofByteArray# inner))+mapTextArray f a@(TA.ByteArray inner) = TA.run $ do+  let len = I# (sizeofByteArray# inner)   m <- TA.new len-  TA.copyI m 0 a 0 len+  TA.copyI len m 0 a 0   let go !ix = if ix < len         then do-          TA.unsafeWrite m ix (charToWord16 (f (word16ToChar (TA.unsafeIndex a ix))))+          TA.unsafeWrite m ix (charToWord8 (f (word8ToChar (TA.unsafeIndex a ix))))           go (ix + 1)         else return ()   go 0   return m {-# INLINE mapTextArray #-} +charToWord8 :: Char -> Word8+charToWord8 = fromIntegral . ord+{-# INLINE charToWord8 #-}++word8ToChar :: Word8 -> Char+word8ToChar = chr . fromIntegral+{-# INLINE word8ToChar #-}+ word16ToChar :: Word16 -> Char word16ToChar = chr . fromIntegral {-# INLINE word16ToChar #-}--charToWord16 :: Char -> Word16-charToWord16 = fromIntegral . ord-{-# INLINE charToWord16 #-}  word16ToInt :: Word16 -> Int word16ToInt = fromIntegral