C# Класс CK.Text.StringMatcher

This class supports "Match and Forward" pattern. On a failed match, the SetError method sets the ErrorMessage. On a successful match, the StartIndex is updated by a call to Forward so that the Head is positioned after the match (and any existing error is cleared). There are 2 main kind of methods: TryMatchXXX that when the match fails returns false but do not call SetErrorand MatchXXX that do set an error on failure. This class does not actually hide/encapsulate a lot of things: it is designed to be extended through extension methods.
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
AddError ( object expectedMessage = null, bool beforeExisting = false, [ callerName = null ) : bool

Adds an error (the message starts with the caller's method name) to the exisitng ones (if any).

BackwardAddError ( int savedStartIndex, object expectedMessage = null, [ callerName = null ) : bool

Moves back the head at a previously index and adds an error as a consequence of any previous errors. The message starts with the caller's method name.

Forward ( int charCount ) : bool

Increments the StartIndex (and decrements Length) with the specified character count and clears any existing error. Always returns true.

MatchChar ( char c ) : bool

Matches an exact single character. If match fails, SetError is called.

MatchText ( string text, System.StringComparison comparisonType = StringComparison.OrdinalIgnoreCase ) : bool

Matches a text.

MatchWhiteSpaces ( int minCount = 1 ) : bool

Matches a sequence of white spaces.

SetError ( object expectedMessage = null, [ callerName = null ) : bool

Sets an error and always returns false. The message starts with the caller's method name. Use SetSuccess to clear any existing error.

SetSuccess ( ) : bool

Clears any error and returns true.

StringMatcher ( string text, int startIndex ) : System

Initializes a new instance of the StringMatcher class on a non null string.

StringMatcher ( string text, int startIndex, int length ) : System

Initializes a new instance of the StringMatcher class on a substring.

ToString ( ) : string

Overridden to return a detailed string with ErrorMessage (if any), the Head character, StartIndex position and whole Text.

TryMatchChar ( char c ) : bool

Attempts to match an exact single character.

TryMatchText ( string text, System.StringComparison comparisonType = StringComparison.OrdinalIgnoreCase ) : bool

Matches a text without setting an error if match fails.

UncheckedMove ( int delta ) : bool

Moves the head without any check and returns always true: typically called by successful TryMatchXXX methods. Can be used to move the head at any position in the Text (or outside it since NO checks are made).

Приватные методы

Метод Описание
FormatMessage ( object expectedMessage, string callerName ) : string

Описание методов

AddError() публичный Метод

Adds an error (the message starts with the caller's method name) to the exisitng ones (if any).
public AddError ( object expectedMessage = null, bool beforeExisting = false, [ callerName = null ) : bool
expectedMessage object /// Optional object. Its will be used to generate an "expected '...'" message. ///
beforeExisting bool /// True to add the error before the exisitng ones (as a consequence: [added] <-- [previous]), /// false to append it (as a cause: [previous] <-- [added])
callerName [ Name of the caller (automatically injected by the compiler).
Результат bool

BackwardAddError() публичный Метод

Moves back the head at a previously index and adds an error as a consequence of any previous errors. The message starts with the caller's method name.
public BackwardAddError ( int savedStartIndex, object expectedMessage = null, [ callerName = null ) : bool
savedStartIndex int Index to reset.
expectedMessage object /// Optional object. Its will be used to generate an "expected '...'" message. ///
callerName [ Name of the caller (automatically injected by the compiler).
Результат bool

Forward() публичный Метод

Increments the StartIndex (and decrements Length) with the specified character count and clears any existing error. Always returns true.
public Forward ( int charCount ) : bool
charCount int The successfully matched character count. /// Must be positive and should not move head past the end of the substring.
Результат bool

MatchChar() публичный Метод

Matches an exact single character. If match fails, SetError is called.
public MatchChar ( char c ) : bool
c char The character that must match.
Результат bool

MatchText() публичный Метод

Matches a text.
public MatchText ( string text, System.StringComparison comparisonType = StringComparison.OrdinalIgnoreCase ) : bool
text string The string that must match. Can not be null nor empty.
comparisonType System.StringComparison Specifies the culture, case, and sort rules.
Результат bool

MatchWhiteSpaces() публичный Метод

Matches a sequence of white spaces.
public MatchWhiteSpaces ( int minCount = 1 ) : bool
minCount int Minimal number of white spaces to match.
Результат bool

SetError() публичный Метод

Sets an error and always returns false. The message starts with the caller's method name. Use SetSuccess to clear any existing error.
public SetError ( object expectedMessage = null, [ callerName = null ) : bool
expectedMessage object /// Optional object. Its will be used to generate an "expected '...'" message. ///
callerName [ Name of the caller (automatically injected by the compiler).
Результат bool

SetSuccess() публичный Метод

Clears any error and returns true.
public SetSuccess ( ) : bool
Результат bool

StringMatcher() публичный Метод

Initializes a new instance of the StringMatcher class on a non null string.
public StringMatcher ( string text, int startIndex ) : System
text string The string to parse.
startIndex int Index where the match must start in .
Результат System

StringMatcher() публичный Метод

Initializes a new instance of the StringMatcher class on a substring.
public StringMatcher ( string text, int startIndex, int length ) : System
text string The string to parse. Can not be null.
startIndex int /// Index where the match must start in . ///
length int /// Number of characters to consider in the string. /// If + length is greater than the length of the string, an is thrown. ///
Результат System

ToString() публичный Метод

Overridden to return a detailed string with ErrorMessage (if any), the Head character, StartIndex position and whole Text.
public ToString ( ) : string
Результат string

TryMatchChar() публичный Метод

Attempts to match an exact single character.
public TryMatchChar ( char c ) : bool
c char The character that must match.
Результат bool

TryMatchText() публичный Метод

Matches a text without setting an error if match fails.
public TryMatchText ( string text, System.StringComparison comparisonType = StringComparison.OrdinalIgnoreCase ) : bool
text string The string that must match. Can not be null nor empty.
comparisonType System.StringComparison Specifies the culture, case, and sort rules.
Результат bool

UncheckedMove() публичный Метод

Moves the head without any check and returns always true: typically called by successful TryMatchXXX methods. Can be used to move the head at any position in the Text (or outside it since NO checks are made).
public UncheckedMove ( int delta ) : bool
delta int Number of characters.
Результат bool