public static FormatSpecificationInfo GetFormatSpecifierInfo(string specification) { #if FX35 if (specification.IsNullOrWhiteSpace()) { return(null); } #else if (string.IsNullOrWhiteSpace(specification)) { return(null); } #endif FormatSpecificationInfo info = new FormatSpecificationInfo() { type = '\0', width = int.MinValue, precision = 6, }; string width = ""; string precision = ""; int start = -1; int fsLength = 1; // TODO: parse parameter index for (int i = 0; i < specification.Length && info.type == '\0'; i++) { char c = specification[i]; switch (c) { case '%': if (start == -1) { start = i; } else { info.type = c; } info.specification = specification.Substring(start, i + 1 - start); fsLength = i + 1; break; // flags case '-': info.flags |= FormatFlags.LeftAlign; break; case '+': info.flags |= FormatFlags.Sign; break; case ' ': info.flags |= FormatFlags.Blank; break; case '#': info.flags |= FormatFlags.Alternate; break; case '\'': info.flags |= FormatFlags.Grouping; break; case '?': info.flags |= FormatFlags.ArchSize; break; // precision case '.': { for (int j = i + 1; j < specification.Length; j++) { if (specification[j] == '*') { info.flags |= FormatFlags.DynamicPrecision; } else if (char.IsNumber(specification[j])) { precision += specification[j]; } else { break; } i++; } } break; // length flags case 'h': info.flags += (int)FormatFlags.IsShort; break; case 'l': info.flags += (int)FormatFlags.IsLong; break; case 'L': info.flags |= FormatFlags.IsLongDouble; break; case 'z': case 'j': case 't': // not supported break; // dynamic width case '*': info.flags |= FormatFlags.DynamicWidth; break; default: { if (char.IsNumber(c)) { if (width == "" && c == '0') { info.flags |= FormatFlags.ZeroPad; } else { width += c; } } else if (char.IsLetter(c) && info.type == '\0') { info.type = c; info.specification = specification.Substring(start, i + 1 - start); fsLength = i + 1; } } break; } } // sign overrides space if (info.flags.HasFlag(FormatFlags.Sign) && info.flags.HasFlag(FormatFlags.Blank)) { info.flags &= ~FormatFlags.Blank; } if (info.flags.HasFlag(FormatFlags.LeftAlign) && info.flags.HasFlag(FormatFlags.ZeroPad)) { info.flags &= ~FormatFlags.ZeroPad; } // unsupported precision for these types if (info.type == 's' || info.type == 'c' || Char.ToUpper(info.type) == 'X' || info.type == 'o') { info.precision = int.MinValue; } #if FX35 if (!precision.IsNullOrWhiteSpace()) { info.precision = Convert.ToInt32(precision); } #else if (!string.IsNullOrWhiteSpace(precision)) { info.precision = Convert.ToInt32(precision); } #endif #if FX35 if (!width.IsNullOrWhiteSpace()) { info.width = Convert.ToInt32(width); } #else if (!string.IsNullOrWhiteSpace(width)) { info.width = Convert.ToInt32(width); } #endif return(info); }
public static FormatSpecificationInfo GetFormatSpecifierInfo(string specification) { if (string.IsNullOrWhiteSpace(specification)) return null; FormatSpecificationInfo info = new FormatSpecificationInfo() { type = '\0', width = int.MinValue, precision = 6, }; string width = ""; string precision = ""; int start = -1; int fsLength = 1; // TODO: parse parameter index for (int i = 0; i < specification.Length && info.type == '\0'; i++) { char c = specification[i]; switch (c) { case '%': if (start == -1) start = i; else info.type = c; info.specification = specification.Substring(start, i + 1 - start); fsLength = i + 1; break; // flags case '-': info.flags |= FormatFlags.LeftAlign; break; case '+': info.flags |= FormatFlags.Sign; break; case ' ': info.flags |= FormatFlags.Blank; break; case '#': info.flags |= FormatFlags.Alternate; break; case '\'': info.flags |= FormatFlags.Grouping; break; case '?': info.flags |= FormatFlags.ArchSize; break; // precision case '.': { for (int j = i + 1; j < specification.Length; j++) { if (specification[j] == '*') info.flags |= FormatFlags.DynamicPrecision; else if (char.IsNumber(specification[j])) precision += specification[j]; else break; i++; } } break; // length flags case 'h': info.flags += (int)FormatFlags.IsShort; break; case 'l': info.flags += (int)FormatFlags.IsLong; break; case 'L': info.flags |= FormatFlags.IsLongDouble; break; case 'z': case 'j': case 't': // not supported break; // dynamic width case '*': info.flags |= FormatFlags.DynamicWidth; break; default: { if (char.IsNumber(c)) { if (width == "" && c == '0') info.flags |= FormatFlags.ZeroPad; else width += c; } else if (char.IsLetter(c) && info.type == '\0') { info.type = c; info.specification = specification.Substring(start, i + 1 - start); fsLength = i + 1; } } break; } } // sign overrides space if (info.flags.HasFlag(FormatFlags.Sign) && info.flags.HasFlag(FormatFlags.Blank)) info.flags &= ~FormatFlags.Blank; if (info.flags.HasFlag(FormatFlags.LeftAlign) && info.flags.HasFlag(FormatFlags.ZeroPad)) info.flags &= ~FormatFlags.ZeroPad; // unsupported precision for these types if (info.type == 's' || info.type == 'c' || Char.ToUpper(info.type) == 'X' || info.type == 'o') { info.precision = int.MinValue; } if (!string.IsNullOrWhiteSpace(precision)) info.precision = Convert.ToInt32(precision); if (!string.IsNullOrWhiteSpace(width)) info.width = Convert.ToInt32(width); return info; }