Ejemplo n.º 1
0
        //
        // Titlecasing:
        // -----------
        // Titlecasing refers to a casing practice wherein the first letter of a word is an uppercase letter
        // and the rest of the letters are lowercase.  The choice of which words to titlecase in headings
        // and titles is dependent on language and local conventions.  For example, "The Merry Wives of Windor"
        // is the appropriate titlecasing of that play's name in English, with the word "of" not titlecased.
        // In German, however, the title is "Die lustigen Weiber von Windsor," and both "lustigen" and "von"
        // are not titlecased.  In French even fewer words are titlecased: "Les joyeuses commeres de Windsor."
        //
        // Moreover, the determination of what actually constitutes a word is language dependent, and this can
        // influence which letter or letters of a "word" are uppercased when titlecasing strings.  For example
        // "l'arbre" is considered two words in French, whereas "can't" is considered one word in English.
        //
        public unsafe String ToTitleCase(String str)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }
            Contract.EndContractBlock();
            if (str.Length == 0)
            {
                return(str);
            }

            StringBuilder result        = new StringBuilder();
            string        lowercaseData = null;

            for (int i = 0; i < str.Length; i++)
            {
                UnicodeCategory charType;
                int             charLen;

                charType = CharUnicodeInfo.InternalGetUnicodeCategory(str, i, out charLen);
                if (Char.CheckLetter(charType))
                {
                    // Do the titlecasing for the first character of the word.
                    i = AddTitlecaseLetter(ref result, ref str, i, charLen) + 1;

                    //
                    // Convert the characters until the end of the this word
                    // to lowercase.
                    //
                    int lowercaseStart = i;

                    //
                    // Use hasLowerCase flag to prevent from lowercasing acronyms (like "URT", "USA", etc)
                    // This is in line with Word 2000 behavior of titlecasing.
                    //
                    bool hasLowerCase = (charType == UnicodeCategory.LowercaseLetter);
                    // Use a loop to find all of the other letters following this letter.
                    while (i < str.Length)
                    {
                        charType = CharUnicodeInfo.InternalGetUnicodeCategory(str, i, out charLen);
                        if (IsLetterCategory(charType))
                        {
                            if (charType == UnicodeCategory.LowercaseLetter)
                            {
                                hasLowerCase = true;
                            }
                            i += charLen;
                        }
                        else if (str[i] == '\'')
                        {
                            i++;
                            if (hasLowerCase)
                            {
                                if (lowercaseData == null)
                                {
                                    lowercaseData = this.ToLower(str);
                                }
                                result.Append(lowercaseData, lowercaseStart, i - lowercaseStart);
                            }
                            else
                            {
                                result.Append(str, lowercaseStart, i - lowercaseStart);
                            }
                            lowercaseStart = i;
                            hasLowerCase   = true;
                        }
                        else if (!IsWordSeparator(charType))
                        {
                            // This category is considered to be part of the word.
                            // This is any category that is marked as false in wordSeprator array.
                            i += charLen;
                        }
                        else
                        {
                            // A word separator. Break out of the loop.
                            break;
                        }
                    }

                    int count = i - lowercaseStart;

                    if (count > 0)
                    {
                        if (hasLowerCase)
                        {
                            if (lowercaseData == null)
                            {
                                lowercaseData = this.ToLower(str);
                            }
                            result.Append(lowercaseData, lowercaseStart, count);
                        }
                        else
                        {
                            result.Append(str, lowercaseStart, count);
                        }
                    }

                    if (i < str.Length)
                    {
                        // not a letter, just append it
                        i = AddNonLetter(ref result, ref str, i, charLen);
                    }
                }
                else
                {
                    // not a letter, just append it
                    i = AddNonLetter(ref result, ref str, i, charLen);
                }
            }
            return(result.ToString());
        }
