optimize readLocalDate

This commit is contained in:
wenshao 2025-02-24 09:29:36 +08:00
parent 8be4081b8a
commit cdceaf3c00
3 changed files with 64 additions and 44 deletions

View File

@ -4524,6 +4524,10 @@ public abstract class JSONReader
return new JSONException(info(message));
}
final JSONException error(String message, Exception cause) {
return new JSONException(info(message), cause);
}
final JSONException error(int offset, int ch) {
throw new JSONValidException("error, offset " + offset + ", char " + (char) ch);
}

View File

@ -4498,7 +4498,7 @@ final class JSONReaderUTF16
LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
ldt = (year | month | dom) == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
@ -4513,26 +4513,8 @@ final class JSONReaderUTF16
return ldt;
}
int nextQuoteOffset = -1;
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
if (chars[i] == quote) {
nextQuoteOffset = i;
}
}
if (nextQuoteOffset != -1
&& nextQuoteOffset - offset > 10
&& chars[nextQuoteOffset - 6] == '-'
&& chars[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(chars, offset, nextQuoteOffset - offset - 6);
int month = IOUtils.digit2(chars, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(chars, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
if (comma = (this.ch == ',')) {
next();
}
LocalDate localDate = readLocalDate0(offset, chars, quote);
if (localDate != null) {
return localDate;
}
}
@ -4540,6 +4522,32 @@ final class JSONReaderUTF16
return super.readLocalDate();
}
private LocalDate readLocalDate0(int offset, char[] chars, char quote) {
int nextQuoteOffset = -1;
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
if (chars[i] == quote) {
nextQuoteOffset = i;
}
}
if (nextQuoteOffset != -1
&& nextQuoteOffset - offset > 10
&& chars[nextQuoteOffset - 6] == '-'
&& chars[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(chars, offset, nextQuoteOffset - offset - 6);
int month = IOUtils.digit2(chars, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(chars, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
if (comma = (this.ch == ',')) {
next();
}
return localDate;
}
return null;
}
public final OffsetDateTime readOffsetDateTime() {
final char[] chars = this.chars;
int offset = this.offset, end = this.end;

View File

@ -5581,11 +5581,11 @@ class JSONReaderUTF8
LocalDate ldt;
try {
ldt = year == 0 && month == 0 && dom == 0
ldt = (year | month | dom) == 0
? null
: LocalDate.of(year, month, dom);
} catch (DateTimeException ex) {
throw new JSONException(info("read date error"), ex);
throw error("read date error", ex);
}
this.offset = offset + 11;
@ -5596,26 +5596,8 @@ class JSONReaderUTF8
return ldt;
}
int nextQuoteOffset = -1;
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
if (bytes[i] == quote) {
nextQuoteOffset = i;
}
}
if (nextQuoteOffset != -1
&& nextQuoteOffset - offset > 10
&& bytes[nextQuoteOffset - 6] == '-'
&& bytes[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(bytes, offset, nextQuoteOffset - offset - 6);
int month = IOUtils.digit2(bytes, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(bytes, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
if (comma = (this.ch == ',')) {
next();
}
LocalDate localDate = readLocalDate0(offset, bytes, quote);
if (localDate != null) {
return localDate;
}
}
@ -5623,6 +5605,32 @@ class JSONReaderUTF8
return super.readLocalDate();
}
private LocalDate readLocalDate0(int offset, byte[] bytes, char quote) {
int nextQuoteOffset = -1;
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
if (bytes[i] == quote) {
nextQuoteOffset = i;
}
}
if (nextQuoteOffset != -1
&& nextQuoteOffset - offset > 10
&& bytes[nextQuoteOffset - 6] == '-'
&& bytes[nextQuoteOffset - 3] == '-'
) {
int year = TypeUtils.parseInt(bytes, offset, nextQuoteOffset - offset - 6);
int month = IOUtils.digit2(bytes, nextQuoteOffset - 5);
int dayOfMonth = IOUtils.digit2(bytes, nextQuoteOffset - 2);
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
this.offset = nextQuoteOffset + 1;
next();
if (comma = (this.ch == ',')) {
next();
}
return localDate;
}
return null;
}
public final OffsetDateTime readOffsetDateTime() {
final byte[] bytes = this.bytes;
int offset = this.offset, end = this.end;
@ -6487,7 +6495,7 @@ class JSONReaderUTF8
}
if (ch != '"' && ch != '\'') {
throw new JSONException(info("syntax error, can not read uuid"));
throw error("syntax error, can not read uuid");
}
final int quote = ch;
final byte[] bytes = this.bytes;