codePointAt function
codePointAt gets a Unicode code point from a JavaScript UTF-16 string handling surrogate pairs appropriately
Implementation
int codePointAt(String str, [int? idx]) {
if (idx == null) {
idx = 0;
}
int code = str.codeUnitAt(idx);
// if a high surrogate
if (0xD800 <= code && code <= 0xDBFF && idx < str.length - 1) {
int hi = code;
int low = str.codeUnitAt(idx + 1);
if (0xDC00 <= low && low <= 0xDFFF) {
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
return hi;
}
// if a low surrogate
if (0xDC00 <= code && code <= 0xDFFF && idx >= 1) {
int hi = str.codeUnitAt(idx - 1);
int low = code;
if (0xD800 <= hi && hi <= 0xDBFF) {
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
return low;
}
//just return the char if an unmatched surrogate half or a
//single-char codepoint
return code;
}