king: add helper for escaping Text into @ta format

Behavior should match (scot %t the-text).
This commit is contained in:
fang 2020-10-28 19:13:23 +01:00
parent 2e7b2ceedb
commit 418b9fda7e
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972

View File

@ -13,6 +13,7 @@ module Urbit.Noun.Conversions
, Mug(..), Path(..), EvilPath(..), Ship(..)
, Lenient(..), pathToFilePath, filePathToPath
, showUD, tshowUD
, textAsTa
) where
import ClassyPrelude hiding (hash)
@ -40,7 +41,9 @@ import Urbit.Noun.Jam (jam)
import Urbit.Ob (patp)
import qualified Data.Char as C
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Numeric as N
-- Noun ------------------------------------------------------------------------
@ -551,6 +554,18 @@ instance FromNoun Knot where
then pure (MkKnot txt)
else fail ("Non-ASCII chars in knot: " <> unpack txt)
-- equivalent of (cury scot %t)
textAsTa :: Text -> Text
textAsTa = ("~~" <>) . concatMap \case
' ' -> "."
'.' -> "~."
'~' -> "~~"
c ->
if C.isAlphaNum c || (c == '-') then
T.singleton c
else
if C.ord c < 0x10 then "~0" else "~"
<> (pack $ N.showHex (C.ord c) ".")
-- Term ------------------------------------------------------------------------