Skip to content

add dotAll regexp flag #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Data/String/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.flagsImpl = function (r) {
multiline: r.multiline,
ignoreCase: r.ignoreCase,
global: r.global,
dotAll: r.dotAll,
sticky: !!r.sticky,
unicode: !!r.unicode
};
Expand Down
2 changes: 2 additions & 0 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ renderFlags (RegexFlags f) =
(if f.global then "g" else "") <>
(if f.ignoreCase then "i" else "") <>
(if f.multiline then "m" else "") <>
(if f.dotAll then "s" else "") <>
(if f.sticky then "y" else "") <>
(if f.unicode then "u" else "")

Expand All @@ -70,6 +71,7 @@ parseFlags s = RegexFlags
{ global: contains (Pattern "g") s
, ignoreCase: contains (Pattern "i") s
, multiline: contains (Pattern "m") s
, dotAll: contains (Pattern "s") s
, sticky: contains (Pattern "y") s
, unicode: contains (Pattern "u") s
}
Expand Down
21 changes: 21 additions & 0 deletions src/Data/String/Regex/Flags.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type RegexFlagsRec =
{ global :: Boolean
, ignoreCase :: Boolean
, multiline :: Boolean
, dotAll :: Boolean
, sticky :: Boolean
, unicode :: Boolean
}
Expand All @@ -22,6 +23,7 @@ noFlags = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, dotAll : false
, sticky: false
, unicode: false
}
Expand All @@ -32,6 +34,7 @@ global = RegexFlags
{ global: true
, ignoreCase: false
, multiline: false
, dotAll : false
, sticky: false
, unicode: false
}
Expand All @@ -42,6 +45,7 @@ ignoreCase = RegexFlags
{ global: false
, ignoreCase: true
, multiline: false
, dotAll : false
, sticky: false
, unicode: false
}
Expand All @@ -52,6 +56,7 @@ multiline = RegexFlags
{ global: false
, ignoreCase: false
, multiline: true
, dotAll : false
, sticky: false
, unicode: false
}
Expand All @@ -62,6 +67,7 @@ sticky = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, dotAll : false
, sticky: true
, unicode: false
}
Expand All @@ -72,15 +78,28 @@ unicode = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, dotAll : false
, sticky: false
, unicode: true
}

-- | Only dotAll flag set to true
dotAll :: RegexFlags
dotAll = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, dotAll : true
, sticky: false
, unicode: false
}

instance semigroupRegexFlags :: Semigroup RegexFlags where
append (RegexFlags x) (RegexFlags y) = RegexFlags
{ global: x.global || y.global
, ignoreCase: x.ignoreCase || y.ignoreCase
, multiline: x.multiline || y.multiline
, dotAll: x.dotAll || y.dotAll
, sticky: x.sticky || y.sticky
, unicode: x.unicode || y.unicode
}
Expand All @@ -93,6 +112,7 @@ instance eqRegexFlags :: Eq RegexFlags where
= x.global == y.global
&& x.ignoreCase == y.ignoreCase
&& x.multiline == y.multiline
&& x.dotAll == y.dotAll
&& x.sticky == y.sticky
&& x.unicode == y.unicode

Expand All @@ -104,6 +124,7 @@ instance showRegexFlags :: Show RegexFlags where
<> (guard flags.global $> "global")
<> (guard flags.ignoreCase $> "ignoreCase")
<> (guard flags.multiline $> "multiline")
<> (guard flags.dotAll $> "dotAll")
<> (guard flags.sticky $> "sticky")
<> (guard flags.unicode $> "unicode")
in
Expand Down
3 changes: 2 additions & 1 deletion test/Test/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.String.Regex
import Data.Array.NonEmpty (NonEmptyArray, fromArray)
import Data.Either (isLeft)
import Data.Maybe (Maybe(..), fromJust)
import Data.String.Regex.Flags (global, ignoreCase, noFlags)
import Data.String.Regex.Flags (dotAll, global, ignoreCase, noFlags)
import Data.String.Regex.Unsafe (unsafeRegex)
import Effect (Effect)
import Effect.Console (log)
Expand All @@ -24,6 +24,7 @@ testStringRegex = do
assert $ "quxbarfoobaz" == replace (unsafeRegex "foo" noFlags) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" global) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"
assert $ "quxbarfoobaz" == replace (unsafeRegex ".foo" dotAll) "qux" "\nfoobarfoobaz"

log "match"
assert $ match (unsafeRegex "^abc$" noFlags) "abc" == Just (nea [Just "abc"])
Expand Down