-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move all endianness/byte-order CPP into one module #659
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense to do so. It can wait until I make a pass at removing the various old-ghc cruft, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, we can already reuse the type itself?.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's added in This isn't a battle worth fighting, though. |
||
= 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the name intentionally different from
targetByteOrder
for cross-compilation purposes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
bytestring
is not a compiler the notion of "target" doesn't really make sense. The name inbase
is confusing.