コード例 #1
0
ファイル: PrintUtil.cs プロジェクト: sekcheong/parser
        private static bool FormatString(string format, object[] args, out List<string> segments)
        {
            segments = null;
            MatchCollection matches = _formatRegex.Matches(format);
            if (matches.Count == 0) return false;

            int lastEnd = 0;
            int argPos = 0;
            segments = new List<string>();

            foreach (Match m in matches) {
                if (lastEnd < m.Index) {
                    segments.Add(format.Substring(lastEnd, m.Index - lastEnd));
                }

                FormatSpecification fs = new FormatSpecification(m);
                if (fs.Width == int.MinValue) {
                    if (!(argPos < args.Length) || !(args[argPos] is int)) throw new Exception("printf width parameter was not provided");
                    fs.Width = (int)args[argPos];
                    argPos = argPos + 1;
                }
                if (fs.Precision == int.MinValue) {
                    if (!(argPos < args.Length) || !(args[argPos] is int)) throw new Exception("printf precision parameter was not provided");
                    fs.Precision = (int)args[argPos];
                    argPos = argPos + 1;
                }
                segments.Add(fs.Format(args[argPos]));

                argPos++;
                lastEnd = m.Index + m.Value.Length;
            }

            if (lastEnd < format.Length) {
                segments.Add(format.Substring(lastEnd));
            }

            return true;
        }