Last active
April 1, 2025 07:21
-
-
Save naturalkei/59eadfe3dd3e808c6faef5941e6b711b to your computer and use it in GitHub Desktop.
check unicode normalize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unorm
check normalize unicode string
setup
usage
check git user.name
unorm --color $(git config --global user.name)