Ejemplo n.º 2
0
        private string punycode_encode(string unicode)
        {
            StringBuilder builder    = new StringBuilder(unicode.Length);
            int           num        = 0;
            int           startIndex = 0;
            int           length     = 0;

            if (unicode.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
            }
            while (num < unicode.Length)
            {
                num = unicode.IndexOfAny(M_Dots, startIndex);
                if (num < 0)
                {
                    num = unicode.Length;
                }
                if (num == startIndex)
                {
                    if (num != unicode.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    break;
                }
                builder.Append("xn--");
                bool         flag         = false;
                BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, startIndex);
                switch (bidiCategory)
                {
                case BidiCategory.RightToLeft:
                case BidiCategory.RightToLeftArabic:
                {
                    flag = true;
                    int index = num - 1;
                    if (char.IsLowSurrogate(unicode, index))
                    {
                        index--;
                    }
                    bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, index);
                    if ((bidiCategory != BidiCategory.RightToLeft) && (bidiCategory != BidiCategory.RightToLeftArabic))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                    break;
                }
                }
                int num6 = 0;
                for (int i = startIndex; i < num; i++)
                {
                    BidiCategory category2 = CharUnicodeInfo.GetBidiCategory(unicode, i);
                    if (flag && (category2 == BidiCategory.LeftToRight))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                    if (!flag && ((category2 == BidiCategory.RightToLeft) || (category2 == BidiCategory.RightToLeftArabic)))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                    }
                    if (basic(unicode[i]))
                    {
                        builder.Append(encode_basic(unicode[i]));
                        num6++;
                    }
                    else if (char.IsSurrogatePair(unicode, i))
                    {
                        i++;
                    }
                }
                int num7 = num6;
                if (num7 == (num - startIndex))
                {
                    builder.Remove(length, "xn--".Length);
                }
                else
                {
                    if (((unicode.Length - startIndex) >= "xn--".Length) && unicode.Substring(startIndex, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "unicode");
                    }
                    int num8 = 0;
                    if (num7 > 0)
                    {
                        builder.Append('-');
                    }
                    int num9  = 0x80;
                    int delta = 0;
                    int num11 = 0x48;
                    while (num6 < (num - startIndex))
                    {
                        int cTest = 0;
                        int num13 = 0x7ffffff;
                        int num12 = startIndex;
                        while (num12 < num)
                        {
                            cTest = char.ConvertToUtf32(unicode, num12);
                            if ((cTest >= num9) && (cTest < num13))
                            {
                                num13 = cTest;
                            }
                            num12 += this.IsSupplementary(cTest) ? 2 : 1;
                        }
                        delta += (num13 - num9) * ((num6 - num8) + 1);
                        num9   = num13;
                        for (num12 = startIndex; num12 < num; num12 += this.IsSupplementary(cTest) ? 2 : 1)
                        {
                            cTest = char.ConvertToUtf32(unicode, num12);
                            if (cTest < num9)
                            {
                                delta++;
                            }
                            if (cTest == num9)
                            {
                                int d     = delta;
                                int num16 = 0x24;
                                while (true)
                                {
                                    int num17 = (num16 <= num11) ? 1 : ((num16 >= (num11 + 0x1a)) ? 0x1a : (num16 - num11));
                                    if (d < num17)
                                    {
                                        break;
                                    }
                                    builder.Append(encode_digit(num17 + ((d - num17) % (0x24 - num17))));
                                    d      = (d - num17) / (0x24 - num17);
                                    num16 += 0x24;
                                }
                                builder.Append(encode_digit(d));
                                num11 = adapt(delta, (num6 - num8) + 1, num6 == num7);
                                delta = 0;
                                num6++;
                                if (this.IsSupplementary(num13))
                                {
                                    num6++;
                                    num8++;
                                }
                            }
                        }
                        delta++;
                        num9++;
                    }
                }
                if ((builder.Length - length) > 0x3f)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                }
                if (num != unicode.Length)
                {
                    builder.Append('.');
                }
                startIndex = num + 1;
                length     = builder.Length;
            }
            if (builder.Length > (0xff - (this.IsDot(unicode[unicode.Length - 1]) ? 0 : 1)))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[] { 0xff - (this.IsDot(unicode[unicode.Length - 1]) ? 0 : 1) }), "unicode");
            }
            return(builder.ToString());
        }
Ejemplo n.º 3
0
        // Token: 0x06002FE3 RID: 12259 RVA: 0x000B7D34 File Offset: 0x000B5F34
        private static string punycode_decode(string ascii)
        {
            if (ascii.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
            }
            if (ascii.Length > 255 - (IdnMapping.IsDot(ascii[ascii.Length - 1]) ? 0 : 1))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[]
                {
                    255 - (IdnMapping.IsDot(ascii[ascii.Length - 1]) ? 0 : 1)
                }), "ascii");
            }
            StringBuilder stringBuilder = new StringBuilder(ascii.Length);
            int           i             = 0;
            int           num           = 0;
            int           num2          = 0;

            while (i < ascii.Length)
            {
                i = ascii.IndexOf('.', num);
                if (i < 0 || i > ascii.Length)
                {
                    i = ascii.Length;
                }
                if (i == num)
                {
                    if (i != ascii.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                    }
                    break;
                }
                else
                {
                    if (i - num > 63)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                    }
                    if (ascii.Length < "xn--".Length + num || !ascii.Substring(num, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                    {
                        stringBuilder.Append(ascii.Substring(num, i - num));
                    }
                    else
                    {
                        num += "xn--".Length;
                        int num3 = ascii.LastIndexOf('-', i - 1);
                        if (num3 == i - 1)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                        }
                        int num4;
                        if (num3 <= num)
                        {
                            num4 = 0;
                        }
                        else
                        {
                            num4 = num3 - num;
                            for (int j = num; j < num + num4; j++)
                            {
                                if (ascii[j] > '\u007f')
                                {
                                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                }
                                stringBuilder.Append((ascii[j] >= 'A' && ascii[j] <= 'Z') ? (ascii[j] - 'A' + 'a') : ascii[j]);
                            }
                        }
                        int k    = num + ((num4 > 0) ? (num4 + 1) : 0);
                        int num5 = 128;
                        int num6 = 72;
                        int num7 = 0;
                        int num8 = 0;
IL_40D:
                        while (k < i)
                        {
                            int num9  = num7;
                            int num10 = 1;
                            int num11 = 36;
                            while (k < i)
                            {
                                int num12 = IdnMapping.decode_digit(ascii[k++]);
                                if (num12 > (134217727 - num7) / num10)
                                {
                                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                }
                                num7 += num12 * num10;
                                int num13 = (num11 <= num6) ? 1 : ((num11 >= num6 + 26) ? 26 : (num11 - num6));
                                if (num12 >= num13)
                                {
                                    if (num10 > 134217727 / (36 - num13))
                                    {
                                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                    }
                                    num10 *= 36 - num13;
                                    num11 += 36;
                                }
                                else
                                {
                                    num6 = IdnMapping.adapt(num7 - num9, stringBuilder.Length - num2 - num8 + 1, num9 == 0);
                                    if (num7 / (stringBuilder.Length - num2 - num8 + 1) > 134217727 - num5)
                                    {
                                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                    }
                                    num5 += num7 / (stringBuilder.Length - num2 - num8 + 1);
                                    num7 %= stringBuilder.Length - num2 - num8 + 1;
                                    if (num5 < 0 || num5 > 1114111 || (num5 >= 55296 && num5 <= 57343))
                                    {
                                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                    }
                                    string value = char.ConvertFromUtf32(num5);
                                    int    num14;
                                    if (num8 > 0)
                                    {
                                        int l = num7;
                                        num14 = num2;
                                        while (l > 0)
                                        {
                                            if (num14 >= stringBuilder.Length)
                                            {
                                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                            }
                                            if (char.IsSurrogate(stringBuilder[num14]))
                                            {
                                                num14++;
                                            }
                                            l--;
                                            num14++;
                                        }
                                    }
                                    else
                                    {
                                        num14 = num2 + num7;
                                    }
                                    stringBuilder.Insert(num14, value);
                                    if (IdnMapping.IsSupplementary(num5))
                                    {
                                        num8++;
                                    }
                                    num7++;
                                    goto IL_40D;
                                }
                            }
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                        }
                        bool         flag         = false;
                        BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(stringBuilder.ToString(), num2);
                        if (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)
                        {
                            flag = true;
                        }
                        for (int m = num2; m < stringBuilder.Length; m++)
                        {
                            if (!char.IsLowSurrogate(stringBuilder.ToString(), m))
                            {
                                bidiCategory = CharUnicodeInfo.GetBidiCategory(stringBuilder.ToString(), m);
                                if ((flag && bidiCategory == BidiCategory.LeftToRight) || (!flag && (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)))
                                {
                                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
                                }
                            }
                        }
                        if (flag && bidiCategory != BidiCategory.RightToLeft && bidiCategory != BidiCategory.RightToLeftArabic)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
                        }
                    }
                    if (i - num > 63)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                    }
                    if (i != ascii.Length)
                    {
                        stringBuilder.Append('.');
                    }
                    num  = i + 1;
                    num2 = stringBuilder.Length;
                }
            }
            if (stringBuilder.Length > 255 - (IdnMapping.IsDot(stringBuilder[stringBuilder.Length - 1]) ? 0 : 1))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[]
                {
                    255 - (IdnMapping.IsDot(stringBuilder[stringBuilder.Length - 1]) ? 0 : 1)
                }), "ascii");
            }
            return(stringBuilder.ToString());
        }
