package org.example.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
    /**
     * 模拟C# string.Format行为
     * @param format 格式字符串，包含 {0}, {1} 等占位符
     * @param args 替换参数数组
     * @return 格式化后的字符串
     * @throws IllegalArgumentException 当格式或参数无效时抛出
     */
    public class StringFormat {

        // 仅匹配 {数字} 格式的占位符（如 {0}, {1}）
        private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{(\\d+)\\}");

        public static String format(String format, Object... args) {
            if (format == null) {
                throw new IllegalArgumentException("format cannot be null");
            }
            if (args == null) {
                throw new IllegalArgumentException("args cannot be null");
            }

            Matcher matcher = PLACEHOLDER_PATTERN.matcher(format);
            StringBuffer result = new StringBuffer();

            while (matcher.find()) {
                int index = Integer.parseInt(matcher.group(1));
                if (index < 0 || index >= args.length) {
                    throw new IllegalArgumentException("Index " + index + " out of bounds");
                }
                String replacement = args[index] == null ? "null" : args[index].toString();
                matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
            }

            matcher.appendTail(result);
            return result.toString();
        }
    }
