【发布时间】:2022-01-14 12:03:39
【问题描述】:
我最近使用 unicode 比较频繁,想知道是否有命令行工具可以在其形式之间转换 unicode。
很高兴能够说:
uni_convert "☃" --string
并且知道字符串在 unicode 中定义为“SNOWMAN”。
【问题讨论】:
我最近使用 unicode 比较频繁,想知道是否有命令行工具可以在其形式之间转换 unicode。
很高兴能够说:
uni_convert "☃" --string
并且知道字符串在 unicode 中定义为“SNOWMAN”。
【问题讨论】:
Perl 的Unicode-Tussle 分发版附带有用的uniprops。
$ uniprops '☃'
U+2603 ‹☃› \N{SNOWMAN}
...
$ uniprops 'U+2603'
U+2603 ‹☃› \N{SNOWMAN}
...
$ uniprops 'SNOWMAN'
U+2603 ‹☃› \N{SNOWMAN}
...
如果您正在编写代码,则需要charnames。
Input To get $code
===== ==============================
$char ord($char)
$name charnames::vianame($name)
Input To get $char
===== ==============================
$code chr($code)
$name chr(charnames::vianame($name))
Input To get $name
===== ==============================
$code charnames::viacode($code)
$char charnames::viacode(ord($char))
vianame 接受官方别名(例如,LF 代表 LINEFEED)。如果希望接受它,您需要自己解析 U+ 符号。 ($code = hex(s/^U\+//r);)
例子:
use strict;
use warnings;
use feature qw( say );
use experimental qw( regex_sets );
use utf8; # Source encoded using UTF-8.
use open ":std", ":encoding(UTF-8)"; # Terminal provides/expects UTF-8.
use charnames qw( :full );
use Encode qw( decode_utf8 );
@ARGV == 1
or die("usage\n");
my $s = decode_utf8($ARGV[0]);
for my $cp ( unpack "W*", $s ) {
my $ch = chr($cp);
if ( $ch =~ /(?[ \p{Print} - \p{Mark} ])/ ) { # Not sure if good enough.
printf "‹%s› ", $ch;
} else {
print "--- ";
}
printf "U+%X ", $cp;
say charnames::viacode($cp);
}
$ uni_id ☃
‹☃› U+2603 SNOWMAN
$ uni_id çà
‹ç› U+E7 LATIN SMALL LETTER C WITH CEDILLA
‹à› U+E0 LATIN SMALL LETTER A WITH GRAVE
其他资源:
提供对Unicode Character Database 中的信息的访问权限。
Unicode Standard 不仅仅是字符和属性。
unichars 来自Unicode-Tussle(例如unichars '\p{Hiragana}')
【讨论】:
这里有一个 awk 可以做到这一点。
从提供最新名称的 unicode.org 下载 this file。
然后:
q=$(printf '%x\n' \'☃)
awk '/^[[:xdigit:]]+/{
str=$0
sub(/^[[:xdigit:]]+[[:blank:]]+/,"",str)
names[$1]=str
}
END{ print names[q] }
' q="$q" names.txt
打印:
SNOWMAN
如果你想走另一条路:
cp=$(awk '/^[[:xdigit:]]+/{
str=$0
sub(/^[[:xdigit:]]+[[:blank:]]+/,"",str)
other_names[str]=$1
}
END{ print other_names[q] }
' q="SNOWMAN" names.txt)
echo -e "\u${cp}"
打印:
☃
如果您有 GNU awk,您可以轻松地将十六进制索引转换为十进制并可以从内部打印。这允许通过定义 q 或 r 来使用单个源文件并以一种或另一种方式使用:
gawk '/^[[:xdigit:]]+/{
str=$0
sub(/^[[:xdigit:]]+[[:blank:]]+/,"",str)
names[$1]=str
other_names[str]=$1
}
END{ print q ? names[q] : sprintf("%c", strtonum("0x" other_names[r])) }
' r='SNOWMAN' names.txt
☃
gawk '/^[[:xdigit:]]+/{
str=$0
sub(/^[[:xdigit:]]+[[:blank:]]+/,"",str)
names[$1]=str
other_names[str]=$1
}
END{ print q ? names[q] : sprintf("%c", strtonum("0x" other_names[r])) }
' q=$(printf '%x\n' \'☃) names.txt
SNOWMAN
【讨论】:
我将代码分成一个文件并创建了一个 repo: https://github.com/poti1/uni_convert
【讨论】: