我具有變化對應於碼點和替代對範圍如下定義:如何沿着UTF-16碼點走?
https://en.wikipedia.org/wiki/UTF-16#Description
我的代碼是基於ConvertUTF.c
從Clang implementation。
我目前正在圍繞如何做到這一點纏繞我的頭。
的代碼是從LLVM的實現,我想了解最相關的是:
unsigned short bytesToWrite = 0;
const u32char_t byteMask = 0xBF;
const u32char_t byteMark = 0x80;
u8char_t* target = *targetStart;
utf_result result = kConversionOk;
const u16char_t* source = *sourceStart;
while (source < sourceEnd) {
u32char_t ch;
const u16char_t* oldSource = source; /* In case we have to back up because of target overflow. */
ch = *source++;
/* If we have a surrogate pair, convert to UTF32 first. */
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
/* If the 16 bits following the high surrogate are in the source buffer... */
if (source < sourceEnd) {
u32char_t ch2 = *source;
/* If it's a low surrogate, convert to UTF32. */
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
++source;
} else if (flags == kStrictConversion) { /* it's an unpaired high surrogate */
--source; /* return to the illegal value itself */
result = kSourceIllegal;
break;
}
} else { /* We don't have the 16 bits following the high surrogate. */
--source; /* return to the high surrogate */
result = kSourceExhausted;
break;
}
} else if (flags == kStrictConversion) {
/* UTF-16 surrogate values are illegal in UTF-32 */
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
--source; /* return to the illegal value itself */
result = kSourceIllegal;
break;
}
}
...
具體來說,他們在評論中說:
如果我們有一個代理對,轉換先到UTF32。
然後:
如果它是一個低代理,則轉換爲UTF32。
我迷路了「如果我們有..」和「如果是......」,我的迴應是在閱讀評論時說:「我們有什麼?還有,這是什麼?」
我相信ch
和ch2
是第一個char16和下一個char16(如果存在的話),檢查是否第二個是代理對的一部分,然後沿着每個char16行走(或者你沿着兩個chars?)直到最後。
我相處的,他們是如何使用UNI_SUR_HIGH_START
,UNI_SUR_HIGH_END
,UNI_SUR_LOW_START
,UNI_SUR_LOW_END
線丟失,以及它們的使用halfShift
和halfBase
。
維基百科還指出:
有重命名「高」和「低」的代理人,企圖「龍頭」和「尾隨」,由於其數值不匹配他們的名字。這在最近的Unicode標準中似乎已被放棄。
在任何回答中記錄「前導」和「尾隨」可能有助於澄清事情。
你讀過[閱讀代理對的Unicode常見問題解答](http://unicode.org/faq/utf_bom.html#utf16-3)嗎? –