package org.tinylog.impl.format.style;

import org.tinylog.core.internal.InternalLogger;

/**
 * Style implementation for setting a configurable minimum length.
 */
public class MinLengthStyle implements Style {

	private final int minLength;
	private final Position position;

	/**
	 * @param minLength The minimum length for the input string
	 * @param position The position for the input string
	 */
	public MinLengthStyle(int minLength, Position position) {
		this.minLength = minLength;
		this.position = position;
	}

	@Override
	public void apply(StringBuilder builder, int start) {
		int totalLength = builder.length();
		int valueLength = totalLength - start;
		int difference = minLength - valueLength;

		if (difference > 0) {
			builder.setLength(totalLength + difference);

			switch (position) {
				case LEFT:
					replaceWithSpaces(builder, totalLength, difference);
					break;
				case CENTER:
					int leftSpaces = difference / 2;
					int rightSpaces = difference - leftSpaces;
					moveString(builder, start, valueLength, leftSpaces);
					replaceWithSpaces(builder, start, leftSpaces);
					replaceWithSpaces(builder, start + leftSpaces + valueLength, rightSpaces);
					break;
				case RIGHT:
					moveString(builder, start, valueLength, difference);
					replaceWithSpaces(builder, start, difference);
					break;
				default:
					InternalLogger.error(null, "Illegal position \"" + position + "\" for min length style");
					break;
			}
		}
	}

	/**
	 * Moves a substring within a string builder.
	 *
	 * @param builder The string builder to modify
	 * @param index The index of the substring to move
	 * @param length The length of the substring to move
	 * @param offset The target position of the substring, relative to the origin index
	 */
	private static void moveString(StringBuilder builder, int index, int length, int offset) {
		String value = builder.substring(index, index + length);
		builder.replace(index + offset, index + offset + length, value);
	}

	/**
	 * Overwrites a sequence in a string builder with space characters.
	 *
	 * @param builder The string builder to modify
	 * @param start The index in the string builder, where to start overwriting
	 * @param count The number of characters to overwrite with a space character
	 */
	private static void replaceWithSpaces(StringBuilder builder, int start, int count) {
		for (int index = start; index < start + count; ++index) {
			builder.setCharAt(index, ' ');
		}
	}

}

@@ -33,16 +33,42 @@
 style;%0A%0A
+import java.util.Arrays;%0A%0A
 import o
@@ -761,57 +761,8 @@
 ) %7B%0A
-%09%09%09builder.setLength(totalLength + difference);%0A%0A
 %09%09%09s
@@ -796,35 +796,30 @@
  LEFT:%0A%09%09%09%09%09
-replaceWith
+insert
 Spaces(build
@@ -915,16 +915,63 @@
 ce / 2;%0A
+%09%09%09%09%09insertSpaces(builder, start, leftSpaces);%0A
 %09%09%09%09%09int
@@ -1019,129 +1019,14 @@
 %09%09%09%09
-moveString(builder, start, valueLength, leftSpaces);%0A%09%09%09%09%09replaceWithSpaces(builder, start, leftSpaces);%0A%09%09%09%09%09replaceWith
+insert
 Spac
@@ -1122,77 +1122,14 @@
 %09%09%09%09
-moveString(builder, start, valueLength, difference);%0A%09%09%09%09%09replaceWith
+insert
 Spac
@@ -1318,32 +1318,51 @@
 %09 * 
-Moves a substring within
+Inserts a sequence of space characters into
  a s
@@ -1458,602 +1458,70 @@
 ndex
- of the substring to move%0A%09 * @param length The length of the substring to move%0A%09 * @param offset The target position of the substring, relative to the origin index%0A%09 */%0A%09private static void moveString(StringBuilder builder, int index, int length, int offset) %7B%0A%09%09String value = builder.substring(index, index + length);%0A%09%09builder.replace(index + offset, index + offset + length, value);%0A%09%7D%0A%0A%09/**%0A%09 * Overwrites a sequence in a string builder with space characters.%0A%09 *%0A%09 * @param builder The string builder to modify%0A%09 * @param start The index in the string builder, where to start overwriting
+, where to insert the space characters into the string builder
 %0A%09 *
@@ -1548,16 +1548,22 @@
 mber of 
+space 
 characte
@@ -1572,40 +1572,14 @@
  to 
-overwrite with a space character
+insert
 %0A%09 *
@@ -1605,19 +1605,14 @@
 oid 
-replaceWith
+insert
 Spac
@@ -1641,21 +1641,21 @@
 er, int 
-start
+index
 , int co
@@ -1667,77 +1667,79 @@
 %7B%0A%09%09
-for (int index = start; index %3C start + count; ++index) %7B
+char%5B%5D spaces = new char%5Bcount%5D;%0A%09%09Arrays.fill(spaces, ' ');
 %0A%09%09
-%09
 builder.
 setC
@@ -1738,16 +1738,13 @@
 der.
-setCharA
+inser
 t(in
@@ -1752,17 +1752,16 @@
 ex, 
-' ');%0A%09%09%7D
+spaces);
 %0A%09%7D%0A

Simplify the min length style implementation