// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.lang.regexp;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;

/**
 * @author Bas Leijdekkers
 */
public class RegExpMatch {

  private final IntList groups = new IntArrayList();

  public void add(int start, int end) {
    groups.add(start);
    groups.add(end);
  }

  /**
   * @return the number of groups (including the 0th group)
   */
  public int count() {
    return groups.size() / 2;
  }

  /**
   * @return the start of the ith group.
   */
  public int start(int i) {
    if (i < 0 || i > count() - 1) throw new IllegalArgumentException();

    return groups.getInt(i * 2);
  }

  /**
   * @return the end of the ith group
   */
  public int end(int i) {
    if (i < 0 || i > count() - 1) throw new IllegalArgumentException();

    return groups.getInt(i * 2 + 1);
  }
}

@@ -978,11 +978,657 @@
  1);%0A  %7D
+%0A%0A  @Override%0A  public boolean equals(Object o) %7B%0A    if (this == o) return true;%0A    if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A    return groups.equals(((RegExpMatch)o).groups);%0A  %7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    return groups.hashCode();%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    StringBuilder builder = new StringBuilder();%0A    builder.append(%22RegExpMatch%7B%22);%0A    final int max = groups.size();%0A    for (int i = 0; i %3C max; i += 2) %7B%0A      builder.append('%5B').append(groups.getInt(i)).append(%22, %22).append(groups.getInt(i + 1)).append('%5D');%0A    %7D%0A    builder.append('%7D');%0A    return builder.toString();%0A  %7D
 %0A%7D%0A

test group matching in GoRegExpMatcher