/** **************************************************************************************** * Returns the currently remaining string (without searching for further delimiter * characters).<br> * After this call #HasNext will return false and #Next will return a nulled Substring. * @param trimming Determines if the token is trimmed in respect to the white space * characters defined in field #Whitespaces. * Defaults to \c Whitespaces.Trim. * @return The rest of the original source string, which was not returned by #Next(), yet. ******************************************************************************************/ public Substring GetRest(Whitespaces trimming = enums.Whitespaces.Trim) { // set start, end and end of tokenizer Actual.Set(Rest); if (trimming == enums.Whitespaces.Trim) { Actual.Trim(Whitespaces); } Rest.SetNull(); return(Actual); }
/** **************************************************************************************** * Returns the next token, which is afterwards also available through field #Actual. * If no further token was available, the returned * \ref cs::aworx::lib::strings::Substring "Substring" will be 'nulled' * (see \ref cs::aworx::lib::strings::Substring::IsNull "Substring.IsNull"). * To prevent this, the availability of a next token should be * checked using method #HasNext(). * * For clarification, see the explanation and sample code in this classes documentation. * * @param trimming Determines if the token is trimmed in respect to the white space * characters defined in field #Whitespaces. * Defaults to \c Whitespaces.Trim. * @param newDelim The delimiter separates the tokens. Defaults to 0, which keeps the * current delimiter intact. * However, it a new delimiter can be provided for every next token. * @return true if a next token was available, false if not. ******************************************************************************************/ public Substring Next(Whitespaces trimming = enums.Whitespaces.Trim, char newDelim = '\0') { if (Rest.IsNull()) { Actual.SetNull(); return(Actual); } // change of delim? if (newDelim != '\0') { delim = newDelim; } // set buf, start and find end Actual.Buf = Rest.Buf; Actual.Start = Rest.Start; int nextDelimiter = Rest.IndexOf(delim); if (nextDelimiter >= 0) { Rest.Start += nextDelimiter + 1; Actual.End = Rest.Start - 2; } else { Actual.End = Rest.End; Rest.SetNull(); } // trim if (trimming == enums.Whitespaces.Trim) { Actual.TrimStart(Whitespaces); Actual.TrimEnd(Whitespaces); } return(Actual); }
//--------------------------------------------------------------------------------------------------------- //--- Test Tokenizer //--------------------------------------------------------------------------------------------------------- void tokenizerTest(string inputString, AString res, char delim, char newDelim, Whitespaces trim, int inpStart = -1, int inpEnd = -1) { Substring inp = new Substring(inputString); if (inpStart >= 0) { inp.Start = inpStart; } if (inpEnd >= 0) { inp.End = inpEnd; } res.Clear(); Tokenizer tok = new Tokenizer(inp, delim); while (tok.HasNext()) { tok.Next(trim).CopyTo(res, true); res._(newDelim); } }
public static void WhitespacesParserIncorrect() => AssertThat(() => Whitespaces.Parse(string.Empty)).ThrowsExactlyException <ParseException>();
public static void WhitespacesCorrect(string input) => AssertThat(Whitespaces.Parse(input)).IsEqualTo(input);
/** **************************************************************************************** * Checks if this object ends with the given string \p consumable. If it does, this * string is cut from the end of object. * * @param consumable The consumable string * @param sensitivity The sensitivity of the comparison. * @param trimBeforeConsume Determines if the string should be (right-) trimmed before the * consume operation. Defaults to \c Whitespaces.Keep. * @return \c true, if this object was starting with \p consumable and consequently the * string was cut. ******************************************************************************************/ public bool ConsumeFromEnd( Substring consumable, Case sensitivity = Case.Sensitive, Whitespaces trimBeforeConsume = Whitespaces.Keep ) { if ( trimBeforeConsume == Whitespaces.Trim ) TrimEnd(); if ( !EndsWith( consumable, sensitivity ) ) return false; ConsumeFromEnd( consumable.Length() ); return true; }
/** **************************************************************************************** * Checks if this object starts with the given string \p consumable. If it does, this * string is cut from this object. * * @param consumable The consumable string * @param sensitivity The sensitivity of the comparison. * @param trimBeforeConsume Determines if the string should be (left-) trimmed before the * consume operation. Defaults to \c Whitespaces.Keep. * @return \c true, if this object was starting with \p consumable and consequently the * string was cut. ******************************************************************************************/ public bool Consume( AString consumable, Case sensitivity = Case.Sensitive, Whitespaces trimBeforeConsume = Whitespaces.Keep ) { if ( trimBeforeConsume == Whitespaces.Trim ) TrimStart(); if ( !StartsWith( consumable, sensitivity ) ) return false; Consume( consumable.Length() ); return true; }
/** **************************************************************************************** * Checks if this object ends with the given character \p consumable. If it does, this * character is cut from the end of object. * * @param consumable The consumable character * @param sensitivity The sensitivity of the comparison. * @param trimBeforeConsume Determines if the string should be (right-) trimmed before the * consume operation. Defaults to \c Whitespaces.Keep. * @return \c true, if this object was starting with \p consumable and consequently the * string was cut by one. ******************************************************************************************/ public bool ConsumeFromEnd( char consumable, Case sensitivity = Case.Sensitive, Whitespaces trimBeforeConsume = Whitespaces.Keep ) { if ( trimBeforeConsume == Whitespaces.Trim ) TrimEnd(); if ( ( sensitivity == Case.Sensitive && CharAtEnd() != consumable ) || ( sensitivity == Case.Ignore && Char.ToUpper(CharAtEnd()) != Char.ToUpper(consumable) ) ) return false; End--; hash= 0; return true; }
//-------------------------------------------------------------------------------------------------- //--- Test Tokenizer //-------------------------------------------------------------------------------------------------- void tokenizerTest( string inputString, AString res, char delim, char newDelim, Whitespaces trim, int inpStart= -1, int inpEnd= -1 ) { Substring inp= new Substring( inputString ); if ( inpStart >= 0 ) inp.Start= inpStart; if ( inpEnd >= 0 ) inp.End= inpEnd; res.Clear(); Tokenizer tknzr= new Tokenizer( inp, delim ); while( tknzr.HasNext() ) { tknzr.Next(trim).CopyTo( res, true ); res._( newDelim ); } }
/** **************************************************************************************** * Returns the currently remaining string (without searching for further delimiter * characters).<br> * After this call #HasNext will return false and #Next will return a nulled Substring. * @param trimming Determines if the token is trimmed in respect to the white space * characters defined in field #Whitespaces. * Defaults to \c Whitespaces.Trim. * @return The rest of the original source string, which was not returned by #Next(), yet. ******************************************************************************************/ public Substring GetRest( Whitespaces trimming= enums.Whitespaces.Trim ) { // set start, end and end of tokenizer Actual.Set( Rest ); if ( trimming == enums.Whitespaces.Trim ) Actual.Trim(Whitespaces); Rest.SetNull(); return Actual; }
/** **************************************************************************************** * Returns the next token, which is afterwards also available through field #Actual. * If no further token was available, the returned * \ref cs::aworx::lib::strings::Substring "Substring" will be 'nulled' * (see \ref cs::aworx::lib::strings::Substring::IsNull "Substring.IsNull"). * To prevent this, the availability of a next token should be * checked using method #HasNext(). * * For clarification, see the explanation and sample code in this classes documentation. * * @param trimming Determines if the token is trimmed in respect to the white space * characters defined in field #Whitespaces. * Defaults to \c Whitespaces.Trim. * @param newDelim The delimiter separates the tokens. Defaults to 0, which keeps the * current delimiter intact. * A new delimiter can be provided for every next token. * @return true if a next token was available, false if not. ******************************************************************************************/ public Substring Next( Whitespaces trimming= enums.Whitespaces.Trim, char newDelim= '\0' ) { if ( Rest.IsNull() ) { Actual.SetNull(); return Actual; } // change of delim? if ( newDelim != '\0' ) delim= newDelim; do { // set buf, start and find end Actual.Buf= Rest.Buf; Actual.Start= Rest.Start; int nextDelimiter= Rest.IndexOf( delim ); if ( nextDelimiter >= 0 ) { Rest.Start+= nextDelimiter + 1; Actual.End= Rest.Start - 2; } else { Actual.End= Rest.End; Rest.SetNull(); } // trim if ( trimming == enums.Whitespaces.Trim ) Actual.Trim(Whitespaces); } while( skipEmptyTokens && Actual.IsEmpty() && Rest.IsNotNull() ); return Actual; }