mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8324665: Loose matching of space separators in the lenient date/time parsing mode
Reviewed-by: joehw, jlu
This commit is contained in:
parent
2d252ee06e
commit
96eb0390d6
4 changed files with 177 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -746,6 +746,10 @@ public abstract class DateFormat extends Format {
|
|||
* <p>This leniency value is overwritten by a call to {@link
|
||||
* #setCalendar(java.util.Calendar) setCalendar()}.
|
||||
*
|
||||
* @implSpec A {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR} in the input
|
||||
* text will match any other {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR}s
|
||||
* in the pattern with lenient parsing; otherwise, it will not match.
|
||||
*
|
||||
* @param lenient when {@code true}, parsing is lenient
|
||||
* @see java.util.Calendar#setLenient(boolean)
|
||||
*/
|
||||
|
|
|
@ -1487,7 +1487,8 @@ public class SimpleDateFormat extends DateFormat {
|
|||
|
||||
switch (tag) {
|
||||
case TAG_QUOTE_ASCII_CHAR:
|
||||
if (start >= textLength || text.charAt(start) != (char)count) {
|
||||
if (start >= textLength ||
|
||||
!charEquals(text.charAt(start), (char)count)) {
|
||||
pos.index = oldStart;
|
||||
pos.errorIndex = start;
|
||||
return null;
|
||||
|
@ -1497,7 +1498,8 @@ public class SimpleDateFormat extends DateFormat {
|
|||
|
||||
case TAG_QUOTE_CHARS:
|
||||
while (count-- > 0) {
|
||||
if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
|
||||
if (start >= textLength ||
|
||||
!charEquals(text.charAt(start), compiledPattern[i++])) {
|
||||
pos.index = oldStart;
|
||||
pos.errorIndex = start;
|
||||
return null;
|
||||
|
@ -1580,6 +1582,13 @@ public class SimpleDateFormat extends DateFormat {
|
|||
return parsedDate;
|
||||
}
|
||||
|
||||
private boolean charEquals(char ch1, char ch2) {
|
||||
return ch1 == ch2 ||
|
||||
isLenient() &&
|
||||
Character.getType(ch1) == Character.SPACE_SEPARATOR &&
|
||||
Character.getType(ch2) == Character.SPACE_SEPARATOR;
|
||||
}
|
||||
|
||||
/* If the next tag/pattern is a <Numeric_Field> then the parser
|
||||
* should consider the count of digits while parsing the contiguous digits
|
||||
* for the current tag/pattern
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -353,6 +353,10 @@ public final class DateTimeFormatterBuilder {
|
|||
* The change will remain in force until the end of the formatter that is eventually
|
||||
* constructed or until {@code parseLenient} is called.
|
||||
*
|
||||
* @implSpec A {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR} in the input
|
||||
* text will not match any other {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR}s
|
||||
* in the pattern with the strict parse style.
|
||||
*
|
||||
* @return this, for chaining, not null
|
||||
*/
|
||||
public DateTimeFormatterBuilder parseStrict() {
|
||||
|
@ -372,6 +376,10 @@ public final class DateTimeFormatterBuilder {
|
|||
* The change will remain in force until the end of the formatter that is eventually
|
||||
* constructed or until {@code parseStrict} is called.
|
||||
*
|
||||
* @implSpec A {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR} in the input
|
||||
* text will match any other {@link Character#SPACE_SEPARATOR SPACE_SEPARATOR}s
|
||||
* in the pattern with the lenient parse style.
|
||||
*
|
||||
* @return this, for chaining, not null
|
||||
*/
|
||||
public DateTimeFormatterBuilder parseLenient() {
|
||||
|
@ -2731,9 +2739,11 @@ public final class DateTimeFormatterBuilder {
|
|||
*/
|
||||
static final class CharLiteralPrinterParser implements DateTimePrinterParser {
|
||||
private final char literal;
|
||||
private final boolean isSpaceSeparator;
|
||||
|
||||
private CharLiteralPrinterParser(char literal) {
|
||||
this.literal = literal;
|
||||
isSpaceSeparator = Character.getType(literal) == Character.SPACE_SEPARATOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2750,9 +2760,10 @@ public final class DateTimeFormatterBuilder {
|
|||
}
|
||||
char ch = text.charAt(position);
|
||||
if (ch != literal) {
|
||||
if (context.isCaseSensitive() ||
|
||||
if ((context.isCaseSensitive() ||
|
||||
(Character.toUpperCase(ch) != Character.toUpperCase(literal) &&
|
||||
Character.toLowerCase(ch) != Character.toLowerCase(literal))) {
|
||||
Character.toLowerCase(ch) != Character.toLowerCase(literal))) &&
|
||||
!spaceEquals(context, ch)) {
|
||||
return ~position;
|
||||
}
|
||||
}
|
||||
|
@ -2766,6 +2777,12 @@ public final class DateTimeFormatterBuilder {
|
|||
}
|
||||
return "'" + literal + "'";
|
||||
}
|
||||
|
||||
private boolean spaceEquals(DateTimeParseContext context, char ch) {
|
||||
return !context.isStrict() && isSpaceSeparator &&
|
||||
Character.getType(ch) == Character.SPACE_SEPARATOR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue