-
Notifications
You must be signed in to change notification settings - Fork 722
Use text package to decode UTF-8 instead of doing it ourselves #11462
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
Open
Bodigrim
wants to merge
3
commits into
master
Choose a base branch
from
utf8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−250
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,110 +1,10 @@ | ||
| module Distribution.Utils.String | ||
| ( -- * Encode to/from UTF8 | ||
| decodeStringUtf8 | ||
| , encodeStringUtf8 | ||
| , trim | ||
| ( trim | ||
| ) where | ||
|
|
||
| import Data.Bits | ||
| import Data.Char (chr, ord) | ||
| import Data.List (dropWhileEnd) | ||
| import Data.Word | ||
| import GHC.Unicode (isSpace) | ||
|
|
||
| -- | Decode 'String' from UTF8-encoded octets. | ||
| -- | ||
| -- Invalid data in the UTF8 stream (this includes code-points @U+D800@ | ||
| -- through @U+DFFF@) will be decoded as the replacement character (@U+FFFD@). | ||
| -- | ||
| -- See also 'encodeStringUtf8' | ||
| decodeStringUtf8 :: [Word8] -> String | ||
| decodeStringUtf8 = go | ||
| where | ||
| go :: [Word8] -> String | ||
| go [] = [] | ||
| go (c : cs) | ||
| | c <= 0x7F = chr (fromIntegral c) : go cs | ||
| | c <= 0xBF = replacementChar : go cs | ||
| | c <= 0xDF = twoBytes c cs | ||
| | c <= 0xEF = moreBytes 3 0x800 cs (fromIntegral $ c .&. 0xF) | ||
| | c <= 0xF7 = moreBytes 4 0x10000 cs (fromIntegral $ c .&. 0x7) | ||
| | c <= 0xFB = moreBytes 5 0x200000 cs (fromIntegral $ c .&. 0x3) | ||
| | c <= 0xFD = moreBytes 6 0x4000000 cs (fromIntegral $ c .&. 0x1) | ||
| | otherwise = replacementChar : go cs | ||
|
|
||
| twoBytes :: Word8 -> [Word8] -> String | ||
| twoBytes c0 (c1 : cs') | ||
| | c1 .&. 0xC0 == 0x80 = | ||
| let d = | ||
| (fromIntegral (c0 .&. 0x1F) `shiftL` 6) | ||
| .|. fromIntegral (c1 .&. 0x3F) | ||
| in if d >= 0x80 | ||
| then chr d : go cs' | ||
| else replacementChar : go cs' | ||
| twoBytes _ cs' = replacementChar : go cs' | ||
|
|
||
| moreBytes :: Int -> Int -> [Word8] -> Int -> [Char] | ||
| moreBytes 1 overlong cs' acc | ||
| | overlong <= acc | ||
| , acc <= 0x10FFFF | ||
| , acc < 0xD800 || 0xDFFF < acc = | ||
| chr acc : go cs' | ||
| | otherwise = | ||
| replacementChar : go cs' | ||
| moreBytes byteCount overlong (cn : cs') acc | ||
| | cn .&. 0xC0 == 0x80 = | ||
| moreBytes | ||
| (byteCount - 1) | ||
| overlong | ||
| cs' | ||
| ((acc `shiftL` 6) .|. fromIntegral cn .&. 0x3F) | ||
| moreBytes _ _ cs' _ = | ||
| replacementChar : go cs' | ||
|
|
||
| replacementChar = '\xfffd' | ||
|
|
||
| -- | Encode 'String' to a list of UTF8-encoded octets | ||
| -- | ||
| -- Code-points in the @U+D800@-@U+DFFF@ range will be encoded | ||
| -- as the replacement character (i.e. @U+FFFD@). | ||
| -- | ||
| -- See also 'decodeUtf8' | ||
| encodeStringUtf8 :: String -> [Word8] | ||
| encodeStringUtf8 [] = [] | ||
| encodeStringUtf8 (c : cs) | ||
| | c <= '\x07F' = | ||
| w8 | ||
| : encodeStringUtf8 cs | ||
| | c <= '\x7FF' = | ||
| (0xC0 .|. w8ShiftR 6) | ||
| : (0x80 .|. (w8 .&. 0x3F)) | ||
| : encodeStringUtf8 cs | ||
| | c <= '\xD7FF' = | ||
| (0xE0 .|. w8ShiftR 12) | ||
| : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) | ||
| : (0x80 .|. (w8 .&. 0x3F)) | ||
| : encodeStringUtf8 cs | ||
| | c <= '\xDFFF' = | ||
| 0xEF | ||
| : 0xBF | ||
| : 0xBD -- U+FFFD | ||
| : encodeStringUtf8 cs | ||
| | c <= '\xFFFF' = | ||
| (0xE0 .|. w8ShiftR 12) | ||
| : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) | ||
| : (0x80 .|. (w8 .&. 0x3F)) | ||
| : encodeStringUtf8 cs | ||
| | otherwise = | ||
| (0xf0 .|. w8ShiftR 18) | ||
| : (0x80 .|. (w8ShiftR 12 .&. 0x3F)) | ||
| : (0x80 .|. (w8ShiftR 6 .&. 0x3F)) | ||
| : (0x80 .|. (w8 .&. 0x3F)) | ||
| : encodeStringUtf8 cs | ||
| where | ||
| w8 = fromIntegral (ord c) :: Word8 | ||
| w8ShiftR :: Int -> Word8 | ||
| w8ShiftR = fromIntegral . shiftR (ord c) | ||
|
|
||
| -- @since 3.8.0.0 | ||
| trim :: String -> String | ||
| trim = dropWhile isSpace . dropWhileEnd isSpace |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Given that we're using the same
replacementChar, couldn't we just useData.Text.Encoding.decodeUtf8Lenient? (It's also in 2.0, if you're worried about that.)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.
This is all a bit awkward for historical reasons. In the modern Haskell ecosystem
Cabal-syntaxshould not be parsingByteStringbut with UTF-8 semantics, this is obviously stupid. And allFieldLineStreamandFieldLineshould wrapText, notByteString. But fixing it properly is a significant breaking change and I don't want to disrupt ongoing work on Cabal formatters.