country 0.2.4.0 → 0.2.4.1
raw patch · 5 files changed
+26/−16 lines, 5 filesdep ~aesondep ~primitivedep ~text
Dependency ranges changed: aeson, primitive, text
Files
- country.cabal +4/−4
- src/Country.hs +3/−3
- src/Country/Unexposed/Names.hs +5/−8
- src/Country/Unexposed/Util.hs +12/−1
- test/Spec.hs +2/−0
country.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: country-version: 0.2.4.0+version: 0.2.4.1 synopsis: Country data type and functions description: The `country` library provides a data type for dealing with@@ -61,7 +61,7 @@ Country.Unexposed.TrieByte Country.Unexposed.Util build-depends:- , aeson >=1.2 && <2.2+ , aeson >=1.2 && <2.3 , attoparsec >=0.13 && <0.15 , base >=4.12 && <5 , bytebuild >=0.3.4 && <0.4@@ -72,10 +72,10 @@ , deepseq >= 1.3.0.2 && <1.5 , entropy >=0.4.1.5 && <0.5 , hashable >=1.2 && <1.5- , primitive >= 0.6.4 && <0.9+ , primitive >= 0.6.4 && <0.10 , primitive-unlifted >= 2.1 , scientific >=0.3 && <0.4- , text >= 2.0 && <2.1+ , text >= 2.0 && <2.2 , text-short >=0.1.3 , unordered-containers >=0.2 && <0.3 default-language: Haskell2010
src/Country.hs view
@@ -40,13 +40,13 @@ 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,charToWord8,word16ToInt,timesTwo,timesThree)+import Country.Unexposed.Util (newZeroedByteArray,mapTextArray,charToWord8,word16ToInt,timesTwo,timesThree) import Country.Unsafe (Country(..)) import Data.Bytes.Types (Bytes(Bytes)) import Data.ByteString (ByteString) import Data.Char (toLower) import Data.Coerce (coerce)-import Data.Primitive (writeByteArray,indexByteArray,unsafeFreezeByteArray,newByteArray)+import Data.Primitive (writeByteArray,indexByteArray,unsafeFreezeByteArray) import Data.Primitive.ByteArray (ByteArray(..)) import Data.Primitive.Ptr (indexOffPtr) import Data.Text (Text)@@ -146,7 +146,7 @@ -- | The elements in this array are Word16 positions :: ByteArray positions = runST $ do- m <- newByteArray (timesTwo numberOfPossibleCodes)+ m <- newZeroedByteArray (timesTwo numberOfPossibleCodes) forM_ (zip (enumFrom (0 :: Word16)) countryNameQuads) $ \(ix,(n,_,_,_)) -> do writeByteArray m (word16ToInt n) ix unsafeFreezeByteArray m
src/Country/Unexposed/Names.hs view
@@ -32,6 +32,7 @@ import Control.DeepSeq (NFData) import Country.Unexposed.Alias (aliases) import Country.Unexposed.Encode.English (countryNameQuads)+import Country.Unexposed.Util (newZeroedByteArray) import Data.Bytes.Types (Bytes(Bytes)) import Data.ByteString (ByteString) import Data.Char (toLower,isAlpha,toUpper)@@ -40,7 +41,7 @@ import Data.HashMap.Strict (HashMap) import Data.Primitive (Array,indexArray,newArray,unsafeFreezeArray,writeArray) import Data.Primitive (sizeOf)-import Data.Primitive (writeByteArray,indexByteArray,unsafeFreezeByteArray,newByteArray)+import Data.Primitive (writeByteArray,indexByteArray,unsafeFreezeByteArray) import Data.Primitive.ByteArray (ByteArray(..)) import Data.Primitive.Types (Prim) import Data.Text (Text)@@ -186,7 +187,7 @@ countryCodeToSequentialMapping :: ByteArray countryCodeToSequentialMapping = runST $ do- numbers <- newByteArray (numberOfPossibleCodes * sizeOf (undefined :: Int))+ numbers <- newZeroedByteArray (numberOfPossibleCodes * sizeOf (undefined :: Int)) forM_ (zip [0 :: Int,1..] orderedCountryCodes) $ \(number,code) -> do writeByteArray numbers (word16ToInt code) number unsafeFreezeByteArray numbers@@ -194,7 +195,7 @@ sequentialToCountryCodeMapping :: ByteArray sequentialToCountryCodeMapping = runST $ do- codes <- newByteArray (actualNumberOfCountries * sizeOf (undefined :: Word16))+ codes <- newZeroedByteArray (actualNumberOfCountries * sizeOf (undefined :: Word16)) forM_ (zip [0 :: Int,1..] orderedCountryCodes) $ \(number,code) -> do writeByteArray codes number (code :: Word16) unsafeFreezeByteArray codes@@ -235,11 +236,7 @@ -- | The elements in this array are Word8 (basically boolean) numericValidities :: ByteArray numericValidities = runST $ do- m <- newByteArray numberOfPossibleCodes- let clear !ix = if ix < numberOfPossibleCodes- then writeByteArray m ix (0 :: Word8)- else return ()- clear 0+ m <- newZeroedByteArray numberOfPossibleCodes forM_ countryNameQuads $ \(n,_,_,_) -> do writeByteArray m (word16ToInt n) (1 :: Word8) unsafeFreezeByteArray m
src/Country/Unexposed/Util.hs view
@@ -2,7 +2,8 @@ {-# LANGUAGE MagicHash #-} module Country.Unexposed.Util- ( mapTextArray+ ( newZeroedByteArray+ , mapTextArray , charToWord8 , word16ToChar , word16ToInt@@ -11,14 +12,24 @@ , half ) where +import Control.Monad.Primitive (PrimMonad, PrimState) import Data.Bits (unsafeShiftL,unsafeShiftR) import Data.Char (chr,ord)+import Data.Primitive.ByteArray (MutableByteArray,newByteArray,fillByteArray) import Data.Word (Word8,Word16) import GHC.Exts (sizeofByteArray#) import GHC.Int (Int(I#)) import qualified Data.Text.Array as TA +newZeroedByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m))+newZeroedByteArray len = do+ arr <- newByteArray len+ let arrStart = 0+ let zeroByte = 0+ fillByteArray arr arrStart len zeroByte+ pure arr+{-# INLINE newZeroedByteArray #-} mapTextArray :: (Char -> Char) -> TA.Array -> TA.Array mapTextArray f a@(TA.ByteArray inner) = TA.run $ do
test/Spec.hs view
@@ -65,6 +65,8 @@ === (indexOffPtr ptr 0 :: Word8, indexOffPtr ptr 1 :: Word8) )+ , testProperty "encode-decode-numeric"+ (\x -> Just x === Country.decodeNumeric (Country.encodeNumeric x)) , testGroup "Continent" [ testProperty "encode-decode-alpha-upper" $ \x -> Just x === Continent.decodeAlpha (Continent.alphaUpper x)