Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hikaru
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
hikaru
Commits
678279bd
Verified
Commit
678279bd
authored
3 years ago
by
jan.hamal.dvorak
Browse files
Options
Downloads
Patches
Plain Diff
Get rid of ICU dependency
Signed-off-by:
Jan Hamal Dvořák
<
mordae@anilinux.org
>
parent
80caa889
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
hikaru.cabal
+3
-1
3 additions, 1 deletion
hikaru.cabal
lib/Hikaru/Media.hs
+47
-28
47 additions, 28 deletions
lib/Hikaru/Media.hs
with
50 additions
and
29 deletions
hikaru.cabal
+
3
−
1
View file @
678279bd
...
@@ -84,7 +84,6 @@ common common
...
@@ -84,7 +84,6 @@ common common
, resourcet >=1.2 && <1.3
, resourcet >=1.2 && <1.3
, string-conversions >=0.4 && <0.5
, string-conversions >=0.4 && <0.5
, text >=1.2 && <1.3
, text >=1.2 && <1.3
, text-icu >=0.7 && <0.8
, time >=1.9 && <1.12
, time >=1.9 && <1.12
, wai >=3.2 && <3.3
, wai >=3.2 && <3.3
, wai-extra >=3.0 && <3.2
, wai-extra >=3.0 && <3.2
...
@@ -114,6 +113,9 @@ library
...
@@ -114,6 +113,9 @@ library
other-modules:
other-modules:
Paths_hikaru
Paths_hikaru
ghc-options:
-Wunused-packages
test-suite spec
test-suite spec
import: common
import: common
...
...
This diff is collapsed.
Click to expand it.
lib/Hikaru/Media.hs
+
47
−
28
View file @
678279bd
...
@@ -17,6 +17,7 @@ module Hikaru.Media
...
@@ -17,6 +17,7 @@ module Hikaru.Media
-- * Parsing
-- * Parsing
,
parseMedia
,
parseMedia
,
pMediaList
-- * Matching
-- * Matching
,
matchMedia
,
matchMedia
...
@@ -24,11 +25,13 @@ module Hikaru.Media
...
@@ -24,11 +25,13 @@ module Hikaru.Media
,
selectMedia
,
selectMedia
)
)
where
where
import
Relude
hiding
(
group
,
find
,
head
)
import
Relude
hiding
(
head
,
get
,
many
)
import
Relude.Unsafe
(
head
)
import
Relude.Unsafe
(
head
)
import
Text.ParserCombinators.ReadP
import
Data.String.Conversions
import
Data.String.Conversions
import
Data.Text.ICU
import
Data.Text
(
toLower
)
import
Data.List
(
lookup
)
import
Data.Char
(
isControl
,
isSpace
)
-- |
-- |
...
@@ -41,6 +44,7 @@ where
...
@@ -41,6 +44,7 @@ where
=
Media
=
Media
{
mediaMainType
::
Text
{
mediaMainType
::
Text
,
mediaSubType
::
Text
,
mediaSubType
::
Text
,
mediaParams
::
[(
Text
,
Text
)]
,
mediaQuality
::
Float
,
mediaQuality
::
Float
}
}
deriving
(
Show
,
Eq
,
Ord
)
deriving
(
Show
,
Eq
,
Ord
)
...
@@ -65,19 +69,48 @@ where
...
@@ -65,19 +69,48 @@ where
-- ]
-- ]
--
--
parseMedia
::
Text
->
[
Media
]
parseMedia
::
Text
->
[
Media
]
parseMedia
=
sortWith
(
negate
.
mediaQuality
)
parseMedia
text
=
case
readP_to_S
pMediaList
(
cs
(
toLower
text
))
of
.
mapMaybe
build
(
m
,
""
)
:
_
->
sortWith
(
negate
.
mediaQuality
)
m
.
mapMaybe
(
group
0
)
_else
->
[]
.
findAll
reMedia
-- |
-- Parser for the media list coded mostly to the RFC 2045.
-- Input is always lowercased and unicode is accepted.
--
pMediaList
::
ReadP
[
Media
]
pMediaList
=
sepBy
pMedia
pSeparator
<*
eof
where
where
build
::
Text
->
Maybe
Media
pMedia
=
do
build
t
=
do
mediaMainType
<-
cs
<$>
pToken
match
<-
find
reParts
t
_
<-
char
'/'
main
<-
group
1
match
mediaSubType
<-
cs
<$>
pToken
sub
<-
group
2
match
mediaParams
<-
many
pParameter
q
<-
(
readMaybe
=<<
cs
<$>
group
3
match
)
<|>
Just
1.0
let
mediaQuality
=
fromMaybe
1.0
do
q
<-
lookup
"q"
mediaParams
readMaybe
(
cs
q
)
return
Media
{
..
}
pParameter
=
do
_
<-
pSpaced
$
char
';'
name
<-
cs
<$>
pToken
_
<-
pSpaced
$
char
'='
value
<-
cs
<$>
pValue
return
(
name
,
value
)
pSeparator
=
pSpaced
$
char
','
pToken
=
pSpaced
$
many1
(
satisfy
(
not
.
quote
))
pValue
=
pToken
<++
pQuotedStr
pQuotedStr
=
pSpaced
$
pQuoted
$
many
(
pExcept
'
\\
'
)
return
$
Media
main
sub
q
pExcept
c
=
satisfy
(
c
/=
)
pSpaced
p
=
skipSpaces
*>
p
<*
skipSpaces
pQuoted
p
=
char
'"'
*>
p
<*
char
'"'
quote
c
=
isControl
c
||
isSpace
c
||
c
`
elem
`
specials
specials
=
"()<>@,;:
\\\"
/[]?="
::
[
Char
]
-- |
-- |
...
@@ -135,18 +168,4 @@ where
...
@@ -135,18 +168,4 @@ where
prod
=
[(
l
,
r
)
|
l
<-
ls
,
r
<-
rs
]
prod
=
[(
l
,
r
)
|
l
<-
ls
,
r
<-
rs
]
-- |
-- Used to split text into individual media elements.
--
reMedia
::
Regex
reMedia
=
"(?:[^,]|
\"
(?:[^
\"
]|
\\\"
)*
\"
)+"
-- |
-- Used to split media element into its individual components.
--
reParts
::
Regex
reParts
=
"([[:alnum:]!#$%&'*+.^_`|~-]+)(?:
\\
s*/
\\
s*([[:alnum:]!#$%&'*+.^_`|~-]+))?(?:.*;q
\\
s*=
\\
s*([0-9.]+))?"
-- vim:set ft=haskell sw=2 ts=2 et:
-- vim:set ft=haskell sw=2 ts=2 et:
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment