package com.github.aureliano.achmed.helper;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringHelper {

	public static final String EMPTY = "";
	private static final String NUMBER_REGEX = "[+-]?\\d+\\.?\\d*";
	
	private StringHelper() {
		super();
	}
	
	public static boolean isEmpty(String text) {
		return ((text == null) || (text.equals(EMPTY)));
	}
	
	public static String toString(Object object) {
		return ((object == null) ? "null" : object.toString());
	}
	
	public static String parse(Object object) {
		return ((object == null) ? null : object.toString());
	}
	
	public static String capitalize(String text) {
		return Character.toUpperCase(text.charAt(0)) + text.substring(1).toLowerCase();
	}
	
	public static String join(Object[] objects) {
		return join(objects, "");
	}
	
	public static String join(Object[] objects, String separator) {
		StringBuilder builder = new StringBuilder();
		int count = 0;
		for (Object object : objects) {
			if (count > 0) {
				builder.append(separator);
			}
		        
		builder.append(object);
		count++;
		}
	
		return builder.toString();
	}

	public static String join(List<?> objects, String separator) {
		return join(objects.toArray(), separator);
	}
	
	public static boolean isNumeric(String value) {
		return (value == null) ? false : value.matches(NUMBER_REGEX);
	}
	
	public static String[] match(String regex, String target) {
		Matcher matcher = Pattern.compile(regex).matcher(target);
		if (!matcher.find()) {
			return null;
		} else if (matcher.groupCount() == 0) {
			return new String[] { matcher.group() };
		}
		
		String[] groups = new String[matcher.groupCount() + 1];
		for (int i = 0; i < groups.length; i++) {
			groups[i] = matcher.group(i);
		}
		
		return groups;
	}
}
@@ -126,16 +126,79 @@
 ttern;%0A%0A
+import com.github.aureliano.achmed.exception.AchmedException;%0A%0A
 public f
@@ -1862,9 +1862,512 @@
 ups;%0A%09%7D%0A
+%09%0A%09public static String amendEnvVars(String text) %7B%0A%09%09String%5B%5D match = StringHelper.match(%22(%5C%5C$%5B%5C%5Cw%5C%5Cd%5D+)%22, text);%0A%09%09if (match == null) %7B%0A%09%09%09return text;%0A%09%09%7D%0A%09%09%0A%09%09for (byte i = 1; i %3C match.length; i++) %7B%0A%09%09%09String regex = Pattern.quote(match%5Bi%5D);%0A%09%09%09String var = System.getenv(match%5Bi%5D.replaceFirst(%22%5E%5C%5C$%22, %22%22));%0A%09%09%09if (var == null) %7B%0A%09%09%09%09throw new AchmedException(%22Could not find any environment variable with name %22 + match%5Bi%5D);%0A%09%09%09%7D%0A%09%09%09text = text.replaceFirst(regex, var);%0A%09%09%7D%0A%09%09%0A%09%09return text;%0A%09%7D%0A
 %7D

Add method to replace environment variables from text.