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

io.stargate.web.resources.ParseUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright The Stargate Authors
 *
 * 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 io.stargate.web.resources;

class ParseUtils {

  /**
   * Returns the index of the first character in toParse from idx that is not a "space".
   *
   * @param toParse the string to skip space on.
   * @param idx the index to start skipping space from.
   * @return the index of the first character in toParse from idx that is not a "space.
   */
  static int skipSpaces(String toParse, int idx) {
    while (idx < toParse.length() && isBlank(toParse.charAt(idx))) {
      ++idx;
    }
    return idx;
  }

  /**
   * Assuming that idx points to the beginning of a CQL value in toParse, returns the index of the
   * first character after this value.
   *
   * @param toParse the string to skip a value form.
   * @param idx the index to start parsing a value from.
   * @return the index ending the CQL value starting at {@code idx}, or a negative value if if idx
   *     doesn't point to the start of a valid CQL value.
   */
  static int skipCqlValue(String toParse, int idx) {
    if (idx >= toParse.length()) {
      return -1;
    }

    if (isBlank(toParse.charAt(idx))) {
      return -1;
    }

    int cbrackets = 0;
    int sbrackets = 0;
    int parens = 0;
    boolean inString = false;

    do {
      char c = toParse.charAt(idx);
      if (inString) {
        if (c == '\'') {
          if (idx + 1 < toParse.length() && toParse.charAt(idx + 1) == '\'') {
            ++idx; // this is an escaped quote, skip it
          } else {
            inString = false;
            if (cbrackets == 0 && sbrackets == 0 && parens == 0) {
              return idx + 1;
            }
          }
        }
        // Skip any other character
      } else if (c == '\'') {
        inString = true;
      } else if (c == '{') {
        ++cbrackets;
      } else if (c == '[') {
        ++sbrackets;
      } else if (c == '(') {
        ++parens;
      } else if (c == '}') {
        if (cbrackets == 0) {
          return idx;
        }

        --cbrackets;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) {
          return idx + 1;
        }
      } else if (c == ']') {
        if (sbrackets == 0) {
          return idx;
        }

        --sbrackets;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) {
          return idx + 1;
        }
      } else if (c == ')') {
        if (parens == 0) {
          return idx;
        }

        --parens;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) {
          return idx + 1;
        }
      } else if (isBlank(c) || !isCqlIdentifierChar(c)) {
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) {
          return idx;
        }
      }
    } while (++idx < toParse.length());

    if (inString || cbrackets != 0 || sbrackets != 0 || parens != 0) {
      return -1;
    }
    return idx;
  }

  /**
   * Assuming that idx points to the beginning of a CQL identifier in toParse, returns the index of
   * the first character after this identifier.
   *
   * @param toParse the string to skip an identifier from.
   * @param idx the index to start parsing an identifier from.
   * @return the index ending the CQL identifier starting at {@code idx}, or a negative value if idx
   *     doesn't point to the start of a valid CQL identifier.
   */
  static int skipCqlId(String toParse, int idx) {
    if (idx >= toParse.length()) {
      return -1;
    }

    char c = toParse.charAt(idx);
    if (isCqlIdentifierChar(c)) {
      while (idx < toParse.length() && isCqlIdentifierChar(toParse.charAt(idx))) {
        idx++;
      }
      return idx;
    }

    if (c != '"') {
      return -1;
    }

    while (++idx < toParse.length()) {
      c = toParse.charAt(idx);
      if (c != '"') {
        continue;
      }

      if (idx + 1 < toParse.length() && toParse.charAt(idx + 1) == '\"') {
        ++idx; // this is an escaped double quote, skip it
      } else {
        return idx + 1;
      }
    }
    return -1;
  }

  static boolean isBlank(int c) {
    return c == ' ' || c == '\t' || c == '\n';
  }

  static boolean isCqlIdentifierChar(int c) {
    return (c >= '0' && c <= '9')
        || (c >= 'a' && c <= 'z')
        || (c >= 'A' && c <= 'Z')
        || c == '-'
        || c == '+'
        || c == '.'
        || c == '_'
        || c == '&';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy