]> granicus.if.org Git - icu/commitdiff
ICU-9968 Return value of SimpleDateFormat subparse not correctly reflecting error...
authorScott Russell <DTownSMR@gmail.com>
Mon, 1 Jul 2013 15:24:42 +0000 (15:24 +0000)
committerScott Russell <DTownSMR@gmail.com>
Mon, 1 Jul 2013 15:24:42 +0000 (15:24 +0000)
X-SVN-Rev: 33871

icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java

index 1d73f663fb2491c4cc0dd1421c766689be747fdd..6f1e0efa35c2a44c39f5f5b4073e02abd7d42858 100644 (file)
@@ -2565,7 +2565,7 @@ public class SimpleDateFormat extends DateFormat {
                 }
                 return start + bestMatchLength;
             }
-        return -start;
+        return ~start;
     }
 
     private int regionMatchesWithOptionalDot(String text, int start, String data, int length) {
@@ -2696,7 +2696,7 @@ public class SimpleDateFormat extends DateFormat {
         }
 
         if (patternCharIndex == -1) {
-            return -start;
+            return ~start;
         }
 
         currentNumberFormat = getNumberFormat(ch);
@@ -2712,7 +2712,7 @@ public class SimpleDateFormat extends DateFormat {
         // of the string, then fail.
         for (;;) {
             if (start >= text.length()) {
-                return -start;
+                return ~start;
             }
             int c = UTF16.charAt(text, start);
             if (!UCharacter.isUWhiteSpace(c) || !PatternProps.isWhiteSpace(c)) {
@@ -2755,14 +2755,14 @@ public class SimpleDateFormat extends DateFormat {
                 if (!parsedNumericLeapMonth) {
                     if (obeyCount) {
                         if ((start+count) > text.length()) {
-                            return -start;
+                            return ~start;
                         }
                         number = parseInt(text, count, pos, allowNegative,currentNumberFormat);
                     } else {
                         number = parseInt(text, pos, allowNegative,currentNumberFormat);
                     }
                     if (number == null && patternCharIndex != 30) {
-                        return -start;
+                        return ~start;
                     }
                 }
 
@@ -2847,7 +2847,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.set(Calendar.YEAR, value);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             case 2: // 'M' - MONTH
             case 26: // 'L' - STAND_ALONE_MONTH
                 if (count <= 2) { // i.e., M/MM, L/LL
@@ -2957,7 +2957,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 23: // 'Z' - TIMEZONE_RFC
             {
@@ -2969,7 +2969,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                     }
-                return -start;
+                return ~start;
                 }
             case 24: // 'v' - TIMEZONE_GENERIC
             {
@@ -2982,7 +2982,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 29: // 'V' - TIMEZONE_SPECIAL
             {
@@ -3008,7 +3008,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 31: // 'O' - TIMEZONE_LOCALIZED_GMT_OFFSET
             {
@@ -3020,7 +3020,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 32: // 'X' - TIMEZONE_ISO
             {
@@ -3049,7 +3049,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 33: // 'x' - TIMEZONE_ISO_LOCAL
             {
@@ -3078,7 +3078,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.setTimeZone(tz);
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
             case 27: // 'Q' - QUARTER
                 if (count <= 2) { // i.e., Q or QQ.
@@ -3148,7 +3148,7 @@ public class SimpleDateFormat extends DateFormat {
                     cal.set(field, number.intValue());
                     return pos.getIndex();
                 }
-                return -start;
+                return ~start;
             }
     }
 
index 88fd6771031c8de844a23f8bef9ad2fae7563bcc..0a1ada6d7daad243f9ede0f72a776a94380a35e4 100644 (file)
@@ -11,6 +11,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.text.ParsePosition;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashSet;
@@ -2333,7 +2334,31 @@ public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk {
         dateBit2 = myCal.getTimeInMillis();
         assertFalse("Fail: error in setMillis, allowed invalid value : " + testMillis + "...returned dayOfMonth : " + dateBit1 + " millis : " + dateBit2, missedException);            
     }
-    
+
+    /**
+     * Test case for ticket 9968
+     * subparse fails to return an error indication when start pos is 0 
+     */
+    public void TestT9968() {
+        SimpleDateFormat sdf0 = new SimpleDateFormat("-MMMM");
+        ParsePosition pos0 = new ParsePosition(0);
+        Date d0 = sdf0.parse("-September", pos0);
+        logln("sdf0: "+pos0.getErrorIndex() + "/" + pos0.getIndex());    
+        assertTrue("Fail: failed a good test", pos0.getErrorIndex() == -1);
+
+        SimpleDateFormat sdf1 = new SimpleDateFormat("-MMMM");
+        ParsePosition pos1 = new ParsePosition(0);
+        Date d1 = sdf1.parse("-????", pos1);
+        logln("sdf1: "+pos1.getErrorIndex() + "/" + pos1.getIndex());    
+        assertTrue("Fail: failed to detect bad parse", pos1.getErrorIndex() == 1);
+
+        SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM");
+        ParsePosition pos2 = new ParsePosition(0);
+        Date d2 = sdf2.parse("????", pos2);
+        logln("sdf2: "+pos2.getErrorIndex() + "/" + pos2.getIndex());    
+        assertTrue("Fail: failed to detect bad parse", pos2.getErrorIndex() == 0);
+        
+    }    
 }
 
 //eof