/*******************************************************************************
 * Copyright (C) 2017 Hugo Dupanloup (Yeregorix)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 ******************************************************************************/
package net.smoofyuniverse.common.util;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.function.Predicate;
import java.util.regex.Pattern;

public class StringUtil {
	public static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").withZone(ZoneId.systemDefault()),
			DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy").withZone(ZoneId.systemDefault()),
			TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss").withZone(ZoneId.systemDefault());
	
	private static final char[] hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	
	public static Predicate<String> regexPredicate(String arg) {
		return Pattern.compile(arg).asPredicate();
	}
	
	public static Predicate<String> simplePredicate(String arg) {
		int first = arg.indexOf('*');
		if (first == -1)
			return (s) -> s.equals(arg);
			
		if (arg.length() == 1)
			return (s) -> true;
			
		int last = arg.lastIndexOf('*');
		String start = arg.substring(0, first), end = arg.substring(last +1);
		
		if (start.isEmpty()) {
			if (end.isEmpty()) {
				String middle = start.substring(last +1);
				return (s) -> s.contains(middle);
			}
			return (s) -> s.endsWith(end);
		}
		
		if (end.isEmpty())
			return (s) -> s.startsWith(start);
		
		return (s) -> s.startsWith(start) && s.endsWith(end);
	}
	
	public static String simpleFormat(Throwable t) {
		String s = t.getClass().getSimpleName();
		if (t.getMessage() != null)
			s += ": " + t.getMessage();
		return s;
	}

	public static String toHexString(byte[] bytes) {
		StringBuffer s = new StringBuffer(bytes.length *2);
		for (byte b : bytes)
			s.append(hexchars[(b & 0xF0) >> 4]).append(hexchars[b & 0x0F]);
		 return s.toString();
	}
}

@@ -2410,76 +2410,9 @@
  +1)
-;%0D%0A%09%09%0D%0A%09%09if (start.isEmpty()) %7B%0D%0A%09%09%09if (end.isEmpty()) %7B%0D%0A%09%09%09%09String
+,
  mid
@@ -2417,21 +2417,19 @@
 iddle = 
-st
 ar
-t
+g
 .substri
@@ -2435,164 +2435,21 @@
 ing(
-la
+fir
 st +1
-);%0D%0A%09%09%09%09return (s) -%3E s.contains(middle);%0D%0A%09%09%09%7D%0D%0A%09%09%09return (s) -%3E s.endsWith(end);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09if (end.isEmpty())%0D%0A%09%09%09return (s) -%3E s.startsWith(star
+, las
 t);%0D
@@ -2507,16 +2507,38 @@
 ith(end)
+ && s.contains(middle)
 ;%0D%0A%09%7D%0D%0A%09

Fix String predicate