System.DateTimeFormat.ParseQuoteString C# (CSharp) Méthode

ParseQuoteString() static private méthode

static private ParseQuoteString ( String format, int pos, StringBuilder result ) : int
format String
pos int
result StringBuilder
Résultat int
        internal static int ParseQuoteString(String format, int pos, StringBuilder result)
        {
            //
            // NOTE : pos will be the index of the quote character in the 'format' string.
            //
            int formatLen = format.Length;
            int beginPos = pos;
            char quoteChar = format[pos++]; // Get the character used to quote the following string.
    
            bool foundQuote = false;
            while (pos < formatLen)
            {
                char ch = format[pos++];        
                if (ch == quoteChar)
                {
                    foundQuote = true;
                    break;
                }
                else if (ch == '\\') {
                    // The following are used to support escaped character.
                    // Escaped character is also supported in the quoted string.
                    // Therefore, someone can use a format like "'minute:' mm\"" to display:
                    //  minute: 45"
                    // because the second double quote is escaped.
                    if (pos < formatLen) {
                        result.Append(format[pos++]);
                    } else {
                            //
                            // This means that '\' is at the end of the formatting string.
                            //
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                    }                    
                } else {
                    result.Append(ch);
                }
            }
            
            if (!foundQuote)
            {
                // Here we can't find the matching quote.
                throw new FormatException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            Environment.GetResourceString("Format_BadQuote"), quoteChar));
            }
            
            //
            // Return the character count including the begin/end quote characters and enclosed string.
            //
            return (pos - beginPos);
        }
    

