Ejemplo n.º 1
0
 private static bool TryParseExactMultipleNumberSpan(string input, string[] formats, IFormatProvider formatProvider, NumberSpanStyles styles, ref NumberSpanResult result)
 {
     if (input == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "input");
         return false;
     }
     if (formats == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "formats");
         return false;
     }
     if (input.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
         return false;
     }
     if (formats.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     for (int i = 0; i < formats.Length; i++)
     {
         if (formats[i] == null || formats[i].Length == 0)
         {
             result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
             return false;
         }
         NumberSpanResult result2 = new NumberSpanResult();
         result2.Init(NumberSpanThrowStyle.None);
         if (TryParseExactNumberSpan(input, formats[i], formatProvider, styles, ref result2))
         {
             result._parsedNumberSpan = result2._parsedNumberSpan;
             return true;
         }
     }
     result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
     return false;
 }
Ejemplo n.º 2
0
 private static bool ProcessTerminalState(ref NumberSpanRawInfo raw, NumberSpanStandardStyles style, ref NumberSpanResult result)
 {
     if (raw._lastSeenTTT == TTT.Num)
     {
         var tok = new NumberSpanToken
         {
             _ttt = TTT.Sep,
             _sep = string.Empty
         };
         if (!raw.ProcessToken(ref tok, ref result))
         {
             result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
             return false;
         }
     }
     switch (raw._numCount)
     {
         case 1: return ProcessTerminal_C(ref raw, style, ref result);
         case 2: return ProcessTerminal_CT(ref raw, style, ref result);
         case 3: return ProcessTerminal_FULL(ref raw, style, ref result);
     }
     result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
     return false;
 }
Ejemplo n.º 3
0
 private static bool TryParseNumberSpan(string input, NumberSpanStandardStyles style, IFormatProvider formatProvider, ref NumberSpanResult result)
 {
     if (input == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "input");
         return false;
     }
     input = input.Trim();
     if (input == string.Empty)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
         return false;
     }
     var tokenizer = new NumberSpanTokenizer();
     tokenizer.Init(input);
     var raw = new NumberSpanRawInfo();
     raw.Init(); //DateTimeFormatInfo.GetInstance(formatProvider));
     for (var token = tokenizer.GetNextToken(); token._ttt != TTT.End; token = tokenizer.GetNextToken())
         if (!raw.ProcessToken(ref token, ref result))
         {
             result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
             return false;
         }
     if (!tokenizer.EOL)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
         return false;
     }
     if (!ProcessTerminalState(ref raw, style, ref result))
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
         return false;
     }
     return true;
 }
Ejemplo n.º 4
0
 private static bool ProcessTerminal_CT(ref NumberSpanRawInfo raw, NumberSpanStandardStyles style, ref NumberSpanResult result)
 {
     if (raw._sepCount != 3 || raw._numCount != 2 || (style & NumberSpanStandardStyles.RequireFull) != NumberSpanStandardStyles.None)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
         return false;
     }
     bool invariant = (style & NumberSpanStandardStyles.Invariant) != NumberSpanStandardStyles.None;
     bool localized = (style & NumberSpanStandardStyles.Localized) != NumberSpanStandardStyles.None;
     bool positive = false;
     bool match = false;
     if (invariant)
     {
         if (raw.FullCTMatch(raw.PositiveInvariant))
         {
             match = true;
             positive = true;
         }
         if (!match && raw.FullCTMatch(raw.NegativeInvariant))
         {
             match = true;
             positive = false;
         }
     }
     if (localized)
     {
         if (!match && raw.FullCTMatch(raw.PositiveLocalized))
         {
             match = true;
             positive = true;
         }
         if (!match && raw.FullCTMatch(raw.NegativeLocalized))
         {
             match = true;
             positive = false;
         }
     }
     long num;
     if (match)
     {
         if (!TryNumberToTicks(positive, raw._numbers[0], raw._numbers[1], _zero, out num))
         {
             result.SetFailure(ParseFailureKind.Overflow, "Overflow_NumberSpanElementTooLarge");
             return false;
         }
         if (!positive)
         {
             num = -num;
             if (num > 0)
             {
                 result.SetFailure(ParseFailureKind.Overflow, "Overflow_NumberSpanElementTooLarge");
                 return false;
             }
         }
         result._parsedNumberSpan._ticks = num;
         return true;
     }
     result.SetFailure(ParseFailureKind.Format, "Format_BadNumberSpan");
     return false;
 }
Ejemplo n.º 5
0
 private static bool TryParseExactNumberSpan(string input, string format, IFormatProvider formatProvider, NumberSpanStyles styles, ref NumberSpanResult result)
 {
     if (input == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "input");
         return false;
     }
     if (format == null)
     {
         result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "format");
         return false;
     }
     if (format.Length == 0)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     //if (format.Length != 1)
     //    return TryParseByFormat(input, format, styles, ref result);
     NumberSpanStandardStyles none = NumberSpanStandardStyles.None;
     if (format[0] == 'c' || format[0] == 't' || format[0] == 'T')
         return TryParseNumberSpanConstant(input, ref result);
     if (format[0] == 'g')
         none = NumberSpanStandardStyles.Localized;
     else if (format[0] == 'G')
         none = NumberSpanStandardStyles.RequireFull | NumberSpanStandardStyles.Localized;
     else
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier");
         return false;
     }
     return TryParseNumberSpan(input, none, formatProvider, ref result);
 }