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

org.antlr.v4.runtime.misc.Utils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2012 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD-3-Clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.runtime.misc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Utils {
	public static String join(Iterable iter, String separator) {
		return join(iter.iterator(), separator);
	}

	public static  String join(T[] array, String separator) {
		return join(Arrays.asList(array), separator);
	}

    // Seriously: why isn't this built in to java? ugh!
    public static  String join(Iterator iter, String separator) {
        StringBuilder buf = new StringBuilder();
        while ( iter.hasNext() ) {
            buf.append(iter.next());
            if ( iter.hasNext() ) {
                buf.append(separator);
            }
        }
        return buf.toString();
    }

	public static boolean equals(Object x, Object y) {
		if (x == y) {
			return true;
		}

		if (x == null || y == null) {
			return false;
		}

		return x.equals(y);
	}

	public static int numNonnull(Object[] data) {
		int n = 0;
		if ( data == null ) return n;
		for (Object o : data) {
			if ( o!=null ) n++;
		}
		return n;
	}

	public  static  void removeAllElements(Collection data, T value) {
		if ( data==null ) return;
		while ( data.contains(value) ) data.remove(value);
	}

	public static String escapeWhitespace(String s, boolean escapeSpaces) {
		StringBuilder buf = new StringBuilder();
		for (char c : s.toCharArray()) {
			if ( c==' ' && escapeSpaces ) buf.append('\u00B7');
			else if ( c=='\t' ) buf.append("\\t");
			else if ( c=='\n' ) buf.append("\\n");
			else if ( c=='\r' ) buf.append("\\r");
			else buf.append(c);
		}
		return buf.toString();
	}

	public static void writeFile(@NotNull File file, @NotNull byte[] content) throws IOException {
		FileOutputStream fos = new FileOutputStream(file);
		try {
			fos.write(content);
		}
		finally {
			fos.close();
		}
	}

	public static void writeFile(@NotNull String fileName, @NotNull String content) throws IOException {
		writeFile(fileName, content, null);
	}

	public static void writeFile(@NotNull String fileName, @NotNull String content, @Nullable String encoding) throws IOException {
		File f = new File(fileName);
		FileOutputStream fos = new FileOutputStream(f);
		OutputStreamWriter osw;
		if (encoding != null) {
			osw = new OutputStreamWriter(fos, encoding);
		}
		else {
			osw = new OutputStreamWriter(fos);
		}

		try {
			osw.write(content);
		}
		finally {
			osw.close();
		}
	}

	@NotNull
	public static char[] readFile(@NotNull String fileName) throws IOException {
		return readFile(fileName, null);
	}

	@NotNull
	public static char[] readFile(@NotNull String fileName, @Nullable String encoding) throws IOException {
		File f = new File(fileName);
		int size = (int)f.length();
		InputStreamReader isr;
		FileInputStream fis = new FileInputStream(fileName);
		if ( encoding!=null ) {
			isr = new InputStreamReader(fis, encoding);
		}
		else {
			isr = new InputStreamReader(fis);
		}
		char[] data = null;
		try {
			data = new char[size];
			int n = isr.read(data);
			if (n < data.length) {
				data = Arrays.copyOf(data, n);
			}
		}
		finally {
			isr.close();
		}
		return data;
	}

	public static  void removeAll(@NotNull List list, @NotNull Predicate predicate) {
		int j = 0;
		for (int i = 0; i < list.size(); i++) {
			T item = list.get(i);
			if (!predicate.eval(item)) {
				if (j != i) {
					list.set(j, item);
				}

				j++;
			}
		}

		if (j < list.size()) {
			list.subList(j, list.size()).clear();
		}
	}

	public static  void removeAll(@NotNull Iterable iterable, @NotNull Predicate predicate) {
		if (iterable instanceof List) {
			removeAll((List)iterable, predicate);
			return;
		}

		for (Iterator iterator = iterable.iterator(); iterator.hasNext(); ) {
			T item = iterator.next();
			if (predicate.eval(item)) {
				iterator.remove();
			}
		}
	}

	/** Convert array of strings to string→index map. Useful for
	 *  converting rulenames to name→ruleindex map.
	 */
	public static Map toMap(String[] keys) {
		Map m = new HashMap();
		for (int i=0; i 0xFFFF
			if (!calculatedPreciseResultSize &&
			    Character.isSupplementaryCodePoint(codePoint)) {
				resultArray = Arrays.copyOf(resultArray, charArraySize(data));
				calculatedPreciseResultSize = true;
			}

			// This will throw IllegalArgumentException if
			// the code point is not a valid Unicode code point
			int charsWritten = Character.toChars(codePoint, resultArray, resultIdx);
			resultIdx += charsWritten;
		}

		return resultArray;
	}

	private static int charArraySize(IntegerList data) {
		int result = 0;
		for (int i = 0; i < data.size(); i++) {
			result += Character.charCount(data.get(i));
		}

		return result;
	}

	/**
	 * @since 4.5
	 */
	@NotNull
	public static IntervalSet toSet(@NotNull BitSet bits) {
		IntervalSet s = new IntervalSet();
		int i = bits.nextSetBit(0);
		while ( i >= 0 ) {
			s.add(i);
			i = bits.nextSetBit(i+1);
		}
		return s;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy