Returns the codepoint of a character at the specified position. Returns a replacement character for invalid values. @param token token @param pos character position @return current character
(final byte[] token, final int pos)
| 218 | * @return current character |
| 219 | */ |
| 220 | public static int cp(final byte[] token, final int pos) { |
| 221 | // 0xxxxxxx |
| 222 | final byte b = token[pos]; |
| 223 | if(b >= 0) return b; |
| 224 | // number of bytes to be read |
| 225 | final int cl = cl(b); |
| 226 | if(b < -64 || pos + cl > token.length) return REPLACEMENT; |
| 227 | // 110xxxxx 10xxxxxx |
| 228 | if(cl == 2) return (b & 0x1F) << 6 | token[pos + 1] & 0x3F; |
| 229 | // 1110xxxx 10xxxxxx 10xxxxxx |
| 230 | if(cl == 3) return (b & 0x0F) << 12 | (token[pos + 1] & 0x3F) << 6 | |
| 231 | token[pos + 2] & 0x3F; |
| 232 | // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 233 | return (b & 0x07) << 18 | (token[pos + 1] & 0x3F) << 12 | |
| 234 | (token[pos + 2] & 0x3F) << 6 | token[pos + 3] & 0x3F; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns the byte length of a UTF-8 character. |