Use IndexOfAnyExceptInRange in ZipHelper.GetEncoding (#79357)
authorStephen Toub <stoub@microsoft.com>
Wed, 7 Dec 2022 21:51:16 +0000 (16:51 -0500)
committerGitHub <noreply@github.com>
Wed, 7 Dec 2022 21:51:16 +0000 (16:51 -0500)
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs

index c4f46c1..22d784e 100644 (file)
@@ -22,16 +22,14 @@ namespace System.IO.Compression
 
         internal static Encoding GetEncoding(string text)
         {
-            foreach (char c in text)
+            if (text.AsSpan().IndexOfAnyExceptInRange((char)32, (char)126) >= 0)
             {
                 // The Zip Format uses code page 437 when the Unicode bit is not set. This format
                 // is the same as ASCII for characters 32-126 but differs otherwise. If we can fit
                 // the string into CP437 then we treat ASCII as acceptable.
-                if (c > 126 || c < 32)
-                {
-                    return Encoding.UTF8;
-                }
+                return Encoding.UTF8;
             }
+
             return Encoding.ASCII;
         }