harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r423075 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java test/java/tests/api/java/util/ScannerTest.java
Date Tue, 18 Jul 2006 12:53:07 GMT
Author: gharley
Date: Tue Jul 18 05:53:06 2006
New Revision: 423075

URL: http://svn.apache.org/viewvc?rev=423075&view=rev
Log:
HARMONY 907 : [luni] Implementation of java.util.Scanner.nextLine

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?rev=423075&r1=423074&r2=423075&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
Tue Jul 18 05:53:06 2006
@@ -64,6 +64,15 @@
     private static final Pattern BOOLEAN_PATTERN = Pattern.compile(
             "true|false", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
     
+    // Pattern used to recognize line terminator
+    private static final Pattern LINE_TERMINATOR = Pattern
+            .compile("\n|\r\n|\r|\u0085|\u2028|\u2029"); //$NON-NLS-1$
+    
+    // Pattern used to recognize a line with a line terminator.
+    private static final Pattern LINE_PATTERN = Pattern
+            .compile(".*(" + LINE_TERMINATOR.pattern() + //$NON-NLS-1$
+                    ")|.+(" + LINE_TERMINATOR.pattern() + ")?"); //$NON-NLS-1$ //$NON-NLS-2$
+    
     // The pattern matching anything
     private static final Pattern ANY_PATTERN = Pattern.compile("(?s).*"); //$NON-NLS-1$
 
@@ -1142,9 +1151,55 @@
         return intValue;
     }
 
-    //TODO: To implement this feature
+    /**
+     * Returns the skipped input and advances the scanner to the beginning of
+     * the next line. The returned result will exclude any line terminator.
+     * 
+     * When searching, if no line terminator is found, then a large amount of
+     * input will be cached. If no line at all can be found, a
+     * NoSuchElementException will be thrown out.
+     * 
+     * @return the skipped line
+     * @throws IllegalStateException
+     *             if the scanner is closed
+     * @throws NoSuchElementException
+     *             if no line can be found, e.g. when input is an empty string
+     */
     public String nextLine() {
-        throw new NotYetImplementedException();
+        checkClosed();
+
+        matcher.usePattern(LINE_PATTERN);
+        matcher.region(findStartIndex, bufferLength);
+        String result = null;
+        while (true) {
+            if (matcher.find()) {
+                if (inputExhausted || bufferLength != matcher.end()) {
+                    matchSuccessful = true;
+                    findStartIndex = matcher.end();
+                    result = matcher.group();
+                    break;
+                }
+            } else {
+                if (inputExhausted) {
+                    matchSuccessful = false;
+                    throw new NoSuchElementException();
+                }
+            }
+            if (!inputExhausted) {
+                readMore();
+                resetMatcher();
+            } else {
+                break;
+            }
+        }
+        // Find text without line terminator here.
+        if (null != result) {
+            Matcher terminatorMatcher = LINE_TERMINATOR.matcher(result);
+            if (terminatorMatcher.find()) {
+                result = result.substring(0, terminatorMatcher.start());
+            }
+        }
+        return result;
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?rev=423075&r1=423074&r2=423075&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
Tue Jul 18 05:53:06 2006
@@ -4745,6 +4745,140 @@
         }
     }
     
+    /**
+     * @tests java.util.Scanner#nextLine()
+     */
+    public void test_nextLine() {
+        s = new Scanner("");
+        s.close();
+        try {
+            s.nextLine();
+        } catch (IllegalStateException e) {
+            // expected
+        }
+        
+        s = new Scanner("test\r\ntest");
+        String result = s.nextLine();
+        assertEquals("test", result);
+        MatchResult matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(6, matchResult.end());
+
+        s = new Scanner("\u0085");
+        result = s.nextLine();
+        assertEquals("", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("\u2028");
+        result = s.nextLine();
+        assertEquals("", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("\u2029");
+        result = s.nextLine();
+        assertEquals("", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        
+        s = new Scanner("");
+        try {
+            result = s.nextLine();
+            fail("Should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+        try {
+            s.match();
+            fail("Should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+
+        s = new Scanner("Ttest");
+        result = s.nextLine();
+        assertEquals("Ttest", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(5, matchResult.end());
+
+        s = new Scanner("\r\n");
+        result = s.nextLine();
+        assertEquals("", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(2, matchResult.end());
+
+        char[] chars = new char[1024];
+        Arrays.fill(chars, 'a');
+        StringBuilder stringBuilder = new StringBuilder();
+        stringBuilder.append(chars);
+        chars = new char[] { '+', '-' };
+        stringBuilder.append(chars);
+        stringBuilder.append("\u2028");
+        s = new Scanner(stringBuilder.toString());
+        result = s.nextLine();
+
+        assertEquals(stringBuilder.toString().substring(0, 1026), result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1027, matchResult.end());
+
+        chars = new char[1023];
+        Arrays.fill(chars, 'a');
+        stringBuilder = new StringBuilder();
+        stringBuilder.append(chars);
+        stringBuilder.append("\r\n");
+        s = new Scanner(stringBuilder.toString());
+        result = s.nextLine();
+
+        assertEquals(stringBuilder.toString().substring(0, 1023), result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1025, matchResult.end());
+
+        s = new Scanner("  ");
+        result = s.nextLine();
+        assertEquals("  ", result);
+
+        s = new Scanner("test\n\n\n");
+        result = s.nextLine();
+        assertEquals("test", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(5, matchResult.end());
+        result = s.nextLine();
+        matchResult = s.match();
+        assertEquals(5, matchResult.start());
+        assertEquals(6, matchResult.end());
+
+        s = new Scanner("\n\n\n");
+        result = s.nextLine();
+        assertEquals("", result);
+        matchResult = s.match();
+        assertEquals(0, matchResult.start());
+        assertEquals(1, matchResult.end());
+        result = s.nextLine();
+        matchResult = s.match();
+        assertEquals(1, matchResult.start());
+        assertEquals(2, matchResult.end());
+        
+        s = new Scanner("123 test\n   ");
+        int value = s.nextInt();
+        assertEquals(123, value);
+        
+        result = s.nextLine();
+        assertEquals(" test", result);
+        
+        s = new Scanner("test\n ");
+        result = s.nextLine();
+        assertEquals("test", result);
+    }
+    
     protected void setUp() throws Exception {
         super.setUp();
 



Mime
View raw message