23
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macのファイル名のための安全なNFC正規化

Last updated at Posted at 2016-10-10

事の発端はこちら 結合文字列をUnicode正規化で合成する方法の危険性
そこで
結合文字列を合成する安全な方法を考える
のJavaScript版です。

JavaScriptでは、String.normalizeメソッドでNFC正規化ができますが、これを使うと、互換漢字も変換されてしまいます。結合文字だけを元通り合成する関数を作りました。

function macNFC(name) {
	var myReg = /[^\u{0340}\u{0341}\u{0343}\u{0344}\u{0374}\u{037E}\u{0387}\u{0958}-\u{095F}\u{09DC}\u{09DD}\u{09DF}\u{0A33}\u{0A36}\u{0A59}-\u{0A5B}\u{0A5E}\u{0B5C}\u{0B5D}\u{0F43}\u{0F4D}\u{0F52}\u{0F57}\u{0F5C}\u{0F69}\u{0F73}\u{0F75}\u{0F76}\u{0F78}\u{0F81}\u{0F93}\u{0F9D}\u{0FA2}\u{0FA7}\u{0FAC}\u{0FB9}\u{1F71}\u{1F73}\u{1F75}\u{1F77}\u{1F79}\u{1F7B}\u{1F7D}\u{1FBB}\u{1FBE}\u{1FC9}\u{1FCB}\u{1FD3}\u{1FDB}\u{1FE3}\u{1FEB}\u{1FEE}\u{1FEF}\u{1FF9}\u{1FFB}\u{1FFD}\u{2000}\u{2001}\u{2126}\u{212A}\u{212B}\u{2329}\u{232A}\u{2ADC}\u{F900}-\u{FAFF}\u{FB1D}\u{FB1F}\u{FB2A}-\u{FB36}\u{FB38}-\u{FB3C}\u{FB3E}\u{FB40}\u{FB41}\u{FB43}\u{FB44}\u{FB46}-\u{FB4E}\u{1D15E}-\u{1D164}\u{1D1BB}-\u{1D1C0}\u{2F800}-\u{2FA1F}]+/gu;
	 if(name.normalize) name =  name.replace(myReg,function(r){return r.normalize('NFC')}) ;
	 return name ;
}

実働サンプルはこちら
(OSXのSafariでは、入力フィールドにペーストした時点でNFC変換されてしまいます。Chromeで試してみてください)

23
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?