Skip to content

Commit bf217ba

Browse files
committed
Unicode types optimizations wip
1 parent acce50e commit bf217ba

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

pub/functora/functora.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ common pkg-prelude
122122
, witch-mini
123123
, with-utf8
124124

125+
if ((impl(ghcjs) || arch(javascript)) || os(wasi))
126+
build-depends: ghcjs-base
127+
125128
common pkg-qr
126129
import: pkg
127130
hs-source-dirs: src/qr

pub/functora/src/bolt11/Functora/Bolt11.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,7 @@ data Bolt11HrpAmt = Bolt11HrpAmt
284284
}
285285
deriving stock (Eq, Ord, Show, Data, Generic)
286286

287-
inspectBolt11HrpAmt ::
288-
( From String a,
289-
Semigroup a,
290-
IsString a
291-
) =>
292-
Bolt11HrpAmt ->
293-
a
287+
inspectBolt11HrpAmt :: (IsString a, Semigroup a) => Bolt11HrpAmt -> a
294288
inspectBolt11HrpAmt (Bolt11HrpAmt amt mul) =
295289
if sat > 1_000_000
296290
then inspectBolt11HrpAmt' btc <> " BTC"
@@ -308,7 +302,7 @@ inspectBolt11HrpAmt (Bolt11HrpAmt amt mul) =
308302

309303
inspectBolt11HrpAmt' ::
310304
forall a b.
311-
( From String a,
305+
( IsString a,
312306
From b Integer,
313307
Integral b
314308
) =>

pub/functora/src/prelude/Functora/Prelude.hs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
{-# LANGUAGE CPP #-}
2+
13
module Functora.Prelude
24
( -- * Reexport
35
-- $reexport
46
module X,
57
LiftTH,
8+
Unicode,
69

710
-- * Show
811
-- $show
@@ -351,16 +354,33 @@ import UnliftIO as X
351354
import UnliftIO.MVar as X (modifyMVar)
352355
import Witch.Mini as X
353356
import qualified Prelude
357+
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
358+
import qualified Data.JSString as JS
359+
#endif
354360

355361
-- $reexport
356362
-- Reexport
357363

358364
type LiftTH = TH.Lift
359365

366+
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
367+
type Unicode = JS.JSString
368+
#else
369+
type Unicode = Text
370+
#endif
371+
360372
-- $show
361373
-- Show
362374

363-
inspect :: forall dst src. (Show src, Data src, IsString dst) => src -> dst
375+
inspect ::
376+
forall dst src.
377+
( Show src,
378+
Data src,
379+
Typeable dst,
380+
IsString dst
381+
) =>
382+
src ->
383+
dst
364384
inspect =
365385
display @dst @src
366386
. Syb.everywhere (Syb.mkT prettyByteString)
@@ -404,7 +424,7 @@ defaultRatioFormat =
404424

405425
inspectRatio ::
406426
forall a b.
407-
( From String a,
427+
( IsString a,
408428
From b Integer,
409429
Integral b
410430
) =>
@@ -432,14 +452,14 @@ inspectRatio fmt signedRational =
432452
>= limit
433453
&& ratioFormatDecimalPlacesTotalLimitOverflow fmt
434454
== DecimalPlacesOverflowExponent ->
435-
from @String @a
455+
fromString @a
436456
. Scientific.formatScientific Scientific.Exponent Nothing
437457
. either fst fst
438458
. Scientific.fromRationalRepetend Nothing
439459
$ signedNumerator
440460
% signedDenominator
441461
_ ->
442-
from @String @a
462+
fromString @a
443463
$ (if signedRational < 0 then "-" else mempty)
444464
<> Prelude.shows
445465
quotient
@@ -481,7 +501,7 @@ inspectRatio fmt signedRational =
481501

482502
inspectRatioDef ::
483503
forall a b.
484-
( From String a,
504+
( IsString a,
485505
From b Integer,
486506
Integral b
487507
) =>
@@ -496,8 +516,18 @@ roundRational decimalPlaces input =
496516
where
497517
mult = 10 ^ decimalPlaces
498518

499-
display :: forall dst src. (Show src, Typeable src, IsString dst) => src -> dst
519+
display ::
520+
forall dst src.
521+
( Show src,
522+
Typeable src,
523+
Typeable dst,
524+
IsString dst
525+
) =>
526+
src ->
527+
dst
500528
display x
529+
| Just HRefl <- typeOf x `eqTypeRep` typeRep @dst =
530+
x
501531
| Just HRefl <- typeOf x `eqTypeRep` typeRep @String =
502532
fromString x
503533
| Just HRefl <- typeOf x `eqTypeRep` typeRep @Text =
@@ -515,7 +545,18 @@ display x
515545
(fromString . from @TL.Text @String)
516546
(TLE.decodeUtf8' x)
517547
| otherwise =
548+
defDisplay
549+
where
550+
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
551+
defDisplay
552+
| Just HRefl <- typeOf x `eqTypeRep` typeRep @JS.JSString =
553+
fromString $ JS.unpack x
554+
| otherwise =
555+
Universum.show x
556+
#else
557+
defDisplay =
518558
Universum.show x
559+
#endif
519560

520561
prettyByteString :: ByteString -> ByteString
521562
prettyByteString raw =
@@ -761,7 +802,7 @@ utf8FromLatin1 raw =
761802

762803
qq ::
763804
forall inp out e.
764-
( From String inp,
805+
( IsString inp,
765806
Typeable out,
766807
TH.Lift out,
767808
Data e,
@@ -785,7 +826,7 @@ qq parser =
785826
<> ") with the failure ("
786827
<> inspect e
787828
<> ")"
788-
case parser $ from @String @inp x0 of
829+
case parser $ fromString @inp x0 of
789830
Left e -> fatal e
790831
Right x -> TH.lift x
791832
}
@@ -1031,7 +1072,8 @@ prevEnum x
10311072

10321073
type Textual mono =
10331074
( Seq.Textual mono,
1034-
Container mono
1075+
Container mono,
1076+
Typeable mono
10351077
)
10361078

10371079
strip :: (Textual mono) => mono -> mono

pub/functora/src/soplate/Functora/Soplate.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ inspectSop ::
4747
Typeable src,
4848
Soplate ByteString src,
4949
Soplate BL.ByteString src,
50+
Typeable dst,
5051
IsString dst
5152
) =>
5253
src ->

0 commit comments

Comments
 (0)