重新实现ToUpper()

Col*_*ard 3 language-agnostic unicode localization internationalization toupper

如果ToUpper()不存在,你会怎么写?i18n和L10n的奖励积分

由此产生的好奇心:http://thedailywtf.com/Articles/The-Long-Way-toUpper.aspx

Aar*_*lla 8

  1. 我下载了Unicode表
  2. 我将表导入数据库
  3. 我写了一个方法upper().

这是一个示例实现;)

public static String upper(String s) {
    if (s == null) {
        return null;
    }

    final int N = s.length(); // Mind the optimization!
    PreparedStatement stmtName = null;
    PreparedStatement stmtSmall = null;
    ResultSet rsName = null;
    ResultSet rsSmall = null;
    StringBuilder buffer = new StringBuilder (N); // Much faster than StringBuffer!
    try {
        conn = DBFactory.getConnection();
        stmtName = conn.prepareStatement("select name from unicode.chart where codepoint = ?");
        // TODO Optimization: Maybe move this in the if() so we don't create this
        // unless there are uppercase characters in the string.
        stmtSmall = conn.prepareStatement("select codepoint from unicode.chart where name = ?");
        for (int i=0; i<N; i++) {
            int c = s.charAt(i);
            stmtName.setInt(1, c);
            rsName = stmtName.execute();
            if (rsName.next()) {
                String name = rsName.getString(1);
                if (name.contains(" SMALL ")) {
                    name = name.replaceAll(" SMALL ", " CAPITAL ");

                    stmtSmall.setString(1, name);
                    rsSmall = stmtSmall.execute();
                    if (rsSmall.next()) {
                        c = rsSmall.getInt(1);
                    }

                    rsSmall = DBUtil.close(rsSmall);
                }
            }
            rsName = DBUtil.close(rsName);
        }
    }
    finally {
        // Always clean up
        rsSmall = DBUtil.close(rsSmall);
        rsName = DBUtil.close(rsName);
        stmtSmall = DBUtil.close(stmtSmall);
        stmtName = DBUtil.close(stmtName);
    }

    // TODO Optimization: Maybe read the table once into RAM at the start
    // Would waste a lot of memory, though :/
    return buffer.toString();
}
Run Code Online (Sandbox Code Playgroud)

;)

注意:您可以在unicode.org上找到的unicode图表包含字符/代码点的名称.对于大写的字符,这个字符串将包含"SMALL"(注意空格或者它可能匹配"SMALLER"等).现在,您可以搜索"SMALL"替换为"CAPITAL"的类似名称.如果你找到它,你就找到了自然版本.


lep*_*pie 7

我不认为SO可以在一个帖子中处理unicode表的大小:)

不幸的是,它不像char.ToUpper()每个角色那么容易.

例:

(string-upcase "Straße")    ? "STRASSE"
(string-downcase "Straße")  ? "straße"
(string-upcase "????")      ? "????"
(string-downcase "????")    ? "????"
(string-downcase "?????")   ? "?????"
(string-downcase "???? ?")  ? "???? ?"
(string-upcase "????")      ? "????"
(string-upcase "????")      ? "????"
Run Code Online (Sandbox Code Playgroud)

  • 大写ß刚刚在4月份通过更新一些ISO标准添加到Unicode标准中,因此字体支持非常少见.:)此外,Duden还没有接受它为标准语言,所以你的*是*正确的.:)只是想指出另一种未来的可能性. (2认同)

Dou*_*der 5

没有静态表就足够了,因为在知道正确的变换之前需要先了解语言.

例如土耳其语i需要去?(U + 0130)而其他语言需要去I(U + 0049).和iU + 0069是同一个角色.

  • UFF.我猜这就是为什么一个合适的i18n库占用> 10MB的原因.疯狂的人.为什么我们的祖先不能满足于一个简单的单一写作系统呢?:P (3认同)