public MatchChar ( char c ) : bool | ||
c | char | The character that must match. |
Результат | bool |
private static void CheckDateTimeStamp( DateTimeStamp t ) { string s = t.ToString(); var m = new StringMatcher( "X" + s + "Y" ); Assert.That( m.MatchChar( 'X' ) ); DateTimeStamp parsed; Assert.That( m.MatchDateTimeStamp( out parsed ) && parsed == t ); Assert.That( m.MatchChar( 'Y' ) ); m = new StringMatcher( s.Insert( 2, "X" ) ); Assert.That( m.MatchDateTimeStamp( out parsed ), Is.False ); Assert.That( m.ErrorMessage, Is.Not.Null ); int i; Assert.That( m.MatchInt32( out i ) && i == 20 ); }
public void matching_FileNameUniqueTimeUtcFormat() { DateTime t = DateTime.UtcNow; string s = t.ToString( FileUtil.FileNameUniqueTimeUtcFormat ); var m = new StringMatcher( "X" + s + "Y" ); Assert.That( m.MatchChar( 'X' ) ); DateTime parsed; Assert.That( m.MatchFileNameUniqueTimeUtcFormat( out parsed ) && parsed == t ); Assert.That( m.MatchChar( 'Y' ) ); m = new StringMatcher( s.Insert( 2, "X" ) ); Assert.That( m.MatchFileNameUniqueTimeUtcFormat( out parsed ), Is.False ); int i; Assert.That( m.MatchInt32( out i ) && i == 20 ); }
/// <summary> /// Visits a comma seprarated list of "property" : ... fields until a closing } is found /// or <see cref="Matcher"/>.<see cref="StringMatcher.IsEnd">IsEnd</see> becomes true. /// </summary> /// <returns>True on success. On error a message may be retrieved from the <see cref="Matcher"/>.</returns> public virtual bool VisitObjectContent() { int propertyNumber = 0; while (!_m.IsEnd) { SkipWhiteSpaces(); if (_m.TryMatchChar('}')) { return(true); } int startPropertyIndex = _m.StartIndex; string propName; if (!_m.TryMatchJSONQuotedString(out propName)) { return(false); } SkipWhiteSpaces(); if (!_m.MatchChar(':') || !VisitObjectProperty(startPropertyIndex, propName, propertyNumber)) { return(false); } SkipWhiteSpaces(); // This accepts e trailing comma at the end of a property list: ..."a":0,} is not an error. _m.TryMatchChar(','); ++propertyNumber; } return(false); }
/// <summary> /// Matches a very simple version of a JSON object content: this match stops at the first closing }. /// Whitespaces and JS comments (//... or /* ... */) are skipped. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <param name="o">The read object on success as a list of KeyValuePair.</param> /// <returns>True on success, false on error.</returns> public static bool MatchJSONObjectContent(this StringMatcher @this, out List <KeyValuePair <string, object> > o) { o = null; while ([email protected]) { @this.SkipWhiteSpacesAndJSComments(); string propName; object value; if (@this.TryMatchChar('}')) { if (o == null) { o = new List <KeyValuePair <string, object> >(); } return(true); } if ([email protected](out propName)) { o = null; return(@this.SetError("Quoted Property Name.")); } @this.SkipWhiteSpacesAndJSComments(); if ([email protected](':') || !MatchJSONObject(@this, out value)) { o = null; return(false); } if (o == null) { o = new List <KeyValuePair <string, object> >(); } o.Add(new KeyValuePair <string, object>(propName, value)); @this.SkipWhiteSpacesAndJSComments(); // This accepts e trailing comma at the end of a property list: ..."a":0,} is not an error. @this.TryMatchChar(','); } return(@this.SetError("JSON object definition but reached end of match.")); }