/**
 *  Copyright (c) 2015-2016 Angelo ZERR.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - translation and adaptation to Java
 */
package org.eclipse.textmate4e.core.internal.oniguruma;

import java.util.ArrayList;
import java.util.List;

import org.joni.Region;

public class OnigResult implements IOnigNextMatchResult {

	private int index;
	private Region region;
	private List<IOnigCaptureIndex> captureIndices;

	public OnigResult(Region region, int indexInScanner) {
		this.captureIndices = new ArrayList<IOnigCaptureIndex>();
		this.update(indexInScanner, region);
	}

	@Override
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void update(int index, Region region) {
		this.index = index;
		this.region = region;
		this.captureIndices.clear();
	}

	@Override
	public IOnigCaptureIndex[] getCaptureIndices() {
		if (region.beg.length != captureIndices.size()) {
			captureIndices.clear();
			int captureStart = -1, captureEnd = -1;
			for (int i = 0; i < region.beg.length; i++) {
				captureStart = region.beg[i];
				captureEnd = region.end[i];
				captureIndices.add(new OnigCaptureIndex(i, captureStart, captureEnd));
			}
		}
		return captureIndices.toArray(new IOnigCaptureIndex[0]);
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append("{\n");
		result.append("  \"index\": ");
		result.append(getIndex());
		result.append(",\n");
		result.append("  \"captureIndices\": [\n");
		int i = 0;
		for (IOnigCaptureIndex captureIndex : getCaptureIndices()) {
			if (i > 0) {
				result.append(",\n");
			}
			result.append("    ");
			result.append(captureIndex);
			i++;
		}
		result.append("\n");
		result.append("  ]\n");
		result.append("}");
		return result.toString();
	}

	public int LocationAt(int index) {
		if (region.beg.length > 0) {
			return region.beg[0] + index;
		}
		return 0;
	}

	public int count() {
		return region.beg.length;
	}

}

@@ -463,63 +463,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 impo
@@ -561,24 +561,33 @@
 te int index
+InScanner
 ;%0D%0A%09private 
@@ -586,16 +586,22 @@
 private 
+final 
 Region r
@@ -617,21 +617,16 @@
 private 
-List%3C
 IOnigCap
@@ -634,17 +634,18 @@
 ureIndex
-%3E
+%5B%5D
  capture
@@ -724,59 +724,23 @@
 his.
-captureIndices = new ArrayList%3CIOnigCaptureIndex%3E()
+region = region
 ;%0D%0A%09
@@ -745,23 +745,16 @@
 %0A%09%09this.
-update(
 indexInS
@@ -763,17 +763,25 @@
 nner
-, region)
+ = indexInScanner
 ;%0D%0A%09
@@ -833,24 +833,33 @@
 return index
+InScanner
 ;%0D%0A%09%7D%0D%0A%0D%0A%09pu
@@ -907,151 +907,25 @@
 ndex
- = index;%0D%0A%09%7D%0D%0A%0D%0A%09public void update(int index, Region region) %7B%0D%0A%09%09this.index = index;%0D%0A%09%09this.region = region;%0D%0A%09%09this.captureIndices.clear()
+InScanner = index
 ;%0D%0A%09
@@ -1002,29 +1002,8 @@
 if (
-region.beg.length != 
 capt
@@ -1016,15 +1016,16 @@
 ices
-.size()
+ == null
 ) %7B%0D
@@ -1046,16 +1046,51 @@
 ices
-.clear()
+ = new IOnigCaptureIndex%5Bregion.beg.length%5D
 ;%0D%0A%09
@@ -1272,13 +1272,14 @@
 ices
-.add(
+%5Bi%5D = 
 new 
@@ -1323,17 +1323,16 @@
 tureEnd)
-)
 ;%0D%0A%09%09%09%7D%0D
@@ -1364,42 +1364,8 @@
 ices
-.toArray(new IOnigCaptureIndex%5B0%5D)
 ;%0D%0A%09

Create array of IOnigCaptureIndex