Skip to content

Commit 484beb8

Browse files
committed
core: Add showCodePoint
1 parent 13d3474 commit 484beb8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

unicode-data/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.6.0 (July 2024)
44

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

89
## 0.5.0 (July 2024)

unicode-data/lib/Unicode/Char.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ module Unicode.Char
3535
, module Unicode.Char.Normalization
3636
, module Unicode.Char.Identifiers
3737

38-
-- * Re-export from @base@
38+
-- * Utils
39+
, showCodePoint
40+
41+
-- * Re-export from @base@
3942
, ord
4043
, chr
4144
)
@@ -50,3 +53,26 @@ import Unicode.Char.Identifiers
5053
import Unicode.Char.Numeric
5154
import Unicode.Char.Normalization
5255
import Unicode.Internal.Char.Version (unicodeVersion)
56+
57+
-- [NOTE] Code inspired from 'showIntAtBase'
58+
-- | Show the code point of a character using the Unicode Standard convention:
59+
-- hexadecimal codepoint padded with zeros if inferior to 4 characters.
60+
--
61+
-- >>> showCodePoint '\xf' ""
62+
-- "000F"
63+
-- >>> showCodePoint '\x1ffff' ""
64+
-- "1FFFF"
65+
--
66+
-- @since 0.6.0
67+
showCodePoint :: Char -> ShowS
68+
showCodePoint c = pad . showIt (quotRem cp 16)
69+
where
70+
cp = ord c
71+
pad | cp <= 0x00f = \s -> '0' : '0' : '0' : s
72+
| cp <= 0x0ff = \s -> '0' : '0' : s
73+
| cp <= 0xfff = ('0' :)
74+
| otherwise = id
75+
showIt (n, d) r = case intToDigiT d of
76+
!c' -> case n of
77+
0 -> c' : r
78+
_ -> showIt (quotRem n 16) (c' : r)

0 commit comments

Comments
 (0)