Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String.Format #12

Merged
merged 4 commits into from
Jun 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 241 additions & 0 deletions source/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,246 @@ public static String IsInterned(String str)
return str;
}

/// <summary>
/// Replaces the format items in a string with the string representations of corresponding objects in a specified array.
/// </summary>
/// <param name="format">A composite format string</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.</returns>
public static string Format(string format, params object[] args)
{
var index = 0;
var alignment = 0;
var chr = '\0';
var len = format.Length;
var fmt = Empty;
var token = Empty;
var output = Empty;

if (format is null)
{
throw new ArgumentNullException("format can't be null");
}

for (var i = 0; i < len; i++)
{
token = Empty;
chr = format[i];

if (chr == '{')
{
if (i + 1 == len)
{
throw new ArgumentException("Format error: no closed brace, column " + i);
}

if (format[i + 1] == '{')
{
output += chr;
i++;
continue;
}
else
{
alignment = 0;
fmt = Empty;

for (i++; i < len; i++)
{
chr = format[i];

if (chr >= '0' && chr <= '9')
{
token += chr;
}
else if (chr == ',' || chr == ':' || chr == '}')
{
break;
}
else
{
throw new ArgumentException("Format error: wrong symbol at {}, column " + i);
}
}

if (token.Length > 0)
{
index = int.Parse(token);
}
else
{
throw new ArgumentException("Format error: empty {}, column " + i);
}

if (chr == ',')
{
if (format[i + 1] == '-')
{
token = "-";
i++;
}
else
{
token = Empty;
}

for (i++; i < len; i++)
{
chr = format[i];

if (chr >= '0' && chr <= '9')
{
token += chr;
}
else if (chr == ':' || chr == '}')
{
break;
}
else
{
throw new ArgumentException("Format error: wrong symbol at alignment, column " + i);
}
}

if (token.Length > 0)
{
alignment = int.Parse(token);
}
else
{
throw new ArgumentException("Format error: empty alignment, column " + i);
}
}

if (chr == ':')
{
token = Empty;
for (i++; i < len; i++)
{
chr = format[i];

if (chr == '}')
{
break;
}
else
{
token += chr;
}
}

if (token.Length > 0)
{
fmt = token;
}
else
{
throw new ArgumentException("Format error: empty format after ':', column " + i);
}
}
}

if (chr != '}')
{
throw new ArgumentException("Format error: no closed brace, column " + i);
}

if (fmt.Length > 0)
{
var method = args[index].GetType().GetMethod("ToString", new Type[] { typeof(string) });
token = (method is null)
? args[index].ToString()
: method.Invoke(args[index], new object[] { token }).ToString();
}
else
{
token = args[index].ToString();
}

if (alignment > 0)
{
output += token.PadLeft(alignment);
}
else if (alignment < 0)
{
output += token.PadRight(Math.Abs(alignment));
}
else
{
output += token;
}
}
else if (chr == '}')
{
if (i + 1 == len)
{
throw new ArgumentException("Format error: no closed brace, column " + i);
}

if (format[i + 1] == '}')
{
output += chr;
i++;
}
else
{
throw new ArgumentException("Format error: no closed brace, column " + i);
}
}
else
{
output += chr;
}
}

return output;
}

/// <summary>
/// Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.
/// </summary>
/// <param name="totalWidth">The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.</param>
/// <param name="paddingChar">A Unicode padding character.</param>
/// <returns></returns>
public String PadLeft(int totalWidth, char paddingChar = ' ')
{
if (totalWidth < 0)
{
throw new ArgumentOutOfRangeException("totalWidth can't be less than 0");
}

if (Length >= totalWidth)
{
return this;
}
else
{
return new String(paddingChar, totalWidth - Length) + this;
}
}

/// <summary>
/// Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.
/// </summary>
/// <param name="totalWidth">The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.</param>
/// <param name="paddingChar">A Unicode padding character.</param>
/// <returns></returns>
public String PadRight(int totalWidth, char paddingChar = ' ')
{
if (totalWidth < 0)
{
throw new ArgumentOutOfRangeException("totalWidth can't be less than 0");
}

if (Length >= totalWidth)
{
return this;
}
else
{
return this + new String(paddingChar, totalWidth - Length);
}
}

}
}