readDataURI :: String -- ^ URI
            -> Maybe (String,String,Bool,String)
               -- ^ Maybe (mime,charset,isBase64,data)
readDataURI uri =
    let prefix = "data:"
    in  if not (prefix `isPrefixOf` uri)
        then Nothing
        else
            let rest = drop (length prefix) uri
                meta = takeWhile (/= ',') rest  -- without trailing ','
                uridata = drop (length meta + 1) rest
                parts = split (== ';') meta
                (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
            in  Just (mime,cs,enc,uridata)
 where
   upd str m@(mime,cs,enc)
       | isMimeType str               = (str,cs,enc)
       | "charset=" `isPrefixOf` str  = (mime,drop (length "charset=") str,enc)
       | str ==  "base64"             = (mime,cs,True)
       | otherwise                    = m

-- Without parameters like ;charset=...; see RFC 2045, 5.1
readDataURI :: String -- ^ URI
            -> Maybe (String,String,Bool,String)
readDataURI uri =
    let prefix = "data:"
    in  if not (prefix `isPrefixOf` uri)
        then Nothing
        else
            let rest = drop (length prefix) uri
                meta = takeWhile (/= ',') rest  -- without trailing ','
                uridata = drop (length meta + 1) rest
                parts = split (== ';') meta
                (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
            in  Just (mime,cs,enc,uridata)
 where
   upd str m@(mime,cs,enc)
       | isMimeType str               = (str,cs,enc)
       | "charset=" `isPrefixOf` str  = (mime,drop (length "charset=") str,enc)
       | str ==  "base64"             = (mime,cs,True)
       | otherwise                    = m

-- Without parameters like ;charset=...; see RFC 2045, 5.1
readDataURI uri =
    let prefix = "data:"
    in  if not (prefix `isPrefixOf` uri)
        then Nothing
        else
            let rest = drop (length prefix) uri
                meta = takeWhile (/= ',') rest  -- without trailing ','
                uridata = drop (length meta + 1) rest
                parts = split (== ';') meta
                (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
            in  Just (mime,cs,enc,uridata)
 where
   upd str m@(mime,cs,enc)
       | isMimeType str               = (str,cs,enc)
       | "charset=" `isPrefixOf` str  = (mime,drop (length "charset=") str,enc)
       | str ==  "base64"             = (mime,cs,True)
       | otherwise                    = m

-- Without parameters like ;charset=...; see RFC 2045, 5.1