Net.Pkcs11Interop.Common.Pkcs11Uri.Extract C# (CSharp) Method

Extract() private method

Extracts PKCS#11 URI from text and removes all whitespaces
private Extract ( string text ) : string
text string Text that contains PKCS#11 URI
return string
        private string Extract(string text)
        {
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException("text");

            StringBuilder stringBuilder = new StringBuilder(text.Length);
            for (int i = 0; i < text.Length; i++)
                if (!char.IsWhiteSpace(text[i]))
                    stringBuilder.Append(text[i]);

            string uri = stringBuilder.ToString();

            int firstCharPosition = 0;
            int lastCharPosition = 0;

            firstCharPosition = uri.IndexOf(Pkcs11UriSpec.Pk11UriSchemeName + Pkcs11UriSpec.Pk11UriAndPathSeparator, StringComparison.Ordinal);
            if (firstCharPosition > 0)
            {
                if (uri[firstCharPosition - 1] == '"')
                {
                    uri = uri.Remove(0, firstCharPosition);
                    lastCharPosition = uri.IndexOf('"');
                }
                else if (uri[firstCharPosition - 1] == '<')
                {
                    uri = uri.Remove(0, firstCharPosition);
                    lastCharPosition = uri.IndexOf('>');
                }
                else
                    throw new Pkcs11UriException("URI is not delimited within double quotes or angle brackets");

                if (lastCharPosition < 0)
                    throw new Pkcs11UriException("URI is not correctly delimited within double quotes or angle brackets");

                uri = uri.Substring(0, lastCharPosition);
            }

            return uri;
        }