2008-10-13 01:52:48 +04:00
|
|
|
{-|
|
|
|
|
|
2008-10-15 06:11:30 +04:00
|
|
|
A 'Commodity' is a symbol representing a currency or some other kind of
|
2008-10-18 23:30:07 +04:00
|
|
|
thing we are tracking, and some display preferences that tell how to
|
|
|
|
display 'Amount's of the commodity - is the symbol on the left or right,
|
|
|
|
are thousands separated by comma, significant decimal places and so on.
|
2008-10-13 01:52:48 +04:00
|
|
|
|
|
|
|
-}
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-25 01:51:56 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
|
2010-05-20 03:08:53 +04:00
|
|
|
module Hledger.Data.Commodity
|
2008-10-13 01:52:48 +04:00
|
|
|
where
|
2018-05-22 04:52:34 +03:00
|
|
|
import Data.Char (isDigit)
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.List
|
2012-11-20 01:20:10 +04:00
|
|
|
import Data.Maybe (fromMaybe)
|
2018-03-25 01:51:56 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
import Data.Monoid
|
2018-03-25 01:51:56 +03:00
|
|
|
#endif
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
import qualified Data.Text as T
|
2012-11-20 01:20:10 +04:00
|
|
|
-- import qualified Data.Map as M
|
2011-05-28 08:11:44 +04:00
|
|
|
|
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Utils
|
2008-10-13 01:52:48 +04:00
|
|
|
|
|
|
|
|
2014-04-30 22:28:47 +04:00
|
|
|
-- characters that may not be used in a non-quoted commodity symbol
|
2019-02-14 16:14:52 +03:00
|
|
|
nonsimplecommoditychars = "0123456789-+.@*;\n \"{}=" :: String
|
2010-05-27 05:31:50 +04:00
|
|
|
|
2018-05-22 04:52:34 +03:00
|
|
|
isNonsimpleCommodityChar :: Char -> Bool
|
|
|
|
isNonsimpleCommodityChar c = isDigit c || c `textElem` otherChars
|
|
|
|
where
|
|
|
|
otherChars = "-+.@*;\n \"{}=" :: T.Text
|
|
|
|
textElem = T.any . (==)
|
|
|
|
|
|
|
|
quoteCommoditySymbolIfNeeded s | T.any (isNonsimpleCommodityChar) s = "\"" <> s <> "\""
|
2010-05-27 05:38:23 +04:00
|
|
|
| otherwise = s
|
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
commodity = ""
|
2008-10-15 10:00:10 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- handy constructors for tests
|
|
|
|
-- unknown = commodity
|
|
|
|
-- usd = "$"
|
|
|
|
-- eur = "€"
|
|
|
|
-- gbp = "£"
|
|
|
|
-- hour = "h"
|
2008-10-13 01:52:48 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- Some sample commodity' names and symbols, for use in tests..
|
|
|
|
commoditysymbols =
|
|
|
|
[("unknown","")
|
|
|
|
,("usd","$")
|
|
|
|
,("eur","€")
|
|
|
|
,("gbp","£")
|
|
|
|
,("hour","h")
|
|
|
|
]
|
2008-10-13 01:52:48 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- | Look up one of the sample commodities' symbol by name.
|
2016-05-08 02:18:04 +03:00
|
|
|
comm :: String -> CommoditySymbol
|
2014-09-11 00:07:53 +04:00
|
|
|
comm name = snd $ fromMaybe
|
|
|
|
(error' "commodity lookup failed")
|
2012-11-20 01:20:10 +04:00
|
|
|
(find (\n -> fst n == name) commoditysymbols)
|
2008-10-13 01:52:48 +04:00
|
|
|
|
2008-10-18 23:30:07 +04:00
|
|
|
-- | Find the conversion rate between two commodities. Currently returns 1.
|
2016-05-08 02:18:04 +03:00
|
|
|
conversionRate :: CommoditySymbol -> CommoditySymbol -> Double
|
2009-06-05 13:44:20 +04:00
|
|
|
conversionRate _ _ = 1
|
2008-10-13 01:52:48 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- -- | Convert a list of commodities to a map from commodity symbols to
|
|
|
|
-- -- unique, display-preference-canonicalised commodities.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- canonicaliseCommodities :: [CommoditySymbol] -> Map.Map String CommoditySymbol
|
2012-11-20 01:20:10 +04:00
|
|
|
-- canonicaliseCommodities cs =
|
|
|
|
-- Map.fromList [(s,firstc{precision=maxp}) | s <- symbols,
|
|
|
|
-- let cs = commoditymap ! s,
|
|
|
|
-- let firstc = head cs,
|
|
|
|
-- let maxp = maximum $ map precision cs
|
|
|
|
-- ]
|
|
|
|
-- where
|
|
|
|
-- commoditymap = Map.fromList [(s, commoditieswithsymbol s) | s <- symbols]
|
|
|
|
-- commoditieswithsymbol s = filter ((s==) . symbol) cs
|
|
|
|
-- symbols = nub $ map symbol cs
|
2010-12-27 23:26:22 +03:00
|
|
|
|