//-------------------------------------------------------------------------------------------------- //--- Test Constructors //-------------------------------------------------------------------------------------------------- void constructorTest( String inputString, AString result, bool trim ) { Substring tok= new Substring( inputString ); if ( trim ) tok.Trim(); tok.CopyTo( result ); }
public void SubstringConstructor() { AString astr= new AString(); AString res= new AString(); constructorTest( "a" , res, false ); UT_EQ( "a", res ); constructorTest( " a" , res, false ); UT_EQ( " a", res ); constructorTest( "a " , res, false ); UT_EQ( "a ", res ); constructorTest( "a b" , res, false ); UT_EQ( "a b", res ); constructorTest( " a b" , res, false ); UT_EQ( " a b", res ); constructorTest( "a b " , res, false ); UT_EQ( "a b ", res ); constructorTest( "a" , res, true ); UT_EQ( "a", res ); constructorTest( " a" , res, true ); UT_EQ( "a", res ); constructorTest( "a " , res, true ); UT_EQ( "a", res ); constructorTest( "a b" , res, true ); UT_EQ( "a b", res ); constructorTest( " a b" , res, true ); UT_EQ( "a b", res ); constructorTest( "a b " , res, true ); UT_EQ( "a b", res ); // changing whitespaces { { astr.Clear()._( "xy xz abc xy" ); Substring subs= new Substring(); subs.Set(astr).Trim("xy ".ToCharArray()).CopyTo( res ); UT_EQ( "z abc", res ); } { Substring subs= new Substring( "xy xz abc xy" ); subs.TrimStart("xy ".ToCharArray()); subs.TrimEnd("xy ".ToCharArray()); subs.CopyTo( res ); UT_EQ( "z abc", res ); } } // test other constructors { astr.Clear()._( " astring "); UT_TRUE ( (new Substring()).IsEmpty() ); UT_TRUE ( (new Substring()).IsNull() ); UT_EQ( "astring", (new Substring( astr)).Trim() .ToString() ); UT_EQ( "str", (new Substring( astr, 2, 3 )).ToString() ); UT_EQ( "", (new Substring( astr, 20, 3 )).ToString() ); UT_TRUE ( (new Substring( astr, 20, 3 )).IsEmpty() ); UT_FALSE( (new Substring( astr, 20, 3 )).IsNull() ); Substring s2= new Substring( astr); UT_EQ( "astring", new Substring( s2.Trim().ToString() ).ToString() ); UT_EQ( "str", (new Substring( (new Substring( astr, 2,3 )))).ToString() ); } }
public void DeleteInsertSet() { AString ms= new AString(); // delete { ms.Clear()._("0123456789"); ms.Delete ( 5 ); UT_EQ( "01234", ms ); ms.Clear()._("0123456789"); ms.Delete ( 5, 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.Delete ( 5, -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.Delete ( 5, 1 ); UT_EQ( "012346789", ms ); ms.Clear()._("0123456789"); ms.Delete ( 5, 100 ); UT_EQ( "01234", ms ); ms.Clear()._("0123456789"); ms.Delete ( -5, 10 ); UT_EQ( "56789", ms ); ms.Clear()._("0123456789"); ms.Delete_NC ( 5, 5 ); UT_EQ( "01234", ms ); ms.Clear()._("0123456789"); ms.Delete_NC ( 0, 5 ); UT_EQ( "56789", ms ); ms.Clear()._("0123456789"); ms.Delete_NC ( 0, 1 ); UT_EQ( "123456789", ms ); ms.Clear()._("0123456789"); ms.Delete_NC ( 9, 1 ); UT_EQ( "012345678", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( -2 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 1 ); UT_EQ( "123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 2 ); UT_EQ( "23456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 9 ); UT_EQ( "9", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 10 ); UT_EQ( "", ms ); ms.Clear()._("0123456789"); ms.DeleteStart ( 11 ); UT_EQ( "", ms ); ms.Clear()._("0123456789"); ms.DeleteStart_NC ( 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart_NC ( 1 ); UT_EQ( "123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart_NC ( 2 ); UT_EQ( "23456789", ms ); ms.Clear()._("0123456789"); ms.DeleteStart_NC ( 9 ); UT_EQ( "9", ms ); ms.Clear()._("0123456789"); ms.DeleteStart_NC ( 10 ); UT_EQ( "", ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( -2 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 1 ); UT_EQ( "012345678" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 2 ); UT_EQ( "01234567" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 9 ); UT_EQ( "0" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 10 ); UT_EQ( "" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd ( 11 ); UT_EQ( "" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd_NC ( 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.DeleteEnd_NC ( 1 ); UT_EQ( "012345678" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd_NC ( 2 ); UT_EQ( "01234567" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd_NC ( 9 ); UT_EQ( "0" , ms ); ms.Clear()._("0123456789"); ms.DeleteEnd_NC ( 10 ); UT_EQ( "" , ms ); } // insertChars { ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, 20 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 0, 0 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, 0 ); UT_EQ( " 0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, 1 ); UT_EQ( "0 123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 2, 0 ); UT_EQ( " 0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 2, 1 ); UT_EQ( "0 123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, 11 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( ' ', 1, 10 ); UT_EQ( "0123456789 ", ms ); ms.Clear()._("0123456789"); ms.InsertChars ( '@', 3, 5 ); UT_EQ( "01234@@@56789",ms ); } // InsertAt { ms.Clear()._("0123456789"); ms.InsertAt ( "TST", -2 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 0 ); UT_EQ( "TST0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 1 ); UT_EQ( "0TST123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 8 ); UT_EQ( "01234567TST89", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 9 ); UT_EQ( "012345678TST9", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 10 ); UT_EQ( "0123456789TST", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 11 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( "TST", 12 ); UT_EQ( "0123456789", ms ); AString astr= new AString( "TST" ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , -2 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 0 ); UT_EQ( "TST0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 1 ); UT_EQ( "0TST123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 8 ); UT_EQ( "01234567TST89", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 9 ); UT_EQ( "012345678TST9", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 10 ); UT_EQ( "0123456789TST", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 11 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( astr , 12 ); UT_EQ( "0123456789", ms ); Substring subs= new Substring( "0123TSTxyz".ToCharArray(), 4, 3 ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , -2 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , -1 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 0 ); UT_EQ( "TST0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 1 ); UT_EQ( "0TST123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 8 ); UT_EQ( "01234567TST89", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 9 ); UT_EQ( "012345678TST9", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 10 ); UT_EQ( "0123456789TST", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 11 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.InsertAt ( subs , 12 ); UT_EQ( "0123456789", ms ); } // ReplaceSubstring { String r= "ABC"; ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 0, 10) ; UT_EQ( "ABC", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, -10, 100) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, -10, 5 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, -10, 10 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, -10, 11 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 0, 1 ) ; UT_EQ( "ABC123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 0, 2 ) ; UT_EQ( "ABC23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 1, 1 ) ; UT_EQ( "0ABC23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 1, 2 ) ; UT_EQ( "0ABC3456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 8, 1 ) ; UT_EQ( "01234567ABC9", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 8, 2 ) ; UT_EQ( "01234567ABC", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( r, 8, 3 ) ; UT_EQ( "0123456789", ms ); Substring s= new Substring( "0123ABCxyz".ToCharArray(), 4, 3 ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 0, 10) ; UT_EQ( "ABC", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, -10, 100) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, -10, 5 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, -10, 10 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, -10, 11 ) ; UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 0, 1 ) ; UT_EQ( "ABC123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 0, 2 ) ; UT_EQ( "ABC23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 1, 1 ) ; UT_EQ( "0ABC23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 1, 2 ) ; UT_EQ( "0ABC3456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 8, 1 ) ; UT_EQ( "01234567ABC9", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 8, 2 ) ; UT_EQ( "01234567ABC", ms ); ms.Clear()._("0123456789"); ms.ReplaceSubstring( s, 8, 3 ) ; UT_EQ( "0123456789", ms ); } // ReplaceRegion { ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 0, 10 ); UT_EQ( "@@@@@@@@@@", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', -10, 100 ); UT_EQ( "@@@@@@@@@@", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', -10, 5 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', -10, 10 ); UT_EQ( "0123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', -10, 11 ); UT_EQ( "@123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 0, 1 ); UT_EQ( "@123456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 0, 2 ); UT_EQ( "@@23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 1, 1 ); UT_EQ( "0@23456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 1, 2 ); UT_EQ( "0@@3456789", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 8, 1 ); UT_EQ( "01234567@9", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 8, 2 ); UT_EQ( "01234567@@", ms ); ms.Clear()._("0123456789"); ms.ReplaceRegion ( '@', 8, 3 ); UT_EQ( "01234567@@", ms ); } }
/** **************************************************************************************** * Cuts the given number of characters from the beginning of the Substring and optionally * places the portion that was cut in parameter \p target (if provided).<br> * Parameter \p regionLength is checked to be between 0 and length. If negative, nothing * is cut and \p target is set empty. If \p regionLength is greater than this * objects' length, all contents is 'moved' to \p target. * * @param regionLength The length of the region at the start to delete. * @param target An optional target \b %Substring that receives the portion that * is cut from this object. Defaults to null. * * @return The new length of the substring. ******************************************************************************************/ public int Consume( int regionLength, Substring target= null ) { if ( regionLength < 0 ) { if ( target != null ) target.Clear(); return Length(); } if ( regionLength > Length() ) regionLength= Length(); if ( target != null ) target.Set( this, 0, regionLength ); Start+= regionLength; hash= 0; return Length(); }
// ############################################################################################# // Reimplementing interface of grand-parent class SmartLock // ############################################################################################# /** **************************************************************************************** * Invokes grand-parent's method and in addition, if field #usesStdStreams is set, * registers with * \ref cs::aworx::lib::ALIB::StdOutputStreamsLock "ALIB.StdOutputStreamsLock", respectively * * @param newAcquirer The acquirer to add. * @return The new number of \e acquirers set. ******************************************************************************************/ public override int AddAcquirer( ThreadLock newAcquirer ) { // register with ALIB lockers (if not done yet) if ( usesStdStreams ) { ALIB.Lock.Acquire(); int stdStreamLockRegistrationCounter= this.stdStreamLockRegistrationCounter++; ALIB.Lock.Release(); if ( stdStreamLockRegistrationCounter == 0 ) ALIB.StdOutputStreamsLock.AddAcquirer( this ); } Variable variable= new Variable(); // import autosizes from configuration (last session) if ( variable.Define( ALox.AUTO_SIZES, GetName()).Load() != 0 ) AutoSizes.Import( variable.GetString() ); // import "max elapsed time" from configuration (last session) if ( variable.Define( ALox.MAX_ELAPSED_TIME, GetName()).Load() != 0 ) { long maxInSecs= variable.GetInteger(); Substring attrValue= new Substring(); if ( variable.GetAttribute( "limit", attrValue ) ) { long maxMax; attrValue.ConsumeLong( out maxMax ); if ( maxInSecs > maxMax ) maxInSecs= maxMax; } MetaInfo.MaxElapsedTime.FromSeconds( maxInSecs ); } // Variable <name>_FORMAT / <typeName>_FORMAT: ALIB.ASSERT_WARNING( ALox.FORMAT.DefaultValue == null, "Default value of variable FORMAT should be kept null" ); if( 0 == variable.Define( ALox.FORMAT, GetName() ).Load() && 0 == variable.Define( ALox.FORMAT, GetTypeName() ).Load() ) { // no variable created, yet. Let's create a 'personal' one on our name variable.Define( ALox.FORMAT, GetName() ); variable.AddString( MetaInfo.Format ); variable.AddString( MetaInfo.VerbosityError ); variable.AddString( MetaInfo.VerbosityWarning ); variable.AddString( MetaInfo.VerbosityInfo ); variable.AddString( MetaInfo.VerbosityVerbose ); variable.Store(); } else { MetaInfo.Format ._()._( variable.GetString(0) ); if( variable.Size() >= 2 ) MetaInfo.VerbosityError = variable.GetString(1).ToString(); if( variable.Size() >= 3 ) MetaInfo.VerbosityWarning= variable.GetString(2).ToString(); if( variable.Size() >= 4 ) MetaInfo.VerbosityInfo = variable.GetString(3).ToString(); if( variable.Size() >= 5 ) MetaInfo.VerbosityVerbose= variable.GetString(4).ToString(); } // Variable <name>_FORMAT_DATE_TIME / <typeName>_FORMAT_DATE_TIME: ALIB.ASSERT_WARNING( ALox.FORMAT_DATE_TIME.DefaultValue == null, "Default value of variable FORMAT_DATE_TIME should be kept null" ); if( 0 == variable.Define( ALox.FORMAT_DATE_TIME, GetName() ).Load() && 0 == variable.Define( ALox.FORMAT_DATE_TIME, GetTypeName() ).Load() ) { // no variable created, yet. Let's create a 'personal' one on our name variable.Define( ALox.FORMAT_DATE_TIME, GetName() ); variable.AddString( MetaInfo.DateFormat ); variable.AddString( MetaInfo.TimeOfDayFormat ); variable.AddString( MetaInfo.TimeElapsedDays ); variable.Store(); } else { MetaInfo.DateFormat = variable.GetString(0).ToString(); if( variable.Size() >= 2 ) MetaInfo.TimeOfDayFormat = variable.GetString(1).ToString(); if( variable.Size() >= 3 ) MetaInfo.TimeElapsedDays = variable.GetString(2).ToString(); } // Variable <name>FORMAT_TIME_DIFF / <typeName>FORMAT_TIME_DIFF: ALIB.ASSERT_WARNING( ALox.FORMAT_TIME_DIFF.DefaultValue == null, "Default value of variable FORMAT_TIME_DIFF should be kept null" ); if( 0 == variable.Define( ALox.FORMAT_TIME_DIFF, GetName() ).Load() && 0 == variable.Define( ALox.FORMAT_TIME_DIFF, GetTypeName() ).Load() ) { // no variable created, yet. Let's create a 'personal' one on our name variable.Define( ALox.FORMAT_TIME_DIFF, GetName() ); variable.AddInteger ( MetaInfo.TimeDiffMinimum); variable.AddString( MetaInfo.TimeDiffNone ); variable.AddString( MetaInfo.TimeDiffNanos ); variable.AddString( MetaInfo.TimeDiffMicros ); variable.AddString( MetaInfo.TimeDiffMillis ); variable.AddString( MetaInfo.TimeDiffSecs ); variable.AddString( MetaInfo.TimeDiffMins ); variable.AddString( MetaInfo.TimeDiffHours ); variable.AddString( MetaInfo.TimeDiffDays ); variable.Store(); } else { MetaInfo.TimeDiffMinimum= variable.GetInteger (0); if( variable.Size() >= 2 ) MetaInfo.TimeDiffNone = variable.GetString(1).ToString(); if( variable.Size() >= 3 ) MetaInfo.TimeDiffNanos = variable.GetString(2).ToString(); if( variable.Size() >= 4 ) MetaInfo.TimeDiffMicros = variable.GetString(3).ToString(); if( variable.Size() >= 5 ) MetaInfo.TimeDiffMillis = variable.GetString(4).ToString(); if( variable.Size() >= 6 ) MetaInfo.TimeDiffSecs = variable.GetString(5).ToString(); if( variable.Size() >= 7 ) MetaInfo.TimeDiffMins = variable.GetString(6).ToString(); if( variable.Size() >= 8 ) MetaInfo.TimeDiffHours = variable.GetString(7).ToString(); if( variable.Size() >= 9 ) MetaInfo.TimeDiffDays = variable.GetString(8).ToString(); } // Variable <name>FORMAT_MULTILINE / <typeName>FORMAT_MULTILINE: ALIB.ASSERT_WARNING( ALox.FORMAT_MULTILINE.DefaultValue == null, "Default value of variable FORMAT_MULTILINE should be kept null" ); if( 0 == variable.Define( ALox.FORMAT_MULTILINE, GetName() ).Load() && 0 == variable.Define( ALox.FORMAT_MULTILINE, GetTypeName() ).Load() ) { // no variable created, yet. Let's create a 'personal' one on our name variable.Define( ALox.FORMAT_MULTILINE, GetName() ); variable.AddInteger( MultiLineMsgMode ); variable.AddString ( FmtMultiLineMsgHeadline ); variable.AddString ( FmtMultiLinePrefix ); variable.AddString ( FmtMultiLineSuffix ); variable.Store(); } else { MultiLineMsgMode= (int) variable.GetInteger(0) ; if( variable.Size() >= 2 ) FmtMultiLineMsgHeadline= variable.GetString(1).ToString(); if( variable.Size() >= 3 ) FmtMultiLinePrefix = variable.GetString(2).ToString(); if( variable.Size() >= 4 ) FmtMultiLineSuffix = variable.GetString(3).ToString(); if( variable.Size() >= 5 ) { if (variable.GetString(4).Equals( "nulled" , Case.Ignore ) ) MultiLineDelimiter= null; else MultiLineDelimiter= variable.GetString(4).ToString(); } if( variable.Size() >= 6 ) MultiLineDelimiterRepl = variable.GetString(5).ToString(); } // call parents' implementation return base.AddAcquirer( newAcquirer ); }
/** **************************************************************************************** * Searches in the values of this variable for the pattern * <c>attrName = result</c> and * sets parameter \p result to the string following this pattern. * * @param attrName The name of the attribute searched. * @param[out] result A substring with the result. * @param attrDelim The delimiter to search for. Defaults to '='. * @return \c true if the attribute was found, \c false otherwise. ******************************************************************************************/ public bool GetAttribute( String attrName, Substring result, char attrDelim= '=' ) { for ( int i= 0; i< Size(); i++ ) { result.Set( GetString(i ) ); if ( result.Consume( attrName, Case.Ignore, Whitespaces.Trim ) && result.Consume( attrDelim, Case.Ignore, Whitespaces.Trim ) ) { result.Trim(); return true; } } return false; }
// ############################################################################################# // file IO // ############################################################################################# /** **************************************************************************************** * Clears all configuration data and reads the file. It might happen that lines are * ignored or otherwise marked as faulty. All numbers of such lines get collected in * field LinesWithReadErrors. * @return Returns the #Status of the operation. ******************************************************************************************/ public IniFile.Status ReadFile() { Reset(); LastStatus= Status.OK; // read all variables StreamReader file; try { file= new StreamReader( FileName.ToString() ); } catch( Exception ) { return LastStatus= Status.ERROR_OPENING_FILE; } String lineS; AString name= new AString(); AString value= new AString(); AString comments= new AString(); Section actSection= (IniFile.Section) Sections[0]; Substring line= new Substring(); int lineNo= 0; bool fileHeaderRead= false; char[] separatorCharacters= value._( "=" )._( CString.DefaultWhitespaces ) .ToString().ToCharArray(); LinesWithReadErrors.Clear(); while ( (lineS= file.ReadLine()) != null ) { lineNo++; bool isEmpty= line.Set( lineS ).Trim().IsEmpty(); bool isCommentLine= startsWithCommentSymbol( line ); if ( isCommentLine ) { if ( comments.IsNotEmpty() ) comments.NewLine(); comments._(line); continue; } // still processing file header? if ( !fileHeaderRead ) { fileHeaderRead= true; FileComments._()._( comments ); comments.Clear(); } // empty line? if ( isEmpty ) { if ( comments.IsNotEmpty() ) comments.NewLine(); continue; } // section line if ( line.Consume( '[' ) ) { fileHeaderRead= true; // we do not care if there is no closing bracket. But if there is one, we remove it. if( !line.ConsumeFromEnd(']') ) LinesWithReadErrors.Add( lineNo ); // search the section in our section list (if section existed already, new comments // are dropped) actSection= (IniFile.Section) SearchOrCreateSection( line, comments); comments.Clear(); continue; } // variable line? value.Clear(); int idx= line.IndexOfAny( separatorCharacters, Inclusion.Include ); if( idx < 0 ) { name._()._( line ); line.Clear(); } else { name._()._( line.Buf, line.Start, idx ); line.Consume( idx ); value._(line); } // read continues as long as lines end with '\' (must not be '\\') while ( line.CharAtEnd() == '\\' && line.CharAtEnd(1) != '\\' ) { value.NewLine(); if ( (lineS= file.ReadLine()) == null ) { // last line of the file ended with '\' ! line.Clear(); break; } line.Set( lineS ).TrimEnd(); value._( line ); } // insert entry with raw value { IniFile.Entry entry= (IniFile.Entry) actSection.GetEntry( name, true ); entry.Values .Clear(); entry.Comments._()._( comments ); entry.RawValue._()._( value ); // if there is just no raw value, we add an empty string to the entries' values if ( value.IsEmpty() ) entry.Values.Add( new AString() ); } comments.Clear(); } file.Close(); return LastStatus; }
/** **************************************************************************************** * 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; }
bool Load( Variable variable, bool searchOnly= false ) { if ( args == null ) return false; int optionLength= variable.Fullname.Length(); Substring actVar= new Substring(); for ( int i= 0; i < args.Length ; i++ ) { // remove whitespaces (if somebody would work with quotation marks...) // and request '-' and allow a second '-' if ( !actVar.Set( args[i] ).Trim().Consume('-') ) continue; actVar.Consume( '-' ); if ( variable.Fullname.CompareTo( args[i], Case.Ignore, actVar.Start, optionLength, 0, optionLength ) == 0) { //again, lets trim before searching the = sign (really almost unnecessary) actVar.Start+= optionLength; if ( actVar.IsEmpty() ) { if ( !searchOnly ) variable.AddString(); return true; } if ( actVar.Consume( '=', Case.Sensitive, Whitespaces.Trim ) ) { if ( !searchOnly ) { actVar.Trim(); StringConverter.LoadFromString( variable, actVar ); } return true; } } } return false; }
public void IndexOf() { Substring subs; // indexOf() { subs= new Substring("ABCD"); UT_EQ( -1, subs.IndexOf('X') ); UT_EQ( 0, subs.IndexOf('A') ); UT_EQ( 1, subs.IndexOf('B') ); UT_EQ( 2, subs.IndexOf('C') ); UT_EQ( 3, subs.IndexOf('D') ); } // search characters subs.Set( "abc@" + "abcd abcd" + "abc@de", 4, 9 ); { UT_EQ( -1 , subs.IndexOf( '@', -5 ) ); UT_EQ( -1 , subs.IndexOf( '@' ) ); UT_EQ( -1 , subs.IndexOf( '@', 5 ) ); UT_EQ( -1 , subs.IndexOf( '@', 150 ) ); UT_EQ( 0 , subs.IndexOf( 'a' ) ); UT_EQ( 1 , subs.IndexOf( 'b' ) ); UT_EQ( 2 , subs.IndexOf( 'c' ) ); UT_EQ( 0 , subs.IndexOf( 'a', 0 ) ); UT_EQ( 1 , subs.IndexOf( 'b', 0 ) ); UT_EQ( 2 , subs.IndexOf( 'c', 0 ) ); UT_EQ( 5 , subs.IndexOf( 'a', 1 ) ); UT_EQ( 1 , subs.IndexOf( 'b', 1 ) ); UT_EQ( 2 , subs.IndexOf( 'c', 1 ) ); UT_EQ( 5 , subs.IndexOf( 'a', 2 ) ); UT_EQ( 6 , subs.IndexOf( 'b', 2 ) ); UT_EQ( 2 , subs.IndexOf( 'c', 2 ) ); UT_EQ( 5 , subs.IndexOf( 'a', 3 ) ); UT_EQ( 6 , subs.IndexOf( 'b', 3 ) ); UT_EQ( 7 , subs.IndexOf( 'c', 3 ) ); UT_EQ( 8 , subs.IndexOf( 'd', 7 ) ); UT_EQ( 8 , subs.IndexOf( 'd', 8 ) ); UT_EQ( -1 , subs.IndexOf( 'd', 9 ) ); } // search null, empty string subs.Set( "abc@" + "abcd abcd" + "abc@de", 4, 9 ); { UT_EQ( 0 , subs.IndexOf( (AString) null ) ); UT_EQ( 5 , subs.IndexOf( (AString) null, 5 ) ); UT_EQ( -1 , subs.IndexOf( (AString) null, 50 ) ); UT_EQ( 0 , subs.IndexOf( (AString) null, - 5 ) ); UT_EQ( 0 , subs.IndexOf( (String) null ) ); UT_EQ( 5 , subs.IndexOf( (String) null, 5 ) ); UT_EQ( -1 , subs.IndexOf( (String) null, 50 ) ); UT_EQ( 0 , subs.IndexOf( (String) null, - 5 ) ); UT_EQ( 0 , subs.IndexOf( "", - 5 ) ); UT_EQ( 0 , subs.IndexOf( "", 0 ) ); UT_EQ( 4 , subs.IndexOf( "", 4 ) ); UT_EQ( -1 , subs.IndexOf( "", 100 ) ); } // search subs.Set( "abc@" + "abcd abcd" + "abc@de", 4, 9 ); { UT_EQ( 0 , subs.IndexOf( "abcd" ) ); UT_EQ( 1 , subs.IndexOf( "b" ) ); UT_EQ( 4 , subs.IndexOf( " abcd" ) ); UT_EQ( 5 , subs.IndexOf( "abcd", 1 ) ); UT_EQ( 0 , subs.IndexOf( "abcd",- 1 ) ); UT_EQ( -1, subs.IndexOf( "xyz", -10 ) ); } // ignore case String t= "Hello A-Worx util"; subs.Set( "abc@" + t + "abc@de", 4, t.Length ); { UT_EQ( 6 , subs.IndexOf( "a-worx", 0 ,Case.Ignore ) ); UT_EQ( 6 , subs.IndexOf( "a-worx", 1 ,Case.Ignore ) ); UT_EQ( 6 , subs.IndexOf( "a-worx", -10 ,Case.Ignore ) ); UT_EQ( 6 , subs.IndexOf( "a-worx", 6 ,Case.Ignore ) ); UT_EQ( -1, subs.IndexOf( "a-worx", 7 ,Case.Ignore ) ); UT_EQ( -1, subs.IndexOf( "a-worx", 100 ,Case.Ignore ) ); UT_EQ( 0, subs.IndexOf( "hel", 0 ,Case.Ignore ) ); UT_EQ( -1, subs.IndexOf( "hel", 1 ,Case.Ignore ) ); UT_EQ( 13, subs.IndexOf( "util", 1 ,Case.Ignore ) ); UT_EQ( 13, subs.IndexOf( "UTIL", 5 ,Case.Ignore ) ); UT_EQ( 13, subs.IndexOf( "UTIL", 13 ,Case.Ignore ) ); UT_EQ( -1, subs.IndexOf( "UTIL", 14 ,Case.Ignore ) ); } // ------------------ search one of several characters ------------------ subs.Set( "abc@" + "abcd abcde" + "abc@de", 4, 10 ); { // search one of int l= subs.Length(); UT_EQ( 4, subs.IndexOfAny ( CString.DefaultWhitespaces,Inclusion.Include ) ); UT_EQ( -1, subs.IndexOfAny ( "x" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.IndexOfAny ( "xy" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.IndexOfAny ( "xyz" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( 3, subs.IndexOfAny ( "xyd" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( 3, subs.IndexOfAny ( "d" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( 3, subs.IndexOfAny ( "xyd" .ToCharArray() ,Inclusion.Include, -2 ) ); UT_EQ( 8, subs.IndexOfAny ( "xyd" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( -1, subs.IndexOfAny ( "xyd" .ToCharArray() ,Inclusion.Include, 20 ) ); UT_EQ( -1, subs.LastIndexOfAny( "" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.LastIndexOfAny( "x" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.LastIndexOfAny( "xy" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.LastIndexOfAny( "xyz" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( 8, subs.LastIndexOfAny( "xyd" .ToCharArray() ,Inclusion.Include ) ); UT_EQ( -1, subs.LastIndexOfAny( "xyd" .ToCharArray() ,Inclusion.Include, -2 ) ); UT_EQ( -1, subs.LastIndexOfAny( "xyd" .ToCharArray() ,Inclusion.Include, 2 ) ); UT_EQ( 3, subs.LastIndexOfAny( "xyd" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( 0, subs.LastIndexOfAny( "a" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( 1, subs.LastIndexOfAny( "b" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( 1, subs.LastIndexOfAny( "ba" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( 0, subs.LastIndexOfAny( "xa" .ToCharArray() ,Inclusion.Include, 4 ) ); UT_EQ( 8, subs.LastIndexOfAny( "xyd" .ToCharArray() ,Inclusion.Include, 20 ) ); UT_EQ( 8, subs.LastIndexOfAny( "d" .ToCharArray() ,Inclusion.Include, 20 ) ); UT_EQ( 9, subs.LastIndexOfAny( "e" .ToCharArray() ,Inclusion.Include, 20 ) ); // search NOT one of UT_EQ( 0, subs.IndexOfAny ( "" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 0, subs.IndexOfAny ( "x" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 0, subs.IndexOfAny ( "xy" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 0, subs.IndexOfAny ( "xyz" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 1, subs.IndexOfAny ( "a" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 2, subs.IndexOfAny ( "ba" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 3, subs.IndexOfAny ( "abc" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 3, subs.IndexOfAny ( "acb" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 3, subs.IndexOfAny ( "cba" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 3, subs.IndexOfAny ( "xcba" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-1, subs.LastIndexOfAny( "" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-1, subs.LastIndexOfAny( "x" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-1, subs.LastIndexOfAny( "xy" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-2, subs.LastIndexOfAny( "e" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-3, subs.LastIndexOfAny( "de" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-4, subs.LastIndexOfAny( "cde" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-4, subs.LastIndexOfAny( "ced" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( l-4, subs.LastIndexOfAny( "ecd" .ToCharArray() ,Inclusion.Exclude ) ); UT_EQ( 5, subs.LastIndexOfAny( "ecd" .ToCharArray() ,Inclusion.Exclude, 5 ) ); UT_EQ( 4, subs.LastIndexOfAny( "ecd" .ToCharArray() ,Inclusion.Exclude, 4 ) ); UT_EQ( 1, subs.LastIndexOfAny( "acd" .ToCharArray() ,Inclusion.Exclude, 3 ) ); UT_EQ( -1, subs.LastIndexOfAny( "abc" .ToCharArray() ,Inclusion.Exclude, 2 ) ); UT_EQ( 3, subs.LastIndexOfAny( "xay" .ToCharArray() ,Inclusion.Exclude, 3 ) ); UT_EQ( 2, subs.LastIndexOfAny( "d" .ToCharArray() ,Inclusion.Exclude, 3 ) ); UT_EQ( -1, subs.LastIndexOfAny( "a" .ToCharArray() ,Inclusion.Exclude, 0 ) ); } }
/** **************************************************************************************** * Interprets given \p src as a boolean value. * \ref cs::aworx::lib::enums::Inclusion "enums.Inclusion". * If the case insensitive comparison of the first non-whitespace characters of the string with * with values "t", "1", "y", "on", "ok" * matches, \c true is returned. * Otherwise, including the case that \p src is 'nulled', \c false is returned. * * @param src The string to 'parse'. * * @returns The \b %Case value read. ******************************************************************************************/ public static bool ReadBoolean( Substring src ) { int idx= src.IndexOfAny( CString.DefaultWhitespaces, Inclusion.Exclude ); if ( idx >= 0 ) { char c= Char.ToLower( src.CharAt(idx) ); foreach ( char v in trueValuesBoolean ) if ( c == v ) return true; char c2= Char.ToLower( src.CharAt( idx + 1 ) ); if ( c == 'o' && ( c2 == 'n' || c2 == 'k' ) ) return true; } return false; }
public void FrontEnd() { // empty substring { Substring subs= new Substring(); UT_EQ( '\0', subs.CharAtStart( ) ); UT_EQ( '\0', subs.CharAt( 0 ) ); UT_EQ( '\0', subs.CharAt( 1 ) ); UT_EQ( '\0', subs.CharAt(-1 ) ); UT_EQ( '\0', subs.CharAt( 2 ) ); UT_EQ( '\0', subs.CharAt(-2 ) ); UT_EQ( '\0', subs.CharAtEnd ( ) ); UT_EQ( '\0', subs.CharAtEnd ( 0 ) ); UT_EQ( '\0', subs.CharAtEnd ( 1 ) ); UT_EQ( '\0', subs.CharAtEnd (-1 ) ); UT_EQ( '\0', subs.CharAtEnd ( 2 ) ); UT_EQ( '\0', subs.CharAtEnd (-2 ) ); } // empty substring { Substring subs= new Substring("aaaaaaaaaaaa"); subs.Start= 5; subs.End= 4; UT_EQ( '\0', subs.CharAtStart( ) ); UT_EQ( '\0', subs.CharAt( 0 ) ); UT_EQ( '\0', subs.CharAt( 1 ) ); UT_EQ( '\0', subs.CharAt(-1 ) ); UT_EQ( '\0', subs.CharAt( 2 ) ); UT_EQ( '\0', subs.CharAt(-2 ) ); UT_EQ( '\0', subs.CharAtEnd ( ) ); UT_EQ( '\0', subs.CharAtEnd ( 0 ) ); UT_EQ( '\0', subs.CharAtEnd ( 1 ) ); UT_EQ( '\0', subs.CharAtEnd (-1 ) ); UT_EQ( '\0', subs.CharAtEnd ( 2 ) ); UT_EQ( '\0', subs.CharAtEnd (-2 ) ); } // substring of length 1 { Substring subs= new Substring("aaaaaaaaaaaa"); subs.Start= subs.End= 5; UT_EQ('a', subs.CharAtStart( ) ); UT_EQ('a', subs.CharAt( 0 ) ); UT_EQ('\0', subs.CharAt( 1 ) ); UT_EQ('\0', subs.CharAt(-1 ) ); UT_EQ('\0', subs.CharAt( 2 ) ); UT_EQ('\0', subs.CharAt(-2 ) ); UT_EQ('a', subs.CharAtEnd ( ) ); UT_EQ('a', subs.CharAtEnd ( 0 ) ); UT_EQ('\0', subs.CharAtEnd ( 1 ) ); UT_EQ('\0', subs.CharAtEnd (-1 ) ); UT_EQ('\0', subs.CharAtEnd ( 2 ) ); UT_EQ('\0', subs.CharAtEnd (-2 ) ); } // substring of length 2 { Substring subs= new Substring("aaaaabbbbbb"); subs.End= subs.IndexOf('b'); subs.Start= subs.End - 1; UT_EQ('a', subs.CharAtStart( ) ); UT_EQ('a', subs.CharAt( 0 ) ); UT_EQ('b', subs.CharAt( 1 ) ); UT_EQ('\0', subs.CharAt(-1 ) ); UT_EQ('\0', subs.CharAt( 2 ) ); UT_EQ('\0', subs.CharAt(-2 ) ); UT_EQ('b', subs.CharAtEnd ( ) ); UT_EQ('b', subs.CharAtEnd ( 0 ) ); UT_EQ('a', subs.CharAtEnd ( 1 ) ); UT_EQ('\0', subs.CharAtEnd (-1 ) ); UT_EQ('\0', subs.CharAtEnd ( 2 ) ); UT_EQ('\0', subs.CharAtEnd (-2 ) ); } }
public void Consume() { // null substring { Substring s= new Substring(); Substring r= new Substring("oldval"); UT_EQ( '\0', s.Consume ( ) ); UT_EQ( 0, s.Consume ( 0 , r) ); UT_TRUE(r.IsNull()); UT_EQ( 0, s.Consume ( 5 , r) ); UT_TRUE(r.IsNull()); UT_EQ( false, s.Consume ( 'a' ) ); UT_EQ( false, s.Consume ( "word" ) ); UT_EQ( '\0', s.ConsumeFromEnd( ) ); UT_EQ( 0, s.ConsumeFromEnd( 0 ) ); UT_EQ( 0, s.ConsumeFromEnd( 5 ) ); UT_EQ( false, s.ConsumeFromEnd( 'a' ) ); UT_EQ( false, s.ConsumeFromEnd( "word" ) ); } // empty substring { Substring s= new Substring("aaaaaaaaaaaa"); Substring r= new Substring("oldval"); s.Start= 5; s.End= 4; UT_EQ( '\0', s.Consume ( ) ); UT_EQ( 0, s.Consume ( 0 ,r ) ); UT_TRUE( r.IsNotNull()); UT_TRUE(r.IsEmpty()); UT_EQ( 0, s.Consume ( 5 ,r ) ); UT_TRUE( r.IsNotNull()); UT_TRUE(r.IsEmpty()); UT_EQ( false, s.Consume ( 'a' ) ); UT_EQ( false, s.Consume ( "word" ) ); UT_EQ( '\0', s.ConsumeFromEnd( ) ); UT_EQ( 0, s.ConsumeFromEnd( 0 ) ); UT_EQ( 0, s.ConsumeFromEnd( 5 ) ); UT_EQ( false, s.ConsumeFromEnd( 'a' ) ); UT_EQ( false, s.ConsumeFromEnd( "word" ) ); } // substring of length 1 { Substring s= new Substring("aaaaaaaaaaaa"); Substring r= new Substring("oldval"); s.Start= s.End= 5; UT_EQ( 'a', s.Consume ( ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( 1, s.Consume ( 0 ) ); UT_EQ( 1, s.Length() ); s.Start= s.End= 5; UT_EQ( 0, s.Consume ( 1 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("a")); s.Start= s.End= 5; UT_EQ( 0, s.Consume ( 5 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("a")); s.Start= s.End= 5; UT_EQ( true, s.Consume ( 'a' ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( false, s.Consume ( 'b' ) ); UT_EQ( 1, s.Length() ); s.Start= s.End= 5; UT_EQ( false, s.Consume ( "word" ) ); UT_EQ( 1, s.Length() ); s.Start= s.End= 5; UT_EQ( 'a', s.ConsumeFromEnd( ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( 1, s.ConsumeFromEnd( 0 ) ); UT_EQ( 1, s.Length() ); s.Start= s.End= 5; UT_EQ( 0, s.ConsumeFromEnd( 1 ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( 0, s.ConsumeFromEnd( 5 ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( true, s.ConsumeFromEnd( 'a' ) ); UT_EQ( 0, s.Length() ); s.Start= s.End= 5; UT_EQ( false, s.ConsumeFromEnd( 'b' ) ); UT_EQ( 1, s.Length() ); s.Start= s.End= 5; UT_EQ( false, s.ConsumeFromEnd( "word" ) ); UT_EQ( 1, s.Length() ); } // substring of length 2 { Substring s= new Substring("12ab3456"); Substring r= new Substring("oldval"); s.Start= 2; s.End= 3; UT_EQ( 'a', s.Consume ( ) ); UT_EQ( 1, s.Length() ); UT_EQ( 'b', s.Consume ( ) ); UT_EQ( 0, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( 'b', s.ConsumeFromEnd( ) ); UT_EQ( 1, s.Length() ); UT_EQ( 'a', s.ConsumeFromEnd( ) ); UT_EQ( 0, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( 2, s.Consume ( 0 , r ) ); UT_EQ( 2, s.Length() ); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); s.Start= 2; s.End= 3; UT_EQ( 1, s.Consume ( 1 , r ) ); UT_EQ( 1, s.Length() ); UT_TRUE(r.Equals("a")); s.Start= 2; s.End= 3; UT_EQ( 0, s.Consume ( 2 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("ab")); s.Start= 2; s.End= 3; UT_EQ( 0, s.Consume ( 3 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("ab")); s.Start= 2; s.End= 3; UT_EQ( 2, s.ConsumeFromEnd( 0 , r ) ); UT_EQ( 2, s.Length() ); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); s.Start= 2; s.End= 3; UT_EQ( 1, s.ConsumeFromEnd( 1 , r ) ); UT_EQ( 1, s.Length() ); UT_TRUE(r.Equals("b")); s.Start= 2; s.End= 3; UT_EQ( 0, s.ConsumeFromEnd( 2 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("ab")); s.Start= 2; s.End= 3; UT_EQ( 0, s.ConsumeFromEnd( 3 , r ) ); UT_EQ( 0, s.Length() ); UT_TRUE(r.Equals("ab")); s.Start= 2; s.End= 3; UT_EQ( false, s.Consume ( 'b' ) ); UT_EQ( 2, s.Length() ); UT_EQ( true, s.Consume ( 'a' ) ); UT_EQ( 1, s.Length() ); UT_EQ( true, s.Consume ( 'b' ) ); UT_EQ( 0, s.Length() ); UT_EQ( false, s.Consume ( 'a' ) ); UT_EQ( 0, s.Length() ); UT_EQ( false, s.Consume ( 'b' ) ); UT_EQ( 0, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( false, s.ConsumeFromEnd( 'a' ) ); UT_EQ( 2, s.Length() ); UT_EQ( true, s.ConsumeFromEnd( 'b' ) ); UT_EQ( 1, s.Length() ); UT_EQ( true, s.ConsumeFromEnd( 'a' ) ); UT_EQ( 0, s.Length() ); UT_EQ( false, s.ConsumeFromEnd( 'b' ) ); UT_EQ( 0, s.Length() ); UT_EQ( false, s.ConsumeFromEnd( 'a' ) ); UT_EQ( 0, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( false, s.Consume ( "word" ) ); UT_EQ( 2, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( false, s.Consume ( "AB" ) ); UT_EQ( 2, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( true, s.Consume ( "ab" ) ); UT_EQ( 0, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( false, s.ConsumeFromEnd( "word" ) ); UT_EQ( 2, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( false, s.ConsumeFromEnd( "AB" ) ); UT_EQ( 2, s.Length() ); s.Start= 2; s.End= 3; UT_EQ( true, s.ConsumeFromEnd( "ab" ) ); UT_EQ( 0, s.Length() ); } // 3 words { Substring s= new Substring("word1 word2 word3"); UT_EQ( 'w', s.Consume ( ) ); UT_EQ( 'o', s.Consume ( ) ); UT_EQ( 'r', s.Consume ( ) ); UT_EQ( 'd', s.Consume ( ) ); UT_EQ( '1', s.Consume ( ) ); UT_EQ( false , s.Consume ('w' ) ); UT_EQ( true , s.Consume ('w' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( true , s.Consume ('o' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.Consume ('o' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( true , s.Consume ('r' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.Consume ("D2" , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.Consume ("D2" ) ); UT_EQ( true , s.Consume ("d2" ) ); UT_EQ( 2 , s.Consume ( 4 ) ); UT_EQ( "d3" , s.ToString() ); s= new Substring("word1 word2 word3"); UT_EQ( '3', s.ConsumeFromEnd( ) ); UT_EQ( 'd', s.ConsumeFromEnd( ) ); UT_EQ( 'r', s.ConsumeFromEnd( ) ); UT_EQ( 'o', s.ConsumeFromEnd( ) ); UT_EQ( 'w', s.ConsumeFromEnd( ) ); UT_EQ( false , s.ConsumeFromEnd('2' ) ); UT_EQ( true , s.ConsumeFromEnd('2' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( true , s.ConsumeFromEnd('d' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.ConsumeFromEnd('d' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( true , s.ConsumeFromEnd('r' , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.ConsumeFromEnd("WO" , Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( false , s.ConsumeFromEnd("WO" ) ); UT_EQ( true , s.ConsumeFromEnd("wo" ) ); UT_EQ( 2 , s.ConsumeFromEnd( 4 ) ); UT_EQ( "wo" , s.ToString() ); } // consume AString, Substring { Substring s= new Substring("word1 word2 word3 word4"); Substring sConsume= new Substring( "1234word12", 4, 4 ); AString aConsume= new AString ( "word" ); UT_EQ( true, s.Consume ( sConsume ) ); UT_EQ( false, s.Consume ( sConsume ) ); UT_EQ( '1', s.Consume ( ) ); UT_EQ( false, s.Consume ( sConsume ) ); UT_EQ( true, s.Consume ( sConsume, Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( '2', s.Consume ( ) ); UT_EQ( ' ', s.Consume ( ) ); UT_EQ( true, s.Consume ( aConsume ) ); UT_EQ( false, s.Consume ( aConsume ) ); UT_EQ( '3', s.Consume ( ) ); UT_EQ( false, s.Consume ( aConsume ) ); UT_EQ( true, s.Consume ( aConsume, Case.Sensitive, Whitespaces.Trim ) ); s.Set("1word 2word 3word 4word"); UT_EQ( true, s.ConsumeFromEnd( sConsume ) ); UT_EQ( false, s.ConsumeFromEnd( sConsume ) ); UT_EQ( '4', s.ConsumeFromEnd( ) ); UT_EQ( false, s.ConsumeFromEnd( sConsume ) ); UT_EQ( true, s.ConsumeFromEnd( sConsume, Case.Sensitive, Whitespaces.Trim ) ); UT_EQ( '3', s.ConsumeFromEnd( ) ); UT_EQ( ' ', s.ConsumeFromEnd( ) ); UT_EQ( true, s.ConsumeFromEnd( aConsume ) ); UT_EQ( false, s.ConsumeFromEnd( aConsume ) ); UT_EQ( '2', s.ConsumeFromEnd( ) ); UT_EQ( false, s.ConsumeFromEnd( aConsume ) ); UT_EQ( true, s.ConsumeFromEnd( aConsume, Case.Sensitive, Whitespaces.Trim ) ); } }
public void IniFileTest() { // write sample config file //UT_PRINT(""); UT_PRINT( "### Configuration with IniFile ###" ); String iniFileContents= "##########################################################################" +"\n" +"## unit test config file" +"\n" +"##########################################################################" +"\n" +"// this is also a comment" +"\n" +"; and this is as well" +"\n" +"" +"\n" +"HOME= overwritten_by_environment" +"\n" +"HOMEPATH= overwritten_by_environment" +"\n" +"" +"\n" +"concat= start =5, \\" +"\n" +" end =32, \\" +"\n" +" \\#no comment, \\" +"\n" +" \\;nocomment, \\" +"\n" +" ;a comment, \\" +"\n" +" getsLonger, \\" +"\n" +" getsLongerxxx, \\" +"\n" +" getsshorter, \\" +"\n" +" getsLongerxxxxx, \\" +"\n" +" getsLongerxxxxxxxxx, \\" +"\n" +" getsshorterxx, \\" +"\n" +" last" +"\n" +"" +"\n" +"" +"\n" +"CUBA=a country" +"\n" +"# The size " +"\n" +" SIZE= 25 " +"\n" +"" +"\n" +"# doble comment line" +"\n" +"# double, i meant" +"\n" +"2Comments= much talk" +"\n" +"" +"\n" +"# A great section" +"\n" +"[Great Section] " +"\n" +"SectionVar=5" +"\n" +"Double=12.3" +"\n" +"Tricky= backslash\\\\" +"\n" +"# A 2nd section" +"\n" +"[2nd Section] " +"\n" +"SectionVar=6" +"\n" +"" +"\n" +"" +"\n" +"[Great Section] " +"\n" +"SECTION_CONTINUED = yEs" +"\n" +"" +"\n" +"" +"\n" +"[ESC] " +"\n" +"Blanks= \" x \"" +"\n" +"Blanks2= \" x \" \\" +"\n" +" \" y \" " +"\n" +"Tabs=\t\t\\tx\\t" +"\n" +"nrslash= \"\\n\\r//\\\\\"" +"\n" ; String fileName= Environment.CurrentDirectory + "/unittest_testiniFile.cfg"; // write sample config file { StreamWriter file= new StreamWriter( fileName ); file.Write( iniFileContents ); file.Close(); } IniFile iniFile= new IniFile( fileName ); UT_TRUE( (IniFile.Status.OK == iniFile.LastStatus) ); // check some values Variable var= new Variable(); iniFile.Load( var.Define( "", "CUBA") ); UT_EQ( "a country", var.GetString() ); iniFile.Load( var.Define( "", "cUbA") ); UT_EQ( "a country", var.GetString() ); iniFile.Load( var.Define( "", "SIZE") ); UT_EQ( "25", var.GetString() ); iniFile.Load( var.Define( "", "concat", ',') ); UT_EQ( 11 , var.Size()); UT_EQ( "start =5" , var.GetString(0) ); UT_EQ( "end =32" , var.GetString(1) ); UT_EQ( "#no comment" , var.GetString(2) ); UT_EQ( ";nocomment" , var.GetString(3) ); iniFile.Load( var.Define( "ESC", "Blanks" ) ); UT_EQ( " x " , var.GetString() ); iniFile.Load( var.Define( "ESC", "Blanks2" ) ); UT_EQ( " x y " , var.GetString() ); iniFile.Load( var.Define( "ESC", "Tabs" ) ); UT_EQ( "\tx\t" , var.GetString() ); iniFile.Load( var.Define( "ESC", "nrslash" ) ); UT_EQ( "\n\r//\\" , var.GetString() ); iniFile.Load( var.Define( "Great Section", "SectionVar" ) ); UT_EQ( "5" , var.GetString() ); iniFile.Load( var.Define( "2nd Section", "SectionVar" ) ); UT_EQ( "6" , var.GetString() ); iniFile.Load( var.Define( "Great Section", "SECTION_CONTINUED") ); UT_EQ( "yEs", var.GetString() ); iniFile.Load( var.Define( "Great Section", "Tricky" ) ); UT_EQ( "backslash\\", var.GetString() ); // add it to ALIB config ALox.Init(); ALIB.Config.InsertPlugin( iniFile, Configuration.PrioIniFile ); ALIB.Config.Load( var.Define( "", "CUBA" ) ); UT_EQ( "a country" , var.GetString() ); ALIB.Config.Load( var.Define( "", "cUbA" ) ); UT_EQ( "a country" , var.GetString() ); ALIB.Config.Load( var.Define( "", "SIZE" ) ); UT_EQ( "25" , var.GetString() ); ALIB.Config.Load( var.Define( "", "concat" ) ); UT_EQ( 11 , var.Size()); UT_EQ( "start =5" , var.GetString(0) ); UT_EQ( "end =32" , var.GetString(1) ); ALIB.Config.Load( var.Define( "Great Section", "SectionVar" ) ); UT_EQ( "5" , var.GetString() ); ALIB.Config.Load( var.Define( "2nd Section", "SectionVar" ) ); UT_EQ( "6" , var.GetString() ); ALIB.Config.Load( var.Define( "Great Section", "SECTION_CONTINUED" ) ); UT_EQ( "yEs" , var.GetString() ); ALIB.Config.Load( var.Define( "Great Section", "Tricky" ) ); UT_EQ( "backslash\\", var.GetString() ); ALIB.Config.Load( var.Define( "Great Section", "SECTION_CONTINUED" ) ); UT_TRUE( var.IsTrue() ); // check if environment variable "home" overwrites INI file AString vIniFile= new AString(); iniFile.Load( var.Define( "", "hOme" ) ); UT_EQ( "overwritten_by_environment", var.GetString() ); int prio= ALIB.Config.Load( var.Define("", "hOme" )); if (prio != Configuration.PrioEnvironment ) // Windows platform? { prio= ALIB.Config.Load( var.Define("", "hOmePAth") ); iniFile.Load( var.Define( "", "hOmePAth") ); UT_EQ( "overwritten_by_environment", var.GetString() ); } UT_EQ( Configuration.PrioEnvironment, prio ); UT_TRUE( var.GetString().Length() > 0 ); UT_TRUE( !vIniFile.Equals( var.GetString()) ); // change a value and write a new one var.Define( "New Section", "newvar"); var.Priority= Configuration.PrioIniFile; UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store( var, "new" ) ); ALIB.Config.Load ( var.Define("New Section", "newvar") ); UT_EQ( "new", var.GetString() ); var.Define( "", "newvar"); var.Priority= Configuration.PrioIniFile; UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store( var, "aworx") ); ALIB.Config.Load ( var.Define("", "newvar") ); UT_EQ( "aworx", var.GetString() ); var.Define( "", "newvarList", ','); var.AddString("val1=5"); var.AddString("val2=10"); var.AddString("val3=hello"); var.Priority= Configuration.PrioIniFile; UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store(var) ); ALIB.Config.Load ( var.Define( "", "newvarList") ); var.Define( "", "commented", ',', "2lines" ); var.Priority= Configuration.PrioIniFile; UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store( var, "this is c-line 1 \nand this line 2" ) ); // write the file iniFile.FileName._(".writeback.txt"); iniFile.WriteFile(); // load the written file into another config IniFile readBack= new IniFile( iniFile.FileName.ToString() ); Variable varBack= new Variable(); // compare all UT_TRUE( (IniFile.Status.OK == readBack.LastStatus) ); { AString msg= new AString(); Substring orig= new Substring(); Substring back= new Substring(); foreach ( IniFile.Section section in iniFile.Sections ) { foreach ( IniFile.Entry entry in section.Entries ) { msg.Clear()._( "Reading variable " ).Field()._( section.Name )._( '/' )._( entry.Name ); UT_PRINT( msg ); char delim= '\0'; if( entry.Name.Equals("concat") || entry.Name.Equals("newvarList") ) delim= ','; iniFile .Load( var .Define( section.Name, entry.Name, delim) ); readBack.Load( varBack.Define( section.Name, entry.Name, delim) ); UT_EQ( var.Size(), varBack.Size() ); for ( int i= 0; i< var.Size(); i++ ) { int idx= var.GetString(i).IndexOf('='); if( idx < 0 ) { UT_EQ( var.GetString(i), varBack.GetString(i) ); } else { int idxBack= varBack.GetString(i).IndexOf('='); orig.Set( var .GetString(i), 0, idx ); back.Set( varBack.GetString(i), 0, idxBack ); UT_EQ( orig.Trim().ToString(), back.Trim().ToString() ); orig.Set( var .GetString(i), idx +1 ); back.Set( varBack.GetString(i), idxBack +1 ); UT_EQ( orig.Trim().ToString(), back.Trim().ToString() ); } } } } } readBack.Load ( var.Define( "New Section", "newvar" ) ); UT_EQ( "new" , var.GetString() ); readBack.Load ( var.Define( "", "newvar" ) ); UT_EQ( "aworx", var.GetString() ); ALIB.Config.RemovePlugin( iniFile ); ALIB.Config.InsertPlugin( readBack, Configuration.PrioIniFile ); ALIB.Config.Load ( var.Define( "New Section", "newvar") ); UT_EQ( "new" , var.GetString() ); ALIB.Config.Load ( var.Define( "", "newvar") ); UT_EQ( "aworx" , var.GetString() ); ALIB.Config.RemovePlugin( readBack ); }
/** **************************************************************************************** * Search the given \b %Substring in the this. * * @param needle The string to search. * @param startIdx The index to start the search at. Optional and defaults to 0. * @param sensitivity If true, the compare is case insensitive. Optional and defaults to * false. * @return -1 if the string is not found. Otherwise the index of first occurrence. ******************************************************************************************/ public int IndexOf( Substring needle, int startIdx= 0, Case sensitivity= Case.Sensitive ) { int length= Length(); if ( startIdx < 0 ) startIdx= 0; else if ( startIdx >= length ) return -1; int idx= needle != null ? CString.IndexOfString( needle.Buf, needle.Start, needle.Length(), Buf, Start + startIdx, length - startIdx, sensitivity ) : CString.IndexOfString( null, 0, 0, Buf, Start + startIdx, length - startIdx, sensitivity ); if ( idx < 0) return -1; return idx < 0 ? -1 : idx - Start; }
/** **************************************************************************************** * Splits this substring into two parts. What remains in this object is the region * from 0 to \p position. * \p target receives the rest. If \p separatorWidth is given, this is subtracted from * the front of \p target. * * @param position The index where this object is split. * @param target The target substring to receive the right part of the string. * @param separatorWidth This does not change what remains in this object, but defines * the number of characters that are cut from the front of the * \p target. Defaults to 0. * * @return \c this to allow concatenated calls. ******************************************************************************************/ public Substring Split( int position, Substring target, int separatorWidth =0 ) { CString.AdjustRegion( Length(), ref position, ref separatorWidth ); target.Set( this, position + separatorWidth, Length() - position - separatorWidth ); End= Start + position -1; return this; }
public void _() { // append char[] { AString ms= new AString(); char[] csNull= null; char[] csEmpty= "".ToCharArray(); ms._( csNull ); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( csEmpty); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNotNull() ); ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._("1234567".ToCharArray()); UT_EQ ( ms.Capacity(), 16 ); UT_EQ( "1234567", ms ); ms._("89" .ToCharArray()); UT_TRUE( ms.Length()> 7 ); UT_EQ( "123456789", ms ); char[] t= "0123456789".ToCharArray(); ms.Clear()._ ( t, 5); UT_EQ( ms, "56789" ); ms.Clear()._ ( t, 5, 100); UT_EQ( ms, "56789" ); ms.Clear()._ ( t, -5); UT_EQ( ms, "0123456789" ); ms.Clear()._ ( t, -5, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, 50, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, 10, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, -5, 10); UT_EQ( ms, "01234" ); ms.Clear()._ ( t, -5, 100); UT_EQ( ms, "0123456789" ); // _NC ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._NC( csEmpty,0,0); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms.Clear()._NC( t, 5,3); UT_EQ( ms, "567" ); } // append AString { AString ms= new AString(); AString asNull= null; AString asEmpty= new AString(""); AString t= new AString( "012" ); ms._( asNull ); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( asEmpty); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNotNull() ); ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( t ); UT_EQ ( ms.Capacity(), 16 ); UT_EQ( "012" , ms ); ms._( t ); UT_TRUE( ms.Length()> 3 ); UT_EQ( "012012", ms ); t.Clear()._( "0123456789" ); ms.Clear()._ ( t, 5); UT_EQ( ms, "56789" ); ms.Clear()._ ( t, 5, 100); UT_EQ( ms, "56789" ); ms.Clear()._ ( t, -5); UT_EQ( ms, "0123456789" ); ms.Clear()._ ( t, -5, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, 50, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, 10, 3); UT_EQ( ms, "" ); ms.Clear()._ ( t, -5, 10); UT_EQ( ms, "01234" ); ms.Clear()._ ( t, -5, 100); UT_EQ( ms, "0123456789" ); ms.Clear()._NC( t ); UT_EQ( ms, "0123456789" ); ms.Clear()._NC( t, 1, 9 ); UT_EQ( ms, "123456789" ); ms.Clear()._NC( t, 1, 2 ); UT_EQ( ms, "12" ); ms.Clear()._NC( t, 1, 8 ); UT_EQ( ms, "12345678" ); ms.Clear()._NC( t, 1, 9 ); UT_EQ( ms, "123456789" ); ms.Clear()._NC( t, 5, 5 ); UT_EQ( ms, "56789" ); // _NC ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._NC( asEmpty); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms.Clear()._NC( t, 5, 3); UT_EQ( ms, "567" ); } // append Substring { AString ms= new AString(); Substring ssNull= null; Substring ssEmpty= new Substring( "" ); Substring t= new Substring( "01234" ); ms._( ssNull ); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( ssEmpty); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNotNull() ); ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( t ); UT_EQ ( ms.Capacity(), 16 ); UT_EQ( "01234" , ms ); ms._( t ); UT_TRUE( ms.Length()> 5 ); UT_EQ( "0123401234", ms ); t.Consume(); ms.Clear()._( t ); UT_EQ( ms, "1234" ); t.Consume(); ms.Clear()._( t ); UT_EQ( ms, "234" ); t.ConsumeFromEnd(); ms.Clear()._( t ); UT_EQ( ms, "23" ); // _NC ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._NC( ssEmpty ); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms.Clear()._NC( t); UT_EQ( ms, "23" ); } // append String { AString ms= new AString(); String sEmpty= ""; String t= "012"; ms._( sEmpty); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNotNull() ); ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._( t ); UT_EQ ( ms.Capacity(), 16 ); UT_EQ( "012" , ms ); ms._( t ); UT_TRUE( ms.Length()> 3 ); UT_EQ( "012012", ms ); t= "0123456789"; ms.Clear()._( t, 5); UT_EQ( ms, "56789" ); ms.Clear()._( t, 5, 100); UT_EQ( ms, "56789" ); ms.Clear()._( t, -5); UT_EQ( ms, "0123456789" ); ms.Clear()._( t, -5, 3); UT_EQ( ms, "" ); ms.Clear()._( t, 50, 3); UT_EQ( ms, "" ); ms.Clear()._( t, 10, 3); UT_EQ( ms, "" ); ms.Clear()._( t, -5, 10); UT_EQ( ms, "01234" ); ms.Clear()._( t, -5, 100); UT_EQ( ms, "0123456789" ); // _NC ms.SetNull(); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms._NC( sEmpty ); UT_EQ ( ms.Length(), 0 ); UT_TRUE( ms.IsNull() ); ms.Clear()._NC( t); UT_EQ( ms, "0123456789" ); ms.Clear()._NC( t ,2,3); UT_EQ( ms, "234" ); } // append String Builders { AString ms= new AString(); StringBuilder t= new StringBuilder( "012"); ms._( t ); UT_EQ ( ms.Capacity(), 16 ); UT_EQ( "012" , ms ); ms._( t ); UT_TRUE( ms.Length()> 3 ); UT_EQ( "012012", ms ); t.Clear().Append("0123456789"); ms.Clear()._( t, 5); UT_EQ( ms, "56789" ); ms.Clear()._( t, 5, 100); UT_EQ( ms, "56789" ); ms.Clear()._( t, -5); UT_EQ( ms, "0123456789" ); ms.Clear()._( t, -5, 3); UT_EQ( ms, "" ); ms.Clear()._( t, 50, 3); UT_EQ( ms, "" ); ms.Clear()._( t, 10, 3); UT_EQ( ms, "" ); ms.Clear()._( t, -5, 10); UT_EQ( ms, "01234" ); ms.Clear()._( t, -5, 100); UT_EQ( ms, "0123456789" ); } }
/** **************************************************************************************** * Interprets given \p src as a value of enum type * \ref cs::aworx::lib::enums::Inclusion "enums.Inclusion". * If the case insensitive comparison of the first non-whitespace characters of the string * with values "i", "y", "t", "1" * matches, \b %Inclusion.Include is returned. * Otherwise, including the case that \p src is 'nulled', \b %Inclusion.Exclude is returned. * * @param src The string to 'parse'. * * @returns The \b %Inclusion value read. ******************************************************************************************/ public static Inclusion ReadInclusion( Substring src ) { int idx= src.IndexOfAny( CString.DefaultWhitespaces, Inclusion.Exclude ); if ( idx >= 0 ) { int c= Char.ToLower( src.CharAt(idx) ); foreach ( char v in trueValuesInclusion ) if ( c == v ) return Inclusion.Include; } return Inclusion.Exclude; }
public void Constructors() { AString ms; ms= new AString(); UT_TRUE ( ms.Capacity() == 0 && ms.Length() == 0 ); ms= new AString(25); UT_TRUE ( ms.Capacity() == 25 && ms.Length() == 0 ); ms= new AString("Test"); UT_TRUE ( ms.Capacity() >= 4 && ms.Length() == 4 ); AString tMSEmpty= new AString ( ); ms= new AString( (AString) null ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() == 0 ); ms._( "was null" ); UT_TRUE ( ms.Length() == 8 ); ms= new AString( tMSEmpty ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tMSEmpty, 1, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tMSEmpty, -1000, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tMSEmpty, -1000, 1 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tMSEmpty, -100, 1000); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); AString tMS= new AString ( "0123456789" ); ms= new AString( tMS ); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tMS, 5); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tMS, 5, 100); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tMS, -5); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tMS, -5, 3); UT_TRUE ( ms.Capacity() > 0 && ms.Length() == 0 ); UT_EQ( ms, ""); ms= new AString( tMS, 50, 3); UT_TRUE ( ms.Capacity() > 0 && ms.Length() == 0 ); UT_EQ( ms, ""); ms= new AString( tMS, 10, 3); UT_TRUE ( ms.Capacity() > 0 && ms.Length() == 0 ); UT_EQ( ms, ""); ms= new AString( tMS, -5, 10); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "01234"); ms= new AString( tMS, -5, 100); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); Substring tSS= new Substring ( "ABCDE0123456789FG".ToCharArray(), 5,10 ); ms= new AString( tSS ); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); String tS= "0123456789"; ms= new AString( (String) null ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() == 0 ); ms._( "was null" ); UT_TRUE ( ms.Length() == 8 ); ms= new AString( "" ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( "", 1, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( "", -1000, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( "", -1000, 1 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( "", -100, 1000); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tS ); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tS, 5); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tS, 5, 100); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tS, -5); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tS, -5, 10); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "01234"); ms= new AString( tS, -5, 100); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); StringBuilder tSBEmpty= new StringBuilder ( ); ms= new AString( (StringBuilder) null ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() == 0 ); ms._( "was null" ); UT_TRUE ( ms.Length() == 8 ); ms= new AString( tSBEmpty ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tSBEmpty, 1, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tSBEmpty, -1000, 0 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tSBEmpty, -1000, 1 ); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); ms= new AString( tSBEmpty, -100, 1000); UT_TRUE ( ms.Length() == 0 ); UT_TRUE ( ms.Capacity() > 0 ); StringBuilder tSB= new StringBuilder ( "0123456789" ); ms= new AString( tSB ); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tSB, 5); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tSB, 5, 100); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "56789"); ms= new AString( tSB, -5); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); ms= new AString( tSB, -5, 10); UT_TRUE ( ms.Capacity() >= 5 && ms.Length() == 5 ); UT_EQ( ms, "01234"); ms= new AString( tSB, -5, 100); UT_TRUE ( ms.Capacity() >= 10 && ms.Length() == 10 ); UT_EQ( ms, "0123456789"); }
/** **************************************************************************************** * Cuts the given number of characters from the end of the Substring and optionally * places the portion that was cut in parameter \p target (if provided).<br> * Parameter \p regionLength is checked to be between 0 and length. If negative, nothing * is cut and \p target is set empty. If \p regionLength is greater than this * objects' length, all contents is 'moved' to \p target. * * @param regionLength The length of the region at the start to delete. * @param target An optional target \b %Substring that receives the portion that * is cut from this object. Defaults to null. * * @return The new length of the substring. ******************************************************************************************/ public int ConsumeFromEnd( int regionLength, Substring target= null ) { if ( regionLength < 0 ) { if ( target != null ) target.Clear(); return Length(); } if ( regionLength > Length() ) regionLength= Length(); if ( target != null ) target.Set( this, Length() - regionLength, regionLength ); End-= regionLength; hash= 0; return Length(); }
/** **************************************************************************************** * Interprets given \p src as a value of enum type * \ref aworx.lib::enums::Case "enums.Case". * If the case insensitive comparison of the first non-whitespace characters of the string * with values "s", "y", "t", "1" * matches, \b %Case.Sensitive is returned. * Otherwise, including the case that \p src is 'nulled', \b %Case.Ignore is returned. * * @param src The string to 'parse'. * * @returns The \b %Case value read. ******************************************************************************************/ public static Case ReadCase( Substring src ) { int idx= src.IndexOfAny( CString.DefaultWhitespaces, Inclusion.Exclude ); if ( idx >= 0 ) { int c= Char.ToLower( src.CharAt(idx) ); foreach ( char v in trueValuesCase ) if ( c == v ) return Case.Sensitive; } return Case.Ignore; }
public void Trim() { Substring subs= new Substring(); { String t; t= "" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "" , subs.ToString() ); t= " " ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "" , subs.ToString() ); t= " " ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "" , subs.ToString() ); t= "abc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= "abc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= " abc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= " abc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= " \tabc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= "\t abc" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= "abc\r\n" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= "\tabc\r\n" ; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc" , subs.ToString() ); t= "\tabc\rx\n"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ( "abc\rx" , subs.ToString() ); } }
void InternalizeValue( Substring src, AString dest ) { src.Trim(); bool inUnquoted= true; bool inQuote= false; bool lastWasSlash= false; while( src.IsNotEmpty() ) { char c= src.Consume(); if( lastWasSlash ) { lastWasSlash= false; char escChr= c == '\\' ? '\\' : c == 'n' ? '\n' : c == 'r' ? '\r' : c == 't' ? '\t' : c == '"' ? '"' : c; dest._(escChr); continue; } if( c== '\\' ) { lastWasSlash= true; continue; } if( c== '"' ) { inQuote= !inQuote; inUnquoted= false; continue; } if( inQuote || inUnquoted ) { dest._(c); continue; } if( CString.IndexOf( CString.DefaultWhitespaces, c) >= 0 ) continue; inUnquoted= true; dest._(c); } }
public void CompareTo() { Substring tss= new Substring(); // null string comparison String nullString= null; UT_TRUE( tss.CompareTo( nullString) == 0 ); UT_TRUE( tss.Equals( nullString) ); tss.Set( "" ); UT_TRUE( tss.CompareTo( nullString) != 0 ); UT_TRUE( !tss.Equals( nullString) ); tss.SetNull(); UT_TRUE( tss.CompareTo( nullString) == 0 ); UT_TRUE( tss.Equals( nullString) ); String t= "abcde"; tss= new Substring("01"+t+"234", 2, t.Length ); int result, resSys; result= tss.CompareTo( t ); UT_EQ( t.CompareTo( t ), result ); result= tss.CompareTo( t + "x" ); UT_EQ( t.CompareTo( t + "x" ), result ); result= tss.CompareTo( t.Substring (0, t.Length -1 ) ); UT_EQ( t.CompareTo( t.Substring (0, t.Length -1 ) ), result ); result= tss.CompareTo( "pad" + t, Case.Sensitive, 3 ); UT_EQ( 0, result ); result= tss.CompareTo( "pad" + t, Case.Sensitive, 3, 2 ); UT_EQ( 1, result ); // greater/smaller strings { String greater= "x"; String greater2= "abcdef"; String smaller= "aaa"; String smaller2= "abcd"; result= tss.CompareTo( greater ); resSys= t.CompareTo( greater ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( greater2 ); resSys= t.CompareTo( greater2 ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller ); resSys= t.CompareTo( smaller ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller2 ); resSys= t.CompareTo( smaller2 ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); String ut= t.ToUpper(); UT_FALSE( 0 == tss.CompareTo( ut ) ); UT_FALSE( 0 == tss.CompareTo( ut, Case.Sensitive )); UT_TRUE ( 0 == tss.CompareTo( ut, Case.Ignore )); } { AString greater= new AString( "x" ); AString greater2= new AString( "abcdef" ); AString smaller= new AString( "aaa" ); AString smaller2= new AString( "abcd" ); result= tss.CompareTo( greater ); resSys= t.CompareTo( greater .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( greater2 ); resSys= t.CompareTo( greater2 .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller ); resSys= t.CompareTo( smaller .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller2 ); resSys= t.CompareTo( smaller2 .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); AString ut= new AString (t.ToUpper()); UT_FALSE( 0 == tss.CompareTo( ut ) ); UT_FALSE( 0 == tss.CompareTo( ut, Case.Sensitive )); UT_TRUE ( 0 == tss.CompareTo( ut, Case.Ignore )); } { Substring greater= new Substring( "123x" , 3 ); Substring greater2= new Substring( "123abcdef" , 3 ); Substring smaller= new Substring( "123aaa" , 3 ); Substring smaller2= new Substring( "123abcd" , 3 ); result= tss.CompareTo( greater ); resSys= t.CompareTo( greater .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( greater2 ); resSys= t.CompareTo( greater2 .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller ); resSys= t.CompareTo( smaller .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); result= tss.CompareTo( smaller2 ); resSys= t.CompareTo( smaller2 .ToString() ); UT_TRUE( result==resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0) ); Substring ut= new Substring( t.ToUpper() ); UT_FALSE( 0 == tss.CompareTo( ut ) ); UT_FALSE( 0 == tss.CompareTo( ut, Case.Sensitive )); UT_TRUE ( 0 == tss.CompareTo( ut, Case.Ignore )); } }
// ############################################################################################# // Internals // ############################################################################################# /** **************************************************************************************** * Internal, recursive helper of #Find. * * @param domainPath Path to search. * @param sensitivity Denotes if domain name search is treated case sensitive or not. * @param maxCreate The maximum number of sub domains that are created if not * found at the end of the path. * @param[out] wasCreated Output parameter that is set \c true if domain was not found * and hence created. * @return The domain found or created. ******************************************************************************************/ protected Domain findRecursive( Substring domainPath, Case sensitivity, int maxCreate, ref bool wasCreated ) { //--- get act sub-name and rest of path domainPath.Consume( Separator ); int endSubName= domainPath.IndexOf( Separator ); ALIB.ASSERT_ERROR( endSubName != 0, "Internal Error" ); // find end of actual domain name and save rest Substring restOfDomainPath= tSubstring2; restOfDomainPath.SetNull(); if ( endSubName > 0 ) domainPath.Split( endSubName, restOfDomainPath, 1 ); // search sub-domain Domain subDomain= null; // "." if( domainPath.Equals( "." ) ) subDomain= this; // ".." else if( domainPath.Equals( ".." ) ) subDomain= Parent != null ? Parent : this; // search in sub-domain else { int i; bool fixedOnce= false; for(;;) { for( i= 0; i< SubDomains.Count; i++ ) { int comparison= SubDomains[i].Name.CompareTo( domainPath, sensitivity ); if( comparison >= 0 ) { if ( comparison == 0 ) subDomain= SubDomains[i]; break; } } // domain found? if ( subDomain != null ) break; // try and fix name if( !fixedOnce ) { fixedOnce= true; bool illegalCharacterFound= false; for( int cp= 0; cp< domainPath.Length() ; ++cp ) { char c= domainPath.CharAt(cp); if ( c < '-' || c > 'z' || c == '<' || c == '>' || c == '[' || c == ']' || c == '=' || c == '?' || c == ';' || c == ':' || c == '\\'|| c == '\''|| c == '.' || c == ',' ) { illegalCharacterFound= true; domainPath.Buf[domainPath.Start + cp]= '#'; } } if ( illegalCharacterFound ) continue; } // create if ( maxCreate == 0 ) return null; wasCreated= true; SubDomains.Insert( i, subDomain= new Domain( this, new AString( domainPath ) ) ); maxCreate--; if ( maxCreate == 0 ) return subDomain; break; } } // recursion? if ( restOfDomainPath.IsNotEmpty() ) { domainPath.Set( restOfDomainPath ); return subDomain.findRecursive( domainPath, sensitivity, maxCreate, ref wasCreated ); } // that's it return subDomain; }
public void ParseNumbers() { // ConsumeInteger() { Substring subs= new Substring(); int result; UT_EQ( false, subs.ConsumeInteger( out result ) ); UT_EQ( 0, result ); subs.Set( "" ); UT_EQ( false, subs.ConsumeInteger( out result ) ); UT_EQ( 0, result ); subs.Set( " ABC" ); UT_EQ( false, subs.ConsumeInteger( out result ) ); UT_EQ( 0, result ); subs.Set( " 12345" ); UT_EQ( true , subs.ConsumeInteger( out result ) ); UT_EQ( 12345, result ); subs.Set( " 12 45" ); UT_EQ( true , subs.ConsumeInteger( out result ) ); UT_EQ( 12, result ); UT_EQ( true , subs.ConsumeInteger( out result ) ); UT_EQ( 45, result ); subs.Set( " 42 ; 7 ; 6 "); UT_EQ( true , subs.ConsumeInteger( out result ) ); UT_EQ( 42, result ); UT_EQ( false, subs.ConsumeInteger( out result ) ); UT_EQ( 0, result ); UT_EQ( false, subs.ConsumeInteger( out result ) ); UT_EQ( 0, result ); char[] ws= " ;".ToCharArray(); subs.Set( " 42 ; 7 ; 6 "); UT_EQ( true , subs.ConsumeInteger( out result, ws ) ); UT_EQ( 42, result ); UT_EQ( true , subs.ConsumeInteger( out result, ws ) ); UT_EQ( 7, result ); UT_EQ( true , subs.ConsumeInteger( out result, ws ) ); UT_EQ( 6, result ); UT_EQ( false, subs.ConsumeInteger( out result, ws ) ); UT_EQ( 0, result ); UT_EQ( false, subs.ConsumeInteger( out result, ws ) ); UT_EQ( 0, result ); } // ConsumeFloat() { Substring subs= new Substring(); double result; UT_EQ( false, subs.ConsumeFloat ( out result ) ); UT_EQ( 0.0, result ); subs.Set( "" ) ; UT_EQ( false, subs.ConsumeFloat ( out result ) ); UT_EQ( 0.0, result ); subs.Set( " ABC" ) ; UT_EQ( false, subs.ConsumeFloat ( out result ) ); UT_EQ( 0.0, result ); subs.Set( " 12345" ) ; UT_EQ( true , subs.ConsumeFloat ( out result ) ); UT_EQ( 12345.0, result ); subs.Set( " 12.45 " ) ; UT_EQ( true , subs.ConsumeFloat ( out result ) ); UT_EQ( 12.45, result ); subs.Set( " 12 45" ) ; UT_EQ( true , subs.ConsumeFloat ( out result ) ); UT_EQ( 12.0, result ); UT_EQ( true , subs.ConsumeFloat ( out result ) ); UT_EQ( 45.0, result ); char[] ws= " ;".ToCharArray(); subs.Set( " 42.3 ; 0.7 ; 6 " ); UT_EQ( true , subs.ConsumeFloat ( out result, null, ws ) ); UT_EQ( 42.3, result ); UT_EQ( true , subs.ConsumeFloat ( out result, null, ws ) ); UT_EQ( 0.7, result ); UT_EQ( true , subs.ConsumeFloat ( out result, null, ws ) ); UT_EQ( 6.0, result ); UT_EQ( false, subs.ConsumeFloat ( out result, null, ws ) ); UT_EQ( 0.0, result ); UT_EQ( false, subs.ConsumeFloat ( out result, null, ws ) ); UT_EQ( 0.0, result ); } }
/** ******************************************************************************************** * Processes the next command found in the format string, by writing formatted information * into the given buffer. * The given Substring holds the next command. When method returns, the command is cut * from the front. * * @param logger The logger that we are embedded in. * @param domain The <em>Log Domain</em>. * @param verbosity The verbosity. This has been checked to be active already on this * stage and is provided to be able to be logged out only. * @param scope Information about the scope of the <em>Log Statement</em>.. * @param dest The buffer to write meta information into. * @param variable The variable to read (may have more characters appended) * * @return The number of tab sequences that were written (by adding ESC::TAB to the buffer). **********************************************************************************************/ protected virtual int processVariable( TextLogger logger, Domain domain, Verbosity verbosity, ScopeInfo scope, AString dest, Substring variable ) { // process commands char c2; switch ( variable.Consume() ) { // scope info case 'S': { // read sub command AString val; switch( c2= variable.Consume() ) { case 'P': // SP: full path { int length; String path= scope.GetFullPath( out length ); if ( length > 0 ) { dest._( path, 0, length ); return 0; } val= NoSourceFileInfo; } break; case 'p': // Sp: trimmed path { val= scope.GetTrimmedPath(); if ( val.IsEmpty() ) val= NoSourceFileInfo; } break; case 'F': // file name { val= scope.GetFileName(); if ( val.IsEmpty() ) val= NoSourceFileInfo; } break; case 'f': // file name without extension { val= scope.GetFileNameWithoutExtension(); if ( val.IsEmpty() ) val= NoSourceFileInfo; } break; case 'M': // method name { String method= scope.GetMethod(); if ( method.Length == 0 ) dest._( NoMethodInfo ); else dest._( method ); return 0; } case 'L': // line number { dest._( scope.GetLineNumber() ); return 0; } default: { if( !warnedOnce ) { warnedOnce= true; ALIB.WARNING( "Unknown format variable '%S" + c2 + "\' (only one warning)" ); } dest._( "%ERROR" ); return 0; } } dest._( val ); return 0; } // %Tx: Time case 'T': { // read sub command c2= variable.Consume(); // %TD: Date if ( c2 == 'D' ) { // get time stamp as DateTime once if ( callerDateTime == null ) callerDateTime= scope.GetTimeStamp().InDotNetDateTime(); // avoid the allocation of a) a StringBuilder (yes, a string builder is allocated inside StringBuilder.AppendFormat!) // and b) a DateTime object, if the format is the unchanged standard. And it is faster anyhow. if ( DateFormat.Equals( "yyyy-MM-dd" ) ) { dest ._( callerDateTime.Value.Year, 4 )._( '-' ) ._( callerDateTime.Value.Month, 2 )._( '-' ) ._( callerDateTime.Value.Day, 2 ); } // support user defined standards else { // detect changes of format string since last log if ( detectDateFormatChanges != DateFormat ) { detectDateFormatChanges= DateFormat; dateFormatString= "{0:" + DateFormat + "}"; } // get date string from system and append to log buffer formatSB.Clear(); formatSB.AppendFormat( CultureInfo.InvariantCulture, dateFormatString, callerDateTime ); dest._( formatSB ); } } // %TT: Time of Day else if ( c2 == 'T' ) { // get time stamp as DateTime once if ( callerDateTime == null ) callerDateTime= scope.GetTimeStamp().InDotNetDateTime(); // avoid the allocation of a) a StringBuilder (yes, a string builder is allocated inside StringBuilder.AppendFormat!) // and b) a DateTime object, if the format is the unchanged standard. And it is faster anyhow. if ( TimeOfDayFormat.Equals( "HH:mm:ss" ) ) { dest ._( callerDateTime.Value.Hour, 2 )._( ':' ) ._( callerDateTime.Value.Minute, 2 )._( ':' ) ._( callerDateTime.Value.Second, 2 ); } // support user defined standards else { // detect changes of format string since last log if ( detectTimeOfDayFormatChanges != TimeOfDayFormat ) { detectTimeOfDayFormatChanges= TimeOfDayFormat; timeOfDayFormatString= "{0:" + TimeOfDayFormat + "}"; } // get time string from system and append to log buffer formatSB.Clear(); formatSB.AppendFormat( CultureInfo.InvariantCulture, timeOfDayFormatString, callerDateTime); dest ._( formatSB ); } } // %TC: Time elapsed since created else if ( c2 == 'C' ) { elapsedTime.Set( scope.GetTimeStamp() ); elapsedTime.Sub( logger.TimeOfCreation ); if( MaxElapsedTime.Raw() < elapsedTime.Raw() ) MaxElapsedTime.Set( elapsedTime ); long maxElapsedSecs= MaxElapsedTime.InSeconds(); TimeSpan elapsed= new TimeSpan( elapsedTime.Raw() ); if ( maxElapsedSecs >= 60*60*24 ) dest._( elapsed.Days )._NC( TimeElapsedDays ); if ( maxElapsedSecs >= 60*60 ) dest._( elapsed.Hours , maxElapsedSecs >= 60*60*10 ? 2 : 1 )._( ':' ); if ( maxElapsedSecs >= 60 ) dest._( elapsed.Minutes, maxElapsedSecs >= 10*60 ? 2 : 1 )._( ':' ); dest._( elapsed.Seconds, maxElapsedSecs > 9 ? 2 : 1 )._NC( '.' ); dest._( elapsed.Milliseconds, 3 ); } // %TL: Time elapsed since last log call else if ( c2 == 'L' ) writeTimeDiff( dest, scope.GetTimeStamp().Since(logger.TimeOfLastLog).InNanos() ); else { if( !warnedOnce ) { warnedOnce= true; ALIB.WARNING( "Unknown format variable '%T" + c2 + "\' (only one warning)" ); } dest._( "%ERROR" ); } return 0; } // thread name / ID case 't': { c2= variable.Consume(); if ( c2 == 'N' ) { dest.Field() ._( scope.GetThreadName() ) .Field( logger.AutoSizes.Next( scope.GetThreadName().Length(), 0 ), Alignment.Center ); } else if ( c2 == 'I' ) { tmpAString._()._( scope.GetThreadID() ); dest.Field() ._( tmpAString ) .Field( logger.AutoSizes.Next( tmpAString .Length(), 0 ), Alignment.Center ); } else { if( !warnedOnce ) { warnedOnce= true; ALIB.WARNING( "Unknown format variable '%t" + c2 + "\' (only one warning)" ); } dest._( "%ERROR" ); } return 0; } case 'L': { c2= variable.Consume(); if ( c2 == 'G' ) dest._NC( logger.GetName() ); else if ( c2 == 'X' ) dest._NC( scope.GetLoxName() ); else { if( !warnedOnce ) { warnedOnce= true; ALIB.WARNING( "Unknown format variable '%L" + c2 + "\' (only one warning)" ); } dest._( "%ERROR" ); return 0; } return 0; } case 'P': { dest._NC( Util.GetProcessName() ); return 0; } case 'V': dest._ ( verbosity == Verbosity.Error ? VerbosityError : verbosity == Verbosity.Warning ? VerbosityWarning : verbosity == Verbosity.Info ? VerbosityInfo : VerbosityVerbose ); return 0; case 'D': { dest.Field()._( domain.FullPath ).Field( logger.AutoSizes.Next( domain.FullPath.Length(), 0 ), Alignment.Left ); return 0; } case '#': dest._( logger.CntLogs, LogNumberMinDigits ); return 0; // A: Auto tab case 'A': { // read extra space from format string int oldStart= variable.Start; int extraSpace; variable.ConsumeInteger( out extraSpace ); if ( oldStart == variable.Start ) extraSpace= 1; // insert ESC code to jump to next tab level extraSpace= Math.Min( extraSpace, 10 + ('Z'-'A') ); char escNo= extraSpace < 10 ? (char) ( '0' + extraSpace ) : (char) ( 'A' + extraSpace ); dest._( "\x1Bt" )._( escNo ); return 1; } default: { if( !warnedOnce ) { warnedOnce= true; ALIB.WARNING( "Unknown format variable \'" + variable.Buf[variable.Start - 1] + "\'" ); } dest._( "%ERROR" ); } return 0; } }
/** **************************************************************************************** * Interprets given \p src as a verbosity. * A case insensitive comparison of only the first (!) character of the start of the string * is performed (against 'v', 'i', 'w' and 'e'). * If no match is found, \e %Verbosity::Off is returned. * @param src The string to 'parse'. * @returns The verbosity read. ******************************************************************************************/ public static Verbosity ReadVerbosity( Substring src ) { int idx= src.IndexOfAny( CString.DefaultWhitespaces, Inclusion.Exclude ); if ( idx >= 0 ) { char c= Char.ToLower( src.CharAt(idx) ); if ( c == 'v' ) return Verbosity.Verbose; if ( c == 'i' ) return Verbosity.Info; if ( c == 'w' ) return Verbosity.Warning; if ( c == 'e' ) return Verbosity.Error; } return Verbosity.Off; }
/** **************************************************************************************** * Helper method used when reading file. * @param subs A sub-string. * @return true if provided substring starts with comment character. ******************************************************************************************/ protected bool startsWithCommentSymbol( Substring subs ) { int i= commentChars.IndexOf( subs.CharAtStart() ); return ( i >= 0 && i < 2) || ( i == 2 && subs.CharAt(1) == '/' ); }
/** ############################################################################################ * @name Set Data ##@{ ########################################################################################*/ /** **************************************************************************************** * Sets the substring to represent a region of the given \b %Substring. * * @param src The substring to copy from. * @param regionStart The start of the region within \p src. Defaults to 0. * @param regionLength The length of the region within \p src. * If negative, length of the provided \e src is used. * Defaults to -1. * @return \c this to allow concatenated calls. ******************************************************************************************/ public Substring Set( Substring src, int regionStart =0, int regionLength =-1 ) { Buf= src.Buf; Start= src.Start + regionStart; End= Start + ( regionLength < 0 ? src.Length() - regionStart : regionLength ) -1; hash= 0; return this; }