ユニコード対応文字列プログラムのコードサンプルは誰にもありますか?プログラミング言語は重要ではありません。基本的にはunixコマンド "strings"と同じことをしたいが、Unicodeテキスト(UTF-16またはUTF-8)でも機能し、英語の文字や句読点を引きます。 (私は他のアルファベットではなく英語の文字のみを気にします)。ユニコード対応の文字列(1)プログラム
ありがとうございます!
ユニコード対応文字列プログラムのコードサンプルは誰にもありますか?プログラミング言語は重要ではありません。基本的にはunixコマンド "strings"と同じことをしたいが、Unicodeテキスト(UTF-16またはUTF-8)でも機能し、英語の文字や句読点を引きます。 (私は他のアルファベットではなく英語の文字のみを気にします)。ユニコード対応の文字列(1)プログラム
ありがとうございます!
あなたはただそれを使いたいのですか、何らかの理由でコードを主張していますか?
私のDebianシステムでは、strings
コマンドがこれをそのまま実行できるようです。マンページからの抜粋を参照してください。
--encoding=encoding
Select the character encoding of the strings that are to be found. Possible values for encoding are: s = single-7-bit-byte characters (ASCII, ISO 8859,
etc., default), S = single-8-bit-byte characters, b = 16-bit bigendian, l = 16-bit littleendian, B = 32-bit bigendian, L = 32-bit littleendian. Useful
for finding wide character strings.
編集:OK。私はC#を知らないのですが、これはやや毛深いかもしれませんが、基本的には、ゼロと英字が交互に並ぶシーケンスを検索する必要があります。
byte b;
int i=0;
while(!endOfInput()) {
b=getNextByte();
LoopBegin:
if(!isEnglish(b)) {
if(i>0) // report successful match of length i
i=0;
continue;
}
if(endOfInput()) break;
if((b=getNextByte())!=0)
goto LoopBegin;
i++; // found another character
}
これはリトルエンディアンで有効です。
私は同様の問題があり、 "strings -e ...
"を試しましたが、固定幅の文字コードのオプションが見つかりました。 (UTF-8エンコーディングは可変幅です)。
デフォルトでは、ASCII文字以外の文字は余分にstrings
オプションが必要です。これには、ほとんどすべての英語以外の文字列が含まれます。
「-e S
」(単一の8ビットの文字)出力には、UTF-8の文字が含まれています。
私は入力ファイルに "strings -e S ... | iconv ...
"を適用する非常にシンプルな(意見付きの)Perlスクリプトを書いています。
私はそれを特定の制限のために調整するのは簡単だと信じています。 使用法:いくつかの状況でutf8strings [options] file*
#!/usr/bin/perl -s
our ($all,$windows,$enc); ## use -all ignore the "3 letters word" restriction
use strict;
use utf8::all;
$enc = "ms-ansi" if $windows; ##
$enc = "utf8" unless $enc ; ## defaul encoding=utf8
my $iconv = "iconv -c -f $enc -t utf8 |";
for (@ARGV){ s/(.*)/strings -e S '$1'| $iconv/;}
my $word=qr/[a-zçáéíóúâêôàèìòùüãõ]{3}/i; # adapt this to your case
while(<>){
# next if /regular expressions for common garbage/;
print if ($all or /$word/);
}
、このアプローチはいくつかの余分なゴミを生産します。
英語とUTF-8の場合、文字列(1)はすでにOKであるはずです。 – mouviciel
言語が問題でない場合は、なぜ文字列ユーティリティ自体のソースをチェックしないのですか? –