Skip to content

Instantly share code, notes, and snippets.

@naturalkei
Last active April 1, 2025 07:21
Show Gist options
  • Save naturalkei/59eadfe3dd3e808c6faef5941e6b711b to your computer and use it in GitHub Desktop.
Save naturalkei/59eadfe3dd3e808c6faef5941e6b711b to your computer and use it in GitHub Desktop.
check unicode normalize
#/usr/bin/env bash
# author: [email protected]
# date: 2025-03-24
# description: check normalize unicode string
# gist: https://gist.github.com/naturalkei/59eadfe3dd3e808c6faef5941e6b711b
main() {
local string
local color_opt=false
local verbose_opt=false
while [[ $# -gt 0 ]]; do
case "$1" in
--color)
color_opt=true
shift
;;
--verbose)
verbose_opt=true
shift
;;
*)
string="$1"
shift
;;
esac
done
local nfc_string=$(echo "$string" | uconv -f utf-8 -t utf-8 -x nfc)
local nfd_string=$(echo "$string" | uconv -f utf-8 -t utf-8 -x nfd)
local nfkc_string=$(echo "$string" | uconv -f utf-8 -t utf-8 -x nfkc)
local nfkd_string=$(echo "$string" | uconv -f utf-8 -t utf-8 -x nfkd)
if $color_opt; then
echo "* Input(${#string}): \033[32m$string\033[0m"
else
echo "* Input(${#string}): $string"
fi
if $verbose_opt; then
echo "* Input UTF-8 bytes: $(echo -n "$string" | xxd -p)"
echo "* NFC UTF-8 bytes: $(echo -n "$nfc_string" | xxd -p)"
echo "* NFD UTF-8 bytes: $(echo -n "$nfd_string" | xxd -p)"
echo "* NFKC UTF-8 bytes: $(echo -n "$nfkc_string" | xxd -p)"
echo "* NFKD UTF-8 bytes: $(echo -n "$nfkd_string" | xxd -p)"
fi
local result
local color
if [ "$string" = "$nfc_string" ]; then
result="NFC"
color="32"
elif [ "$string" = "$nfd_string" ]; then
result="NFD"
color="31"
elif [ "$string" = "$nfkc_string" ]; then
result="NFKC"
color="33"
elif [ "$string" = "$nfkd_string" ]; then
result="NFKD"
color="33"
else
result="Mixed"
color="33"
fi
if $color_opt; then
echo "* Result: \033[${color}m$result\033[0m"
else
echo "* Result: $result"
fi
}
main "$@"
@naturalkei
Copy link
Author

naturalkei commented Mar 25, 2025

unorm

check normalize unicode string

setup

# setup uconv(icu) script
brew install icu4c
echo 'export PATH="/opt/homebrew/opt/icu4c@77/bin:$PATH"' >> ~/.zshrc

# setup unorm script
curl -sSL https://bit.ly/nk-unorm-2 > /usr/local/bin/unorm
chmod +x /usr/local/bin/unorm

usage

usage: unorm [--color] [--verbose] <string>
example: unorm "홍길동"
example: unorm --color "홍길동"
example: unorm --verbose "홍길동"
example: unorm --color --verbose "홍길동"

check git user.name

unorm --color $(git config --global user.name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment