org.freecompany.util.text
Class Scanner

java.lang.Object
  extended by org.freecompany.util.text.Scanner
Direct Known Subclasses:
Tokenizer

public class Scanner
extends java.lang.Object

Locates individual characters and sequences of characters within a block of characters. This class utilizes the CharSequence interface for scan targets, allowing a wide array of container objects to be searched.

The scanner provides set of static methods to perform character sequence scanning relative to either a character sequence or a state object as provided by the caller.


Constructor Summary
Scanner()
           
 
Method Summary
protected static int end(java.lang.CharSequence target, java.lang.CharSequence sequence)
           
static boolean endsWith(java.lang.CharSequence target, java.lang.CharSequence sequence)
          Convenience method that scans in order to determine if the token is the last set of characters in the target.
static int last(java.lang.CharSequence target, java.lang.CharSequence sequence)
          Convenience method that reverse scans an entire target.
static boolean match(java.lang.CharSequence target, java.lang.CharSequence sequence, int start)
          Convenience method that matches the portion of the sequence from the provided index to the end.
static boolean match(java.lang.CharSequence target, java.lang.CharSequence sequence, int start, int end)
          Matches the provided sequence against the target at the indicated index.
static int scan(java.lang.CharSequence target, java.lang.CharSequence sequence)
          Convenience method that scans between the start and the end of the target.
static int scan(java.lang.CharSequence target, java.lang.CharSequence sequence, int start)
          Convenience method which scans between the provided index and the end of the target.
static int scan(java.lang.CharSequence target, java.lang.CharSequence sequence, int start, int end)
          Scans the provided target sequence for the whole of the provided sequence occurring between the start and end indexes.
static int scan(State state, java.lang.CharSequence sequence)
          Scans for the next occurance of the provided sequence starting at the index stored in the internal counter.
static int skip(java.lang.CharSequence target, java.lang.CharSequence sequence)
          Skips a sequence using the default index of zero, meaning this method only searches the beginning of a sequence for a particular match.
static int skip(java.lang.CharSequence target, java.lang.CharSequence sequence, int index)
          Skips a sequence if and only if it is found at the given index.
static boolean skip(State state, java.lang.CharSequence sequence)
          Skips a sequence if it is present at the cursor.
static boolean startsWith(java.lang.CharSequence target, java.lang.CharSequence sequence)
          Convenience method that scans in order to determine if the token is the first set of characters in the target.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scanner

public Scanner()
Method Detail

scan

public static final int scan(State state,
                             java.lang.CharSequence sequence)
Scans for the next occurance of the provided sequence starting at the index stored in the internal counter. The internal counter will be updated to the end of the matched sequence or the next index after the last possible match of the sequence. This is so that the target can be reset with a longer sequence and scanning will resume at the next possible index where the match can occur.

Returns:
the index of the next match, or -1 if no match was found.

skip

public static final boolean skip(State state,
                                 java.lang.CharSequence sequence)
Skips a sequence if it is present at the cursor. This method will reset the cursor to the new position if a match is found or leave it untouched if not.


startsWith

public static final boolean startsWith(java.lang.CharSequence target,
                                       java.lang.CharSequence sequence)
Convenience method that scans in order to determine if the token is the first set of characters in the target.


endsWith

public static final boolean endsWith(java.lang.CharSequence target,
                                     java.lang.CharSequence sequence)
Convenience method that scans in order to determine if the token is the last set of characters in the target.


last

public static final int last(java.lang.CharSequence target,
                             java.lang.CharSequence sequence)
Convenience method that reverse scans an entire target. This is equivalent to calling scan with the start and end arguments reversed.


scan

public static final int scan(java.lang.CharSequence target,
                             java.lang.CharSequence sequence)
Convenience method that scans between the start and the end of the target. This is equivalent to calling scan with zero as the third argument and the length of the sequence subtracted from the length of the target as the fourth argument.


scan

public static final int scan(java.lang.CharSequence target,
                             java.lang.CharSequence sequence,
                             int start)
Convenience method which scans between the provided index and the end of the target. This is equivalent to calling the scan method with the length of the sequence subtracted from the length of the target as the fourth argument.


scan

public static final int scan(java.lang.CharSequence target,
                             java.lang.CharSequence sequence,
                             int start,
                             int end)
Scans the provided target sequence for the whole of the provided sequence occurring between the start and end indexes. Reverse scans if the start is greater than the end.

Returns:
the index of the first character of the sequence in the target, or -1 if the sequence was not found.

skip

public static final int skip(java.lang.CharSequence target,
                             java.lang.CharSequence sequence)
Skips a sequence using the default index of zero, meaning this method only searches the beginning of a sequence for a particular match.


skip

public static final int skip(java.lang.CharSequence target,
                             java.lang.CharSequence sequence,
                             int index)
Skips a sequence if and only if it is found at the given index.

Returns:
the new index after possibly skipping a sequence. If the sequence was found then it returns the index after advancing past the match. If not, the current index is returned.

match

public static final boolean match(java.lang.CharSequence target,
                                  java.lang.CharSequence sequence,
                                  int start)
Convenience method that matches the portion of the sequence from the provided index to the end. This is equivalent to calling the match method with the length of the sequence added to the start index as the fourth argument.


match

public static final boolean match(java.lang.CharSequence target,
                                  java.lang.CharSequence sequence,
                                  int start,
                                  int end)
Matches the provided sequence against the target at the indicated index. This method tests that the target contains as many of this exact sequences characters as fit between the start and end indexes.

Returns:
true if the target contains the sequence between the start and end positions, ignoring extra characters or false otherwise.

end

protected static final int end(java.lang.CharSequence target,
                               java.lang.CharSequence sequence)