All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.mines.jtk.util.StringParser Maven / Gradle / Ivy

The newest version!
/****************************************************************************
Copyright 2006, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.util;

import java.util.NoSuchElementException;

/**
 * Parse a string containing zero or more string literals.
 * @author Dave Hale, Colorado School of Mines
 * @version 06/20/1998, 08/24/2006.
 */
public class StringParser {

  /**
   * Construct a string parser for the specified string.
   * @param s string to parse.
   */
  public StringParser(String s) {
    _str = s;
    _len = s.length();
    _pos = 0;
    _end = -1;
  }

  /**
   * Determine whether the parser has more strings.
   * @return true, if more strings; false, otherwise.
   */
  public boolean hasMoreStrings() {
    final char space = ' ';
    final char quote = '\"';
    final char backslash = '\\';

    // If already positioned on a string that has not yet been got.
    if (_pos<=_end) return true;

    // Nothing left if current position beyond string length.
    if (_pos>_len || _str==null) return false;

    // Skip whitespace and remember next non-whitespace character.
    char c = 0;
    while (_pos<_len) {
      c = _str.charAt(_pos);
      if (c>space) break;
      ++_pos;
    }

    // Nothing left if current position beyond string length.
    if (_pos>=_len) return false;

    // If non-whitespace character is a quote, then string is
    // everything (including whitespace) up to matching quote
    // or end of string, whichever comes first.
    // Otherwise, if not quoted, string is everything up to next 
    // whitespace or end of string, whichever comes first.
    // When inside quotes, handle escaped quotes.
    if (c==quote) {
      ++_pos;
      boolean in_escape = false;
      for (_end=_pos; _end<_len; ++_end) {
        c = _str.charAt(_end);
        if (in_escape) {
          in_escape = false;
        } else if (c==backslash) {
          in_escape = true;
        } else if (c==quote) {
          break;
        }
      }
    } else {
      for (_end=_pos+1; _end<_len; ++_end) {
        c = _str.charAt(_end);
        if (c<=space) break;
      }
    }
    return true;
  }

  /**
   * Get the next string.
   * @return next string.
   * @exception NoSuchElementException if parser has no more strings.
   */
  public String nextString() {

    // Ensure we have have another string to get.
    if (!hasMoreStrings()) {
      throw new NoSuchElementException(
        "StringParser.nextString: no more strings in "+_str+".");
    }

    // Update the current position so that it will be correct after
    // we return the current string.
    int pos = _pos;
    _pos = _end+1;
    return replaceEscapes(_str.substring(pos,_end));
  }

  ///////////////////////////////////////////////////////////////////////////
  // Private.

  private String _str = null; // The string to parse for strings.
  private int _pos = 0; // Current position (< _end if have string at _pos).
  private int _end = 0; // One position beyond end of current string.
  private int _len = 0; // Length of string.

  // Note this does not handle all possible escapes.
  private String replaceEscapes(String s) {
    final char backslash = '\\';
    if (s==null || s.indexOf(backslash)<0) return s;
    StringBuilder sb = new StringBuilder(s.length());
    boolean in_escape = false;
    for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy