confusables.txt to insert query converter
authorunc0rr
Thu, 23 Jan 2014 23:28:16 +0400
changeset 10064 bf1a5ef4ef14
parent 10063 52e293f14977
child 10065 35bd559c1973
confusables.txt to insert query converter
tools/confuse.hs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/confuse.hs	Thu Jan 23 23:28:16 2014 +0400
@@ -0,0 +1,27 @@
+{-# LANGUAGE OverloadedStrings #-}
+module Confuse where
+
+import Numeric
+import Data.Char
+import Control.Monad
+import qualified Data.ByteString as B
+import qualified Data.ByteString.UTF8 as UTF8
+
+hx :: [Char] -> String
+hx cs = let ch = (chr . fst . last . readHex $ cs) in
+            case ch of
+                 '\'' -> "''"
+                 '\\' -> "\\\\"
+                 c -> c : []
+
+conv :: String -> B.ByteString
+conv s = B.concat ["('", UTF8.fromString i, "', '", UTF8.fromString r, "')"]
+    where
+        i :: String
+        i = hx s
+        r :: String
+        r = concatMap hx . words . takeWhile ((/=) ';') . tail $ dropWhile ((/=) '\t') s
+
+main = do
+    ll <- liftM (filter (isHexDigit . head) . filter (not . null) . lines) $ readFile "confusables.txt"
+    B.writeFile "insert.sql" . B.intercalate ",\n" . map conv $ ll