Skip to content

Commit

Permalink
Add stringCI
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jan 4, 2012
1 parent 086a89b commit 5ac0ce3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2011 Simon Hengel <sol@typeful.net>
Copyright (c) 2011, 2012 Simon Hengel <sol@typeful.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion attoparsec-parsec.cabal
Expand Up @@ -3,7 +3,7 @@ version: 3.1.2.0
synopsis: An Attoparsec compatibility layer for Parsec
license: MIT
license-file: LICENSE
copyright: (c) 2011 Simon Hengel
copyright: (c) 2011, 2012 Simon Hengel
author: Simon Hengel
maintainer: Simon Hengel <sol@typeful.net>
build-type: Simple
Expand Down
18 changes: 17 additions & 1 deletion src/Data/Attoparsec/Text/Parsec.hs
Expand Up @@ -39,7 +39,7 @@ module Data.Attoparsec.Text.Parsec (

-- * Efficient string handling
, string
-- , stringCI
, stringCI
, skipSpace
, skipWhile
-- , I.scan
Expand Down Expand Up @@ -72,6 +72,7 @@ module Data.Attoparsec.Text.Parsec (
) where

import Prelude hiding (take, takeWhile)
import Data.Char (toLower, toUpper)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as L
Expand Down Expand Up @@ -145,6 +146,21 @@ space = Parsec.space
string :: String -> Parser Text
string s = Text.pack <$> Parsec.string s

-- | Satisfy a literal string, ignoring case.
--
-- NOTE: No proper case folding is done, yet. Currently @stringCI s@ is just
--
-- > char (toLower c) <|> char (toUpper c)
--
-- for each character of @s@. The implementation from @Data.Attoparsec.Text@
-- tries to do proper case folding, but is actually buggy (see
-- <https://github.com/bos/attoparsec/issues/6>). As long as you deal with
-- characters from the ASCII range, both implementations should be fine.
stringCI :: Text -> Parser Text
stringCI = fmap Text.pack . sequence . map f . Text.unpack
where
f c = char (toLower c) <|> char (toUpper c)

-- | Skip over white space.
skipSpace :: Parser ()
skipSpace = Parsec.spaces
Expand Down
7 changes: 7 additions & 0 deletions test/Spec.hs
Expand Up @@ -36,3 +36,10 @@ spec = do

it "consumes nothing, if n is negative" $ do
parseOnly ((,) <$> take (-3) <*> takeText) "foobar" `shouldBe` Right ("", "foobar")

describe "stringCI" $ do
it "ignores case" $ do
parseOnly (stringCI "fooBAR") "FOObar" `shouldBe` Right "FOObar"

it "does proper case folding" $ (const . pending) "ignored" $ do
parseOnly (stringCI "dass") "da\223" `shouldBe` Right "da\223"

0 comments on commit 5ac0ce3

Please sign in to comment.