如何判断一个NSString在NFD中是否被规范化?

发布于 2024-09-17 19:33:12 字数 922 浏览 9 评论 0原文

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

上下文:

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

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

我能想到的解决方案:

//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)的理解是否正确?

I need to determine whether a given NSString is in NFD form. How do i do that?

Context :

The file path I get from Mac OS (in the form of an NSString) is in canonical decomposed form (NFD). This is true especially when the filesystem is HFSPlus.
http://developer.apple.com/mac/library/technotes/tn/tn1150.html#CanonicalDecomposition

I need a precomposed string out of this. Now, I want to run the precomposedStringWithCanonicalMapping function only if I know that the filename is decomposed in NFD form.

The solution that I could think of:

//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);
}

Is my solution OK? Also, is my understanding about the filesystem output (in NFD) correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-24 19:33:12

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

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

If you need a precomposed string (NFC), the simplest and safest thing to do is to always run precomposedStringWithCanonicalMapping, regardless of whether the string is NFD or not. For example, it's possible you might get a string where some characters are precomposed and some are decomposed.

Note that the HFS+ filesystem uses a modified version of NFD, where some code-point ranges are kept pre-composed for compatibility with Mac OS 9; I don't know whether the decomposedStringWithCanonicalMapping function uses the same rules as HFS+.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文