From 12596fd7f23898c4c289bb4d1cae40b1c93f0373 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Fri, 20 Dec 2024 22:19:45 +0100 Subject: [PATCH] Changes --- .../openeggbert/jdotnet/System/String_.java | 107 +++++++++++++++--- .../com/openeggbert/jdotnet/System/int_.java | 65 +++++++++++ 2 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/openeggbert/jdotnet/System/int_.java diff --git a/src/main/java/com/openeggbert/jdotnet/System/String_.java b/src/main/java/com/openeggbert/jdotnet/System/String_.java index b0e015e..95cf7a5 100644 --- a/src/main/java/com/openeggbert/jdotnet/System/String_.java +++ b/src/main/java/com/openeggbert/jdotnet/System/String_.java @@ -4,6 +4,7 @@ */ package com.openeggbert.jdotnet.System; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.Getter; @@ -26,29 +27,41 @@ public class String_ { } public static String Format(String template, Object... args) { - if (template == null || args == null) { - throw new IllegalArgumentException("Template and arguments must not be null."); - } + if (template == null || args == null) { + throw new IllegalArgumentException("Template and arguments must not be null."); + } - Pattern pattern = Pattern.compile("\\{(\\d+)}"); - Matcher matcher = pattern.matcher(template); - StringBuffer result = new StringBuffer(); + StringBuilder result = new StringBuilder(); + int length = template.length(); - while (matcher.find()) { - String placeholder = matcher.group(1); - int index = Integer.parseInt(placeholder); + for (int i = 0; i < length; i++) { + char c = template.charAt(i); + 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) { 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 - matcher.appendTail(result); - - return result.toString(); + return result.toString(); } public String_(char ch, int times) { @@ -68,4 +81,70 @@ public class String_ { public String toString() { 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); + } + } diff --git a/src/main/java/com/openeggbert/jdotnet/System/int_.java b/src/main/java/com/openeggbert/jdotnet/System/int_.java new file mode 100644 index 0000000..d315c86 --- /dev/null +++ b/src/main/java/com/openeggbert/jdotnet/System/int_.java @@ -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); + } + } + +}