/// <summary> /// Get the <see cref="IString"/> of a <see cref="ILineString"/>. /// /// If parameter "String" exists and <paramref name="resolver"/> is provided then value is resolved using /// the default string format or the format that can be found. /// </summary> /// <param name="line"></param> /// <param name="resolver">(optional) type resolver that resolves "IStringFormat" parameter into type. Returns null, if could not resolve, exception if resolve fails</param> /// <param name="fallbackStringFormat">(optional) fallback string format to use in case line didnt have one</param> /// <returns>value</returns> /// <exception cref="LineException">error parsing</exception> public static IString GetString(this ILine line, IResolver resolver = null, IStringFormat fallbackStringFormat = null) { for (ILine part = line; part != null; part = part.GetPreviousPart()) { if (part is ILineString valuePart && valuePart.String != null) { return(valuePart.String); } if (resolver != null && part is ILineParameterEnumerable lineParameters) { foreach (ILineParameter parameter in lineParameters) { if (parameter.ParameterName == "String" && parameter.ParameterValue != null) { IStringFormat stringFormat = line.FindStringFormat(resolver) ?? fallbackStringFormat; return(stringFormat.Parse(parameter.ParameterValue)); } } } if (part is ILineParameter lineParameter && lineParameter.ParameterName == "String" && lineParameter.ParameterValue != null) { IStringFormat stringFormat = line.FindStringFormat(resolver) ?? fallbackStringFormat; return(stringFormat.Parse(lineParameter.ParameterValue)); } } return(StatusString.Null); }
/// <summary> /// Try get string that implements <see cref="IString"/>. /// </summary> /// <param name="line"></param> /// <param name="result"></param> /// <param name="resolver">(optional) type resolver that resolves "StringFormat" parameter into type. Returns null, if could not resolve, exception if resolve fails</param> /// <returns>true if part was found</returns> public static bool TryGetString(this ILine line, out IString result, IResolver resolver = null) { try { for (ILine part = line; part != null; part = part.GetPreviousPart()) { if (part is ILineString valuePart && valuePart.String != null) { result = valuePart.String; return(true); } if (resolver != null && part is ILineParameterEnumerable lineParameters) { foreach (ILineParameter parameter in lineParameters) { if (parameter.ParameterName == "String" && parameter.ParameterValue != null) { IStringFormat stringFormat = line.FindStringFormat(resolver); result = stringFormat.Parse(parameter.ParameterValue); return(true); } } } if (resolver != null && part is ILineParameter lineParameter && lineParameter.ParameterName == "String" && lineParameter.ParameterValue != null) { IStringFormat stringFormat = line.FindStringFormat(resolver); result = stringFormat.Parse(lineParameter.ParameterValue); return(true); } } result = new StatusString(null, LineStatus.StringFormatFailedNull); return(false); } catch (Exception) { result = new StatusString(null, LineStatus.FailedUnknownReason); return(false); } }
public static void Main(string[] args) { { #region Snippet_0a IStringFormat stringFormat = CSharpFormat.Default; IString str = stringFormat.Parse("Hello, {0}."); #endregion Snippet_0a } { #region Snippet_0b ILine line = LineRoot.Global.Key("").Format("Hello, {0}."); #endregion Snippet_0b } { #region Snippet_1a IStringFormat stringFormat = TextFormat.Default; IString str = stringFormat.Parse("{in braces}"); #endregion Snippet_1a } { #region Snippet_1b ILine line = LineRoot.Global.Key("").Text("{in braces}"); #endregion Snippet_1b } { #region Snippet_2a IStringFormat stringFormat = CSharpFormat.Default; IString str = stringFormat.Parse("Hello, {0}."); #endregion Snippet_2a #region Snippet_2b ILine line = LineRoot.Global.Key("Hello").String(str); #endregion Snippet_2b #region Snippet_2c LineString lineString = line.Value("Corellia Melody").ResolveString(); #endregion Snippet_2c #region Snippet_2d if (!lineString.Failed) { Console.WriteLine(lineString.Value); } #endregion Snippet_2d } { #region Snippet_3 // Convert unescaped string "{not placeholder}" to IString IString ss = TextFormat.Default.Parse("{not placeholder}"); // Escape with CSharpFormat to "\{not placeholder\}" string str = CSharpFormat.Default.Print(ss); // Convert escaped string back to IString IString cs = CSharpFormat.Default.Parse(str); // Unescape IString back to "{not placeholder}" string tt = TextFormat.Default.Print(cs); #endregion Snippet_3 Console.WriteLine(tt); } { #region Snippet_4 #endregion Snippet_4 } { #region Snippet_5 #endregion Snippet_5 } { #region Snippet_6 #endregion Snippet_6 } { #region Snippet_7 #endregion Snippet_7 } { #region Snippet_8 #endregion Snippet_8 } }