Created
April 10, 2020 14:53
-
-
Save enebo/04d2397a917c066658819b5c548631b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* If you know you have an ASCII ByteList you should do something else. This will continue walking the | |
* bytelist 'while' as long as each continues to be true. When it stops being true it will return the | |
* last byte index processed (on full walk it will be length otherwise the beginning of the codepoint | |
* which did not satisfy each. | |
* | |
* @param bytelist of the mbc-laden bytes | |
* @param offset place in bytes to search past begin | |
* @param each the closure which walks the codepoints | |
* @return length if all codepoints match. index (ignoring begin) if not. | |
*/ | |
public static int eachCodePointWhile(ByteList bytelist, int offset, CodePoint each) { | |
byte[] bytes = bytelist.unsafeBytes(); | |
int len = bytelist.getRealSize(); | |
Encoding encoding = bytelist.getEncoding(); | |
int begin = bytelist.begin(); | |
int end = begin + len; | |
int n; | |
for (int i = 0, p = begin + offset; p < len; i++, p += n) { | |
n = StringSupport.length(encoding, bytes, p, end); | |
if (!each.call(i, encoding.mbcToCode(bytes, p, end), encoding)) { | |
return p; | |
} | |
} | |
return len; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment