Avoid ToCharArray allocation in JsonCamelCaseNamingPolicy (dotnet/corefx#41363)
authorStephen Toub <stoub@microsoft.com>
Thu, 26 Sep 2019 21:30:31 +0000 (14:30 -0700)
committerGitHub <noreply@github.com>
Thu, 26 Sep 2019 21:30:31 +0000 (14:30 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/4cfc7ecdf44fee10b86477c67eb9c7d335cf3269

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonCamelCaseNamingPolicy.cs

index f29ed11..172d45d 100644 (file)
@@ -13,8 +13,21 @@ namespace System.Text.Json
                 return name;
             }
 
+#if BUILDING_INBOX_LIBRARY
+            return string.Create(name.Length, name, (chars, name) =>
+            {
+                name.AsSpan().CopyTo(chars);
+                FixCasing(chars);
+            });
+#else
             char[] chars = name.ToCharArray();
+            FixCasing(chars);
+            return new string(chars);
+#endif
+        }
 
+        private static void FixCasing(Span<char> chars)
+        {
             for (int i = 0; i < chars.Length; i++)
             {
                 if (i == 1 && !char.IsUpper(chars[i]))
@@ -38,8 +51,6 @@ namespace System.Text.Json
 
                 chars[i] = char.ToLowerInvariant(chars[i]);
             }
-
-            return new string(chars);
         }
     }
 }