4

我需要确定给定的 NSString 是否为 NFD 形式。我怎么做?

语境 :

我从 Mac OS 获得的文件路径(以 NSString 的形式)是规范分解形式(NFD)。当文件系统是 HFSPlus 时尤其如此。 http://developer.apple.com/mac/library/technotes/tn/tn1150.html#CanonicalDecomposition

我需要一个预先组合的字符串。precomposedStringWithCanonicalMapping现在,只有当我知道文件名以 NFD 形式分解时,我才想运行该函数。

我能想到的解决方案:

//works on the idea that NFD(NFD(x)) = NFD(x)
BOOL IsCanonicallyDecompsed(NSString *initialFilePath) {
  //decompose the string to NFD form
  NSString *nfdFormOfStr = [initialFilePath decomposedStringWithCanonicalMapping];
  char *ndfFormUTF8 = [nfdFormOfStr  UTF8String];
  char *intialPathUTF8 = [initialFilePath UTF8String];
  return (strcmp(ndfFormUTF8, intialPathUTF8) == 0);
}

我的解决方案好吗?另外,我对文件系统输出(在 NFD 中)的理解是否正确?

4

1 回答 1

3

如果您需要预先组合的字符串 (NFC),最简单和最安全的做法是始终运行precomposedStringWithCanonicalMapping,无论该字符串是否为 NFD。例如,您可能会得到一个字符串,其中一些字符是预先组合的,而一些字符是分解的。

请注意,HFS+ 文件系统使用 NFD 的修改版本,其中一些代码点范围是预先组合的,以便与 Mac OS 9 兼容;我不知道该decomposedStringWithCanonicalMapping函数是否使用与 HFS+ 相同的规则。

于 2013-01-09T03:10:40.050 回答