Esempio n. 1
0
  public static void decodeQueryString(String s, int fromIndex, Map<String, String> params) {
    if (fromIndex < 0) {
      return;
    }
    if (fromIndex >= s.length()) {
      return;
    }

    int queryStringLength = s.contains("#") ? s.indexOf("#") : s.length();

    String name = null;
    int pos = fromIndex; // Beginning of the unprocessed region
    int i; // End of the unprocessed region
    char c = 0; // Current character
    for (i = fromIndex; i < queryStringLength; i++) {
      c = s.charAt(i);
      if (c == '=' && name == null) {
        if (pos != i) {
          name = decodeComponent(s.substring(pos, i));
        }
        pos = i + 1;
      } else if (c == '&' || c == ';') {
        if (name == null && pos != i) {
          // We haven't seen an `=' so far but moved forward.
          // Must be a param of the form '&a&' so add it with
          // an empty value.
          addParam(params, decodeComponent(s.substring(pos, i)), "");
        } else if (name != null) {
          addParam(params, name, decodeComponent(s.substring(pos, i)));
          name = null;
        }
        pos = i + 1;
      }
    }

    if (pos != i) { // Are there characters we haven't dealt with?
      if (name == null) { // Yes and we haven't seen any `='.
        addParam(params, decodeComponent(s.substring(pos, i)), "");
      } else { // Yes and this must be the last value.
        addParam(params, name, decodeComponent(s.substring(pos, i)));
      }
    } else if (name != null) { // Have we seen a name without value?
      addParam(params, name, "");
    }
  }