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

net.amygdalum.util.text.CharUtils Maven / Gradle / Ivy

The newest version!
package net.amygdalum.util.text;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public final class CharUtils {

	public static final int CHAR_RANGE = 1 << 16;

	private CharUtils() {
	}

    public static char[] fromWrapped(Collection charlist) {
        char[] chars = new char[charlist.size()];
        int i = 0;
        for (Character c : charlist) {
            chars[i] = c.charValue();
            i++;
        }
        return chars;
    }

	public static char[] revert(char[] chars) {
		final int ri = chars.length - 1;
		char[] reversechars = new char[chars.length];
		for (int i = 0; i < reversechars.length; i++) {
			reversechars[i] = chars[ri - i];
		}
		return reversechars;
	}

	public static int lastIndexOf(char[] pattern, char[] block) {
		nextPos: for (int i = pattern.length - block.length; i >= 0; i--) {
			for (int j = block.length - 1; j >= 0; j--) {
				if (pattern[j + i] == block[j]) {
					continue;
				} else {
					continue nextPos;
				}
			}
			return i;
		}
		return -1;
	}

	public static boolean isAsciiPrintable(char ch) {
		return ch >= 32 && ch < 127;
	}

	public static String charToString(char ch) {
		if (isAsciiPrintable(ch)) {
			return String.valueOf(ch);
		} else {
			StringBuilder buffer = new StringBuilder("\\u");
			String hex = Integer.toHexString((int) ch);
			for (int i = 0; i < 4 - hex.length(); i++) {
				buffer.append('0');
			}
			buffer.append(hex);
			return buffer.toString();
		}
	}

	public static char after(char c) {
		return (char) (c + 1);
	}

	public static char before(char c) {
		return (char) (c - 1);
	}

	public static char computeMinChar(char[] pattern) {
		char min = Character.MAX_VALUE;
		for (int i = 0; i < pattern.length; i++) {
			if (pattern[i] < min) {
				min = pattern[i];
			}
		}
		return min;
	}

	public static char computeMinChar(Collection patterns) {
		char min = Character.MAX_VALUE;
		for (char[] pattern : patterns) {
			for (int i = 0; i < pattern.length; i++) {
				if (pattern[i] < min) {
					min = pattern[i];
				}
			}
		}
		return min;
	}

	public static char computeMaxChar(char[] pattern) {
		char max = Character.MIN_VALUE;
		for (int i = 0; i < pattern.length; i++) {
			if (pattern[i] > max) {
				max = pattern[i];
			}
		}
		return max;
	}

	public static char computeMaxChar(Collection patterns) {
		char max = Character.MIN_VALUE;
		for (char[] pattern : patterns) {
			for (int i = 0; i < pattern.length; i++) {
				if (pattern[i] > max) {
					max = pattern[i];
				}
			}
		}
		return max;
	}

	public static int minLength(List patterns) {
		int len = Integer.MAX_VALUE;
		for (char[] pattern : patterns) {
			if (pattern.length < len) {
				len = pattern.length;
			}
		}
		return len;
	}

	public static int minLength(char[][] patterns) {
		int len = Integer.MAX_VALUE;
		for (char[] pattern : patterns) {
			if (pattern.length < len) {
				len = pattern.length;
			}
		}
		return len;
	}

	public static int maxLength(List patterns) {
		int len = Integer.MIN_VALUE;
		for (char[] pattern : patterns) {
			if (pattern.length > len) {
				len = pattern.length;
			}
		}
		return len;
	}

	public static int maxLength(char[][] patterns) {
		int len = Integer.MIN_VALUE;
		for (char[] pattern : patterns) {
			if (pattern.length > len) {
				len = pattern.length;
			}
		}
		return len;
	}

	public static int[] lengths(List patterns) {
		SortedSet lengths = new TreeSet<>();
		for (char[] pattern : patterns) {
			lengths.add(pattern.length);
		}
		int[] lengthsArray = new int[lengths.size()];
		int i = lengthsArray.length - 1;
		for (int length : lengths) {
			lengthsArray[i] = length;
			i--;
		}
		return lengthsArray;
	}

	public static List asList(char[] chars) {
		List list = new ArrayList<>(chars.length);
		for (int i = 0; i < chars.length; i++) {
			list.add(chars[i]);
		}
		return list;
	}

	public static List reverseList(char[] chars) {
		List list = new ArrayList<>(chars.length);
		for (int i = chars.length - 1; i >= 0; i--) {
			list.add(chars[i]);
		}
		return list;
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy