Example #1
0
        /// <summary>Encodes the specified number of characters in a  substring of domain name labels that include Unicode characters outside the US-ASCII character range. The substring is converted to a string of displayable Unicode characters in the US-ASCII character range and is formatted according to the IDNA standard. </summary>
        /// <param name="unicode">The string to convert, which consists of one or more domain name labels delimited with label separators.</param>
        /// <param name="index">A zero-based offset into <paramref name="unicode" /> that specifies the start of the substring.</param>
        /// <param name="count">The number of characters to convert in the substring that starts at the position specified by  <paramref name="index" /> in the <paramref name="unicode" /> string. </param>
        /// <returns>The equivalent of the substring specified by the <paramref name="unicode" />, <paramref name="index" />, and <paramref name="count" /> parameters, consisting of displayable Unicode characters in the US-ASCII character range (U+0020 to U+007E) and formatted according to the IDNA standard.</returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///         <paramref name="unicode" /> is <see langword="null" />.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///         <paramref name="index" /> or <paramref name="count" /> is less than zero.-or-
        ///         <paramref name="index" /> is greater than the length of <paramref name="unicode" />.-or-
        ///         <paramref name="index" /> is greater than the length of <paramref name="unicode" /> minus <paramref name="count" />.</exception>
        /// <exception cref="T:System.ArgumentException">
        ///         <paramref name="unicode" /> is invalid based on the <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> and <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> properties, and the IDNA standard.</exception>
        // Token: 0x06002FD0 RID: 12240 RVA: 0x000B7130 File Offset: 0x000B5330
        public string GetAscii(string unicode, int index, int count)
        {
            if (unicode == null)
            {
                throw new ArgumentNullException("unicode");
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (index > unicode.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            if (index > unicode.Length - count)
            {
                throw new ArgumentOutOfRangeException("unicode", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            unicode = unicode.Substring(index, count);
            if (Environment.IsWindows8OrAbove)
            {
                return(this.GetAsciiUsingOS(unicode));
            }
            if (IdnMapping.ValidateStd3AndAscii(unicode, this.UseStd3AsciiRules, true))
            {
                return(unicode);
            }
            if (unicode[unicode.Length - 1] <= '\u001f')
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequence", new object[]
                {
                    unicode.Length - 1
                }), "unicode");
            }
            bool flag = unicode.Length > 0 && IdnMapping.IsDot(unicode[unicode.Length - 1]);

            unicode = unicode.Normalize(this.m_bAllowUnassigned ? ((NormalizationForm)13) : ((NormalizationForm)269));
            if (!flag && unicode.Length > 0 && IdnMapping.IsDot(unicode[unicode.Length - 1]))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
            }
            if (this.UseStd3AsciiRules)
            {
                IdnMapping.ValidateStd3AndAscii(unicode, true, false);
            }
            return(IdnMapping.punycode_encode(unicode));
        }
Example #2
0
        /// <summary>编码包含 US-ASCII 字符范围以外的 Unicode 字符的域名称标签子字符串的指定字符数。子串转换为在 US-ASCII 字符范围内可显示的“ Unicode ”字符串并根据 IDNA 标准格式化。</summary>
        /// <returns>由 <paramref name="unicode" />、<paramref name="index" /> 和 <paramref name="count" /> 参数指定的子字符串的等效项,包括 US-ASCII 字符范围(U+0020 至 U+007E)内的可显示 Unicode 字符并根据 IDNA 标准格式化。</returns>
        /// <param name="unicode">要转换的字符串,它包含一个或多个由标签分隔符分隔的域名标签。</param>
        /// <param name="index">
        /// <paramref name="unicode" /> 的从零开始的偏移量,用于指定子字符串的起始位置。</param>
        /// <param name="count">要在 <paramref name="unicode" /> 字符串中的 <paramref name="index" /> 指定的位置开始的子字符串中转换的字符数。</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="unicode" /> 为 null。</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// <paramref name="index" /> 或 <paramref name="count" /> 小于零。- 或 -<paramref name="index" /> 大于 <paramref name="unicode" /> 的长度。- 或 -<paramref name="index" /> 大于 <paramref name="unicode" /> 的长度减去 <paramref name="count" />。</exception>
        /// <exception cref="T:System.ArgumentException">根据 <see cref="P:System.Globalization.IdnMapping.AllowUnassigned" /> 和 <see cref="P:System.Globalization.IdnMapping.UseStd3AsciiRules" /> 属性以及 IDNA 标准,<paramref name="unicode" /> 是无效的。</exception>
        public string GetAscii(string unicode, int index, int count)
        {
            if (unicode == null)
            {
                throw new ArgumentNullException("unicode");
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (index > unicode.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            if (index > unicode.Length - count)
            {
                throw new ArgumentOutOfRangeException("unicode", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            unicode = unicode.Substring(index, count);
            if (Environment.IsWindows8OrAbove)
            {
                return(this.GetAsciiUsingOS(unicode));
            }
            if (IdnMapping.ValidateStd3AndAscii(unicode, this.UseStd3AsciiRules, true))
            {
                return(unicode);
            }
            string str1   = unicode;
            int    index1 = str1.Length - 1;

            if ((int)str1[index1] <= 31)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequence", (object)(unicode.Length - 1)), "unicode");
            }
            int num;

            if (unicode.Length > 0)
            {
                string str2   = unicode;
                int    index2 = str2.Length - 1;
                num = IdnMapping.IsDot(str2[index2]) ? 1 : 0;
            }
            else
            {
                num = 0;
            }
            unicode = unicode.Normalize(this.m_bAllowUnassigned ? (NormalizationForm)13 : (NormalizationForm)269);
            if (num == 0 && unicode.Length > 0)
            {
                string str2   = unicode;
                int    index2 = str2.Length - 1;
                if (IdnMapping.IsDot(str2[index2]))
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                }
            }
            if (this.UseStd3AsciiRules)
            {
                IdnMapping.ValidateStd3AndAscii(unicode, true, false);
            }
            return(IdnMapping.punycode_encode(unicode));
        }