diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..11b706d --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License + +Copyright (c) 2005 Uwe Schmidt, Martin Schmidt, Torben Kuseler + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..cd7dc32 --- /dev/null +++ b/Setup.hs @@ -0,0 +1,3 @@ +#!/usr/bin/env runhaskell +import Distribution.Simple +main = defaultMain diff --git a/hxt-unicode.cabal b/hxt-unicode.cabal new file mode 100644 index 0000000..1e0a993 --- /dev/null +++ b/hxt-unicode.cabal @@ -0,0 +1,39 @@ +Name: hxt-unicode +Version: 9.0.2.4 +Synopsis: Unicode en-/decoding functions for utf8, iso-latin-* and other encodings +Description: Unicode encoding and decoding functions for utf8, iso-latin-* and somes other encodings, + used in the Haskell XML Toolbox. + ISO Latin 1 - 16, utf8, utf16, ASCII are supported. Decoding + is done with lazy functions, errors may be detected or ignored. +Homepage: http://www.fh-wedel.de/~si/HXmlToolbox/index.html +License: MIT +License-file: LICENSE +Author: Uwe Schmidt +Maintainer: Uwe Schmidt +Homepage: https://github.com/UweSchmidt/hxt +Copyright: Copyright (c) 2010- Uwe Schmidt +Stability: Stable +Category: Text +Build-type: Simple + +Cabal-version: >=1.6 + +Library + Exposed-modules: + Data.Char.IsoLatinTables + Data.String.EncodingNames + Data.String.UTF8 + Data.String.UTF8Decoding + Data.String.Unicode + + Build-depends: base >= 4 && < 5 , + hxt-charproperties >= 9 && < 10 + + hs-source-dirs: src + + ghc-options: -Wall + ghc-prof-options: -caf-all + +Source-Repository head + Type: git + Location: git://github.com/UweSchmidt/hxt.git diff --git a/src/Data/Char/IsoLatinTables.hs b/src/Data/Char/IsoLatinTables.hs new file mode 100644 index 0000000..2894f03 --- /dev/null +++ b/src/Data/Char/IsoLatinTables.hs @@ -0,0 +1,719 @@ +module Data.Char.IsoLatinTables +where + +iso_8859_2 :: [(Char, Char)] +iso_8859_2 + = [ ('\161', '\260' ) + , ('\162', '\728' ) + , ('\163', '\321' ) + , ('\165', '\317' ) + , ('\166', '\346' ) + , ('\169', '\352' ) + , ('\170', '\350' ) + , ('\171', '\356' ) + , ('\172', '\377' ) + , ('\174', '\381' ) + , ('\175', '\379' ) + , ('\177', '\261' ) + , ('\178', '\731' ) + , ('\179', '\322' ) + , ('\181', '\318' ) + , ('\182', '\347' ) + , ('\183', '\711' ) + , ('\185', '\353' ) + , ('\186', '\351' ) + , ('\187', '\357' ) + , ('\188', '\378' ) + , ('\189', '\733' ) + , ('\190', '\382' ) + , ('\191', '\380' ) + , ('\192', '\340' ) + , ('\195', '\258' ) + , ('\197', '\313' ) + , ('\198', '\262' ) + , ('\200', '\268' ) + , ('\202', '\280' ) + , ('\204', '\282' ) + , ('\207', '\270' ) + , ('\208', '\272' ) + , ('\209', '\323' ) + , ('\210', '\327' ) + , ('\213', '\336' ) + , ('\216', '\344' ) + , ('\217', '\366' ) + , ('\219', '\368' ) + , ('\222', '\354' ) + , ('\224', '\341' ) + , ('\227', '\259' ) + , ('\229', '\314' ) + , ('\230', '\263' ) + , ('\232', '\269' ) + , ('\234', '\281' ) + , ('\236', '\283' ) + , ('\239', '\271' ) + , ('\240', '\273' ) + , ('\241', '\324' ) + , ('\242', '\328' ) + , ('\245', '\337' ) + , ('\248', '\345' ) + , ('\249', '\367' ) + , ('\251', '\369' ) + , ('\254', '\355' ) + , ('\255', '\729' ) + ] + +iso_8859_3 :: [(Char, Char)] +iso_8859_3 + = [ ('\161', '\294' ) + , ('\162', '\728' ) + , ('\166', '\292' ) + , ('\169', '\304' ) + , ('\170', '\350' ) + , ('\171', '\286' ) + , ('\172', '\308' ) + , ('\175', '\379' ) + , ('\177', '\295' ) + , ('\182', '\293' ) + , ('\185', '\305' ) + , ('\186', '\351' ) + , ('\187', '\287' ) + , ('\188', '\309' ) + , ('\191', '\380' ) + , ('\197', '\266' ) + , ('\198', '\264' ) + , ('\213', '\288' ) + , ('\216', '\284' ) + , ('\221', '\364' ) + , ('\222', '\348' ) + , ('\229', '\267' ) + , ('\230', '\265' ) + , ('\245', '\289' ) + , ('\248', '\285' ) + , ('\253', '\365' ) + , ('\254', '\349' ) + , ('\255', '\729' ) + ] + +iso_8859_4 :: [(Char, Char)] +iso_8859_4 + = [ ('\161', '\260' ) + , ('\162', '\312' ) + , ('\163', '\342' ) + , ('\165', '\296' ) + , ('\166', '\315' ) + , ('\169', '\352' ) + , ('\170', '\274' ) + , ('\171', '\290' ) + , ('\172', '\358' ) + , ('\174', '\381' ) + , ('\177', '\261' ) + , ('\178', '\731' ) + , ('\179', '\343' ) + , ('\181', '\297' ) + , ('\182', '\316' ) + , ('\183', '\711' ) + , ('\185', '\353' ) + , ('\186', '\275' ) + , ('\187', '\291' ) + , ('\188', '\359' ) + , ('\189', '\330' ) + , ('\190', '\382' ) + , ('\191', '\331' ) + , ('\192', '\256' ) + , ('\199', '\302' ) + , ('\200', '\268' ) + , ('\202', '\280' ) + , ('\204', '\278' ) + , ('\207', '\298' ) + , ('\208', '\272' ) + , ('\209', '\325' ) + , ('\210', '\332' ) + , ('\211', '\310' ) + , ('\217', '\370' ) + , ('\221', '\360' ) + , ('\222', '\362' ) + , ('\224', '\257' ) + , ('\231', '\303' ) + , ('\232', '\269' ) + , ('\234', '\281' ) + , ('\236', '\279' ) + , ('\239', '\299' ) + , ('\240', '\273' ) + , ('\241', '\326' ) + , ('\242', '\333' ) + , ('\243', '\311' ) + , ('\249', '\371' ) + , ('\253', '\361' ) + , ('\254', '\363' ) + , ('\255', '\729' ) + ] + +iso_8859_5 :: [(Char, Char)] +iso_8859_5 + = [ ('\161', '\1025' ) + , ('\162', '\1026' ) + , ('\163', '\1027' ) + , ('\164', '\1028' ) + , ('\165', '\1029' ) + , ('\166', '\1030' ) + , ('\167', '\1031' ) + , ('\168', '\1032' ) + , ('\169', '\1033' ) + , ('\170', '\1034' ) + , ('\171', '\1035' ) + , ('\172', '\1036' ) + , ('\174', '\1038' ) + , ('\175', '\1039' ) + , ('\176', '\1040' ) + , ('\177', '\1041' ) + , ('\178', '\1042' ) + , ('\179', '\1043' ) + , ('\180', '\1044' ) + , ('\181', '\1045' ) + , ('\182', '\1046' ) + , ('\183', '\1047' ) + , ('\184', '\1048' ) + , ('\185', '\1049' ) + , ('\186', '\1050' ) + , ('\187', '\1051' ) + , ('\188', '\1052' ) + , ('\189', '\1053' ) + , ('\190', '\1054' ) + , ('\191', '\1055' ) + , ('\192', '\1056' ) + , ('\193', '\1057' ) + , ('\194', '\1058' ) + , ('\195', '\1059' ) + , ('\196', '\1060' ) + , ('\197', '\1061' ) + , ('\198', '\1062' ) + , ('\199', '\1063' ) + , ('\200', '\1064' ) + , ('\201', '\1065' ) + , ('\202', '\1066' ) + , ('\203', '\1067' ) + , ('\204', '\1068' ) + , ('\205', '\1069' ) + , ('\206', '\1070' ) + , ('\207', '\1071' ) + , ('\208', '\1072' ) + , ('\209', '\1073' ) + , ('\210', '\1074' ) + , ('\211', '\1075' ) + , ('\212', '\1076' ) + , ('\213', '\1077' ) + , ('\214', '\1078' ) + , ('\215', '\1079' ) + , ('\216', '\1080' ) + , ('\217', '\1081' ) + , ('\218', '\1082' ) + , ('\219', '\1083' ) + , ('\220', '\1084' ) + , ('\221', '\1085' ) + , ('\222', '\1086' ) + , ('\223', '\1087' ) + , ('\224', '\1088' ) + , ('\225', '\1089' ) + , ('\226', '\1090' ) + , ('\227', '\1091' ) + , ('\228', '\1092' ) + , ('\229', '\1093' ) + , ('\230', '\1094' ) + , ('\231', '\1095' ) + , ('\232', '\1096' ) + , ('\233', '\1097' ) + , ('\234', '\1098' ) + , ('\235', '\1099' ) + , ('\236', '\1100' ) + , ('\237', '\1101' ) + , ('\238', '\1102' ) + , ('\239', '\1103' ) + , ('\240', '\8470' ) + , ('\241', '\1105' ) + , ('\242', '\1106' ) + , ('\243', '\1107' ) + , ('\244', '\1108' ) + , ('\245', '\1109' ) + , ('\246', '\1110' ) + , ('\247', '\1111' ) + , ('\248', '\1112' ) + , ('\249', '\1113' ) + , ('\250', '\1114' ) + , ('\251', '\1115' ) + , ('\252', '\1116' ) + , ('\253', '\167' ) + , ('\254', '\1118' ) + , ('\255', '\1119' ) + ] + +iso_8859_6 :: [(Char, Char)] +iso_8859_6 + = [ ('\172', '\1548' ) + , ('\187', '\1563' ) + , ('\191', '\1567' ) + , ('\193', '\1569' ) + , ('\194', '\1570' ) + , ('\195', '\1571' ) + , ('\196', '\1572' ) + , ('\197', '\1573' ) + , ('\198', '\1574' ) + , ('\199', '\1575' ) + , ('\200', '\1576' ) + , ('\201', '\1577' ) + , ('\202', '\1578' ) + , ('\203', '\1579' ) + , ('\204', '\1580' ) + , ('\205', '\1581' ) + , ('\206', '\1582' ) + , ('\207', '\1583' ) + , ('\208', '\1584' ) + , ('\209', '\1585' ) + , ('\210', '\1586' ) + , ('\211', '\1587' ) + , ('\212', '\1588' ) + , ('\213', '\1589' ) + , ('\214', '\1590' ) + , ('\215', '\1591' ) + , ('\216', '\1592' ) + , ('\217', '\1593' ) + , ('\218', '\1594' ) + , ('\224', '\1600' ) + , ('\225', '\1601' ) + , ('\226', '\1602' ) + , ('\227', '\1603' ) + , ('\228', '\1604' ) + , ('\229', '\1605' ) + , ('\230', '\1606' ) + , ('\231', '\1607' ) + , ('\232', '\1608' ) + , ('\233', '\1609' ) + , ('\234', '\1610' ) + , ('\235', '\1611' ) + , ('\236', '\1612' ) + , ('\237', '\1613' ) + , ('\238', '\1614' ) + , ('\239', '\1615' ) + , ('\240', '\1616' ) + , ('\241', '\1617' ) + , ('\242', '\1618' ) + ] + +iso_8859_7 :: [(Char, Char)] +iso_8859_7 + = [ ('\161', '\8216' ) + , ('\162', '\8217' ) + , ('\164', '\8364' ) + , ('\165', '\8367' ) + , ('\170', '\890' ) + , ('\175', '\8213' ) + , ('\180', '\900' ) + , ('\181', '\901' ) + , ('\182', '\902' ) + , ('\184', '\904' ) + , ('\185', '\905' ) + , ('\186', '\906' ) + , ('\188', '\908' ) + , ('\190', '\910' ) + , ('\191', '\911' ) + , ('\192', '\912' ) + , ('\193', '\913' ) + , ('\194', '\914' ) + , ('\195', '\915' ) + , ('\196', '\916' ) + , ('\197', '\917' ) + , ('\198', '\918' ) + , ('\199', '\919' ) + , ('\200', '\920' ) + , ('\201', '\921' ) + , ('\202', '\922' ) + , ('\203', '\923' ) + , ('\204', '\924' ) + , ('\205', '\925' ) + , ('\206', '\926' ) + , ('\207', '\927' ) + , ('\208', '\928' ) + , ('\209', '\929' ) + , ('\211', '\931' ) + , ('\212', '\932' ) + , ('\213', '\933' ) + , ('\214', '\934' ) + , ('\215', '\935' ) + , ('\216', '\936' ) + , ('\217', '\937' ) + , ('\218', '\938' ) + , ('\219', '\939' ) + , ('\220', '\940' ) + , ('\221', '\941' ) + , ('\222', '\942' ) + , ('\223', '\943' ) + , ('\224', '\944' ) + , ('\225', '\945' ) + , ('\226', '\946' ) + , ('\227', '\947' ) + , ('\228', '\948' ) + , ('\229', '\949' ) + , ('\230', '\950' ) + , ('\231', '\951' ) + , ('\232', '\952' ) + , ('\233', '\953' ) + , ('\234', '\954' ) + , ('\235', '\955' ) + , ('\236', '\956' ) + , ('\237', '\957' ) + , ('\238', '\958' ) + , ('\239', '\959' ) + , ('\240', '\960' ) + , ('\241', '\961' ) + , ('\242', '\962' ) + , ('\243', '\963' ) + , ('\244', '\964' ) + , ('\245', '\965' ) + , ('\246', '\966' ) + , ('\247', '\967' ) + , ('\248', '\968' ) + , ('\249', '\969' ) + , ('\250', '\970' ) + , ('\251', '\971' ) + , ('\252', '\972' ) + , ('\253', '\973' ) + , ('\254', '\974' ) + ] + +iso_8859_8 :: [(Char, Char)] +iso_8859_8 + = [ ('\170', '\215' ) + , ('\186', '\247' ) + , ('\223', '\8215' ) + , ('\224', '\1488' ) + , ('\225', '\1489' ) + , ('\226', '\1490' ) + , ('\227', '\1491' ) + , ('\228', '\1492' ) + , ('\229', '\1493' ) + , ('\230', '\1494' ) + , ('\231', '\1495' ) + , ('\232', '\1496' ) + , ('\233', '\1497' ) + , ('\234', '\1498' ) + , ('\235', '\1499' ) + , ('\236', '\1500' ) + , ('\237', '\1501' ) + , ('\238', '\1502' ) + , ('\239', '\1503' ) + , ('\240', '\1504' ) + , ('\241', '\1505' ) + , ('\242', '\1506' ) + , ('\243', '\1507' ) + , ('\244', '\1508' ) + , ('\245', '\1509' ) + , ('\246', '\1510' ) + , ('\247', '\1511' ) + , ('\248', '\1512' ) + , ('\249', '\1513' ) + , ('\250', '\1514' ) + , ('\253', '\8206' ) + , ('\254', '\8207' ) + ] + +iso_8859_9 :: [(Char, Char)] +iso_8859_9 + = [ ('\208', '\286' ) + , ('\221', '\304' ) + , ('\222', '\350' ) + , ('\240', '\287' ) + , ('\253', '\305' ) + , ('\254', '\351' ) + ] + +iso_8859_10 :: [(Char, Char)] +iso_8859_10 + = [ ('\161', '\260' ) + , ('\162', '\274' ) + , ('\163', '\290' ) + , ('\164', '\298' ) + , ('\165', '\296' ) + , ('\166', '\310' ) + , ('\168', '\315' ) + , ('\169', '\272' ) + , ('\170', '\352' ) + , ('\171', '\358' ) + , ('\172', '\381' ) + , ('\174', '\362' ) + , ('\175', '\330' ) + , ('\177', '\261' ) + , ('\178', '\275' ) + , ('\179', '\291' ) + , ('\180', '\299' ) + , ('\181', '\297' ) + , ('\182', '\311' ) + , ('\184', '\316' ) + , ('\185', '\273' ) + , ('\186', '\353' ) + , ('\187', '\359' ) + , ('\188', '\382' ) + , ('\189', '\8213' ) + , ('\190', '\363' ) + , ('\191', '\331' ) + , ('\192', '\256' ) + , ('\199', '\302' ) + , ('\200', '\268' ) + , ('\202', '\280' ) + , ('\204', '\278' ) + , ('\209', '\325' ) + , ('\210', '\332' ) + , ('\215', '\360' ) + , ('\217', '\370' ) + , ('\224', '\257' ) + , ('\231', '\303' ) + , ('\232', '\269' ) + , ('\234', '\281' ) + , ('\236', '\279' ) + , ('\241', '\326' ) + , ('\242', '\333' ) + , ('\247', '\361' ) + , ('\249', '\371' ) + , ('\255', '\312' ) + ] + +iso_8859_11 :: [(Char, Char)] +iso_8859_11 + = [ ('\161', '\3585' ) + , ('\162', '\3586' ) + , ('\163', '\3587' ) + , ('\164', '\3588' ) + , ('\165', '\3589' ) + , ('\166', '\3590' ) + , ('\167', '\3591' ) + , ('\168', '\3592' ) + , ('\169', '\3593' ) + , ('\170', '\3594' ) + , ('\171', '\3595' ) + , ('\172', '\3596' ) + , ('\173', '\3597' ) + , ('\174', '\3598' ) + , ('\175', '\3599' ) + , ('\176', '\3600' ) + , ('\177', '\3601' ) + , ('\178', '\3602' ) + , ('\179', '\3603' ) + , ('\180', '\3604' ) + , ('\181', '\3605' ) + , ('\182', '\3606' ) + , ('\183', '\3607' ) + , ('\184', '\3608' ) + , ('\185', '\3609' ) + , ('\186', '\3610' ) + , ('\187', '\3611' ) + , ('\188', '\3612' ) + , ('\189', '\3613' ) + , ('\190', '\3614' ) + , ('\191', '\3615' ) + , ('\192', '\3616' ) + , ('\193', '\3617' ) + , ('\194', '\3618' ) + , ('\195', '\3619' ) + , ('\196', '\3620' ) + , ('\197', '\3621' ) + , ('\198', '\3622' ) + , ('\199', '\3623' ) + , ('\200', '\3624' ) + , ('\201', '\3625' ) + , ('\202', '\3626' ) + , ('\203', '\3627' ) + , ('\204', '\3628' ) + , ('\205', '\3629' ) + , ('\206', '\3630' ) + , ('\207', '\3631' ) + , ('\208', '\3632' ) + , ('\209', '\3633' ) + , ('\210', '\3634' ) + , ('\211', '\3635' ) + , ('\212', '\3636' ) + , ('\213', '\3637' ) + , ('\214', '\3638' ) + , ('\215', '\3639' ) + , ('\216', '\3640' ) + , ('\217', '\3641' ) + , ('\218', '\3642' ) + , ('\223', '\3647' ) + , ('\224', '\3648' ) + , ('\225', '\3649' ) + , ('\226', '\3650' ) + , ('\227', '\3651' ) + , ('\228', '\3652' ) + , ('\229', '\3653' ) + , ('\230', '\3654' ) + , ('\231', '\3655' ) + , ('\232', '\3656' ) + , ('\233', '\3657' ) + , ('\234', '\3658' ) + , ('\235', '\3659' ) + , ('\236', '\3660' ) + , ('\237', '\3661' ) + , ('\238', '\3662' ) + , ('\239', '\3663' ) + , ('\240', '\3664' ) + , ('\241', '\3665' ) + , ('\242', '\3666' ) + , ('\243', '\3667' ) + , ('\244', '\3668' ) + , ('\245', '\3669' ) + , ('\246', '\3670' ) + , ('\247', '\3671' ) + , ('\248', '\3672' ) + , ('\249', '\3673' ) + , ('\250', '\3674' ) + , ('\251', '\3675' ) + ] + +iso_8859_13 :: [(Char, Char)] +iso_8859_13 + = [ ('\161', '\8221' ) + , ('\165', '\8222' ) + , ('\168', '\216' ) + , ('\170', '\342' ) + , ('\175', '\198' ) + , ('\180', '\8220' ) + , ('\184', '\248' ) + , ('\186', '\343' ) + , ('\191', '\230' ) + , ('\192', '\260' ) + , ('\193', '\302' ) + , ('\194', '\256' ) + , ('\195', '\262' ) + , ('\198', '\280' ) + , ('\199', '\274' ) + , ('\200', '\268' ) + , ('\202', '\377' ) + , ('\203', '\278' ) + , ('\204', '\290' ) + , ('\205', '\310' ) + , ('\206', '\298' ) + , ('\207', '\315' ) + , ('\208', '\352' ) + , ('\209', '\323' ) + , ('\210', '\325' ) + , ('\212', '\332' ) + , ('\216', '\370' ) + , ('\217', '\321' ) + , ('\218', '\346' ) + , ('\219', '\362' ) + , ('\221', '\379' ) + , ('\222', '\381' ) + , ('\224', '\261' ) + , ('\225', '\303' ) + , ('\226', '\257' ) + , ('\227', '\263' ) + , ('\230', '\281' ) + , ('\231', '\275' ) + , ('\232', '\269' ) + , ('\234', '\378' ) + , ('\235', '\279' ) + , ('\236', '\291' ) + , ('\237', '\311' ) + , ('\238', '\299' ) + , ('\239', '\316' ) + , ('\240', '\353' ) + , ('\241', '\324' ) + , ('\242', '\326' ) + , ('\244', '\333' ) + , ('\248', '\371' ) + , ('\249', '\322' ) + , ('\250', '\347' ) + , ('\251', '\363' ) + , ('\253', '\380' ) + , ('\254', '\382' ) + , ('\255', '\8217' ) + ] + +iso_8859_14 :: [(Char, Char)] +iso_8859_14 + = [ ('\161', '\7682' ) + , ('\162', '\7683' ) + , ('\164', '\266' ) + , ('\165', '\267' ) + , ('\166', '\7690' ) + , ('\168', '\7808' ) + , ('\170', '\7810' ) + , ('\171', '\7691' ) + , ('\172', '\7922' ) + , ('\175', '\376' ) + , ('\176', '\7710' ) + , ('\177', '\7711' ) + , ('\178', '\288' ) + , ('\179', '\289' ) + , ('\180', '\7744' ) + , ('\181', '\7745' ) + , ('\183', '\7766' ) + , ('\184', '\7809' ) + , ('\185', '\7767' ) + , ('\186', '\7811' ) + , ('\187', '\7776' ) + , ('\188', '\7923' ) + , ('\189', '\7812' ) + , ('\190', '\7813' ) + , ('\191', '\7777' ) + , ('\208', '\372' ) + , ('\215', '\7786' ) + , ('\222', '\374' ) + , ('\240', '\373' ) + , ('\247', '\7787' ) + , ('\254', '\375' ) + ] + +iso_8859_15 :: [(Char, Char)] +iso_8859_15 + = [ ('\164', '\8364' ) + , ('\166', '\352' ) + , ('\168', '\353' ) + , ('\180', '\381' ) + , ('\184', '\382' ) + , ('\188', '\338' ) + , ('\189', '\339' ) + , ('\190', '\376' ) + ] + +iso_8859_16 :: [(Char, Char)] +iso_8859_16 + = [ ('\161', '\260' ) + , ('\162', '\261' ) + , ('\163', '\321' ) + , ('\164', '\8364' ) + , ('\165', '\8222' ) + , ('\166', '\352' ) + , ('\168', '\353' ) + , ('\170', '\536' ) + , ('\172', '\377' ) + , ('\174', '\378' ) + , ('\175', '\379' ) + , ('\178', '\268' ) + , ('\179', '\322' ) + , ('\180', '\381' ) + , ('\181', '\8221' ) + , ('\184', '\382' ) + , ('\185', '\269' ) + , ('\186', '\537' ) + , ('\188', '\338' ) + , ('\189', '\339' ) + , ('\190', '\376' ) + , ('\191', '\380' ) + , ('\195', '\258' ) + , ('\197', '\262' ) + , ('\208', '\272' ) + , ('\209', '\323' ) + , ('\213', '\336' ) + , ('\215', '\346' ) + , ('\216', '\368' ) + , ('\221', '\280' ) + , ('\222', '\538' ) + , ('\227', '\259' ) + , ('\229', '\263' ) + , ('\240', '\273' ) + , ('\241', '\324' ) + , ('\245', '\337' ) + , ('\247', '\347' ) + , ('\248', '\369' ) + , ('\253', '\281' ) + , ('\254', '\539' ) + ] + diff --git a/src/Data/String/EncodingNames.hs b/src/Data/String/EncodingNames.hs new file mode 100644 index 0000000..639b737 --- /dev/null +++ b/src/Data/String/EncodingNames.hs @@ -0,0 +1,55 @@ +-- ------------------------------------------------------------ + +{- | + Module : Data.String.EncodingNames + Copyright : Copyright (C) 2010 Uwe Schmidt + License : MIT + + Maintainer : Uwe Schmidt (uwe@fh-wedel.de) + Stability : stable + Portability: portable + + Constants for character encodings + +-} + +-- ------------------------------------------------------------ + +module Data.String.EncodingNames +where + +-- ------------------------------------------------------------ +-- +-- encoding names + +isoLatin1 + , iso8859_1, iso8859_2, iso8859_3, iso8859_4, iso8859_5 + , iso8859_6, iso8859_7, iso8859_8, iso8859_9, iso8859_10 + , iso8859_11, iso8859_13, iso8859_14, iso8859_15, iso8859_16 + , usAscii, ucs2, utf8, utf16, utf16be, utf16le, unicodeString :: String + +isoLatin1 = iso8859_1 +iso8859_1 = "ISO-8859-1" +iso8859_2 = "ISO-8859-2" +iso8859_3 = "ISO-8859-3" +iso8859_4 = "ISO-8859-4" +iso8859_5 = "ISO-8859-5" +iso8859_6 = "ISO-8859-6" +iso8859_7 = "ISO-8859-7" +iso8859_8 = "ISO-8859-8" +iso8859_9 = "ISO-8859-9" +iso8859_10 = "ISO-8859-10" +iso8859_11 = "ISO-8859-11" +iso8859_13 = "ISO-8859-13" +iso8859_14 = "ISO-8859-14" +iso8859_15 = "ISO-8859-15" +iso8859_16 = "ISO-8859-16" +usAscii = "US-ASCII" +ucs2 = "ISO-10646-UCS-2" +utf8 = "UTF-8" +utf16 = "UTF-16" +utf16be = "UTF-16BE" +utf16le = "UTF-16LE" +unicodeString = "UNICODE" + +-- ------------------------------------------------------------ diff --git a/src/Data/String/UTF8.hs b/src/Data/String/UTF8.hs new file mode 100644 index 0000000..4adaf0f --- /dev/null +++ b/src/Data/String/UTF8.hs @@ -0,0 +1,378 @@ +{- + +Copyright (c) 2002, members of the Haskell Internationalisation Working +Group All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +* Neither the name of the Haskell Internationalisation Working Group nor + the names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +This module provides lazy stream encoding/decoding facilities for UTF-8, +the Unicode Transformation Format with 8-bit words. + +2002-09-02 Sven Moritz Hallberg + +-} + +{- + +2007-04-30 Henning Thielemann: +Slight changes to make decode lazy. +The calls of 'reverse' in the original version have broken laziness +and thus had memory leaks. + +-} + +module Data.String.UTF8 + ( encode + , decode + , decodeEmbedErrors + , encodeOne + , decodeOne + , Error + -- Haddock does not want to document signatures with private types + -- these functions should be moved to a utility module + ) where + +import Data.Char (ord, chr) +import Data.Word (Word8, Word16, Word32) +import Data.Bits (shiftL, shiftR, (.&.), (.|.)) + +import Data.List (unfoldr) + +-- - UTF-8 in General - + +-- Adapted from the Unicode standard, version 3.2, +-- Table 3.1 "UTF-8 Bit Distribution" (excluded are UTF-16 encodings): + +-- Scalar 1st Byte 2nd Byte 3rd Byte 4th Byte +-- 000000000xxxxxxx 0xxxxxxx +-- 00000yyyyyxxxxxx 110yyyyy 10xxxxxx +-- zzzzyyyyyyxxxxxx 1110zzzz 10yyyyyy 10xxxxxx +-- 000uuuzzzzzzyyyyyyxxxxxx 11110uuu 10zzzzzz 10yyyyyy 10xxxxxx + +-- Also from the Unicode standard, version 3.2, +-- Table 3.1B "Legal UTF-8 Byte Sequences": + +-- Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte +-- U+0000..U+007F 00..7F +-- U+0080..U+07FF C2..DF 80..BF +-- U+0800..U+0FFF E0 A0..BF 80..BF +-- U+1000..U+CFFF E1..EC 80..BF 80..BF +-- U+D000..U+D7FF ED 80..9F 80..BF +-- U+D800..U+DFFF ill-formed +-- U+E000..U+FFFF EE..EF 80..BF 80..BF +-- U+10000..U+3FFFF F0 90..BF 80..BF 80..BF +-- U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF +-- U+100000..U+10FFFF F4 80..8F 80..BF 80..BF + + + +-- - Encoding Functions - + +-- Must the encoder ensure that no illegal byte sequences are output or +-- can we trust the Haskell system to supply only legal values? +-- For now I include error case for the surrogate values U+D800..U+DFFF and +-- out-of-range scalars. + +-- The function is pretty much a transscript of table 3.1B with error checks. +-- It dispatches the actual encoding to functions specific to the number of +-- required bytes. + +encodeOne :: Char -> [Word8] +encodeOne c + -- The report guarantees in (6.1.2) that this won't happen: + -- | n < 0 = error "encodeUTF8: ord returned a negative value" + | n < 0x0080 = encodeOne_onebyte n8 + | n < 0x0800 = encodeOne_twobyte n16 + | n < 0xD800 = encodeOne_threebyte n16 + | n < 0xE000 = error "encodeUTF8: ord returned a surrogate value" + | n < 0x10000 = encodeOne_threebyte n16 + -- Haskell 98 only talks about 16 bit characters, but ghc handles 20.1. + | n < 0x10FFFF = encodeOne_fourbyte n32 + | otherwise = error "encodeUTF8: ord returned a value above 0x10FFFF" + where + n = ord c :: Int + n8 = fromIntegral n :: Word8 + n16 = fromIntegral n :: Word16 + n32 = fromIntegral n :: Word32 + + +-- With the above, a stream decoder is trivial: + +encode :: [Char] -> [Word8] +encode = concatMap encodeOne + + +-- Now follow the individual encoders for certain numbers of bytes... +-- _ +-- / | __ ___ __ __ +-- / ^| // /__/ // // +-- /.==| \\ //_ // // +-- It's // || // \_/_//_//_ and it's here to stay! + +encodeOne_onebyte :: Word8 -> [Word8] +encodeOne_onebyte cp = [cp] + + +-- 00000yyyyyxxxxxx -> 110yyyyy 10xxxxxx + +encodeOne_twobyte :: Word16 -> [Word8] +encodeOne_twobyte cp = [(0xC0.|.ys), (0x80.|.xs)] + where + xs, ys :: Word8 + ys = fromIntegral (shiftR cp 6) + xs = (fromIntegral cp) .&. 0x3F + + +-- zzzzyyyyyyxxxxxx -> 1110zzzz 10yyyyyy 10xxxxxx + +encodeOne_threebyte :: Word16 -> [Word8] +encodeOne_threebyte cp = [(0xE0.|.zs), (0x80.|.ys), (0x80.|.xs)] + where + xs, ys, zs :: Word8 + xs = (fromIntegral cp) .&. 0x3F + ys = (fromIntegral (shiftR cp 6)) .&. 0x3F + zs = fromIntegral (shiftR cp 12) + + +-- 000uuuzzzzzzyyyyyyxxxxxx -> 11110uuu 10zzzzzz 10yyyyyy 10xxxxxx + +encodeOne_fourbyte :: Word32 -> [Word8] +encodeOne_fourbyte cp = [0xF0.|.us, 0x80.|.zs, 0x80.|.ys, 0x80.|.xs] + where + xs, ys, zs, us :: Word8 + xs = (fromIntegral cp) .&. 0x3F + ys = (fromIntegral (shiftR cp 6)) .&. 0x3F + zs = (fromIntegral (shiftR cp 12)) .&. 0x3F + us = fromIntegral (shiftR cp 18) + + + +-- - Decoding - + +-- The decoding is a bit more involved. The byte sequence could contain all +-- sorts of corruptions. The user must be able to either notice or ignore these +-- errors. + +-- I will first look at the decoding of a single character. The process +-- consumes a certain number of bytes from the input. It returns the +-- remaining input and either an error and the index of its occurance in the +-- byte sequence or the decoded character. + +data Error + +-- The first byte in a sequence starts with either zero, two, three, or four +-- ones and one zero to indicate the length of the sequence. If it doesn't, +-- it is invalid. It is dropped and the next byte interpreted as the start +-- of a new sequence. + + = InvalidFirstByte + +-- All bytes in the sequence except the first match the bit pattern 10xxxxxx. +-- If one doesn't, it is invalid. The sequence up to that point is dropped +-- and the "invalid" byte interpreted as the start of a new sequence. The error +-- includes the length of the partial sequence and the number of expected bytes. + + | InvalidLaterByte Int -- the byte at relative index n was invalid + +-- If a sequence ends prematurely, it has been truncated. It dropped and +-- decoding stops. The error reports the actual and expected lengths of the +-- sequence. + + | Truncated Int Int -- only n of m expected bytes were present + +-- Some sequences would represent code points which would be encoded as a +-- shorter sequence by a conformant encoder. Such non-shortest sequences are +-- considered erroneous and dropped. The error reports the actual and +-- expected number of bytes used. + + | NonShortest Int Int -- n instead of m bytes were used + +-- Unicode code points are in the range of [0..0x10FFFF]. Any values outside +-- of those bounds are simply invalid. + + | ValueOutOfBounds + +-- There is no such thing as "surrogate pairs" any more in UTF-8. The +-- corresponding code points now form illegal byte sequences. + + | Surrogate + deriving (Show, Eq) + + +-- Second, third, and fourth bytes share the common requirement to start +-- with the bit sequence 10. So, here's the function to check that property. + +first_bits_not_10 :: Word8 -> Bool +first_bits_not_10 b + | (b.&.0xC0) /= 0x80 = True + | otherwise = False + + +-- Erm, OK, the single-character decoding function's return type is a bit +-- longish. It is a tripel: + +-- - The first component contains the decoded character or an error +-- if the byte sequence was erroneous. +-- - The second component contains the number of bytes that were consumed +-- from the input. +-- - The third component contains the remaining bytes of input. + +decodeOne :: [Word8] -> (Either Error Char, Int, [Word8]) +decodeOne bs@(b1:rest) + | b1 < 0x80 = decodeOne_onebyte bs + | b1 < 0xC0 = (Left InvalidFirstByte, 1, rest) + | b1 < 0xE0 = decodeOne_twobyte bs + | b1 < 0xF0 = decodeOne_threebyte bs + | b1 < 0xF5 = decodeOne_fourbyte bs + | otherwise = (Left ValueOutOfBounds, 1, rest) +decodeOne [] = error "UTF8.decodeOne: No input" + + +-- 0xxxxxxx -> 000000000xxxxxxx + +decodeOne_onebyte :: [Word8] -> (Either Error Char, Int, [Word8]) +decodeOne_onebyte (b:bs) = (Right (cpToChar b), 1, bs) +decodeOne_onebyte[] = error "UTF8.decodeOne_onebyte: No input (can't happen)" + +cpToChar :: Integral a => a -> Char +cpToChar = chr . fromIntegral + + +-- 110yyyyy 10xxxxxx -> 00000yyyyyxxxxxx + +decodeOne_twobyte :: [Word8] -> (Either Error Char, Int, [Word8]) +decodeOne_twobyte (_:[]) + = (Left (Truncated 1 2), 1, []) +decodeOne_twobyte (b1:b2:bs) + | b1 < 0xC2 = (Left (NonShortest 2 1), 2, bs) + | first_bits_not_10 b2 = (Left (InvalidLaterByte 1), 1, (b2:bs)) + | otherwise = (Right (cpToChar result), 2, bs) + where + xs, ys, result :: Word32 + xs = fromIntegral (b2.&.0x3F) + ys = fromIntegral (b1.&.0x1F) + result = shiftL ys 6 .|. xs +decodeOne_twobyte[] = error "UTF8.decodeOne_twobyte: No input (can't happen)" + + +-- 1110zzzz 10yyyyyy 10xxxxxx -> zzzzyyyyyyxxxxxx + +decodeOne_threebyte :: [Word8] -> (Either Error Char, Int, [Word8]) +decodeOne_threebyte (_:[]) = threebyte_truncated 1 +decodeOne_threebyte (_:_:[]) = threebyte_truncated 2 +decodeOne_threebyte bs@(b1:b2:b3:rest) + | first_bits_not_10 b2 + = (Left (InvalidLaterByte 1), 1, drop 1 bs) + | first_bits_not_10 b3 + = (Left (InvalidLaterByte 2), 2, drop 2 bs) + | result < 0x0080 + = (Left (NonShortest 3 1), 3, rest) + | result < 0x0800 + = (Left (NonShortest 3 2), 3, rest) + | result >= 0xD800 && result < 0xE000 + = (Left Surrogate, 3, rest) + | otherwise + = (Right (cpToChar result), 3, rest) + where + xs, ys, zs, result :: Word32 + xs = fromIntegral (b3.&.0x3F) + ys = fromIntegral (b2.&.0x3F) + zs = fromIntegral (b1.&.0x0F) + result = shiftL zs 12 .|. shiftL ys 6 .|. xs +decodeOne_threebyte[] + = error "UTF8.decodeOne_threebyte: No input (can't happen)" + +threebyte_truncated :: Int -> (Either Error Char, Int, [Word8]) +threebyte_truncated n = (Left (Truncated n 3), n, []) + + +-- 11110uuu 10zzzzzz 10yyyyyy 10xxxxxx -> 000uuuzzzzzzyyyyyyxxxxxx + +decodeOne_fourbyte :: [Word8] -> (Either Error Char, Int, [Word8]) +decodeOne_fourbyte (_:[]) = fourbyte_truncated 1 +decodeOne_fourbyte (_:_:[]) = fourbyte_truncated 2 +decodeOne_fourbyte (_:_:_:[]) = fourbyte_truncated 3 +decodeOne_fourbyte bs@(b1:b2:b3:b4:rest) + | first_bits_not_10 b2 + = (Left (InvalidLaterByte 1), 1, drop 1 bs) + | first_bits_not_10 b3 + = (Left (InvalidLaterByte 2), 2, drop 2 bs) + | first_bits_not_10 b4 + = (Left (InvalidLaterByte 3), 3, drop 3 bs) + | result < 0x0080 + = (Left (NonShortest 4 1), 4, rest) + | result < 0x0800 + = (Left (NonShortest 4 2), 4, rest) + | result < 0x10000 + = (Left (NonShortest 4 3), 4, rest) + | result > 0x10FFFF + = (Left ValueOutOfBounds, 4, rest) + | otherwise + = (Right (cpToChar result), 4, rest) + where + xs, ys, zs, us, result :: Word32 + xs = fromIntegral (b4 .&. 0x3F) + ys = fromIntegral (b3 .&. 0x3F) + zs = fromIntegral (b2 .&. 0x3F) + us = fromIntegral (b1 .&. 0x07) + result = xs .|. shiftL ys 6 .|. shiftL zs 12 .|. shiftL us 18 +decodeOne_fourbyte[] + = error "UTF8.decodeOne_fourbyte: No input (can't happen)" + +fourbyte_truncated :: Int -> (Either Error Char, Int, [Word8]) +fourbyte_truncated n = (Left (Truncated n 4), n, []) + + +-- The decoder examines all input, recording decoded characters as well as +-- error-index pairs along the way. + +decode :: [Word8] -> ([Char], [(Error,Int)]) +decode = swap . partitionEither . decodeEmbedErrors + +decodeEmbedErrors :: [Word8] -> [Either (Error,Int) Char] +decodeEmbedErrors = + unfoldr (\(pos,xs) -> + toMaybe + (not $ null xs) + (let (c,n,rest) = decodeOne xs + in (either (\err -> Left (err,pos)) Right c, + (pos+n,rest)))) . + (,) 0 + +swap :: (a,b) -> (b,a) +swap (x,y) = (y,x) +{-# INLINE swap #-} + +partitionEither :: [Either a b] -> ([a], [b]) +partitionEither = + foldr (\x ~(ls,rs) -> either (\l -> (l:ls,rs)) (\r -> (ls,r:rs)) x) ([],[]) +{-# INLINE partitionEither #-} + +toMaybe :: Bool -> a -> Maybe a +toMaybe False _ = Nothing +toMaybe True x = Just x +{-# INLINE toMaybe #-} + +-- ------------------------------------------------------------ diff --git a/src/Data/String/UTF8Decoding.hs b/src/Data/String/UTF8Decoding.hs new file mode 100644 index 0000000..792fdad --- /dev/null +++ b/src/Data/String/UTF8Decoding.hs @@ -0,0 +1,52 @@ +-- ------------------------------------------------------------ + +{- | + Module : Data.String.UTF8Decoding + Copyright : Copyright (C) 2010- Uwe Schmidt + License : MIT + + Maintainer : Uwe Schmidt (uwe@fh-wedel.de) + Stability : stable + Portability: portable + + Interface for Data.Char.UTF8 funtions + +-} + +-- ------------------------------------------------------------ + +module Data.String.UTF8Decoding ( + decodeUtf8, + decodeUtf8EmbedErrors, + decodeUtf8IgnoreErrors, + ) +where + +import qualified Data.String.UTF8 as UTF8 +import Data.Word (Word8) + +-- | calls 'Data.Char.UTF8.decode' for parsing and decoding UTF-8 + +decodeUtf8 :: String -> (String, [String]) +decodeUtf8 str + = (res, map (uncurry toErrStr) errs) + where + (res, errs) = UTF8.decode . stringToByteString $ str + +decodeUtf8IgnoreErrors :: String -> String +decodeUtf8IgnoreErrors + = fst . decodeUtf8 + +decodeUtf8EmbedErrors :: String -> [Either String Char] +decodeUtf8EmbedErrors str + = map (either (Left . uncurry toErrStr) Right) $ + UTF8.decodeEmbedErrors $ stringToByteString $ str + +stringToByteString :: String -> [Word8] +stringToByteString = map (toEnum . fromEnum) + +toErrStr :: UTF8.Error -> Int -> String +toErrStr err pos + = " at input position " ++ show pos ++ ": " ++ show err + +-- ------------------------------------------------------------ diff --git a/src/Data/String/Unicode.hs b/src/Data/String/Unicode.hs new file mode 100644 index 0000000..1bbebdb --- /dev/null +++ b/src/Data/String/Unicode.hs @@ -0,0 +1,649 @@ +-- ------------------------------------------------------------ + +{- | + Module : Data.String.Unicode + Copyright : Copyright (C) 2010- Uwe Schmidt + License : MIT + + Maintainer : Uwe Schmidt (uwe@fh-wedel.de) + Stability : stable + Portability: portable + + Unicode and UTF-8 Conversion Functions + +-} + +-- ------------------------------------------------------------ + +module Data.String.Unicode + ( + -- * Unicode Type declarations + Unicode, + UString, + UTF8Char, + UTF8String, + UStringWithErrors, + DecodingFct, + DecodingFctEmbedErrors, + + utf8ToUnicode + , utf8ToUnicodeEmbedErrors + , latin1ToUnicode + , ucs2ToUnicode + , ucs2BigEndianToUnicode + , ucs2LittleEndianToUnicode + , utf16beToUnicode + , utf16leToUnicode + + , unicodeCharToUtf8 + + , unicodeToUtf8 + , unicodeToXmlEntity + , unicodeToLatin1 + , unicodeRemoveNoneAscii + , unicodeRemoveNoneLatin1 + + , intToCharRef + , intToCharRefHex + , intToHexString + + , getDecodingFct + , getDecodingFctEmbedErrors + , getOutputEncodingFct + + , normalizeNL + , guessEncoding + + , getOutputEncodingFct' + , unicodeCharToUtf8' + , unicodeCharToXmlEntity' + , unicodeCharToLatin1' + ) +where + +import Data.Char (toUpper) + +import Data.Char.IsoLatinTables +import Data.Char.Properties.XMLCharProps (isXml1ByteChar, + isXmlLatin1Char) + +import Data.String.EncodingNames +import Data.String.UTF8Decoding (decodeUtf8, + decodeUtf8EmbedErrors) + +-- ------------------------------------------------------------ + +-- | Unicode is represented as the Char type +-- Precondition for this is the support of Unicode character range +-- in the compiler (e.g. ghc but not hugs) + +type Unicode = Char + +-- | the type for Unicode strings + +type UString = [Unicode] + +-- | UTF-8 charachters are represented by the Char type + +type UTF8Char = Char + +-- | UTF-8 strings are implemented as Haskell strings + +type UTF8String = String + +-- | Decoding function with a pair containing the result string and a list of decoding errors as result + +type DecodingFct = String -> (UString, [String]) + +type UStringWithErrors = [Either String Char] + +-- | Decoding function where decoding errors are interleaved with decoded characters + +type DecodingFctEmbedErrors = String -> UStringWithErrors + +-- ------------------------------------------------------------ + +-- | +-- conversion from Unicode strings (UString) to UTF8 encoded strings. + +unicodeToUtf8 :: UString -> UTF8String +unicodeToUtf8 = concatMap unicodeCharToUtf8 + +-- | +-- conversion from Unicode (Char) to a UTF8 encoded string. + +unicodeCharToUtf8 :: Unicode -> UTF8String +unicodeCharToUtf8 c + | i >= 0 && i <= 0x0000007F -- 1 byte UTF8 (7 bits) + = [ toEnum i ] + | i >= 0x00000080 && i <= 0x000007FF -- 2 byte UTF8 (5 + 6 bits) + = [ toEnum (0xC0 + i `div` 0x40) + , toEnum (0x80 + i `mod` 0x40) + ] + | i >= 0x00000800 && i <= 0x0000FFFF -- 3 byte UTF8 (4 + 6 + 6 bits) + = [ toEnum (0xE0 + i `div` 0x1000) + , toEnum (0x80 + (i `div` 0x40) `mod` 0x40) + , toEnum (0x80 + i `mod` 0x40) + ] + | i >= 0x00010000 && i <= 0x001FFFFF -- 4 byte UTF8 (3 + 6 + 6 + 6 bits) + = [ toEnum (0xF0 + i `div` 0x40000) + , toEnum (0x80 + (i `div` 0x1000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x40) `mod` 0x40) + , toEnum (0x80 + i `mod` 0x40) + ] + | i >= 0x00200000 && i <= 0x03FFFFFF -- 5 byte UTF8 (2 + 6 + 6 + 6 + 6 bits) + = [ toEnum (0xF8 + i `div` 0x1000000) + , toEnum (0x80 + (i `div` 0x40000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x1000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x40) `mod` 0x40) + , toEnum (0x80 + i `mod` 0x40) + ] + | i >= 0x04000000 && i <= 0x7FFFFFFF -- 6 byte UTF8 (1 + 6 + 6 + 6 + 6 + 6 bits) + = [ toEnum (0xFC + i `div` 0x40000000) + , toEnum (0x80 + (i `div` 0x1000000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x40000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x1000) `mod` 0x40) + , toEnum (0x80 + (i `div` 0x40) `mod` 0x40) + , toEnum (0x80 + i `mod` 0x40) + ] + | otherwise -- other values not supported + = error ("unicodeCharToUtf8: illegal integer argument " ++ show i) + where + i = fromEnum c + +-- ------------------------------------------------------------ + +-- | +-- code conversion from latin1 to Unicode + +latin1ToUnicode :: String -> UString +latin1ToUnicode = id + +latinToUnicode :: [(Char, Char)] -> String -> UString +latinToUnicode tt + = map charToUni + where + charToUni c = + foldr (\(src,dst) r -> + case compare c src of + EQ -> dst + LT -> c {- not found in table -} + GT -> r) c tt + +-- | conversion from ASCII to unicode with check for legal ASCII char set +-- +-- Structure of decoding function copied from 'Data.Char.UTF8.decode'. + +decodeAscii :: DecodingFct +decodeAscii + = swap . partitionEither . decodeAsciiEmbedErrors + +decodeAsciiEmbedErrors :: String -> UStringWithErrors +decodeAsciiEmbedErrors str + = map (\(c,pos) -> if isValid c + then Right c + else Left (toErrStr c pos)) posStr + where + posStr = zip str [(0::Int)..] + toErrStr errChr pos + = " at input position " ++ show pos ++ ": none ASCII char " ++ show errChr + isValid x = x < '\x80' + +-- | +-- UCS-2 big endian to Unicode conversion + +ucs2BigEndianToUnicode :: String -> UString + +ucs2BigEndianToUnicode (b : l : r) + = toEnum (fromEnum b * 256 + fromEnum l) : ucs2BigEndianToUnicode r + +ucs2BigEndianToUnicode [] + = [] + +ucs2BigEndianToUnicode _ + = [] -- error "illegal UCS-2 byte input sequence with odd length" + -- is ignored (garbage in, garbage out) + +-- ------------------------------------------------------------ + +-- | +-- UCS-2 little endian to Unicode conversion + +ucs2LittleEndianToUnicode :: String -> UString + +ucs2LittleEndianToUnicode (l : b : r) + = toEnum (fromEnum b * 256 + fromEnum l) : ucs2LittleEndianToUnicode r + +ucs2LittleEndianToUnicode [] + = [] + +ucs2LittleEndianToUnicode [_] + = [] -- error "illegal UCS-2 byte input sequence with odd length" + -- is ignored + +-- ------------------------------------------------------------ + +-- | +-- UCS-2 to UTF-8 conversion with byte order mark analysis + +ucs2ToUnicode :: String -> UString + +ucs2ToUnicode ('\xFE':'\xFF':s) -- 2 byte mark for big endian encoding + = ucs2BigEndianToUnicode s + +ucs2ToUnicode ('\xFF':'\xFE':s) -- 2 byte mark for little endian encoding + = ucs2LittleEndianToUnicode s + +ucs2ToUnicode s + = ucs2BigEndianToUnicode s -- default: big endian + +-- ------------------------------------------------------------ + +-- | +-- UTF-8 to Unicode conversion with deletion of leading byte order mark, as described in XML standard F.1 + +utf8ToUnicode :: DecodingFct + +utf8ToUnicode ('\xEF':'\xBB':'\xBF':s) -- remove byte order mark ( XML standard F.1 ) + = decodeUtf8 s + +utf8ToUnicode s + = decodeUtf8 s + +utf8ToUnicodeEmbedErrors :: DecodingFctEmbedErrors + +utf8ToUnicodeEmbedErrors ('\xEF':'\xBB':'\xBF':s) -- remove byte order mark ( XML standard F.1 ) + = decodeUtf8EmbedErrors s + +utf8ToUnicodeEmbedErrors s + = decodeUtf8EmbedErrors s + +-- ------------------------------------------------------------ + +-- | +-- UTF-16 big endian to UTF-8 conversion with removal of byte order mark + +utf16beToUnicode :: String -> UString + +utf16beToUnicode ('\xFE':'\xFF':s) -- remove byte order mark + = ucs2BigEndianToUnicode s + +utf16beToUnicode s + = ucs2BigEndianToUnicode s + +-- ------------------------------------------------------------ + +-- | +-- UTF-16 little endian to UTF-8 conversion with removal of byte order mark + +utf16leToUnicode :: String -> UString + +utf16leToUnicode ('\xFF':'\xFE':s) -- remove byte order mark + = ucs2LittleEndianToUnicode s + +utf16leToUnicode s + = ucs2LittleEndianToUnicode s + + +-- ------------------------------------------------------------ + +-- | +-- substitute all Unicode characters, that are not legal 1-byte +-- UTF-8 XML characters by a character reference. +-- +-- This function can be used to translate all text nodes and +-- attribute values into pure ascii. +-- +-- see also : 'unicodeToLatin1' + +unicodeToXmlEntity :: UString -> String +unicodeToXmlEntity + = escape isXml1ByteChar (intToCharRef . fromEnum) + +-- | +-- substitute all Unicode characters, that are not legal latin1 +-- UTF-8 XML characters by a character reference. +-- +-- This function can be used to translate all text nodes and +-- attribute values into ISO latin1. +-- +-- see also : 'unicodeToXmlEntity' + +unicodeToLatin1 :: UString -> String +unicodeToLatin1 + = escape isXmlLatin1Char (intToCharRef . fromEnum) + + +-- | +-- substitute selected characters +-- The @check@ function returns 'True' whenever a character needs to substitution +-- The function @esc@ computes a substitute. + +escape :: (Unicode -> Bool) -> (Unicode -> String) -> UString -> String +escape check esc = + concatMap (\uc -> if check uc then [uc] else esc uc) + +-- | +-- removes all non ascii chars, may be used to transform +-- a document into a pure ascii representation by removing +-- all non ascii chars from tag and attibute names +-- +-- see also : 'unicodeRemoveNoneLatin1', 'unicodeToXmlEntity' + +unicodeRemoveNoneAscii :: UString -> String +unicodeRemoveNoneAscii + = filter isXml1ByteChar + +-- | +-- removes all non latin1 chars, may be used to transform +-- a document into a pure ascii representation by removing +-- all non ascii chars from tag and attibute names +-- +-- see also : 'unicodeRemoveNoneAscii', 'unicodeToLatin1' + +unicodeRemoveNoneLatin1 :: UString -> String +unicodeRemoveNoneLatin1 + = filter isXmlLatin1Char + +-- ------------------------------------------------------------ + +-- | +-- convert an Unicode into a XML character reference. +-- +-- see also : 'intToCharRefHex' + +intToCharRef :: Int -> String +intToCharRef i + = "&#" ++ show i ++ ";" + +-- | +-- convert an Unicode into a XML hexadecimal character reference. +-- +-- see also: 'intToCharRef' + +intToCharRefHex :: Int -> String +intToCharRefHex i + = "&#x" ++ h2 ++ ";" + where + h1 = intToHexString i + h2 = if length h1 `mod` 2 == 1 + then '0': h1 + else h1 + +-- ------------------------------------------------------------ + +intToHexString :: Int -> String +intToHexString i + | i == 0 + = "0" + | i > 0 + = intToStr i + | otherwise + = error ("intToHexString: negative argument " ++ show i) + where + intToStr 0 = "" + intToStr i' = intToStr (i' `div` 16) ++ [fourBitsToChar (i' `mod` 16)] + +fourBitsToChar :: Int -> Char +fourBitsToChar i = "0123456789ABCDEF" !! i +{-# INLINE fourBitsToChar #-} + +-- ------------------------------------------------------------ +-- +-- | White Space (XML Standard 2.3) and +-- end of line handling (2.11) +-- +-- \#x0D and \#x0D\#x0A are mapped to \#x0A + +normalizeNL :: String -> String +normalizeNL ('\r' : '\n' : rest) = '\n' : normalizeNL rest +normalizeNL ('\r' : rest) = '\n' : normalizeNL rest +normalizeNL (c : rest) = c : normalizeNL rest +normalizeNL [] = [] + + +-- ------------------------------------------------------------ + +-- | +-- the table of supported character encoding schemes and the associated +-- conversion functions into Unicode:q + +{- +This table could be derived from decodingTableEither, +but this way it is certainly more efficient. +-} + +decodingTable :: [(String, DecodingFct)] +decodingTable + = [ (utf8, utf8ToUnicode ) + , (isoLatin1, liftDecFct latin1ToUnicode ) + , (usAscii, decodeAscii ) + , (ucs2, liftDecFct ucs2ToUnicode ) + , (utf16, liftDecFct ucs2ToUnicode ) + , (utf16be, liftDecFct utf16beToUnicode ) + , (utf16le, liftDecFct utf16leToUnicode ) + , (iso8859_2, liftDecFct (latinToUnicode iso_8859_2) ) + , (iso8859_3, liftDecFct (latinToUnicode iso_8859_3) ) + , (iso8859_4, liftDecFct (latinToUnicode iso_8859_4) ) + , (iso8859_5, liftDecFct (latinToUnicode iso_8859_5) ) + , (iso8859_6, liftDecFct (latinToUnicode iso_8859_6) ) + , (iso8859_7, liftDecFct (latinToUnicode iso_8859_7) ) + , (iso8859_8, liftDecFct (latinToUnicode iso_8859_8) ) + , (iso8859_9, liftDecFct (latinToUnicode iso_8859_9) ) + , (iso8859_10, liftDecFct (latinToUnicode iso_8859_10) ) + , (iso8859_11, liftDecFct (latinToUnicode iso_8859_11) ) + , (iso8859_13, liftDecFct (latinToUnicode iso_8859_13) ) + , (iso8859_14, liftDecFct (latinToUnicode iso_8859_14) ) + , (iso8859_15, liftDecFct (latinToUnicode iso_8859_15) ) + , (iso8859_16, liftDecFct (latinToUnicode iso_8859_16) ) + , (unicodeString, liftDecFct id ) + , ("", liftDecFct id ) -- default + ] + where + liftDecFct df = \ s -> (df s, []) + +-- | +-- the lookup function for selecting the decoding function + +getDecodingFct :: String -> Maybe DecodingFct +getDecodingFct enc + = lookup (map toUpper enc) decodingTable + + +-- | +-- Similar to 'decodingTable' but it embeds errors +-- in the string of decoded characters. + +decodingTableEmbedErrors :: [(String, DecodingFctEmbedErrors)] +decodingTableEmbedErrors + = [ (utf8, utf8ToUnicodeEmbedErrors ) + , (isoLatin1, liftDecFct latin1ToUnicode ) + , (usAscii, decodeAsciiEmbedErrors ) + , (ucs2, liftDecFct ucs2ToUnicode ) + , (utf16, liftDecFct ucs2ToUnicode ) + , (utf16be, liftDecFct utf16beToUnicode ) + , (utf16le, liftDecFct utf16leToUnicode ) + , (iso8859_2, liftDecFct (latinToUnicode iso_8859_2) ) + , (iso8859_3, liftDecFct (latinToUnicode iso_8859_3) ) + , (iso8859_4, liftDecFct (latinToUnicode iso_8859_4) ) + , (iso8859_5, liftDecFct (latinToUnicode iso_8859_5) ) + , (iso8859_6, liftDecFct (latinToUnicode iso_8859_6) ) + , (iso8859_7, liftDecFct (latinToUnicode iso_8859_7) ) + , (iso8859_8, liftDecFct (latinToUnicode iso_8859_8) ) + , (iso8859_9, liftDecFct (latinToUnicode iso_8859_9) ) + , (iso8859_10, liftDecFct (latinToUnicode iso_8859_10) ) + , (iso8859_11, liftDecFct (latinToUnicode iso_8859_11) ) + , (iso8859_13, liftDecFct (latinToUnicode iso_8859_13) ) + , (iso8859_14, liftDecFct (latinToUnicode iso_8859_14) ) + , (iso8859_15, liftDecFct (latinToUnicode iso_8859_15) ) + , (iso8859_16, liftDecFct (latinToUnicode iso_8859_16) ) + , (unicodeString, liftDecFct id ) + , ("", liftDecFct id ) -- default + ] + where + liftDecFct df = map Right . df + +-- | +-- the lookup function for selecting the decoding function + +getDecodingFctEmbedErrors :: String -> Maybe DecodingFctEmbedErrors +getDecodingFctEmbedErrors enc + = lookup (map toUpper enc) decodingTableEmbedErrors + + + +-- | +-- the table of supported output encoding schemes and the associated +-- conversion functions from Unicode + +outputEncodingTable :: [(String, (UString -> String))] +outputEncodingTable + = [ (utf8, unicodeToUtf8 ) + , (isoLatin1, unicodeToLatin1 ) + , (usAscii, unicodeToXmlEntity ) + , (unicodeString, id ) + , ("", unicodeToUtf8 ) -- default + ] + +-- | +-- the lookup function for selecting the encoding function + +getOutputEncodingFct :: String -> Maybe (String -> UString) +getOutputEncodingFct enc + = lookup (map toUpper enc) outputEncodingTable + +-- ------------------------------------------------------------ +-- + +guessEncoding :: String -> String + +guessEncoding ('\xFF':'\xFE':'\x00':'\x00':_) = "UCS-4LE" -- with byte order mark +guessEncoding ('\xFF':'\xFE':_) = "UTF-16LE" -- with byte order mark + +guessEncoding ('\xFE':'\xFF':'\x00':'\x00':_) = "UCS-4-3421" -- with byte order mark +guessEncoding ('\xFE':'\xFF':_) = "UTF-16BE" -- with byte order mark + +guessEncoding ('\xEF':'\xBB':'\xBF':_) = utf8 -- with byte order mark + +guessEncoding ('\x00':'\x00':'\xFE':'\xFF':_) = "UCS-4BE" -- with byte order mark +guessEncoding ('\x00':'\x00':'\xFF':'\xFE':_) = "UCS-4-2143" -- with byte order mark + +guessEncoding ('\x00':'\x00':'\x00':'\x3C':_) = "UCS-4BE" -- "<" of " (b,a) +swap (x,y) = (y,x) +{-# INLINE swap #-} + +partitionEither :: [Either a b] -> ([a], [b]) +partitionEither = + foldr (\x ~(ls,rs) -> either (\l -> (l:ls,rs)) (\r -> (ls,r:rs)) x) ([],[]) +{-# INLINE partitionEither #-} + +-- ------------------------------------------------------------ + +-- output encoding for bytestrings + +-- | +-- the table of supported output encoding schemes and the associated +-- conversion functions from Unicode + +type StringFct = String -> String + +outputEncodingTable' :: [(String, (Char -> StringFct))] +outputEncodingTable' + = [ (utf8, unicodeCharToUtf8' ) + , (isoLatin1, unicodeCharToLatin1' ) + , (usAscii, unicodeCharToXmlEntity' ) + , ("", unicodeCharToUtf8' ) -- default + ] + +-- | +-- the lookup function for selecting the encoding function + +getOutputEncodingFct' :: String -> Maybe (Char -> StringFct) +getOutputEncodingFct' enc + = lookup (map toUpper enc) outputEncodingTable' + + +-- ------------------------------------------------------------ + +-- | +-- conversion from Unicode (Char) to a UTF8 encoded string. + +unicodeCharToUtf8' :: Char -> StringFct +unicodeCharToUtf8' c + | i >= 0 && i <= 0x0000007F -- 1 byte UTF8 (7 bits) + = (c :) + + | i >= 0x00000080 && i <= 0x000007FF -- 2 byte UTF8 (5 + 6 bits) + = ((toEnum (0xC0 + i `div` 0x40) ) :) . + ((toEnum (0x80 + i `mod` 0x40)) :) + + | i >= 0x00000800 && i <= 0x0000FFFF -- 3 byte UTF8 (4 + 6 + 6 bits) + = ((toEnum (0xE0 + i `div` 0x1000) ) :) . + ((toEnum (0x80 + (i `div` 0x40) `mod` 0x40)) :) . + ((toEnum (0x80 + i `mod` 0x40)) :) + + | i >= 0x00010000 && i <= 0x001FFFFF -- 4 byte UTF8 (3 + 6 + 6 + 6 bits) -- extension to encode 21 bit values + = ((toEnum (0xF0 + i `div` 0x40000) ) :) . + ((toEnum (0x80 + (i `div` 0x1000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x40) `mod` 0x40)) :) . + ((toEnum (0x80 + i `mod` 0x40)) :) + + | i >= 0x00200000 && i <= 0x03FFFFFF -- 5 byte UTF8 (2 + 6 + 6 + 6 + 6 bits) -- extension to encode 26 bit values + = ((toEnum (0xF8 + i `div` 0x1000000) ) :) . + ((toEnum (0x80 + (i `div` 0x40000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x1000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x40) `mod` 0x40)) :) . + ((toEnum (0x80 + i `mod` 0x40)) :) + + | i >= 0x04000000 && i <= 0x7FFFFFFF -- 6 byte UTF8 (1 + 6 + 6 + 6 + 6 + 6 bits) -- extension to encode 31 bit values + = ((toEnum (0xFC + i `div` 0x40000000) ) :) . + ((toEnum (0x80 + (i `div` 0x1000000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x40000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x1000) `mod` 0x40)) :) . + ((toEnum (0x80 + (i `div` 0x40) `mod` 0x40)) :) . + ((toEnum (0x80 + i `mod` 0x40)) :) + + | otherwise -- other values not supported + = error ("unicodeCharToUtf8: illegal integer argument " ++ show i) + where + i = fromEnum c + +-- ------------------------------------------------------------ + +-- | +-- substitute all Unicode characters, that are not legal 1-byte +-- UTF-8 XML characters by a character reference. + +unicodeCharToXmlEntity' :: Char -> StringFct +unicodeCharToXmlEntity' c + | isXml1ByteChar c = (c :) + | otherwise = ((intToCharRef . fromEnum $ c) ++) + +-- ------------------------------------------------------------ + +-- | +-- substitute all Unicode characters, that are not legal latin1 +-- UTF-8 XML characters by a character reference. + +unicodeCharToLatin1' :: Char -> StringFct +unicodeCharToLatin1' c + | isXmlLatin1Char c = (c :) + | otherwise = ((intToCharRef . fromEnum $ c) ++) + +-- ------------------------------------------------------------ + +