From 8d740cf88a0ad68c77a9af887e65317f898da274 Mon Sep 17 00:00:00 2001 From: Henning Guenther Date: Sat, 19 Jan 2008 17:08:29 -0800 Subject: [PATCH] Refactored getT out of BootString darcs-hash:20080120010829-a4fee-7d617d76cc2e67b0e33662510c9736367396d2ea --- Data/Encoding/BootString.hs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Data/Encoding/BootString.hs b/Data/Encoding/BootString.hs index 7261f60..f9f9a0d 100644 --- a/Data/Encoding/BootString.hs +++ b/Data/Encoding/BootString.hs @@ -56,12 +56,12 @@ punyChar c where norep = throwDyn OutOfRange -threshold :: BootString -> Int -> Int -> Int -threshold bs bias pos - | r > tmax bs = tmax bs - | r < tmin bs = tmin bs - where - r = (base bs)*(pos+1)-bias +getT :: BootString -> Int -> Int -> Int +getT bs k bias + | k <= bias + (tmin bs) = tmin bs + | k >= bias + (tmax bs) = tmax bs + | otherwise = k-bias + adapt :: BootString -> Int -> Int -> Bool -> Int adapt bs delta numpoints firsttime = let @@ -77,11 +77,7 @@ adapt bs delta numpoints firsttime = let decodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int] -> (Int,[Int]) decodeValue bs bias i k w (x:xs) = let ni = i + x*w - t = if k <= bias + (tmin bs) - then tmin bs - else (if k >= bias + (tmax bs) - then tmax bs - else k-bias) + t = getT bs k bias in if x < t then (ni,xs) else decodeValue bs bias ni (k+base bs) (w*(base bs - t)) xs @@ -109,11 +105,7 @@ punyDecode base ext = insertDeltas (decodeValues punycode (length base) (map pun encodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int] encodeValue bs bias delta n c = unfoldr (\(q,k,out) -> let - t = if k <= bias + tmin bs - then tmin bs - else (if k >= bias + tmax bs - then tmax bs - else k - bias) + t = getT bs k bias (nq,dc) = (q-t) `divMod` (base bs - t) in if out then Nothing