idna (empty) → 0.1
raw patch · 4 files changed
+119/−0 lines, 4 filesdep +basedep +encodingdep +stringprepsetup-changed
Dependencies added: base, encoding, stringprep, text
Files
- LICENSE +24/−0
- Setup.hs +2/−0
- Text/IDNA.hs +77/−0
- idna.cabal +16/−0
+ LICENSE view
@@ -0,0 +1,24 @@+* Copyright (c) 2009, George Pollard+* 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 <organization> 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 <copyright holder> ''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 <copyright holder> 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Text/IDNA.hs view
@@ -0,0 +1,77 @@+{-# LANGUAGE OverloadedStrings #-}+module Text.IDNA (acePrefix, toASCII, toUnicode)+where++import Text.StringPrep+import Text.NamePrep+import Control.Monad+import Data.Encoding.BootString+import Data.Encoding.ByteSink+import Data.Encoding+import qualified Data.Text as Text+import qualified Data.Text.Encoding as E++acePrefix :: Text.Text+acePrefix = "xn--"++toASCII allowUnassigned useSTD3ASCIIRules t = do+ step2 <- if Text.any (>'\x7f') t+ then runStringPrep (namePrepProfile allowUnassigned) t+ else return t++ step3 <- if (useSTD3ASCIIRules && (Text.any isLDHascii step2 || Text.head step2 == '-' || Text.last step2 == '-'))+ then Nothing+ else return step2++ step7 <- if (Text.any (>'\x7f') step2)+ then if acePrefix `Text.isPrefixOf` step3+ then Nothing+ else case encodeStrictByteStringExplicit punycode $ Text.unpack step3 of+ Left _ -> Nothing+ Right t -> return $ acePrefix `Text.append` E.decodeASCII t+ else return step3+ + if Text.length step7 <= 63+ then return step7+ else Nothing++isLDHascii c =+ '\x0' <= c && c <= '\x2c' ||+ '\x2e' <= c && c <= '\x2f' ||+ '\x3a' <= c && c <= '\x40' ||+ '\x5b' <= c && c <= '\x60' ||+ '\x7b' <= c && c <= '\x7f' ++toUnicode allowUnassigned useSTD3ASCIIRules t = mergeEither $ do+ step2 <- if Text.any (>'\x7f') t+ then case runStringPrep (namePrepProfile allowUnassigned) t of+ Nothing -> Left t+ Just t' -> return t'+ else return t++ step3 <- if not $ acePrefix `Text.isPrefixOf` step2+ then Left step2+ else return step2+ + let step4 = Text.drop (Text.length acePrefix) step3+ step5 <- case decodeStrictByteStringExplicit punycode $ E.encodeUtf8 step4 of+ Left _ -> Left step3+ Right s -> return (Text.pack s)++ case toASCII allowUnassigned useSTD3ASCIIRules step5 of+ Nothing -> return step3+ Just t -> if t == step3+ then return step5+ else return step3++mergeEither :: Either a a -> a+mergeEither (Left x) = x+mergeEither (Right y) = y++instance Monad (Either a) where+ return = Right+ Left x >>= _ = Left x+ Right y >>= f = f y++tests :: [Text.Text]+tests = ["Bücher","tūdaliņ"]
+ idna.cabal view
@@ -0,0 +1,16 @@+Name: idna+Version: 0.1+Description: Implements IDNA -- Internationalized Domain Names in Applications (RFC 3490).+Synopsis: Implements IDNA (RFC 3490).+License: BSD3+Author: George Pollard <porges@porg.es>+Maintainer: George Pollard <porges@porg.es>+Build-Type: Simple+Cabal-Version: >=1.6+License-file: LICENSE+Category: data text++Library+ Build-Depends: base, encoding>=0.5.1, stringprep>=0.1.2, text>=0.1+ Exposed-modules: Text.IDNA+ ghc-options: -O2 -Wall