Skip to content

Instantly share code, notes, and snippets.

@ali-abrar
Created December 31, 2021 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ali-abrar/388d408881ae366072a6dce63f283cf8 to your computer and use it in GitHub Desktop.
Save ali-abrar/388d408881ae366072a6dce63f283cf8 to your computer and use it in GitHub Desktop.
NFKC string normalization
{-# 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