Ejemplo n.º 1
0
        // ---- SECTION:  private static methods that do the actual work ---------*
        #region TryParseTimeSpan
        //
        //  TryParseTimeSpan
        //
        //  Actions: Common private Parse method called by both Parse and TryParse
        // 
        private static Boolean TryParseTimeSpan(String input, TimeSpanStandardStyles style, IFormatProvider formatProvider, ref TimeSpanResult result) {
            if (input == null) {
                result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input));
                return false;
            }

            input = input.Trim();
            if (input == String.Empty) {
                result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                return false;
            }

            TimeSpanTokenizer tokenizer = new TimeSpanTokenizer();
            tokenizer.Init(input);

            TimeSpanRawInfo raw = new TimeSpanRawInfo();
            raw.Init(DateTimeFormatInfo.GetInstance(formatProvider));

            TimeSpanToken tok = tokenizer.GetNextToken();

            /* The following loop will break out when we reach the end of the str or
             * when we can determine that the input is invalid. */
            while (tok.ttt != TTT.End) {
                if (!raw.ProcessToken(ref tok, ref result)) {
                    result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                    return false;
                }
                tok = tokenizer.GetNextToken();
            }
            if (!tokenizer.EOL) {
                // embedded nulls in the input string
                result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                return false;
            }
            if (!ProcessTerminalState(ref raw, style, ref result)) {
                result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
                return false;
            }
            return true;
        }
 private static bool TryParseTimeSpan(string input, TimeSpanStandardStyles style, IFormatProvider formatProvider, ref TimeSpanResult 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_BadTimeSpan");
         return false;
     }
     TimeSpanTokenizer tokenizer = new TimeSpanTokenizer();
     tokenizer.Init(input);
     TimeSpanRawInfo raw = new TimeSpanRawInfo();
     raw.Init(DateTimeFormatInfo.GetInstance(formatProvider));
     for (TimeSpanToken token = tokenizer.GetNextToken(); token.ttt != TTT.End; token = tokenizer.GetNextToken())
     {
         if (!raw.ProcessToken(ref token, ref result))
         {
             result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
             return false;
         }
     }
     if (!tokenizer.EOL)
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
         return false;
     }
     if (!ProcessTerminalState(ref raw, style, ref result))
     {
         result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan");
         return false;
     }
     return true;
 }