Example #1
0
 public static Uri FormatUri(FormattableString formattableString)
 => formattableString == null
      ? throw new ArgumentNullException(nameof(formattableString))
      : new Uri((FormatStringParser.Parse(formattableString.Format,
                                          (s, i, len) => s.HasWhiteSpace(i, len),
                                          delegate { return(false); })
                 .Any(hws => hws)
                 ? FormattableStringFactory.Create(
                     string.Join(string.Empty, FormatStringParser.Parse(formattableString.Format, (s, i, len) => Regex.Replace(s.Substring(i, len), @"\s+", string.Empty), (s, i, len) => s.Substring(i, len))),
                     formattableString.GetArguments())
                 : formattableString).ToString(UriFormatProvider.InvariantCulture));
		}   // TestFormatItemBuilders


		internal static void TestFormatItemCounters ( )
		{
			const string ANNOUNCE_BEGIN = @"{1}    Format strings to test = {0}{1}";
			const string ANNOUNCE_DONE = @"{1}    {0} format strings have been tested.";
			const string PROGRESS_MSG = @"        String {0,2:N0}: {1}{3}                   Highest Index: {2}{3}";
			const string FORMAT_STRING_ERRORS_PROLOGUE = @"{0} error found in format string: {1}{2}";

			int intNStrings = s_astrFormatStringSamples.Length;

			int [ ] aintHightesItemIndex = new int [ intNStrings ];

			ASCII_Table_Gen ( );

			Console.WriteLine (
				ANNOUNCE_BEGIN ,
				intNStrings ,
				Environment.NewLine );

			try
			{
				for ( int intCurrStr = ARRAY_FIRST_ELEMENT ;
						  intCurrStr < intNStrings ;
						  intCurrStr++ )
				{
					int intStringNumber = intCurrStr
										  + ARRAY_LIST_ORDINAL_TO_SUBSCRIPT;

					FormatStringParser fsp = new FormatStringParser ( s_astrFormatStringSamples [ intCurrStr ] );
					aintHightesItemIndex [ intCurrStr ] = fsp.HighestFormatItemIndex;

					Console.WriteLine (
						PROGRESS_MSG ,
						new object [ ]
                {
                    intStringNumber ,											// Format Item 0
                    s_astrFormatStringSamples [ intCurrStr ] ,					// Format Item 1
                    aintHightesItemIndex [ intCurrStr ] ,						// Format Item 2
                    Environment.NewLine											// Format Item 3
                } );

					if ( fsp.FormatStringErrorCount > FormatStringParser.NO_ERRORS )
					{
						Console.WriteLine (
							FORMAT_STRING_ERRORS_PROLOGUE ,						// Message template string
							fsp.FormatStringErrorCount ,						// Format Item 0 = Error count
							StringTricks.AdjustNumberOfNoun (					// Format Item 1 = Aligned error count
								( uint ) fsp.FormatStringErrorCount ,           //		puintNumber
								Properties.Resources.ERRMSG_LITERAL_ERROR ,     //		pstrNounSingular
								null ,                                          //		pstrPluralForm
								fsp.FormatString ) ,                            //		pstrPhrase
							Environment.NewLine );								// Format Item 2 = Embedded Newline
						ListAllErrors ( fsp.FormatStringErrors );
						Console.WriteLine ( );
					}   // if ( fsp.FormatStringErrorCount > FormatStringParser.NO_ERRORS )
				}   // for ( int intCurrStr = ARRAY_FIRST_ELEMENT ; intCurrStr < intNStrings ; intCurrStr++ )
			}
			catch ( Exception exAll )
			{	// Preserve this expected exception report in the standard output, in addition to sending it to Standard Error.
				Program.s_smTheApp.AppExceptionLogger.ReportException ( exAll );
			}

			//	----------------------------------------------------------------
			//	After adding another format item into which to have WriteLine
			//	stuff the newline, I searched down here, only to find that the
			//	argument list already had one. The lesson from this is that you
			//	can have extra format items in the list, but the list must have
			//	at least enough format items to account for the highest index in
			//	the format control string.
			//	----------------------------------------------------------------

			Console.WriteLine (
				ANNOUNCE_DONE ,
				intNStrings ,
				Environment.NewLine );
		}   // TestFormatItemCounters