summary refs log tree commit diff
path: root/crypto/src/asn1/DerBMPString.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-27 20:00:46 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-27 20:00:46 +0700
commitca24841d2e00c51a0a52df522139fcb096e0995f (patch)
treeea5d86bd2127a80ecc64a0b44f8b114f67291aef /crypto/src/asn1/DerBMPString.cs
parentBcpg: update signature subpackets (diff)
downloadBouncyCastle.NET-ed25519-ca24841d2e00c51a0a52df522139fcb096e0995f.tar.xz
Use string.Create when available
Diffstat (limited to 'crypto/src/asn1/DerBMPString.cs')
-rw-r--r--crypto/src/asn1/DerBMPString.cs23
1 files changed, 23 insertions, 0 deletions
diff --git a/crypto/src/asn1/DerBMPString.cs b/crypto/src/asn1/DerBMPString.cs
index 284a4b830..bf21fa424 100644
--- a/crypto/src/asn1/DerBMPString.cs
+++ b/crypto/src/asn1/DerBMPString.cs
@@ -1,4 +1,7 @@
 using System;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Buffers;
+#endif
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -82,6 +85,16 @@ namespace Org.BouncyCastle.Asn1
                 throw new ArgumentException("malformed BMPString encoding encountered", "contents");
 
             int charLen = byteLen / 2;
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            m_str = string.Create(charLen, contents, (chars, bytes) =>
+            {
+                for (int i = 0; i < chars.Length; ++i)
+                {
+                    chars[i] = (char)((bytes[2 * i] << 8) | (bytes[2 * i + 1] & 0xff));
+                }
+            });
+#else
             char[] cs = new char[charLen];
 
             for (int i = 0; i != charLen; i++)
@@ -90,8 +103,10 @@ namespace Org.BouncyCastle.Asn1
             }
 
             m_str = new string(cs);
+#endif
         }
 
+#if !(NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
         internal DerBmpString(char[] str)
         {
             if (str == null)
@@ -99,6 +114,7 @@ namespace Org.BouncyCastle.Asn1
 
             m_str = new string(str);
         }
+#endif
 
         /**
          * basic constructor
@@ -157,10 +173,17 @@ namespace Org.BouncyCastle.Asn1
             return new DerBmpString(contents);
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        internal static DerBmpString CreatePrimitive<TState>(int length, TState state, SpanAction<char, TState> action)
+        {
+            return new DerBmpString(string.Create(length, state, action));
+        }
+#else
         internal static DerBmpString CreatePrimitive(char[] str)
         {
             // TODO[asn1] Asn1InputStream has a validator/converter that should be unified in this class somehow
             return new DerBmpString(str);
         }
+#endif
     }
 }