You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.6 KiB
120 lines
3.6 KiB
|
14 years ago
|
-- Copyright © 2010-2012 Jon Kristensen. See the LICENSE file in the Pontarius
|
||
|
|
-- distribution for more details.
|
||
|
15 years ago
|
|
||
|
14 years ago
|
-- TODO: More efficient to use Text instead of Strings for ID generation?
|
||
|
15 years ago
|
|
||
|
15 years ago
|
{-# OPTIONS_HADDOCK hide #-}
|
||
|
15 years ago
|
|
||
|
15 years ago
|
{-# LANGUAGE OverloadedStrings #-}
|
||
|
15 years ago
|
|
||
|
14 years ago
|
module Network.XMPP.Utilities (idGenerator) where
|
||
|
14 years ago
|
|
||
|
|
import Network.XMPP.Types
|
||
|
15 years ago
|
|
||
|
14 years ago
|
import Control.Monad.STM
|
||
|
|
import Control.Concurrent.STM.TVar
|
||
|
|
import Prelude
|
||
|
14 years ago
|
import Control.Applicative (many)
|
||
|
15 years ago
|
|
||
|
14 years ago
|
import qualified Data.Attoparsec.Text as AP
|
||
|
14 years ago
|
import qualified Data.Text as Text
|
||
|
15 years ago
|
|
||
|
|
|
||
|
14 years ago
|
-- |
|
||
|
14 years ago
|
-- Creates a new @IdGenerator@. Internally, it will maintain an infinite list of
|
||
|
|
-- IDs ('[\'a\', \'b\', \'c\'...]'). The argument is a prefix to prepend the IDs
|
||
|
|
-- with. Calling the function will extract an ID and update the generator's
|
||
|
|
-- internal state so that the same ID will not be generated again.
|
||
|
14 years ago
|
|
||
|
14 years ago
|
idGenerator :: Text.Text -> IO IdGenerator
|
||
|
14 years ago
|
|
||
|
14 years ago
|
idGenerator prefix = atomically $ do
|
||
|
|
tvar <- newTVar $ ids prefix
|
||
|
|
return $ IdGenerator $ next tvar
|
||
|
14 years ago
|
|
||
|
|
where
|
||
|
|
|
||
|
14 years ago
|
-- Transactionally extract the next ID from the infinite list of IDs.
|
||
|
14 years ago
|
|
||
|
14 years ago
|
next :: TVar [Text.Text] -> IO Text.Text
|
||
|
|
next tvar = atomically $ do
|
||
|
|
list <- readTVar tvar
|
||
|
14 years ago
|
case list of
|
||
|
|
[] -> error "empty list in Utilities.hs"
|
||
|
|
(x:xs) -> do
|
||
|
|
writeTVar tvar xs
|
||
|
|
return x
|
||
|
14 years ago
|
|
||
|
|
-- Generates an infinite and predictable list of IDs, all beginning with the
|
||
|
|
-- provided prefix.
|
||
|
|
|
||
|
|
ids :: Text.Text -> [Text.Text]
|
||
|
14 years ago
|
|
||
|
|
-- Adds the prefix to all combinations of IDs (ids').
|
||
|
14 years ago
|
ids p = map (\ id -> Text.append p id) ids'
|
||
|
14 years ago
|
where
|
||
|
|
|
||
|
|
-- Generate all combinations of IDs, with increasing length.
|
||
|
14 years ago
|
ids' :: [Text.Text]
|
||
|
|
ids' = map Text.pack $ concatMap ids'' [1..]
|
||
|
14 years ago
|
|
||
|
|
-- Generates all combinations of IDs with the given length.
|
||
|
|
ids'' :: Integer -> [String]
|
||
|
|
ids'' 0 = [""]
|
||
|
|
ids'' l = [x:xs | x <- repertoire, xs <- ids'' (l - 1)]
|
||
|
|
|
||
|
|
-- Characters allowed in IDs.
|
||
|
|
repertoire :: String
|
||
|
14 years ago
|
repertoire = ['a'..'z']
|
||
|
|
|
||
|
|
|
||
|
|
-- Converts a "<major>.<minor>" numeric version number to a @Version@ object.
|
||
|
|
versionFromString :: Text.Text -> Maybe Version
|
||
|
|
versionFromString s = case AP.parseOnly versionParser s of
|
||
|
|
Right version -> Just version
|
||
|
|
Left _ -> Nothing
|
||
|
|
|
||
|
|
|
||
|
|
-- Constructs a "Version" based on the major and minor version numbers.
|
||
|
|
versionFromNumbers :: Integer -> Integer -> Version
|
||
|
|
versionFromNumbers major minor = Version major minor
|
||
|
|
|
||
|
|
|
||
|
|
-- Read numbers, a dot, more numbers, and end-of-file.
|
||
|
|
versionParser :: AP.Parser Version
|
||
|
|
versionParser = do
|
||
|
|
major <- AP.many1 AP.digit
|
||
|
14 years ago
|
AP.skip (== '.')
|
||
|
14 years ago
|
minor <- AP.many1 AP.digit
|
||
|
|
AP.endOfInput
|
||
|
14 years ago
|
return $ Version (read major) (read minor)
|
||
|
|
|
||
|
|
|
||
|
|
-- | Parses, validates, and possibly constructs a "LangTag" object.
|
||
|
|
langTag :: Text.Text -> Maybe LangTag
|
||
|
|
langTag s = case AP.parseOnly langTagParser s of
|
||
|
|
Right tag -> Just tag
|
||
|
|
Left _ -> Nothing
|
||
|
|
|
||
|
|
|
||
|
|
-- Parses a language tag as defined by RFC 1766 and constructs a LangTag object.
|
||
|
|
langTagParser :: AP.Parser LangTag
|
||
|
|
langTagParser = do
|
||
|
|
-- Read until we reach a '-' character, or EOF. This is the `primary tag'.
|
||
|
|
primTag <- tag
|
||
|
|
-- Read zero or more subtags.
|
||
|
|
subTags <- many subtag
|
||
|
|
AP.endOfInput
|
||
|
|
return $ LangTag primTag subTags
|
||
|
|
where
|
||
|
|
tag :: AP.Parser Text.Text
|
||
|
|
tag = do
|
||
|
|
t <- AP.takeWhile1 $ AP.inClass tagChars
|
||
|
|
return t
|
||
|
|
subtag :: AP.Parser Text.Text
|
||
|
|
subtag = do
|
||
|
|
AP.skip (== '-')
|
||
|
|
subtag <- tag
|
||
|
|
return subtag
|
||
|
|
tagChars :: [Char]
|
||
|
|
tagChars = ['a'..'z'] ++ ['A'..'Z']
|