tmf: Bug 459493: TmfTimestampFormat doesn't parse extra spaces correctly
authorPatrick Tasse <patrick.tasse@gmail.com>
Mon, 9 Feb 2015 19:15:50 +0000 (14:15 -0500)
committerPatrick Tasse <patrick.tasse@gmail.com>
Mon, 16 Feb 2015 21:49:04 +0000 (16:49 -0500)
Change-Id: I632348edd64ac31fdafaa5ae7bd2e84ea3abb2bb
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/41945
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/event/TmfTimestampFormatTest.java
org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfTimestampFormat.java

index a4f22ac5104d171e0d9ccec6b20b4b1105f7dab6..65e03588851ff1639bcab4d2b1f085995cef70ac 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2014 Ericsson
+ * Copyright (c) 2013, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -206,7 +206,6 @@ public class TmfTimestampFormatTest {
      */
     @Test
     public void testParseDateTime() throws ParseException {
-//        long ref = tsf.parseValue("2014-11-22 12:34:56.123456789"); // Saturday
         long time;
 
         time = new TmfTimestampFormat("yyyy", GMT, CA).parseValue("2014");
@@ -316,6 +315,15 @@ public class TmfTimestampFormatTest {
 
         time = new TmfTimestampFormat("yyyy\"MM\"dd\"HH\"mm\"ss\"SSS\"SSS\"SSS", GMT, CA).parseValue("2014\"11\"22\"12\"34\"56\"123\"456\"789");
         assertEquals("2014-11-22 12:34:56.123456789", tsf.format(time));
+
+        time = new TmfTimestampFormat("yyyy MM dd HH mm ss SSS SSS SSS", GMT, CA).parseValue("2014  11  22  12  34  56  123 456 789");
+        assertEquals("2014-11-22 12:34:56.123456789", tsf.format(time));
+
+        time = new TmfTimestampFormat("yyyy MM dd HH mm ss SSS SSS SSS", GMT, CA).parseValue("2014  1  2  3  4  5 123 456 789");
+        assertEquals("2014-01-02 03:04:05.123456789", tsf.format(time));
+
+        time = new TmfTimestampFormat("yyyy MM dd HH mm ss SSS SSS SSS", GMT, CA).parseValue("2014 \t 1 \t 2 \t 3 \t 4 \t 5 \t 123 456 789");
+        assertEquals("2014-01-02 03:04:05.123456789", tsf.format(time));
     }
 
     /**
index c0fda805ca233bff32b1ea8c730796b980e6aec0..30ff9d171ccef8ba0134fa8f6539424f0dd33890 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2014 Ericsson
+ * Copyright (c) 2012, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -614,13 +614,27 @@ public class TmfTimestampFormat extends SimpleDateFormat {
      * Returns the source string length if decimal separator is not found.
      */
     private int indexOfSourceDecimalSeparator(String source) {
-        String pattern = fPattern.substring(0, fPatternDecimalSeparatorIndex);
         String separator = fDecimalSeparator == '\'' ? "''" : String.valueOf(fDecimalSeparator); //$NON-NLS-1$
-        int sourcePos = source.indexOf(fDecimalSeparator);
-        int patternPos = pattern.indexOf(separator);
-        while (patternPos != -1 && sourcePos != -1) {
+        int patternPos = fPattern.indexOf(separator);
+        int sourcePos = -1;
+        while (patternPos != -1 && patternPos <= fPatternDecimalSeparatorIndex) {
             sourcePos = source.indexOf(fDecimalSeparator, sourcePos + 1);
-            patternPos = pattern.indexOf(separator, patternPos + separator.length());
+            if (sourcePos == -1) {
+                break;
+            }
+            // skip optional spaces and tabs before a pattern letter
+            char p = patternPos < fPattern.length() - 1 ? fPattern.charAt(patternPos + 1) : '\0';
+            if ((p >= 'a' && p <= 'z') || (p >= 'A' && p <= 'Z')) {
+                while (sourcePos < source.length() - 1) {
+                    char s = source.charAt(sourcePos + 1);
+                    if (s == ' ' || s == '\t') {
+                        sourcePos++;
+                    } else {
+                        break;
+                    }
+                }
+            }
+            patternPos = fPattern.indexOf(separator, patternPos + separator.length());
         }
         if (sourcePos == -1) {
             sourcePos = source.length();
This page took 0.029997 seconds and 5 git commands to generate.