private static string CustomStringFormat(string format, object[] args) { var builder = LocalStringBuilderCache.New(format.Length + (args?.Length ?? 0) * 5); CustomStringFormat(builder, format, args); return(LocalStringBuilderCache.GetStringAndRelease(builder)); }
private static string GetEnumMemberName(FieldInfo member) { var attr1 = member.GetCustomAttribute <EnumMemberNameAttribute>(); if (attr1 != null) { return(attr1.Name); } var attr2 = member.GetCustomAttribute <EnumMemberAttribute>(); if (attr2 != null) { return(attr2.Value); } var name = member.Name; var sb = LocalStringBuilderCache.New(name.Length + 5); for (var i = 0; i < name.Length; i++) { var c = name[i]; if (i > 0 && char.IsUpper(c) && char.IsLower(name[i - 1])) { sb.Append('_'); } sb.Append(char.ToUpperInvariant(c)); } return(LocalStringBuilderCache.GetStringAndRelease(sb)); }
private static ILogMessage MakeCore(StringBuilder builder, ILogField[] fields) { if (fields != null && fields.Length > 0) { // Автоматическое добавление завершающей точки if (builder.Length > 0 && builder[builder.Length - 1] != '.') { builder.Append('.'); } // Автоматическое добавление завершающего пробела if (builder.Length > 0 && !char.IsWhiteSpace(builder[builder.Length - 1])) { builder.Append(' '); } for (var i = 0; i < fields.Length; i++) { builder.Append(fields[i].Print(PrintOption.Default)); if (i != fields.Length - 1) { builder.Append(", "); } (fields[i] as IReusable)?.Release(); } } return(LogMessageImpl.Create(LocalStringBuilderCache.GetStringAndRelease(builder))); }
/// <summary> /// Собрать сообщение лога из строки и списка именованных полей /// </summary> public static ILogMessage MakeString(string text, params ILogField[] fields) { var builder = LocalStringBuilderCache.New(); builder.Append(text); return(MakeCore(builder, fields)); }
/// <summary> /// Собрать сообщение лога из $-строки и списка именованных полей /// </summary> public static ILogMessage Make(FormattableString text, params ILogField[] fields) { var builder = LocalStringBuilderCache.New(); FormattingHelper.FormatMessageInPlace(builder, text); return(MakeCore(builder, fields)); }
private static string Normalize(string name) { var sb = LocalStringBuilderCache.New(name.Length); foreach (var c in name) { if (char.IsDigit(c) || char.IsUpper(c) || c == '_') { sb.Append(c); } else if (char.IsLower(c)) { sb.Append(char.ToUpperInvariant(c)); } } return(LocalStringBuilderCache.GetStringAndRelease(sb)); }
internal static string EscapeString(string str, bool quoteIfNecessary = true) { if (str == null) { return(NullStr); } if (str.Length == 0) { return(EmptyStr); } if (str == "null") { return("'null'"); } if (str.Length > 1) { var first = str[0]; var last = str[str.Length - 1]; if (IsMatchingPairOfBrackets(first, last)) { return(str); } } if (!quoteIfNecessary) { return(str); } var shouldEscape = false; for (var i = 0; i < str.Length; i++) { var c = str[i]; if (!char.IsLetterOrDigit(c) && !char.IsPunctuation(c)) { shouldEscape = true; break; } } if (!shouldEscape) { return(str); } var sb = LocalStringBuilderCache.New(str.Length + 2); sb.Append("'"); for (var i = 0; i < str.Length; i++) { var c = str[i]; switch (c) { case '\r': sb.Append("\\r"); break; case '\n': sb.Append("\\n"); break; case '\t': sb.Append("\\t"); break; case '\'': sb.Append("\\'"); break; default: sb.Append(c); break; } } sb.Append("'"); return(sb.ToString()); }