// Path: src/main/java/hextostring/utils/Hex.java
// public class Hex {
// 
// 	/**
// 	 * Converts a hexadecimal string into a readable string.
// 	 *
// 	 * @param hex
// 	 * 			The hexadecimal string.
// 	 * @param charset
// 	 * 			The charset used for the convertion.
// 	 * @return The readable string.
// 	 */
// 	public static String convertToString(String hex, Charset charset) {
// 		return byteListToString(HexStringToByteList(hex), charset);
// 	}
// 
// 	private static String byteArrayToString(byte[] data, Charset charset) {
// 		return new String(data, charset);
// 	}
// 
// 	private static String byteListToString(List<Byte> data, Charset charset) {
// 		byte[] arrayData = new byte[data.size()];
// 		int cmpt = 0;
// 		for (Byte b : data) {
// 			arrayData[cmpt++] = b;
// 		}
// 		return byteArrayToString(arrayData, charset);
// 	}
// 
// 	private static List<Byte> HexStringToByteList(String hex) {
// 		List<Byte> byteList = new LinkedList<>();
// 		boolean littleEnd = false;
// 		int currVal = 0;
// 		for (int i = 0; i < hex.length(); ++i) {
// 			if (littleEnd) {
// 				currVal = currVal | (Character.digit(hex.charAt(i), 16));
// 			} else {
// 				currVal = (Character.digit(hex.charAt(i), 16)) << 4;
// 			}
// 			if (littleEnd) {
// 				byteList.add((byte) (currVal & 0xFF));
// 			}
// 			littleEnd = !littleEnd;
// 		}
// 
// 		return byteList;
// 	}
// 
// }


import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import hextostring.utils.Hex;
package hextostring.replacement;



/**
 * Replaces hexadecimal sequences and patterns by readable characters between
 * minus signs. A string in such an intermediary state will be called an
 * transitory string, or mixed string.
 *
 * @author Maxime PIA
 */
public class HexToStrStrategy extends ReplacementStrategy {

	@Override
	public String replacePattern(String s, String pattern, String replacement) {
		// captured patterns will be converted
		return s.replaceAll(pattern, "-" + replacement.replace("-", "\\-")
			// isolates group references not preceded by an antislash
			.replaceAll("(?<!\\\\)(\\$[0-9]*)", "-$1-") + "-");
	}

	@Override
	public String replaceSequence(String s, String sequence,
		String replacement) {

		return s.replace(sequence, "-" + replacement.replace("-", "\\-") + "-");
	}

	/**
	 * Converts a transitory string into a completely readable string.
	 *
	 * @param s
	 * 			The string to convert.
	 * @param cs
	 * 			The charset used to convert s.
	 * @return A completely readable string.
	 */
	public static String toReadableString(String s, Charset cs) {
		List<String> parts = splitParts(s);
		StringBuilder noHexString = new StringBuilder();
		boolean hexPart = true;
		for (String part : parts) {
			if (hexPart) {
// Path: src/main/java/hextostring/utils/Hex.java
// public class Hex {
// 
// 	/**
// 	 * Converts a hexadecimal string into a readable string.
// 	 *
// 	 * @param hex
// 	 * 			The hexadecimal string.
// 	 * @param charset
// 	 * 			The charset used for the convertion.
// 	 * @return The readable string.
// 	 */
// 	public static String convertToString(String hex, Charset charset) {
// 		return byteListToString(HexStringToByteList(hex), charset);
// 	}
// 
// 	private static String byteArrayToString(byte[] data, Charset charset) {
// 		return new String(data, charset);
// 	}
// 
// 	private static String byteListToString(List<Byte> data, Charset charset) {
// 		byte[] arrayData = new byte[data.size()];
// 		int cmpt = 0;
// 		for (Byte b : data) {
// 			arrayData[cmpt++] = b;
// 		}
// 		return byteArrayToString(arrayData, charset);
// 	}
// 
// 	private static List<Byte> HexStringToByteList(String hex) {
// 		List<Byte> byteList = new LinkedList<>();
// 		boolean littleEnd = false;
// 		int currVal = 0;
// 		for (int i = 0; i < hex.length(); ++i) {
// 			if (littleEnd) {
// 				currVal = currVal | (Character.digit(hex.charAt(i), 16));
// 			} else {
// 				currVal = (Character.digit(hex.charAt(i), 16)) << 4;
// 			}
// 			if (littleEnd) {
// 				byteList.add((byte) (currVal & 0xFF));
// 			}
// 			littleEnd = !littleEnd;
// 		}
// 
// 		return byteList;
// 	}
// 
// }


// Path: src/main/java/hextostring/replacement/HexToStrStrategy.java
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import hextostring.utils.Hex;

package hextostring.replacement;



/**
 * Replaces hexadecimal sequences and patterns by readable characters between
 * minus signs. A string in such an intermediary state will be called an
 * transitory string, or mixed string.
 *
 * @author Maxime PIA
 */
public class HexToStrStrategy extends ReplacementStrategy {

	@Override
	public String replacePattern(String s, String pattern, String replacement) {
		// captured patterns will be converted
		return s.replaceAll(pattern, "-" + replacement.replace("-", "\\-")
			// isolates group references not preceded by an antislash
			.replaceAll("(?<!\\\\)(\\$[0-9]*)", "-$1-") + "-");
	}

	@Override
	public String replaceSequence(String s, String sequence,
		String replacement) {

		return s.replace(sequence, "-" + replacement.replace("-", "\\-") + "-");
	}

	/**
	 * Converts a transitory string into a completely readable string.
	 *
	 * @param s
	 * 			The string to convert.
	 * @param cs
	 * 			The charset used to convert s.
	 * @return A completely readable string.
	 */
	public static String toReadableString(String s, Charset cs) {
		List<String> parts = splitParts(s);
		StringBuilder noHexString = new StringBuilder();
		boolean hexPart = true;
		for (String part : parts) {
			if (hexPart) {
				noHexString.append(Hex.convertToString(part, cs));