From: Mark Davis Date: Wed, 12 Dec 2012 19:17:26 +0000 (+0000) Subject: ICU-9789 Make date parsing slightly more lenient to fix compatibility problems. X-Git-Tag: milestone-59-0-1~3269 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=18f2d4bd81a67280ffd60f32dc70bb03723a8d33;p=icu ICU-9789 Make date parsing slightly more lenient to fix compatibility problems. X-SVN-Rev: 32947 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java index 814a108da54..9a6e1906af3 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java @@ -1640,33 +1640,9 @@ public class SimpleDateFormat extends DateFormat { } else { // Handle literal pattern text literal numericFieldStart = -1; - - String patl = (String)items[i]; - int plen = patl.length(); - int tlen = text.length(); - int idx = 0; - while (idx < plen && pos < tlen) { - char pch = patl.charAt(idx); - char ich = text.charAt(pos); - if (PatternProps.isWhiteSpace(pch) - && PatternProps.isWhiteSpace(ich)) { - // White space characters found in both patten and input. - // Skip contiguous white spaces. - while ((idx + 1) < plen && - PatternProps.isWhiteSpace(patl.charAt(idx + 1))) { - ++idx; - } - while ((pos + 1) < tlen && - PatternProps.isWhiteSpace(text.charAt(pos + 1))) { - ++pos; - } - } else if (pch != ich) { - break; - } - ++idx; - ++pos; - } - if (idx != plen) { + boolean[] complete = new boolean[1]; + pos = matchLiteral(text, pos, items, i, complete); + if (!complete[0]) { // Set the position of mismatch parsePos.setIndex(start); parsePos.setErrorIndex(pos); @@ -1678,6 +1654,18 @@ public class SimpleDateFormat extends DateFormat { } ++i; } + + // Special hack for trailing "." after non-numeric field. + if (pos < text.length()) { + char extra = text.charAt(pos); + if (extra == '.' && isLenient() && items.length != 0) { + // only do if the last field is not numeric + Object lastItem = items[items.length - 1]; + if (lastItem instanceof PatternItem && !((PatternItem)lastItem).isNumeric) { + pos++; // skip the extra "." + } + } + } // At this point the fields of Calendar have been set. Calendar // will fill in default values for missing fields when the time @@ -1854,6 +1842,88 @@ public class SimpleDateFormat extends DateFormat { } } + /** + * Matches text (starting at pos) with patl. Returns the new pos, and sets complete[0] + * if it matched the entire text. Whitespace sequences are treated as singletons. + *

If isLenient and if we fail to match the first time, some special hacks are put into place. + *