packages feed

typesafe-endian (empty) → 0.1.0.0

raw patch · 7 files changed

+350/−0 lines, 7 filesdep +basesetup-changed

Dependencies added: base

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, John Ericson++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.++    * Neither the name of John Ericson nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Endian.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE UnicodeSyntax, CPP #-}++module Data.Endian (+    EndianSensitive(),+    BigEndian(),+    LittleEndian(),+    toBigEndian,+    fromBigEndian,+    toLittleEndian,+    fromLittleEndian+  ) where++import Data.Endian.Internal+import Data.Endian.Wrap++#include <HsBaseConfig.h>++-- | Convert from the native format to big-endian+toBigEndian      ∷ EndianSensitive α ⇒ α → BigEndian α+-- | Convert from big-endian to the native format+fromBigEndian    ∷ EndianSensitive α ⇒ BigEndian α → α+-- | Convert from the native format to little-endian+toLittleEndian   ∷ EndianSensitive α ⇒ α → LittleEndian α+-- | Convert from little-endian to the native format+fromLittleEndian ∷ EndianSensitive α ⇒ LittleEndian α → α++beHelp ∷ EndianSensitive α ⇒ α → α+leHelp ∷ EndianSensitive α ⇒ α → α++#ifdef WORDS_BIGENDIAN+beHelp = id+leHelp = swapEndian+#else+beHelp = swapEndian+leHelp = id+#endif++toBigEndian    = BE . beHelp+toLittleEndian = LE . leHelp+fromBigEndian    (BE a) = beHelp a+fromLittleEndian (LE a) = leHelp a++{-# INLINE toBigEndian #-}+{-# INLINE fromBigEndian #-}+{-# INLINE toLittleEndian #-}+{-# INLINE fromLittleEndian #-}
+ src/Data/Endian/Internal.hs view
@@ -0,0 +1,186 @@+{-# LANGUAGE UnicodeSyntax, CPP #-}++module Data.Endian.Internal (+    EndianSensitive(swapEndian)+  ) where++import Data.Int+import Data.Word+import Data.Bits+import Foreign.C.Types+import Foreign.Ptr (IntPtr, WordPtr)+import System.Posix.Types (CSsize)++#include <HsBaseConfig.h>++-- | Raw, endian-sensitive data+class EndianSensitive α where+  -- | Invert the the endianness of the argument+  swapEndian ∷ α → α++instance EndianSensitive α ⇒ EndianSensitive [α] where+  swapEndian = map swapEndian++instance EndianSensitive Word16 where+  swapEndian = (`rotateR` 8)+  {-# INLINE swapEndian #-}++instance EndianSensitive Word32 where+  swapEndian x  =  (x                  `shiftR` 24)+               .|. ((x .&. 0x00FF0000) `shiftR` 8)+               .|. ((x .&. 0x0000FF00) `shiftL` 8)+               .|. (x                  `shiftL` 24)+  {-# INLINE swapEndian #-}++instance EndianSensitive Word64 where+  swapEndian x+#if WORD_SIZE_IN_BITS == 32+    =  fromIntegral (swapEndian hi)+   .|. (fromIntegral (swapEndian lo) `shiftL` 32)+      where lo, hi ∷ Word32+            lo = fromIntegral x+            hi = fromIntegral (x `shiftR` 32)+#else+    =  (x                          `shiftR` 56)+   .|. ((x .&. 0x00FF000000000000) `shiftR` 40)+   .|. ((x .&. 0x0000FF0000000000) `shiftR` 24)+   .|. ((x .&. 0x000000FF00000000) `shiftR` 8)+   .|. ((x .&. 0x00000000FF000000) `shiftL` 8)+   .|. ((x .&. 0x0000000000FF0000) `shiftL` 24)+   .|. ((x .&. 0x000000000000FF00) `shiftL` 40)+   .|. (x                          `shiftL` 56)+#endif+  {-# INLINE swapEndian #-}++instance EndianSensitive Int16 where+  swapEndian = (`rotateR` 8)+  {-# INLINE swapEndian #-}++instance EndianSensitive Int32 where+  swapEndian = fromIntegral . (swapEndian ∷ Word32 → Word32) . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive Int64 where+  swapEndian = fromIntegral . (swapEndian ∷ Word64 → Word64) . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CShort where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_SHORT → HTYPE_SHORT)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CUShort where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UNSIGNED_SHORT → HTYPE_UNSIGNED_SHORT)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CInt where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_INT → HTYPE_INT)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CUInt where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UNSIGNED_INT → HTYPE_UNSIGNED_INT)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CLong where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_LONG → HTYPE_LONG)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CULong where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UNSIGNED_LONG → HTYPE_UNSIGNED_LONG)+             . fromIntegral+  {-# INLINE swapEndian #-}++#ifdef HAVE_LONG_LONG+instance EndianSensitive CLLong where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_LONG_LONG → HTYPE_LONG_LONG)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CULLong where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UNSIGNED_LONG_LONG+                           → HTYPE_UNSIGNED_LONG_LONG)+             . fromIntegral+  {-# INLINE swapEndian #-}+#endif++instance EndianSensitive CSize where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_SIZE_T → HTYPE_SIZE_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CSsize where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_SSIZE_T → HTYPE_SSIZE_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CUIntPtr where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UINTPTR_T → HTYPE_UINTPTR_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CIntPtr where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_INTPTR_T → HTYPE_INTPTR_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CPtrdiff where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_PTRDIFF_T → HTYPE_PTRDIFF_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CUIntMax where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UINTMAX_T → HTYPE_UINTMAX_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive CIntMax where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_INTMAX_T → HTYPE_INTMAX_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive WordPtr where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_UINTPTR_T → HTYPE_UINTPTR_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++instance EndianSensitive IntPtr where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_INTPTR_T → HTYPE_INTPTR_T)+             . fromIntegral+  {-# INLINE swapEndian #-}++#ifdef HTYPE_WCHAR_T+instance EndianSensitive CWchar where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_WCHAR_T → HTYPE_WCHAR_T)+             . fromIntegral+  {-# INLINE swapEndian #-}+#endif++#ifdef HTYPE_WINT_T+instance EndianSensitive CWint where+  swapEndian = fromIntegral+             . (swapEndian ∷ HTYPE_WINT_T → HTYPE_WINT_T)+             . fromIntegral+  {-# INLINE swapEndian #-}+#endif
+ src/Data/Endian/Unsafe.hs view
@@ -0,0 +1,37 @@+{-# LANGUAGE UnicodeSyntax #-}++module Data.Endian.Unsafe (+    BigEndian(),+    LittleEndian(),++    unsafeAssertBigEndian,+    unsafeAssertLittleEndian,++    unsafeUnwrapBigEndian,+    unsafeUnwrapLittleEndian,++    swapEndian+  ) where++import Data.Endian.Internal+import Data.Endian.Wrap++-- | put in BigEndian newtype without any swapping+unsafeAssertBigEndian    ∷ EndianSensitive α ⇒ α → BigEndian α+unsafeAssertBigEndian    = BE+{-# INLINE unsafeAssertBigEndian #-}++-- | put in LittleEndian newtype without any swapping+unsafeAssertLittleEndian ∷ EndianSensitive α ⇒ α → LittleEndian α+unsafeAssertLittleEndian = LE+{-# INLINE unsafeAssertLittleEndian #-}++-- | pull out of BigEndian newtype without any swapping+unsafeUnwrapBigEndian    ∷ EndianSensitive α ⇒ BigEndian α → α+unsafeUnwrapBigEndian    (BE a) = a+{-# INLINE unsafeUnwrapBigEndian #-}++-- | pull out of LittleEndian newtype without any swapping+unsafeUnwrapLittleEndian ∷ EndianSensitive α ⇒ LittleEndian α → α+unsafeUnwrapLittleEndian (LE a) = a+{-# INLINE unsafeUnwrapBigEndian #-}
+ src/Data/Endian/Wrap.hs view
@@ -0,0 +1,8 @@+{-# LANGUAGE UnicodeSyntax #-}++module Data.Endian.Wrap where++-- | Wrapper, guaranteeing enclosed type is big-endian+newtype BigEndian α = BE α+-- | Wrapper, guaranteeing enclosed type is little-endian+newtype LittleEndian α = LE α
+ typesafe-endian.cabal view
@@ -0,0 +1,41 @@+Name:                typesafe-endian+Version:             0.1.0.0+Category:            Data+Stability:           experimental+Synopsis:            Enforce endianness with types+Description:+  This package provides newtype wrappers for separating data with specified+  endianness from other data of the same type with normal, system-specific+  endianness. Since these wrappers are newtypes, no runtime overhead is+  incurred.++  Currently the underlying 'EndianSensitive' typeclass its instances are taken+  directly from the 'data-endian' package. However, if Haskell or GHC ever gets+  a built-in equivalent, like as is proposed in+  'http://ghc.haskell.org/trac/ghc/ticket/7902', it should be trivial to update+  this to use that instead.++Homepage:            https://github.com/Ericson2314/typesafe-endian+Bug-Reports:         https://github.com/Ericson2314/typesafe-endian/issues++Author:              John Ericson <Ericson2314@Yahoo.com>+Maintainer:          John Ericson <Ericson2314@Yahoo.com>+Copyright:           2013 John Ericson <Ericson2314@Yahoo.com>+License:             BSD3+License-File:        LICENSE++Cabal-Version:       >= 1.6.0+Build-Type:          Simple++Source-Repository head+  Type:                git+  Location:            git://github.com/Ericson2314/typesafe-endian++Library+  Build-Depends:       base < 5+  Hs-Source-Dirs:      src+  GHC-Options:         -Wall+  Exposed-Modules:     Data.Endian+                       Data.Endian.Unsafe+  Other-Modules:       Data.Endian.Internal+                       Data.Endian.Wrap