Skip to content

Commit 6996df8

Browse files
authored
Moved String.Format() to native (#261)
1 parent 7977eda commit 6996df8

File tree

1 file changed

+4
-202
lines changed

1 file changed

+4
-202
lines changed

nanoFramework.CoreLibrary/System/String.cs

Lines changed: 4 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -718,212 +718,14 @@ public static string IsInterned(string str)
718718
#if NANOCLR_REFLECTION
719719

720720
#nullable enable
721-
public static string Format(string format, params object?[] args)
721+
[MethodImpl(MethodImplOptions.InternalCall)]
722+
public static extern string Format(string format, params object?[] args);
722723
#nullable restore
723724

724725
#else
725-
public static string Format(string format, params object[] args)
726+
[MethodImpl(MethodImplOptions.InternalCall)]
727+
public static extern string Format(string format, params object[] args);
726728
#endif
727-
{
728-
var index = 0;
729-
var alignment = 0;
730-
var chr = '\0';
731-
var len = format.Length;
732-
var fmt = Empty;
733-
var token = Empty;
734-
var output = Empty;
735-
736-
if (format is null)
737-
{
738-
throw new ArgumentNullException("format can't be null");
739-
}
740-
741-
for (var i = 0; i < len; i++)
742-
{
743-
token = Empty;
744-
chr = format[i];
745-
746-
if (chr == '{')
747-
{
748-
if (i + 1 == len)
749-
{
750-
throw new ArgumentException("Format error: no closed brace, column " + i);
751-
}
752-
753-
if (format[i + 1] == '{')
754-
{
755-
output += chr;
756-
i++;
757-
continue;
758-
}
759-
else
760-
{
761-
alignment = 0;
762-
fmt = Empty;
763-
764-
for (i++; i < len; i++)
765-
{
766-
chr = format[i];
767-
768-
if (chr >= '0' && chr <= '9')
769-
{
770-
token += chr;
771-
}
772-
else if (chr == ',' || chr == ':' || chr == '}')
773-
{
774-
break;
775-
}
776-
else
777-
{
778-
throw new ArgumentException("Format error: wrong symbol at {}, column " + i);
779-
}
780-
}
781-
782-
if (token.Length > 0)
783-
{
784-
index = int.Parse(token);
785-
}
786-
else
787-
{
788-
throw new ArgumentException("Format error: empty {}, column " + i);
789-
}
790-
791-
if (chr == ',')
792-
{
793-
if (format[i + 1] == '-')
794-
{
795-
token = "-";
796-
i++;
797-
}
798-
else
799-
{
800-
token = Empty;
801-
}
802-
803-
for (i++; i < len; i++)
804-
{
805-
chr = format[i];
806-
807-
if (chr >= '0' && chr <= '9')
808-
{
809-
token += chr;
810-
}
811-
else if (chr == ':' || chr == '}')
812-
{
813-
break;
814-
}
815-
else
816-
{
817-
throw new ArgumentException("Format error: wrong symbol at alignment, column " + i);
818-
}
819-
}
820-
821-
if (token.Length > 0)
822-
{
823-
alignment = int.Parse(token);
824-
}
825-
else
826-
{
827-
throw new ArgumentException("Format error: empty alignment, column " + i);
828-
}
829-
}
830-
831-
if (chr == ':')
832-
{
833-
token = Empty;
834-
for (i++; i < len; i++)
835-
{
836-
chr = format[i];
837-
838-
if (chr == '}')
839-
{
840-
break;
841-
}
842-
else
843-
{
844-
token += chr;
845-
}
846-
}
847-
848-
if (token.Length > 0)
849-
{
850-
fmt = token;
851-
}
852-
else
853-
{
854-
throw new ArgumentException("Format error: empty format after ':', column " + i);
855-
}
856-
}
857-
}
858-
859-
if (chr != '}')
860-
{
861-
throw new ArgumentException("Format error: no closed brace, column " + i);
862-
}
863-
864-
if (fmt.Length > 0)
865-
{
866-
#if NANOCLR_REFLECTION
867-
868-
if (args[index] is null)
869-
{
870-
token = "";
871-
}
872-
else
873-
{
874-
var method = args[index].GetType().GetMethod("ToString", new Type[] { typeof(string) });
875-
token = (method is null)
876-
? args[index].ToString()
877-
: method.Invoke(args[index], new object[] { token }).ToString();
878-
}
879-
#else
880-
throw new NotImplementedException();
881-
#endif // NANOCLR_REFLECTION
882-
883-
}
884-
else
885-
{
886-
token = args[index] == null ? "" : args[index].ToString();
887-
}
888-
889-
if (alignment > 0)
890-
{
891-
output += token.PadLeft(alignment);
892-
}
893-
else if (alignment < 0)
894-
{
895-
output += token.PadRight(MathInternal.Abs(alignment));
896-
}
897-
else
898-
{
899-
output += token;
900-
}
901-
}
902-
else if (chr == '}')
903-
{
904-
if (i + 1 == len)
905-
{
906-
throw new ArgumentException("Format error: no closed brace, column " + i);
907-
}
908-
909-
if (format[i + 1] == '}')
910-
{
911-
output += chr;
912-
i++;
913-
}
914-
else
915-
{
916-
throw new ArgumentException("Format error: no closed brace, column " + i);
917-
}
918-
}
919-
else
920-
{
921-
output += chr;
922-
}
923-
}
924-
925-
return output;
926-
}
927729

928730
/// <summary>
929731
/// 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.

0 commit comments

Comments
 (0)