Changes
This commit is contained in:
parent
bc60a8f561
commit
12596fd7f2
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.openeggbert.jdotnet.System;
|
package com.openeggbert.jdotnet.System;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@ -26,29 +27,41 @@ public class String_ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String Format(String template, Object... args) {
|
public static String Format(String template, Object... args) {
|
||||||
if (template == null || args == null) {
|
if (template == null || args == null) {
|
||||||
throw new IllegalArgumentException("Template and arguments must not be null.");
|
throw new IllegalArgumentException("Template and arguments must not be null.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile("\\{(\\d+)}");
|
StringBuilder result = new StringBuilder();
|
||||||
Matcher matcher = pattern.matcher(template);
|
int length = template.length();
|
||||||
StringBuffer result = new StringBuffer();
|
|
||||||
|
|
||||||
while (matcher.find()) {
|
for (int i = 0; i < length; i++) {
|
||||||
String placeholder = matcher.group(1);
|
char c = template.charAt(i);
|
||||||
int index = Integer.parseInt(placeholder);
|
if (c == '{') {
|
||||||
|
int closingBrace = template.indexOf('}', i);
|
||||||
|
if (closingBrace == -1) {
|
||||||
|
throw new IllegalArgumentException("Unmatched '{' in the template.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String placeholder = template.substring(i + 1, closingBrace);
|
||||||
|
int index;
|
||||||
|
try {
|
||||||
|
index = Integer.parseInt(placeholder);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid placeholder: {" + placeholder + "}");
|
||||||
|
}
|
||||||
|
|
||||||
if (index < 0 || index >= args.length) {
|
if (index < 0 || index >= args.length) {
|
||||||
throw new IllegalArgumentException("Placeholder index out of range: " + index);
|
throw new IllegalArgumentException("Placeholder index out of range: " + index);
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.appendReplacement(result, args[index].toString());
|
result.append(args[index].toString());
|
||||||
|
i = closingBrace; // Skip to the closing brace
|
||||||
|
} else {
|
||||||
|
result.append(c);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Přidej zbývající část šablony
|
return result.toString();
|
||||||
matcher.appendTail(result);
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String_(char ch, int times) {
|
public String_(char ch, int times) {
|
||||||
@ -68,4 +81,70 @@ public class String_ {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return getValue();
|
return getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String Format(Locale l, String format, Object... args) {
|
||||||
|
if (format == null || args == null) {
|
||||||
|
throw new IllegalArgumentException("Format string and arguments must not be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
int argIndex = 0;
|
||||||
|
int length = format.length();
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
char c = format.charAt(i);
|
||||||
|
if (c == '%' && i + 1 < length) {
|
||||||
|
char next = format.charAt(i + 1);
|
||||||
|
if (next == '%') {
|
||||||
|
// Escaped percent sign
|
||||||
|
result.append('%');
|
||||||
|
i++;
|
||||||
|
} else if (argIndex < args.length) {
|
||||||
|
result.append(formatArgument(next, args[argIndex++]));
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Insufficient arguments provided for format string.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argIndex < args.length) {
|
||||||
|
throw new IllegalArgumentException("Too many arguments provided for format string.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatArgument(char formatType, Object arg) {
|
||||||
|
if (arg == null) {
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (formatType) {
|
||||||
|
case 'd': // Integer
|
||||||
|
if (arg instanceof Number) {
|
||||||
|
return Integer.toString(((Number) arg).intValue());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f': // Floating-point
|
||||||
|
if (arg instanceof Number) {
|
||||||
|
return String.valueOf(((Number) arg).doubleValue());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 's': // String
|
||||||
|
return arg.toString();
|
||||||
|
case 'x': // Hexadecimal
|
||||||
|
if (arg instanceof Number) {
|
||||||
|
return Integer.toHexString(((Number) arg).intValue());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unsupported format type: %" + formatType);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid argument type for format type: %" + formatType);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
65
src/main/java/com/openeggbert/jdotnet/System/int_.java
Normal file
65
src/main/java/com/openeggbert/jdotnet/System/int_.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||||
|
*/
|
||||||
|
package com.openeggbert.jdotnet.System;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author robertvokac
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public class int_ {
|
||||||
|
private int value;
|
||||||
|
public int get() {return value;}
|
||||||
|
public void set(int valueIn) {this.value = valueIn;}
|
||||||
|
public static int_ of(int i) {
|
||||||
|
return new int_(i);
|
||||||
|
}
|
||||||
|
public String ToString() {
|
||||||
|
return Integer.toString(value);
|
||||||
|
}
|
||||||
|
public String ToString(String format) {
|
||||||
|
if (format == null || format.isEmpty()) {
|
||||||
|
return Integer.toString(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
char formatSpecifier = format.charAt(0);
|
||||||
|
switch (formatSpecifier) {
|
||||||
|
case 'D': { // Decimal with leading zeros
|
||||||
|
int digits = Integer.parseInt(format.substring(1));
|
||||||
|
String valueStr = Integer.toString(value);
|
||||||
|
int padding = Math.max(0, digits - valueStr.length());
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int i = 0; i < padding; i++) {
|
||||||
|
result.append('0');
|
||||||
|
}
|
||||||
|
result.append(valueStr);
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'X': // Hexadecimal
|
||||||
|
return Integer.toHexString(value).toUpperCase();
|
||||||
|
|
||||||
|
case 'N': { // Number with grouping
|
||||||
|
String valueStr = Integer.toString(value);
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
int length = valueStr.length();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
result.append(valueStr.charAt(i));
|
||||||
|
if ((length - i - 1) % 3 == 0 && i != length - 1) {
|
||||||
|
result.append(','); // Add grouping separator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unsupported format: " + format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user