CharSupport.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:codebuff 作者:
/** Return a string representing the escaped char for code c.  E.g., If c
 *  has value 0x100, you will get "\u0100".  ASCII gets the usual
 *  char (non-hex) representation.  Control characters are spit out
 *  as unicode.  While this is specially set up for returning Java strings,
 *  it can be used by any language target that has the same syntax. :)
 */
public static String getANTLRCharLiteralForChar(int c) {
    if ( c< Lexer.MIN_CHAR_VALUE ) {
        return "'<INVALID>'";
    }
    if ( c<ANTLRLiteralCharValueEscape.length && ANTLRLiteralCharValueEscape[c]!=null ) {
        return '\''+ANTLRLiteralCharValueEscape[c]+'\'';
    }
    if ( Character.UnicodeBlock.of((char)c)==Character.UnicodeBlock.BASIC_LATIN &&
         !Character.isISOControl((char)c) ) {
        if ( c=='\\' ) {
            return "'\\\\'";
        }
        if ( c=='\'') {
            return "'\\''";
        }
        return '\''+Character.toString((char)c)+'\'';
    }
    // turn on the bit above max "\uFFFF" value so that we pad with zeros
    // then only take last 4 digits
    String hex = Integer.toHexString(c|0x10000).toUpperCase().substring(1,5);
    String unicodeStr = "'\\u"+hex+"'";
    return unicodeStr;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号