Created
December 31, 2021 04:44
-
-
Save ali-abrar/388d408881ae366072a6dce63f283cf8 to your computer and use it in GitHub Desktop.
NFKC string normalization
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
{-# Language JavaScriptFFI #-} | |
{-# Language OverloadedStrings #-} | |
import Data.Text (Text) | |
import qualified Data.Text as T | |
describeString :: Text -> String | |
describeString n = mconcat | |
[ T.unpack n | |
, " (" | |
, show n | |
, ")" | |
] | |
foreign import javascript safe | |
"(function() { return ($1['normalize']('NFKC')); })()" | |
normalize :: Text -> Text | |
main :: IO () | |
main = do | |
let n = "\xf1" :: Text | |
n' = "\x6e\x303" :: Text | |
putStrLn $ "Non-normalized 1: " <> describeString n | |
putStrLn $ "Non-normalized 2: " <> describeString n' | |
putStrLn $ "Are they equal? " <> | |
if n == n' | |
then "Yes" | |
else "No" | |
let normal = normalize n | |
normal' = normalize n' | |
putStrLn $ "Normalized 1: " <> describeString (normal) | |
putStrLn $ "Normalized 2: " <> describeString (normal') | |
putStrLn $ "Are they equal? " <> | |
if normal == normal' | |
then "Yes" | |
else "No" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment