com.github.skjolber.stcsv.column.bi.IntCsvColumnValueConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databinder Show documentation
Show all versions of databinder Show documentation
High-performance CSV databinding library
package com.github.skjolber.stcsv.column.bi;
import java.util.function.ObjIntConsumer;
public class IntCsvColumnValueConsumer implements CsvColumnValueConsumer {
protected final ObjIntConsumer consumer;
public IntCsvColumnValueConsumer(ObjIntConsumer consumer) {
this.consumer = consumer;
}
@Override
public void consume(T object, char[] array, int start, int end) {
consumer.accept(object, parseInt(array, start, end));
}
/**
* Helper method to (more) efficiently parse integer numbers from
* String values.
*
* @param ch array
* @param offset start index (inclusive)
* @param end end index (exclusive)
* @return the parsed value
*/
public static int parseInt(char[] ch, int offset, int end) {
/* Ok: let's keep strategy simple: ignoring optional minus sign,
* we'll parse 1 - 9 digits more efficiently;
* otherwise just defer to JDK parse functionality.
*/
int len = end - offset;
boolean neg = (ch[offset] == '-');
// must have 1 - 9 digits after optional sign:
// negative?
if (neg) {
if (len == 1 || len > 10) {
// this handles both overflow and numbers close to overflowing
return Integer.parseInt(new String(ch, end - len, len));
}
offset++;
} else {
if (len > 9) {
// this handles both overflow and numbers close to overflowing
return Integer.parseInt(new String(ch, end - len, len));
}
}
char c = ch[offset++];
if (c > '9' || c < '0') {
return Integer.parseInt(new String(ch, end - len, len));
}
int num = c - '0';
if (offset < end) {
c = ch[offset++];
if (c > '9' || c < '0') {
return Integer.parseInt(new String(ch, end - len, len));
}
num = (num * 10) + (c - '0');
if (offset < end) {
c = ch[offset++];
if (c > '9' || c < '0') {
return Integer.parseInt(new String(ch, end - len, len));
}
num = (num * 10) + (c - '0');
// Let's just loop if we have more than 3 digits:
if (offset < end) {
do {
c = ch[offset++];
if (c > '9' || c < '0') {
return Integer.parseInt(new String(ch, end - len, len));
}
num = (num * 10) + (c - '0');
} while (offset < end);
}
}
}
return neg ? -num : num;
}
}