BifaceMcLeOD The HighGlandeur | Ceci dit, tu peux t'en réécrire un qui soit plus propre.
Voici le code de la classe Java, le transcrire en C++ ne sera pas difficile.
Code :
- import java.util.Enumeration;
- import java.util.NoSuchElementException;
- /**
- * The string tokenizer class allows an application to break a
- * string into tokens. The tokenization method is much simpler
- * than the one used by the <tt>StreamTokenizer</tt> class.
- * The <tt>Tokenizer</tt> methods do not distinguish among
- * identifiers, numbers, and quoted strings, nor do they
- * recognize and skip comments.
- * <p>
- * The set of delimiters (the characters that separate tokens)
- * may be specified either at creation time or on a per-token
- * basis.
- * <p>
- * An instance of <tt>Tokenizer</tt> behaves in one of two
- * ways, depending on whether it was created with the
- * <tt>returnDelims</tt> flag having the value <tt>true</tt>
- * or <tt>false</tt>:
- * <ul>
- * <li>If the flag is <tt>false</tt>, delimiter characters serve
- * to separate tokens. A token is a maximal sequence of
- * consecutive characters that are not delimiters.
- * <li>If the flag is <tt>true</tt>, delimiter characters are
- * themselves considered to be tokens. A token is thus either one delimiter
- * character, or a maximal sequence of consecutive characters that are
- * not delimiters.
- * </ul><p>
- * A <tt>Tokenizer</tt> object internally maintains a current
- * position within the string to be tokenized. Some operations advance this
- * current position past the characters processed.<p>
- * A token is returned by taking a substring of the string that was used to
- * create the <tt>Tokenizer</tt> object.
- * <p>
- * The following is one example of the use of the tokenizer. The code:
- * <blockquote><pre>
- * Tokenizer st = new Tokenizer("this is a test" );
- * while (st.hasMoreTokens()) {
- * println(st.nextToken());
- * }
- * </pre></blockquote>
- * <p>
- * prints the following output:
- * <blockquote><pre>
- * this
- * is
- * a
- * test
- * </pre></blockquote>
- * <p>
- * This class is a replacement for <tt>java.util.StringTokenizer</tt>
- * (Sun's implementation has a bug).
- */
- public class Tokenizer implements Enumeration {
- /**
- * Default delimiters "<tt>\t\n\r\f</tt>":
- * the space character, the tab character, the newline character,
- * the carriage-return character, and the form-feed character.
- */
- public static final String DEFAULT_DELIMITERS = " \t\n\r\f";
- /** String to tokenize. */
- private String str;
- /** Delimiters. */
- private String delimiters;
- /** Flag indicating whether to return the delimiters as tokens. */
- private boolean returnTokens;
- /** Previous token start. */
- private int previous = -1;
- /** Current position in <tt>str</tt> string. */
- private int currentPosition;
- /** Maximal position in <tt>str</tt> string. */
- private int maxPosition;
- /**
- * Constructs a string tokenizer for the specified string. All characters
- * in the <tt>delimiters</tt> argument are the delimiters for separating
- * tokens.
- * <p>
- * If the <tt>returnTokens</tt> flag is <tt>true</tt>, then the delimiter
- * characters are also returned as tokens. Each delimiter is returned as
- * a string of length one. If the flag is <tt>false</tt>, the delimiter
- * characters are skipped and only serve as separators between tokens.
- *
- * @param str a string to be parsed
- * @param delimiters the delimiters
- * @param returnTokens flag indicating whether to return the delimiters
- * as tokens
- */
- public Tokenizer(String str, String delimiters, boolean returnTokens) {
- this.str = str;
- this.delimiters = delimiters;
- this.returnTokens = returnTokens;
- this.maxPosition = str.length();
- }
- /**
- * Constructs a string tokenizer for the specified string. The characters
- * in the <tt>delimiters</tt> argument are the delimiters for separating
- * tokens. Delimiter characters themselves will not be treated as tokens.
- *
- * @param str a string to be parsed
- * @param delimiters the delimiters
- */
- public Tokenizer(String str, String delimiters) {
- this(str, delimiters, false);
- }
- /**
- * Constructs a string tokenizer for the specified string. The character
- * in the <tt>delimiters</tt> argument is the delimiter for separating
- * tokens. Delimiter character themselves will not be treated as token.
- *
- * @param str a string to be parsed
- * @param delimiters the delimiter
- */
- public Tokenizer(String str, char delimiters) {
- this(str, String.valueOf(delimiters), false);
- }
-
- /**
- * Constructs a string tokenizer for the specified string. The tokenizer
- * uses the default delimiter set, which is "<tt> \t\n\r\f</tt>":
- * the space character, the tab character, the newline character, the
- * carriage-return character, and the form-feed character. Delimiter
- * characters themselves will not be treated as tokens.
- *
- * @param str a string to be parsed
- */
- public Tokenizer(String str) {
- this(str, Tokenizer.DEFAULT_DELIMITERS, false);
- }
- /**
- * Tests if there are more tokens available from this tokenizer's string.
- * If this method returns <tt>true</tt>, then a subsequent call to {@link
- * #nextToken nextToken} with no argument will successfully return a token.
- *
- * @return <tt>true</tt> if and only if there is at least one token in the
- * string after the current position; <tt>false</tt> otherwise.
- */
- public boolean hasMoreTokens() {
- if (this.currentPosition < this.maxPosition) {
- return true;
- }
- else if (this.currentPosition == this.maxPosition) {
- return (this.maxPosition == 0 ||
- (this.returnTokens && this.delimiters.indexOf(this.str.charAt(this.previous)) >= 0));
- }
- else {
- return false;
- }
- }
- /**
- * Returns the next token from this string tokenizer.
- *
- * @return the next token from this string tokenizer
- *
- * @throws NoSuchElementException if there are no more tokens in this
- * tokenizer's string.
- */
- public String nextToken() throws NoSuchElementException {
- if (this.currentPosition == this.maxPosition &&
- (this.maxPosition == 0 ||
- (this.returnTokens && this.delimiters.indexOf(this.str.charAt(this.previous)) >= 0))) {
- this.currentPosition++;
- return new String();
- }
-
- if (this.currentPosition >= this.maxPosition) {
- throw new NoSuchElementException();
- }
-
- int start = this.currentPosition;
- String result = null;
-
- if (this.delimiters.indexOf(this.str.charAt(start)) >= 0) {
- if (this.previous == -1 ||
- (this.returnTokens && this.previous != this.currentPosition &&
- this.delimiters.indexOf(this.str.charAt(this.previous)) >= 0)) {
- result = new String();
- }
- else if (this.returnTokens) {
- result = this.str.substring(start, ++this.currentPosition);
- }
-
- if (!returnTokens) {
- currentPosition++;
- }
- }
-
- this.previous = start;
- start = this.currentPosition;
-
- if (result == null) {
- while (this.currentPosition < this.maxPosition &&
- this.delimiters.indexOf(this.str.charAt(this.currentPosition)) < 0) {
- this.currentPosition++;
- }
- }
-
- return ((result == null) ? this.str.substring(start, this.currentPosition) : result);
- }
- /**
- * Returns the next token in this string tokenizer's string. First, the
- * set of characters considered to be delimiters by this <tt>Tokenizer</tt>
- * object is changed to be the characters in the string <tt>delimiters</tt>.
- * Then the next token in the string after the current position is
- * returned. The current position is advanced beyond the recognized token.
- * The new delimiter set remains the default after this call.
- *
- * @param delimiters the new delimiters.
- *
- * @return the next token, after switching to the new delimiter set.
- *
- * @throws NoSuchElementException if there are no more tokens in this
- * tokenizer's string.
- */
- public String nextToken(String delimiters) throws NoSuchElementException {
- this.delimiters = delimiters;
- return this.nextToken();
- }
- /**
- * Returns the same value as the <tt>hasMoreTokens</tt> method.
- * It exists so that this class can implement the
- * <tt>Enumeration</tt> interface.
- *
- * @return <tt>true</tt> if there are more tokens; <tt>false</tt>
- * otherwise.
- */
- public boolean hasMoreElements() {
- return this.hasMoreTokens();
- }
- /**
- * Returns the same value as the <tt>nextToken</tt> method, except that its
- * declared return value is <tt>Object</tt> rather than <tt>String</tt>. It exists so that
- * this class can implement the <tt>Enumeration</tt> interface.
- *
- * @return the next token in the string
- *
- * @throws NoSuchElementException if there are no more tokens in this
- * tokenizer's string
- */
- public Object nextElement() {
- return this.nextToken();
- }
- /**
- * Calculates the number of times that this tokenizer's nextToken method
- * can be called before it generates an exception. The current position
- * is not advanced.
- *
- * @return the number of tokens remaining in the string using the
- * current delimiter set
- */
- public int countTokens() {
- int curr = this.currentPosition;
- int count = 0;
-
- for (int i = curr; i < this.maxPosition; i++) {
- if (this.delimiters.indexOf(this.str.charAt(i)) >= 0) {
- count++;
- }
-
- curr++;
- }
-
- return count + (this.returnTokens ? count : 0) + 1;
- }
- /**
- * Resets this tokenizer's state so the tokenizing starts from the begin.
- */
- public void reset() {
- this.previous = -1;
- this.currentPosition = 0;
- }
- /**
- * Constructs a string tokenizer for the specified string. All characters
- * in the <tt>delimiters/tt> argument are the delimiters for separating tokens.
- * If the <tt>returnTokens</tt> flag is <tt>true</tt>, then the delimiter
- * characters are also returned as tokens. Each delimiter is returned as
- * a string of length one. If the flag is <tt>false</tt>, the delimiter
- * characters are skipped and only serve as separators between tokens.
- * Then tokenizes the <tt>str</tt> and return an <tt>String[]</tt> array
- * with tokens.
- *
- * @param str a string to be parsed
- * @param delimiters the delimiters
- * @param returnTokens flag indicating whether to return the delimiters
- * as tokens
- *
- * @return array with tokens
- */
- public static String[] tokenize(String str, String delimiters,
- boolean returnTokens) {
- Tokenizer tokenizer = new Tokenizer(str, delimiters, returnTokens);
- String[] tokens = new String[tokenizer.countTokens()];
-
- for (int i = 0; i < tokens.length; i++) {
- tokens[i] = tokenizer.nextToken();
- }
-
- return tokens;
- }
- }
|
|