Skip to content
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
206 changes: 4 additions & 202 deletions nanoFramework.CoreLibrary/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,212 +718,14 @@ public static string IsInterned(string str)
#if NANOCLR_REFLECTION

#nullable enable
public static string Format(string format, params object?[] args)
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string Format(string format, params object?[] args);
#nullable restore

#else
public static string Format(string format, params object[] args)
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string Format(string format, params object[] args);
#endif
{
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)
{
#if NANOCLR_REFLECTION

if (args[index] is null)
{
token = "";
}
else
{
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
throw new NotImplementedException();
#endif // NANOCLR_REFLECTION

}
else
{
token = args[index] == null ? "" : args[index].ToString();
}

if (alignment > 0)
{
output += token.PadLeft(alignment);
}
else if (alignment < 0)
{
output += token.PadRight(MathInternal.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.
Expand Down
Loading