Example #1
0
        /// <summary>对基于 IDNA 标准编码、具有指定长度并包含一个或者多个域名标签的子字符串进行解码,解码为一个 Unicode 字符串。</summary>
        /// <returns>由 <paramref name="ascii" />、<paramref name="index" /> 和 <paramref name="count" /> 参数指定的 IDNA 子字符串的 Unicode 等效项。</returns>
        /// <param name="ascii">要解码的字符串,包含根据 IDNA 标准在 US-ASCII 字符范围 (U+0020 至 U+007E) 编码的一个或多个标签。</param>
        /// <param name="index">
        /// <paramref name="ascii" /> 的从零开始的偏移量,用于指定子字符串的起始位置。</param>
        /// <param name="count">开始 <paramref name="ascii" /> 字符串中 <paramref name="index" /> 指定的位置的子字符串中要转换的字符数。</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="ascii" /> 为 null。</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// <paramref name="index" /> 或 <paramref name="count" /> 小于零。- 或 -<paramref name="index" /> 大于 <paramref name="ascii" /> 的长度。- 或 -<paramref name="index" /> 大于 <paramref name="ascii" /> 的长度减去 <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="ascii" /> 是无效的。</exception>
        public string GetUnicode(string ascii, int index, int count)
        {
            if (ascii == null)
            {
                throw new ArgumentNullException("ascii");
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (index > ascii.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            if (index > ascii.Length - count)
            {
                throw new ArgumentOutOfRangeException("ascii", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            if (count > 0 && (int)ascii[index + count - 1] == 0)
            {
                throw new ArgumentException("ascii", Environment.GetResourceString("Argument_IdnBadPunycode"));
            }
            ascii = ascii.Substring(index, count);
            if (Environment.IsWindows8OrAbove)
            {
                return(this.GetUnicodeUsingOS(ascii));
            }
            string unicode = IdnMapping.punycode_decode(ascii);

            if (!ascii.Equals(this.GetAscii(unicode), StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "ascii");
            }
            return(unicode);
        }