Ejemplo n.º 4
0
        private string punycode_decode(string ascii)
        {
            if (ascii.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
            }
            if (ascii.Length > (0xff - (this.IsDot(ascii[ascii.Length - 1]) ? 0 : 1)))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[] { 0xff - (this.IsDot(ascii[ascii.Length - 1]) ? 0 : 1) }), "ascii");
            }
            StringBuilder builder    = new StringBuilder(ascii.Length);
            int           index      = 0;
            int           startIndex = 0;

            for (int i = 0; index < ascii.Length; i = builder.Length)
            {
                index = ascii.IndexOf('.', startIndex);
                if ((index < 0) || (index > ascii.Length))
                {
                    index = ascii.Length;
                }
                if (index == startIndex)
                {
                    if (index != ascii.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                    }
                    break;
                }
                if ((index - startIndex) > 0x3f)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                }
                if ((ascii.Length < ("xn--".Length + startIndex)) || !ascii.Substring(startIndex, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                {
                    builder.Append(ascii.Substring(startIndex, index - startIndex));
                }
                else
                {
                    int num5;
                    startIndex += "xn--".Length;
                    int num4 = ascii.LastIndexOf('-', index - 1);
                    if (num4 == (index - 1))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                    }
                    if (num4 <= startIndex)
                    {
                        num5 = 0;
                    }
                    else
                    {
                        num5 = num4 - startIndex;
                        for (int k = startIndex; k < (startIndex + num5); k++)
                        {
                            if (ascii[k] > '\x007f')
                            {
                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                            }
                            builder.Append(((ascii[k] >= 'A') && (ascii[k] <= 'Z')) ? ((char)((ascii[k] - 'A') + 0x61)) : ascii[k]);
                        }
                    }
                    int num7  = startIndex + ((num5 > 0) ? (num5 + 1) : 0);
                    int num8  = 0x80;
                    int num9  = 0x48;
                    int num10 = 0;
                    int num13 = 0;
                    while (num7 < index)
                    {
                        int num17;
                        int num14 = num10;
                        int num11 = 1;
                        int num12 = 0x24;
                        while (true)
                        {
                            if (num7 >= index)
                            {
                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                            }
                            int num15 = decode_digit(ascii[num7++]);
                            if (num15 > ((0x7ffffff - num10) / num11))
                            {
                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                            }
                            num10 += num15 * num11;
                            int num16 = (num12 <= num9) ? 1 : ((num12 >= (num9 + 0x1a)) ? 0x1a : (num12 - num9));
                            if (num15 < num16)
                            {
                                break;
                            }
                            if (num11 > (0x7ffffff / (0x24 - num16)))
                            {
                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                            }
                            num11 *= 0x24 - num16;
                            num12 += 0x24;
                        }
                        num9 = adapt(num10 - num14, ((builder.Length - i) - num13) + 1, num14 == 0);
                        if ((num10 / (((builder.Length - i) - num13) + 1)) > (0x7ffffff - num8))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                        }
                        num8 += num10 / (((builder.Length - i) - num13) + 1);
                        num10 = num10 % (((builder.Length - i) - num13) + 1);
                        if (((num8 < 0) || (num8 > 0x10ffff)) || ((num8 >= 0xd800) && (num8 <= 0xdfff)))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                        }
                        string str = char.ConvertFromUtf32(num8);
                        if (num13 > 0)
                        {
                            int num18 = num10;
                            num17 = i;
                            while (num18 > 0)
                            {
                                if (num17 >= builder.Length)
                                {
                                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
                                }
                                if (char.IsSurrogate(builder[num17]))
                                {
                                    num17++;
                                }
                                num18--;
                                num17++;
                            }
                        }
                        else
                        {
                            num17 = i + num10;
                        }
                        builder.Insert(num17, str);
                        if (this.IsSupplementary(num8))
                        {
                            num13++;
                        }
                        num10++;
                    }
                    bool         flag         = false;
                    BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(builder.ToString(), i);
                    switch (bidiCategory)
                    {
                    case BidiCategory.RightToLeft:
                    case BidiCategory.RightToLeftArabic:
                        flag = true;
                        break;
                    }
                    for (int j = i; j < builder.Length; j++)
                    {
                        if (!char.IsLowSurrogate(builder.ToString(), j))
                        {
                            bidiCategory = CharUnicodeInfo.GetBidiCategory(builder.ToString(), j);
                            if ((flag && (bidiCategory == BidiCategory.LeftToRight)) || (!flag && ((bidiCategory == BidiCategory.RightToLeft) || (bidiCategory == BidiCategory.RightToLeftArabic))))
                            {
                                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
                            }
                        }
                    }
                    if ((flag && (bidiCategory != BidiCategory.RightToLeft)) && (bidiCategory != BidiCategory.RightToLeftArabic))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
                    }
                }
                if ((index - startIndex) > 0x3f)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
                }
                if (index != ascii.Length)
                {
                    builder.Append('.');
                }
                startIndex = index + 1;
            }
            if (builder.Length > (0xff - (this.IsDot(builder[builder.Length - 1]) ? 0 : 1)))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[] { 0xff - (this.IsDot(builder[builder.Length - 1]) ? 0 : 1) }), "ascii");
            }
            return(builder.ToString());
        }
