Skip to content

Commit

Permalink
Simplify regex matching (with TDFA instead of PCRE)
Browse files Browse the repository at this point in the history
  • Loading branch information
memowe committed Mar 10, 2021
1 parent 4efbff2 commit 7cc34cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for trivialini

## 0.1.2.0 -- 2021-03-07

* Simplify regex matching
* Use TDFA instead of PCRE to minimize external dependencies

## 0.1.1.0 -- 2021-03-07

* Add internal data type simplification (shows with Show)
Expand Down
31 changes: 13 additions & 18 deletions src/Trivialini.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- | Ultra light weight ini file parser
module Trivialini (config, configFile) where

import Data.Map (Map, keys, empty, insert, (!))
import Text.Regex (mkRegex, matchRegex)
import Data.Map (Map, keys, empty, insert, (!))
import Text.Regex.TDFA ((=~))

--
-- Data structure and accessors
Expand Down Expand Up @@ -54,23 +54,18 @@ simplify config@(Config sec defs rest)
rc :: String -> Map String String -> [String] -> Config
-- Build config data line by line (first argument is the current section)
rc sec values [] = Config sec values Empty
rc sec values (line:lines) = case chkSec line of

-- It's a section
(Just [newS]) -> Config sec values $ rc newS empty lines

-- It's not a section
Nothing -> case chkKV line of

-- It's a key value assignment
(Just [key, value]) -> rc sec (insert key value values) lines

-- Neither: ignore
Nothing -> rc sec values lines

rc sec values (line:lines)
| isSec = Config sec values $ rc getSec empty lines
| isKV = rc sec insKV lines
| otherwise = rc sec values lines -- No match: ignore that line
where
chkSec = matchRegex (mkRegex "^\\[(.+)\\]")
chkKV = matchRegex (mkRegex "^(\\S+)\\s+=\\s+(.*)$")
rxSec = "^\\[(.+)\\]"
rxKV = "^([^ ]+) += +(.*)$"
isSec = line =~ rxSec :: Bool
isKV = line =~ rxKV :: Bool
getSec = let [[_, sec]] = line =~ rxSec :: [[String]] in sec
getKV = let [[_, k, v]] = line =~ rxKV :: [[String]] in (k, v)
insKV = let (k, v) = getKV in insert k v values

--
-- Public interface
Expand Down
4 changes: 2 additions & 2 deletions trivialini.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/

name: trivialini
version: 0.1.1.0
version: 0.1.2.0
synopsis: Ultra light weight ini file parser
-- description:
homepage: https://github.com/memowe/trivialini
Expand All @@ -24,6 +24,6 @@ library
build-depends:
base >=4.12 && <4.13,
containers,
regex-compat
regex-tdfa
hs-source-dirs: src
default-language: Haskell2010

0 comments on commit 7cc34cf

Please sign in to comment.