Skip to content

Commit

Permalink
core: Add showCodePoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Jul 3, 2024
1 parent bc0d91b commit f04f272
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions unicode-data/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.6.0 (July 2024)

- Updated to [Unicode 15.1.0](https://www.unicode.org/versions/Unicode15.1.0/).
- Added `showCodePoint` to `Unicode.Char`.
- Added `intToDigiT` to `Unicode.Char.Numeric`.

## 0.5.0 (July 2024)
Expand Down
28 changes: 27 additions & 1 deletion unicode-data/lib/Unicode/Char.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ module Unicode.Char
, module Unicode.Char.Normalization
, module Unicode.Char.Identifiers

-- * Re-export from @base@
-- * Utils
, showCodePoint

-- * Re-export from @base@
, ord
, chr
)
Expand All @@ -50,3 +53,26 @@ import Unicode.Char.Identifiers
import Unicode.Char.Numeric
import Unicode.Char.Normalization
import Unicode.Internal.Char.Version (unicodeVersion)

-- [NOTE] Code inspired from 'showIntAtBase'
-- | Show the code point of a character using the Unicode Standard convention:
-- hexadecimal codepoint padded with zeros if inferior to 4 characters.
--
-- >>> showCodePoint '\xf' ""
-- "000F"
-- >>> showCodePoint '\x1ffff' ""
-- "1FFFF"
--
-- @since 0.6.0
showCodePoint :: Char -> ShowS
showCodePoint c = pad . showIt (quotRem cp 16)
where
cp = ord c
pad | cp <= 0x00f = \s -> '0' : '0' : '0' : s
| cp <= 0x0ff = \s -> '0' : '0' : s
| cp <= 0xfff = ('0' :)
| otherwise = id
showIt (n, d) r = case intToDigiT d of
!c' -> case n of
0 -> c' : r
_ -> showIt (quotRem n 16) (c' : r)

0 comments on commit f04f272

Please sign in to comment.