private string ReplaceText(string format) { StringBuilder builder = new StringBuilder(); foreach (PythonTuple pt in StringFormatParser.Parse(format)) { string text = (string)pt[0]; string fieldName = (string)pt[1]; string formatSpec = (string)pt[2]; string conversionStr = (string)pt[3]; char? conversion = conversionStr != null && conversionStr.Length > 0 ? conversionStr[0] : (char?)null; builder.Append(text); if (fieldName != null) { // get the argument value object argValue = GetArgumentValue(ParseFieldName(fieldName, true)); // apply the conversion argValue = ApplyConversion(conversion, argValue); // handle computed format specifiers formatSpec = ReplaceComputedFormats(formatSpec); // append the string builder.Append(Builtin.format(_context.SharedContext, argValue, formatSpec)); } } return(builder.ToString()); }
/// <inheritdoc/> public IApplicationStructureConfigurationBuilder Include(ApplicationArea area, string format) { if (!format.StartsWith("[")) { format = $"[.]{format}"; } var formatsByArea = new Dictionary <ApplicationArea, IEnumerable <IStringFormat> >(_structureFormats); var parser = new StringFormatParser(); var stringFormat = parser.Parse(format); List <IStringFormat> formats; if (formatsByArea.ContainsKey(area)) { formats = new List <IStringFormat>(formatsByArea[area]); } else { formats = new List <IStringFormat>(); } formats.Add(stringFormat); formatsByArea[area] = formats; var builder = new ApplicationStructureConfigurationBuilder(formatsByArea); return(builder); }
public void Calling_Parse_With_A_String_Without_Placeholders_Returns_Error() { // Arrange var input = "test"; // Act var actual = StringFormatParser.Parse(input); // Assert actual.IsSuccessful.Should().BeFalse(); actual.ErrorMessages.Should().HaveCount(1); actual.ErrorMessages.First().Should().Be("No format placeholders were found"); }
public void Calling_Parse_With_Too_Many_CloseBrackets_Returns_Error() { // Arrange var input = "}}"; // Act var actual = StringFormatParser.Parse(input); // Assert actual.IsSuccessful.Should().BeFalse(); actual.ErrorMessages.Should().HaveCount(1); actual.ErrorMessages.First().Should().Be("Too many close brackets found"); }
public void Calling_Parse_With_Empty_FormatString_Returns_Error() { // Arrange var input = ""; // Act var actual = StringFormatParser.Parse(input, new[] { "A" }); // Assert actual.IsSuccessful.Should().BeFalse(); actual.ErrorMessages.Should().HaveCount(1); actual.ErrorMessages.First().Should().Be("Format string is empty"); }
public void CanParseFormatStringWithArgumentUsedMultipleTimes() { // Arrange const string FormatString = "Hello {0}{1}{1}{1}"; var args = new object[] { "world", "!" }.ToArray(); // Act var actual = StringFormatParser.Parse(FormatString, args); // Assert actual.IsSuccessful.Should().BeTrue(); actual.ErrorMessages.Should().BeEmpty(); var contents = string.Join("|", actual.Values.Select(kvp => string.Format("{0};{1}", kvp.Key, kvp.Value))); contents.Should().Be("0;world|1;!"); }
public void CanParseFormatStringWithFormatter() { // Arrange const string FormatString = "Hello {0}{1} on {2:dd-MM-yyyy}"; var args = new object[] { "world", "!", new DateTime(2018, 1, 1) }.ToArray(); // Act var actual = StringFormatParser.Parse(FormatString, args); // Assert actual.IsSuccessful.Should().BeTrue(); actual.ErrorMessages.Should().BeEmpty(); var contents = string.Join("|", actual.Values.Select(kvp => string.Format("{0};{1}", kvp.Key, kvp.Value))); contents.Should().Be($"0;world|1;!|2;{new DateTime(2018, 1, 1)}"); }
public void CanParseFormatStringWithMissingArguments() { // Arrange const string FormatString = "Hello {0}{1}"; var args = new object[] { "world" }.ToArray(); // Act var actual = StringFormatParser.Parse(FormatString, args); // Assert actual.IsSuccessful.Should().BeFalse(); actual.ErrorMessages.Should().HaveCount(1); actual.ErrorMessages.ElementAt(0).Should().Be("Format placeholders count (2) is not equal to column values count (1), see #MISSING# in format values list (values)"); var contents = string.Join("|", actual.Values.Select(kvp => string.Format("{0};{1}", kvp.Key, kvp.Value))); contents.Should().Be("0;world|1;#MISSING#"); }
public void CanParseFormatStringWithMissingFormatSpecifiers() { // Arrange const string FormatString = "Hello {0}{1"; var args = new object[] { "world", "!" }.ToArray(); // Act var actual = StringFormatParser.Parse(FormatString, args); // Assert actual.IsSuccessful.Should().BeFalse(); actual.ErrorMessages.Should().HaveCount(1); actual.ErrorMessages.ElementAt(0).Should().Be("Warning: Format value 1 was not found in format placeholders"); var contents = string.Join("|", actual.Values.Select(kvp => string.Format("{0};{1}", kvp.Key, kvp.Value))); contents.Should().Be("0;world|1;!"); }
public string Format(LoggerEvent loggerEvent) { var args = new object[_messageNames.Count]; for (int i = 0; i < _messageNames.Count; ++i) { args[i] = string.Empty; if (loggerEvent.Properties.ContainsKey(_messageNames[i])) { object arg = loggerEvent[_messageNames[i]]; if (_singleFileProps.Contains(_messageNames[i])) { arg = SavePropToFile(_messageNames[i], arg); } if (!(arg is string) && (arg is IEnumerable)) { args[i] = ObjectDumper.DumpObject(arg, dumperSetting); } else { args[i] = arg; } } else { switch (_messageNames[i]) { case LoggerEvent.EVENTTYPE_PROPERTY: args[i] = loggerEvent.EventType; break; } } } try { return(string.Format(_format, args)); } catch (Exception ex) { _format = StringFormatParser.Parse(DEFAULT_FORMAT, out _messageNames); CoreApplication.Instance.Logger.LogWarning(Message.Common_SingleLineFormatterFormatError, ex, _userFormat); return(Format(loggerEvent)); } }
public void Init(NameValueConfigurationCollection props) { if (props[PAR_FORMAT] != null && !String.IsNullOrEmpty(props[PAR_FORMAT].Value)) { _userFormat = props[PAR_FORMAT].Value; _format = StringFormatParser.Parse(_userFormat, out _messageNames); _format = _format.Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\t", "\t"); } else { _format = StringFormatParser.Parse(DEFAULT_FORMAT, out _messageNames); } if (props[PAR_ARRAY_DELIMITER] != null && !String.IsNullOrEmpty(props[PAR_ARRAY_DELIMITER].Value)) { dumperSetting.EnumerableDelimiter = props[PAR_ARRAY_DELIMITER].Value; } if (props[PAR_SINGLE_FILE_PROPS] != null && !String.IsNullOrEmpty(props[PAR_SINGLE_FILE_PROPS].Value)) { _singleFileProps.Clear(); _singleFileProps.AddRange(props[PAR_SINGLE_FILE_PROPS].Value.Split(';')); } }
/// <summary> /// Gets the formatting information for the given format. This is a list of tuples. The tuples /// include: /// /// text, field name, format spec, conversion /// </summary> public static IEnumerable <PythonTuple /*!*/> /*!*/ GetFormatInfo(string /*!*/ format) { ContractUtils.RequiresNotNull(format, "format"); return(StringFormatParser.Parse(format)); }
public FormattedTraceListener(string format) { _format = StringFormatParser.Parse(format, out _messageNames); _format = _format.Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\t", "\t"); }