Ejemplo n.º 5
0
        // Token: 0x06002FE2 RID: 12258 RVA: 0x000B7924 File Offset: 0x000B5B24
        private static string punycode_encode(string unicode)
        {
            if (unicode.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
            }
            StringBuilder stringBuilder = new StringBuilder(unicode.Length);
            int           i             = 0;
            int           num           = 0;
            int           num2          = 0;

            while (i < unicode.Length)
            {
                i = unicode.IndexOfAny(IdnMapping.M_Dots, num);
                if (i < 0)
                {
                    i = unicode.Length;
                }
                if (i == num)
                {
                    if (i != unicode.Length)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    break;
                }
                else
                {
                    stringBuilder.Append("xn--");
                    bool         flag         = false;
                    BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num);
                    if (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)
                    {
                        flag = true;
                        int num3 = i - 1;
                        if (char.IsLowSurrogate(unicode, num3))
                        {
                            num3--;
                        }
                        bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num3);
                        if (bidiCategory != BidiCategory.RightToLeft && bidiCategory != BidiCategory.RightToLeftArabic)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                    }
                    int j = 0;
                    for (int k = num; k < i; k++)
                    {
                        BidiCategory bidiCategory2 = CharUnicodeInfo.GetBidiCategory(unicode, k);
                        if (flag && bidiCategory2 == BidiCategory.LeftToRight)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                        if (!flag && (bidiCategory2 == BidiCategory.RightToLeft || bidiCategory2 == BidiCategory.RightToLeftArabic))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
                        }
                        if (IdnMapping.basic((uint)unicode[k]))
                        {
                            stringBuilder.Append(IdnMapping.encode_basic(unicode[k]));
                            j++;
                        }
                        else if (char.IsSurrogatePair(unicode, k))
                        {
                            k++;
                        }
                    }
                    int num4 = j;
                    if (num4 == i - num)
                    {
                        stringBuilder.Remove(num2, "xn--".Length);
                    }
                    else
                    {
                        if (unicode.Length - num >= "xn--".Length && unicode.Substring(num, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "unicode");
                        }
                        int num5 = 0;
                        if (num4 > 0)
                        {
                            stringBuilder.Append('-');
                        }
                        int num6 = 128;
                        int num7 = 0;
                        int num8 = 72;
                        while (j < i - num)
                        {
                            int num9 = 134217727;
                            int num10;
                            for (int l = num; l < i; l += (IdnMapping.IsSupplementary(num10) ? 2 : 1))
                            {
                                num10 = char.ConvertToUtf32(unicode, l);
                                if (num10 >= num6 && num10 < num9)
                                {
                                    num9 = num10;
                                }
                            }
                            num7 += (num9 - num6) * (j - num5 + 1);
                            num6  = num9;
                            for (int l = num; l < i; l += (IdnMapping.IsSupplementary(num10) ? 2 : 1))
                            {
                                num10 = char.ConvertToUtf32(unicode, l);
                                if (num10 < num6)
                                {
                                    num7++;
                                }
                                if (num10 == num6)
                                {
                                    int num11 = num7;
                                    int num12 = 36;
                                    for (;;)
                                    {
                                        int num13 = (num12 <= num8) ? 1 : ((num12 >= num8 + 26) ? 26 : (num12 - num8));
                                        if (num11 < num13)
                                        {
                                            break;
                                        }
                                        stringBuilder.Append(IdnMapping.encode_digit(num13 + (num11 - num13) % (36 - num13)));
                                        num11  = (num11 - num13) / (36 - num13);
                                        num12 += 36;
                                    }
                                    stringBuilder.Append(IdnMapping.encode_digit(num11));
                                    num8 = IdnMapping.adapt(num7, j - num5 + 1, j == num4);
                                    num7 = 0;
                                    j++;
                                    if (IdnMapping.IsSupplementary(num9))
                                    {
                                        j++;
                                        num5++;
                                    }
                                }
                            }
                            num7++;
                            num6++;
                        }
                    }
                    if (stringBuilder.Length - num2 > 63)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
                    }
                    if (i != unicode.Length)
                    {
                        stringBuilder.Append('.');
                    }
                    num  = i + 1;
                    num2 = stringBuilder.Length;
                }
            }
            if (stringBuilder.Length > 255 - (IdnMapping.IsDot(unicode[unicode.Length - 1]) ? 0 : 1))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", new object[]
                {
                    255 - (IdnMapping.IsDot(unicode[unicode.Length - 1]) ? 0 : 1)
                }), "unicode");
            }
            return(stringBuilder.ToString());
        }