• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

msakai / pseudo-boolean / 56

13 Mar 2024 02:01PM UTC coverage: 92.655% (-1.1%) from 93.714%
56

push

github

msakai
Merge branch 'update-ci-202403'

492 of 531 relevant lines covered (92.66%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/src/Data/PseudoBoolean/Internal/TextUtil.hs
1
{-# LANGUAGE BangPatterns, CPP #-}
2
-----------------------------------------------------------------------------
3
-- |
4
-- Module      :  Data.PseudoBoolean.Internal.TextUtil
5
-- Copyright   :  (c) Masahiro Sakai 2012-2014
6
-- License     :  BSD-style
7
-- 
8
-- Maintainer  :  masahiro.sakai@gmail.com
9
-- Stability   :  provisional
10
-- Portability :  non-portable (BangPatterns)
11
--
12
-----------------------------------------------------------------------------
13
module Data.PseudoBoolean.Internal.TextUtil
14
  ( readUnsignedInteger
15
  ) where
16

17
#include "MachDeps.h"
18

19
import Control.Exception
20
import Data.Word
21

22
-- | 'read' allocate too many intermediate 'Integer'.
23
-- Therefore we use this optimized implementation instead.
24
-- Many intermediate values in this implementation will be optimized
25
-- away by worker-wrapper transformation and unboxing.
26
{-# INLINABLE readUnsignedInteger #-}
27
readUnsignedInteger :: String -> Integer 
28
readUnsignedInteger str = assert (result == read str) $ result
×
29
  where
30
    result :: Integer
31
    result = go 0 str
1✔
32

33
    lim :: Word
34
#if !MIN_VERSION_base(4,6,1) && WORD_SIZE_IN_BITS == 32
35
    {- To avoid a bug of maxBound <https://ghc.haskell.org/trac/ghc/ticket/8072> -}
36
    lim = 0xFFFFFFFF `div` 10
37
#else
38
    lim = maxBound `div` 10
1✔
39
#endif
40
  
41
    go :: Integer -> [Char] -> Integer 
42
    go !r [] = r
1✔
43
    go !r ds =
44
      case go2 0 1 ds of
1✔
45
        (r2,b,ds2) -> go (r * fromIntegral b + fromIntegral r2) ds2
1✔
46

47
    go2 :: Word -> Word -> [Char] -> (Word, Word, [Char])
48
    go2 !r !b dds | assert (b > r) (b > lim) = (r,b,dds)
×
49
    go2 !r !b []     = (r, b, [])
1✔
50
    go2 !r !b (d:ds) = go2 (r*10 + charToWord d) (b*10) ds
1✔
51

52
    charToWord :: Char -> Word
53
    charToWord '0' = 0
1✔
54
    charToWord '1' = 1
1✔
55
    charToWord '2' = 2
1✔
56
    charToWord '3' = 3
1✔
57
    charToWord '4' = 4
1✔
58
    charToWord '5' = 5
1✔
59
    charToWord '6' = 6
1✔
60
    charToWord '7' = 7
1✔
61
    charToWord '8' = 8
1✔
62
    charToWord '9' = 9
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc