-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all endianness/byte-order CPP into one module
- Loading branch information
Showing
4 changed files
with
64 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{-# LANGUAGE CPP #-} | ||
|
||
-- | Why does this module exist? There is "GHC.ByteOrder" in base. | ||
-- But that module is /broken/ until base-4.14/ghc-8.10, so we | ||
-- can't rely on it until we drop support for older ghcs. | ||
-- See https://gitlab.haskell.org/ghc/ghc/-/issues/20338 | ||
-- and https://gitlab.haskell.org/ghc/ghc/-/issues/18445 | ||
|
||
#include "MachDeps.h" | ||
|
||
module Data.ByteString.Utils.ByteOrder | ||
( ByteOrder(..) | ||
, hostByteOrder | ||
, whenLittleEndian | ||
, whenBigEndian | ||
) where | ||
|
||
data ByteOrder | ||
= LittleEndian | ||
| BigEndian | ||
|
||
hostByteOrder :: ByteOrder | ||
hostByteOrder = | ||
#ifdef WORDS_BIGENDIAN | ||
BigEndian | ||
#else | ||
LittleEndian | ||
#endif | ||
|
||
-- | If the host is little-endian, applies the given function to the given arg. | ||
-- If the host is big-endian, returns the second argument unchanged. | ||
whenLittleEndian :: (a -> a) -> a -> a | ||
whenLittleEndian fun val = case hostByteOrder of | ||
LittleEndian -> fun val | ||
BigEndian -> val | ||
|
||
-- | If the host is little-endian, returns the second argument unchanged. | ||
-- If the host is big-endian, applies the given function to the given arg. | ||
whenBigEndian :: (a -> a) -> a -> a | ||
whenBigEndian fun val = case hostByteOrder of | ||
LittleEndian -> val | ||
BigEndian -> fun val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters