module Sound.SC3.UGen.Dot.Common where
import Text.Printf
bracket1 :: (a,a) -> [a] -> [a]
bracket1 (l,r) x = [l] ++ x ++ [r]
bracket :: ([a],[a]) -> [a] -> [a]
bracket (l,r) x = l ++ x ++ r
string_pp :: String -> String
string_pp = bracket1 ('"','"')
label_pp :: String -> String
label_pp = bracket ("\"{","}\"")
int_pp :: Int -> String
int_pp = show
right_variant :: ([a] -> [b]) -> [a] -> [b]
right_variant f = reverse . f . reverse
limit_precision_p :: (PrintfArg n) => Bool -> Int -> n -> String
limit_precision_p r n c =
let i = printf "%.*f" n c
in if r
then i
else right_variant (dropWhile (== '.') . dropWhile (== '0')) i
limit_precision_e :: (Read n,Show n) => Int -> n -> String
limit_precision_e n c =
case show c of
"Infinity" -> "Infinity"
c' ->
let (i,j') = break (== '.') c'
j = if null j' then error ("limit_precision_e: no dot: " ++ c') else tail j'
(k,l) = break (== 'e') j
f :: String -> Int
f m = round ((read (take (n + 1) m) :: Read n => n) / (10::Double))
k' = if length k > n
then show (f k)
else k
in i ++ if k == "0"
then l
else "." ++ k' ++ l
limit_precision :: (Floating n,Ord n,Read n,Show n,PrintfArg n) => Bool -> Int -> n -> String
limit_precision r n c =
if c /= 0 && (c < (10 ** fromIntegral ( n)) || c > 1e6)
then limit_precision_e n c
else limit_precision_p r n c