【问题标题】:How to determine whether an NSString is normalized in NFD?NFD中如何判断一个NSString是否被规范化?
【发布时间】:2011-04-09 01:16:33
【问题描述】:

我需要确定给定的 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 中)的理解是否正确?

【问题讨论】:

    标签: objective-c unicode nsstring normalization unicode-normalization


    【解决方案1】:

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

    请注意,HFS+ 文件系统使用 NFD 的修改版本,其中保留了一些代码点范围以与 Mac OS 9 兼容;不知道decomposedStringWithCanonicalMapping函数使用的规则是否和HFS+一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 2018-01-15
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多