Java中的对应unicode括号?

时间:2013-09-22 10:54:23

标签: java unicode

在Java中,括号标记为START_PUNCTUATIONEND_PUNCTUATION字符类型。

说“[”,怎么可能计算“]”(没有硬编码表)?

2 个答案:

答案 0 :(得分:5)

假设“每个START_PUNCTUATION在1-3个代码点之后都有一个等效的END_PUNCTUATION,如果它有一个”,这似乎是真的,这个代码片段应该为每个可能的字符列出它们:

public class EndPunct {
    private static final int UNICODE_MAX = Character.MAX_CODE_POINT;

    public static void main(String args[]) {
        for (int i = 0; i < UNICODE_MAX; i++) {
            if (!Character.isValidCodePoint(i)) {
                continue;
            }
            if (Character.getType(i) == Character.START_PUNCTUATION) {
                Character.UnicodeBlock currentBlock = Character.UnicodeBlock.of(i);
                boolean found = false;
                for (int newchar = i+1 ; newchar < Math.min(UNICODE_MAX, i+3); newchar++) {
                    if (!(Character.UnicodeBlock.of(newchar).equals(currentBlock))) {
                        break;
                    }
                    if (Character.getType(newchar) == Character.END_PUNCTUATION) {
                        System.out.println(toChar(i) + " matches " + toChar(newchar)
                                + " (codepoints u+" + Integer.toHexString(i) + " and u+" +Integer.toHexString(newchar) + ")");  
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    System.out.println("NOT FOUND for " + toChar(i) + " [position u+" + Integer.toHexString(i) + "]");
                }
            }

        } 
    }
    public static String toChar(int codePoint) {
        return new String(Character.toChars(codePoint));
    }
}

从它的输出中,你可以看到这似乎适用于其他字符,除了两个:

( matches ) (codepoints u+28 and u+29)
[ matches ] (codepoints u+5b and u+5d)
{ matches } (codepoints u+7b and u+7d)
༺ matches ༻ (codepoints u+f3a and u+f3b)
༼ matches ༽ (codepoints u+f3c and u+f3d)
᚛ matches ᚜ (codepoints u+169b and u+169c)
NOT FOUND for ‚ [position u+201a]
NOT FOUND for „ [position u+201e]
⁅ matches ⁆ (codepoints u+2045 and u+2046)
⁽ matches ⁾ (codepoints u+207d and u+207e)
₍ matches ₎ (codepoints u+208d and u+208e)
〈 matches 〉 (codepoints u+2329 and u+232a)
❨ matches ❩ (codepoints u+2768 and u+2769)
❪ matches ❫ (codepoints u+276a and u+276b)
❬ matches ❭ (codepoints u+276c and u+276d)
❮ matches ❯ (codepoints u+276e and u+276f)
❰ matches ❱ (codepoints u+2770 and u+2771)
❲ matches ❳ (codepoints u+2772 and u+2773)
❴ matches ❵ (codepoints u+2774 and u+2775)
⟅ matches ⟆ (codepoints u+27c5 and u+27c6)
⟦ matches ⟧ (codepoints u+27e6 and u+27e7)
⟨ matches ⟩ (codepoints u+27e8 and u+27e9)
⟪ matches ⟫ (codepoints u+27ea and u+27eb)
⟬ matches ⟭ (codepoints u+27ec and u+27ed)
⟮ matches ⟯ (codepoints u+27ee and u+27ef)
⦃ matches ⦄ (codepoints u+2983 and u+2984)
⦅ matches ⦆ (codepoints u+2985 and u+2986)
⦇ matches ⦈ (codepoints u+2987 and u+2988)
⦉ matches ⦊ (codepoints u+2989 and u+298a)
⦋ matches ⦌ (codepoints u+298b and u+298c)
⦍ matches ⦎ (codepoints u+298d and u+298e)
⦏ matches ⦐ (codepoints u+298f and u+2990)
⦑ matches ⦒ (codepoints u+2991 and u+2992)
⦓ matches ⦔ (codepoints u+2993 and u+2994)
⦕ matches ⦖ (codepoints u+2995 and u+2996)
⦗ matches ⦘ (codepoints u+2997 and u+2998)
⧘ matches ⧙ (codepoints u+29d8 and u+29d9)
⧚ matches ⧛ (codepoints u+29da and u+29db)
⧼ matches ⧽ (codepoints u+29fc and u+29fd)
⸢ matches ⸣ (codepoints u+2e22 and u+2e23)
⸤ matches ⸥ (codepoints u+2e24 and u+2e25)
⸦ matches ⸧ (codepoints u+2e26 and u+2e27)
⸨ matches ⸩ (codepoints u+2e28 and u+2e29)
〈 matches 〉 (codepoints u+3008 and u+3009)
《 matches 》 (codepoints u+300a and u+300b)
「 matches 」 (codepoints u+300c and u+300d)
『 matches 』 (codepoints u+300e and u+300f)
【 matches 】 (codepoints u+3010 and u+3011)
〔 matches 〕 (codepoints u+3014 and u+3015)
〖 matches 〗 (codepoints u+3016 and u+3017)
〘 matches 〙 (codepoints u+3018 and u+3019)
〚 matches 〛 (codepoints u+301a and u+301b)
〝 matches 〞 (codepoints u+301d and u+301e)
﴾ matches ﴿ (codepoints u+fd3e and u+fd3f)
︗ matches ︘ (codepoints u+fe17 and u+fe18)
︵ matches ︶ (codepoints u+fe35 and u+fe36)
︷ matches ︸ (codepoints u+fe37 and u+fe38)
︹ matches ︺ (codepoints u+fe39 and u+fe3a)
︻ matches ︼ (codepoints u+fe3b and u+fe3c)
︽ matches ︾ (codepoints u+fe3d and u+fe3e)
︿ matches ﹀ (codepoints u+fe3f and u+fe40)
﹁ matches ﹂ (codepoints u+fe41 and u+fe42)
﹃ matches ﹄ (codepoints u+fe43 and u+fe44)
﹇ matches ﹈ (codepoints u+fe47 and u+fe48)
﹙ matches ﹚ (codepoints u+fe59 and u+fe5a)
﹛ matches ﹜ (codepoints u+fe5b and u+fe5c)
﹝ matches ﹞ (codepoints u+fe5d and u+fe5e)
( matches ) (codepoints u+ff08 and u+ff09)
[ matches ] (codepoints u+ff3b and u+ff3d)
{ matches } (codepoints u+ff5b and u+ff5d)
⦅ matches ⦆ (codepoints u+ff5f and u+ff60)
「 matches 」 (codepoints u+ff62 and u+ff63)

u + 201a是single low quotation mark,而u + 201e是double low quotation mark。对于那些,没有匹配的角色。对于其他人来说,这种方法似乎有效,因此似乎适用于每个匹配的人。但是,可能没有任何保证。

答案 1 :(得分:1)

有一个名为“bidi mirroring glyph”的角色属性,它为您提供角色的镜像(如果存在)。正确布局双向文本时需要此属性:从右到左的语言,打开的括号必须向左打开,因此文本布局引擎必须使用右括号)的字形最初在文本中的字符的字形。

不幸的是,标准Java API不允许您访问镜像字形属性,但ICU4J库使用UCharacter.getMirror方法。

“大多数正确”替代方案是从给定的开头字符开始,并检查接下来的几个字符中的一个是否是结束标点字符,并假设它是正确的镜像。阅读mirroring data你可以看到镜子彼此相邻的大部分时间都是极少数例外(例外情况:U + 2298 CIRCLED DIVISION SLASH是U + 29B8圆形反转SOLIDUS的镜像 - 然后,这些字符不在标点符号类别中)