Usage Example

        // Token: 0x060015E6 RID: 5606 RVA: 0x00040744 File Offset: 0x0003E944
        private static string FormatCustomized(DateTime dateTime, string format, DateTimeFormatInfo dtfi, TimeSpan offset)
        {
            Calendar      calendar      = dtfi.Calendar;
            StringBuilder stringBuilder = StringBuilderCache.Acquire(16);
            bool          flag          = calendar.ID == 8;
            bool          flag2         = calendar.ID == 3;
            bool          timeOnly      = true;
            int           i             = 0;

            while (i < format.Length)
            {
                char c = format[i];
                int  num2;
                if (c <= 'K')
                {
                    if (c <= '/')
                    {
                        if (c <= '%')
                        {
                            if (c != '"')
                            {
                                if (c != '%')
                                {
                                    goto IL_64F;
                                }
                                int num = DateTimeFormat.ParseNextChar(format, i);
                                if (num >= 0 && num != 37)
                                {
                                    stringBuilder.Append(DateTimeFormat.FormatCustomized(dateTime, ((char)num).ToString(), dtfi, offset));
                                    num2 = 2;
                                    goto IL_65B;
                                }
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                        }
                        else if (c != '\'')
                        {
                            if (c != '/')
                            {
                                goto IL_64F;
                            }
                            stringBuilder.Append(dtfi.DateSeparator);
                            num2 = 1;
                            goto IL_65B;
                        }
                        StringBuilder stringBuilder2 = new StringBuilder();
                        num2 = DateTimeFormat.ParseQuoteString(format, i, stringBuilder2);
                        stringBuilder.Append(stringBuilder2);
                    }
                    else if (c <= 'F')
                    {
                        if (c != ':')
                        {
                            if (c != 'F')
                            {
                                goto IL_64F;
                            }
                            goto IL_1E3;
                        }
                        else
                        {
                            stringBuilder.Append(dtfi.TimeSeparator);
                            num2 = 1;
                        }
                    }
                    else if (c != 'H')
                    {
                        if (c != 'K')
                        {
                            goto IL_64F;
                        }
                        num2 = 1;
                        DateTimeFormat.FormatCustomizedRoundripTimeZone(dateTime, offset, stringBuilder);
                    }
                    else
                    {
                        num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                        DateTimeFormat.FormatDigits(stringBuilder, dateTime.Hour, num2);
                    }
                }
                else if (c <= 'm')
                {
                    if (c <= '\\')
                    {
                        if (c != 'M')
                        {
                            if (c != '\\')
                            {
                                goto IL_64F;
                            }
                            int num = DateTimeFormat.ParseNextChar(format, i);
                            if (num < 0)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                            stringBuilder.Append((char)num);
                            num2 = 2;
                        }
                        else
                        {
                            num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            int month = calendar.GetMonth(dateTime);
                            if (num2 <= 2)
                            {
                                if (flag)
                                {
                                    DateTimeFormat.HebrewFormatDigits(stringBuilder, month);
                                }
                                else
                                {
                                    DateTimeFormat.FormatDigits(stringBuilder, month, num2);
                                }
                            }
                            else if (flag)
                            {
                                stringBuilder.Append(DateTimeFormat.FormatHebrewMonthName(dateTime, month, num2, dtfi));
                            }
                            else if ((dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != DateTimeFormatFlags.None && num2 >= 4)
                            {
                                stringBuilder.Append(dtfi.internalGetMonthName(month, DateTimeFormat.IsUseGenitiveForm(format, i, num2, 'd') ? MonthNameStyles.Genitive : MonthNameStyles.Regular, false));
                            }
                            else
                            {
                                stringBuilder.Append(DateTimeFormat.FormatMonth(month, num2, dtfi));
                            }
                            timeOnly = false;
                        }
                    }
                    else
                    {
                        switch (c)
                        {
                        case 'd':
                            num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            if (num2 <= 2)
                            {
                                int dayOfMonth = calendar.GetDayOfMonth(dateTime);
                                if (flag)
                                {
                                    DateTimeFormat.HebrewFormatDigits(stringBuilder, dayOfMonth);
                                }
                                else
                                {
                                    DateTimeFormat.FormatDigits(stringBuilder, dayOfMonth, num2);
                                }
                            }
                            else
                            {
                                int dayOfWeek = (int)calendar.GetDayOfWeek(dateTime);
                                stringBuilder.Append(DateTimeFormat.FormatDayOfWeek(dayOfWeek, num2, dtfi));
                            }
                            timeOnly = false;
                            break;

                        case 'e':
                            goto IL_64F;

                        case 'f':
                            goto IL_1E3;

                        case 'g':
                            num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            stringBuilder.Append(dtfi.GetEraName(calendar.GetEra(dateTime)));
                            break;

                        case 'h':
                        {
                            num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            int num3 = dateTime.Hour % 12;
                            if (num3 == 0)
                            {
                                num3 = 12;
                            }
                            DateTimeFormat.FormatDigits(stringBuilder, num3, num2);
                            break;
                        }

                        default:
                            if (c != 'm')
                            {
                                goto IL_64F;
                            }
                            num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            DateTimeFormat.FormatDigits(stringBuilder, dateTime.Minute, num2);
                            break;
                        }
                    }
                }
                else if (c <= 't')
                {
                    if (c != 's')
                    {
                        if (c != 't')
                        {
                            goto IL_64F;
                        }
                        num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                        if (num2 == 1)
                        {
                            if (dateTime.Hour < 12)
                            {
                                if (dtfi.AMDesignator.Length >= 1)
                                {
                                    stringBuilder.Append(dtfi.AMDesignator[0]);
                                }
                            }
                            else if (dtfi.PMDesignator.Length >= 1)
                            {
                                stringBuilder.Append(dtfi.PMDesignator[0]);
                            }
                        }
                        else
                        {
                            stringBuilder.Append((dateTime.Hour < 12) ? dtfi.AMDesignator : dtfi.PMDesignator);
                        }
                    }
                    else
                    {
                        num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                        DateTimeFormat.FormatDigits(stringBuilder, dateTime.Second, num2);
                    }
                }
                else if (c != 'y')
                {
                    if (c != 'z')
                    {
                        goto IL_64F;
                    }
                    num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                    DateTimeFormat.FormatCustomizedTimeZone(dateTime, offset, format, num2, timeOnly, stringBuilder);
                }
                else
                {
                    int year = calendar.GetYear(dateTime);
                    num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                    if (flag2 && !AppContextSwitches.FormatJapaneseFirstYearAsANumber && year == 1 && ((i + num2 < format.Length && format[i + num2] == "年"[0]) || (i + num2 < format.Length - 1 && format[i + num2] == '\'' && format[i + num2 + 1] == "年"[0])))
                    {
                        stringBuilder.Append("元"[0]);
                    }
                    else if (dtfi.HasForceTwoDigitYears)
                    {
                        DateTimeFormat.FormatDigits(stringBuilder, year, (num2 <= 2) ? num2 : 2);
                    }
                    else if (calendar.ID == 8)
                    {
                        DateTimeFormat.HebrewFormatDigits(stringBuilder, year);
                    }
                    else if (num2 <= 2)
                    {
                        DateTimeFormat.FormatDigits(stringBuilder, year % 100, num2);
                    }
                    else
                    {
                        string format2 = "D" + num2;
                        stringBuilder.Append(year.ToString(format2, CultureInfo.InvariantCulture));
                    }
                    timeOnly = false;
                }
IL_65B:
                i += num2;
                continue;
IL_1E3:
                num2 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                if (num2 > 7)
                {
                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                }
                long num4 = dateTime.Ticks % 10000000L;
                num4 /= (long)Math.Pow(10.0, (double)(7 - num2));
                if (c == 'f')
                {
                    stringBuilder.Append(((int)num4).ToString(DateTimeFormat.fixedNumberFormats[num2 - 1], CultureInfo.InvariantCulture));
                    goto IL_65B;
                }
                int num5 = num2;
                while (num5 > 0 && num4 % 10L == 0L)
                {
                    num4 /= 10L;
                    num5--;
                }
                if (num5 > 0)
                {
                    stringBuilder.Append(((int)num4).ToString(DateTimeFormat.fixedNumberFormats[num5 - 1], CultureInfo.InvariantCulture));
                    goto IL_65B;
                }
                if (stringBuilder.Length > 0 && stringBuilder[stringBuilder.Length - 1] == '.')
                {
                    stringBuilder.Remove(stringBuilder.Length - 1, 1);
                    goto IL_65B;
                }
                goto IL_65B;
IL_64F:
                stringBuilder.Append(c);
                num2 = 1;
                goto IL_65B;
            }
            return(StringBuilderCache.GetStringAndRelease(stringBuilder));
        }
All Usage Examples Of System.DateTimeFormat::ParseQuoteString