-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from andreasabel/issue119
[ fixed #119 ] latin1 encoding: each byte counts as 1 char
- Loading branch information
Showing
7 changed files
with
154 additions
and
30 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
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
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,73 @@ | ||
-- -*- haskell -*- | ||
{ | ||
-- Issue 119, | ||
-- reported 2017-10-11 by Herbert Valerio Riedel, | ||
-- fixed 2020-01-26 by Andreas Abel. | ||
-- | ||
-- Problem was: the computed token length (in number of characters) | ||
-- attached to AlexToken is tailored to UTF8 encoding and wrong | ||
-- for LATIN1 encoding. | ||
|
||
module Main where | ||
|
||
import Control.Monad (unless) | ||
import qualified Data.ByteString as B | ||
import Data.Word | ||
import System.Exit (exitFailure) | ||
} | ||
|
||
%encoding "latin1" | ||
|
||
:- | ||
|
||
[\x01-\xff]+ { False } | ||
[\x00] { True } | ||
|
||
{ | ||
type AlexInput = B.ByteString | ||
|
||
alexGetByte :: AlexInput -> Maybe (Word8,AlexInput) | ||
alexGetByte = B.uncons | ||
|
||
alexInputPrevChar :: AlexInput -> Char | ||
alexInputPrevChar = undefined | ||
|
||
-- generated by @alex@ | ||
alexScan :: AlexInput -> Int -> AlexReturn Bool | ||
|
||
{- | ||
GOOD cases: | ||
("012\NUL3","012","\NUL3",3,3,False) | ||
("\NUL0","\NUL","0",1,1,True) | ||
("012","012","",3,3,False) | ||
BAD case: | ||
("0@P`p\128\144\160","0@P`p","",5,8,False) | ||
expected: | ||
("0@P`p\128\144\160","0@P`p\128\144\160","",8,8,False) | ||
-} | ||
main :: IO () | ||
main = do | ||
go (B.pack [0x30,0x31,0x32,0x00,0x33]) -- GOOD | ||
go (B.pack [0x00,0x30]) -- GOOD | ||
go (B.pack [0x30,0x31,0x32]) -- GOOD | ||
|
||
go (B.pack [0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0]) -- WAS: BAD | ||
where | ||
go inp = do | ||
case (alexScan inp 0) of | ||
-- expected invariant: len == B.length inp - B.length inp' | ||
AlexToken inp' len b -> do | ||
let diff = B.length inp - B.length inp' | ||
unless (len == diff) $ do | ||
putStrLn $ "ERROR: reported length and consumed length differ!" | ||
print (inp, B.take len inp, inp', len, diff, b) | ||
exitFailure | ||
_ -> undefined | ||
} |