public override string ToString() { var sbName = new ValueStringBuilder(MethodBase.MethodNameBufferSize); sbName.Append(PropertyType.FormatTypeName()); sbName.Append(' '); sbName.Append(Name); RuntimeType[] arguments = Signature.Arguments; if (arguments.Length > 0) { sbName.Append(" ["); MethodBase.AppendParameters(ref sbName, arguments, Signature.CallingConvention); sbName.Append(']'); } return(sbName.ToString()); }
public override void Update(double totalTime, double frameTime) { base.Update(totalTime, frameTime); if (Time.Ticks > _time_to_update) { _time_to_update = Time.Ticks + 100; if (!NetClient.Socket.IsConnected) { _ping = NetClient.LoginSocket.Statistics.Ping; _deltaBytesReceived = NetClient.LoginSocket.Statistics.DeltaBytesReceived; _deltaBytesSent = NetClient.LoginSocket.Statistics.DeltaBytesSent; } else if (!NetClient.Socket.IsDisposed) { _ping = NetClient.Socket.Statistics.Ping; _deltaBytesReceived = NetClient.Socket.Statistics.DeltaBytesReceived; _deltaBytesSent = NetClient.Socket.Statistics.DeltaBytesSent; } Span <char> span = stackalloc char[128]; ValueStringBuilder sb = new ValueStringBuilder(span); if (IsMinimized) { sb.Append($"Ping: {_ping} ms"); } else { sb.Append($"Ping: {_ping} ms\n{"In:"} {NetStatistics.GetSizeAdaptive(_deltaBytesReceived),-6} {"Out:"} {NetStatistics.GetSizeAdaptive(_deltaBytesSent),-6}"); } _cacheText = sb.ToString(); sb.Dispose(); Vector2 size = Fonts.Bold.MeasureString(_cacheText); _trans.Width = Width = (int)(size.X + 20); _trans.Height = Height = (int)(size.Y + 20); WantUpdateSize = true; } }
public void Ctor_Span_CanAppend() { var vsb = new ValueStringBuilder(new char[1]); Assert.Equal(0, vsb.Length); vsb.Append('a'); Assert.Equal(1, vsb.Length); Assert.Equal("a", vsb.ToString()); }
public void Ctor_InitialCapacity_CanAppend() { var vsb = new ValueStringBuilder(1); Assert.Equal(0, vsb.Length); vsb.Append('a'); Assert.Equal(1, vsb.Length); Assert.Equal("a", vsb.ToString()); }
public static unsafe string Join([Nullable(new byte[] { 1, 2 })] params string[] paths) { if (paths == null) { throw new ArgumentNullException(nameof(paths)); } if (paths.Length == 0) { return(string.Empty); } int num = 0; foreach (string path in paths) { num += path != null ? path.Length : 0; } int capacity = num + (paths.Length - 1); // ISSUE: untyped stack allocation ValueStringBuilder valueStringBuilder = new ValueStringBuilder(new Span <char>((void *)__untypedstackalloc(new IntPtr(520)), 260)); valueStringBuilder.EnsureCapacity(capacity); for (int index = 0; index < paths.Length; ++index) { string path = paths[index]; if (path != null && path.Length != 0) { if (valueStringBuilder.Length == 0) { valueStringBuilder.Append(path); } else { if (!PathInternal.IsDirectorySeparator(valueStringBuilder[valueStringBuilder.Length - 1]) && !PathInternal.IsDirectorySeparator(path[0])) { valueStringBuilder.Append('\\'); } valueStringBuilder.Append(path); } } } return(valueStringBuilder.ToString()); }
private string Format(string[] elements) { // Need to calculate TimeTaken now, if applicable var date = elements[W3CLoggingMiddleware._dateIndex]; var time = elements[W3CLoggingMiddleware._timeIndex]; if (!string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(time) && _loggingFields.HasFlag(W3CLoggingFields.TimeTaken)) { DateTime start = DateTime.ParseExact(date + time, "yyyy-MM-ddHH:mm:ss", CultureInfo.InvariantCulture); var elapsed = DateTime.UtcNow.Subtract(start); elements[W3CLoggingMiddleware._timeTakenIndex] = elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture); } // 200 is around the length of an average cookie-less entry var sb = new ValueStringBuilder(200); var firstElement = true; for (var i = 0; i < elements.Length; i++) { if (_loggingFields.HasFlag((W3CLoggingFields)(1 << i))) { if (!firstElement) { sb.Append(' '); } else { firstElement = false; } // If the element was not logged, or was the empty string, we log it as a dash if (string.IsNullOrEmpty(elements[i])) { sb.Append('-'); } else { sb.Append(elements[i]); } } } return(sb.ToString()); }
// copied from CoreCLR's RuntimeConstructorInfo public override string ToString() { if (toString == null) { var sbName = new ValueStringBuilder(MethodNameBufferSize); // "Void" really doesn't make sense here. But we'll keep it for compat reasons. sbName.Append("Void "); sbName.Append(Name); sbName.Append('('); AppendParameters(ref sbName, GetParameterTypes(), CallingConvention); sbName.Append(')'); toString = sbName.ToString(); } return(toString); }
public void Dispose_ClearsBuilder_ThenReusable() { const string Text1 = "test"; var vsb = new ValueStringBuilder(); vsb.Append(Text1); Assert.Equal(Text1.Length, vsb.Length); vsb.Dispose(); Assert.Equal(0, vsb.Length); Assert.Equal(string.Empty, vsb.ToString()); Assert.True(vsb.TryCopyTo(Span <char> .Empty, out _)); const string Text2 = "another test"; vsb.Append(Text2); Assert.Equal(Text2.Length, vsb.Length); Assert.Equal(Text2, vsb.ToString()); }
public unsafe void Indexer() { const string Text1 = "foobar"; var vsb = new ValueStringBuilder(); vsb.Append(Text1); Assert.Equal(vsb[3], 'b'); vsb[3] = 'c'; Assert.Equal(vsb[3], 'c'); }
public override string ToString() { using var sb = new ValueStringBuilder(); sb.Append('{'); var first = true; foreach (var value in Values) { if (!first) { sb.Append(','); } sb.Append(value); first = false; } sb.Append('}'); return(sb.ToString()); }
internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] parameterTypes, CallingConventions callingConvention) { string comma = ""; for (int i = 0; i < parameterTypes.Length; i++) { Type t = parameterTypes[i]; sbParamList.Append(comma); string typeName = t.FormatTypeName(); // Legacy: Why use "ByRef" for by ref parameters? What language is this? // VB uses "ByRef" but it should precede (not follow) the parameter name. // Why don't we just use "&"? if (t.IsByRef) { sbParamList.Append(typeName.AsSpan().TrimEnd('&')); sbParamList.Append(" ByRef"); } else { sbParamList.Append(typeName); } comma = ", "; } if ((callingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs) { sbParamList.Append(comma); sbParamList.Append("..."); } }
public void Append_String_Large_MatchesStringBuilder(int initialLength, int stringLength) { var sb = new StringBuilder(initialLength); var vsb = new ValueStringBuilder(new char[initialLength]); string s = new string('a', stringLength); sb.Append(s); vsb.Append(s); Assert.Equal(sb.Length, vsb.Length); Assert.Equal(sb.ToString(), vsb.ToString()); }
private string ReadProperties(uint serial, out string htmltext) { bool hasStartColor = false; string result = null; htmltext = string.Empty; if (SerialHelper.IsValid(serial) && World.OPL.TryGetNameAndData(serial, out string name, out string data)) { ValueStringBuilder sbHTML = new ValueStringBuilder(); { ValueStringBuilder sb = new ValueStringBuilder(); { if (!string.IsNullOrEmpty(name)) { if (SerialHelper.IsItem(serial)) { sbHTML.Append("<basefont color=\"yellow\">"); hasStartColor = true; } else { Mobile mob = World.Mobiles.Get(serial); if (mob != null) { sbHTML.Append(Notoriety.GetHTMLHue(mob.NotorietyFlag)); hasStartColor = true; } } sb.Append(name); sbHTML.Append(name); if (hasStartColor) { sbHTML.Append("<basefont color=\"#FFFFFFFF\">"); } } if (!string.IsNullOrEmpty(data)) { sb.Append('\n'); sb.Append(data); sbHTML.Append('\n'); sbHTML.Append(data); } htmltext = sbHTML.ToString(); result = sb.ToString(); sb.Dispose(); sbHTML.Dispose(); } } } return(string.IsNullOrEmpty(result) ? null : result); }
public void Append_CharInt_MatchesStringBuilder() { var sb = new StringBuilder(); var vsb = new ValueStringBuilder(); for (int i = 1; i <= 100; i++) { sb.Append((char)i, i); vsb.Append((char)i, i); } Assert.Equal(sb.Length, vsb.Length); Assert.Equal(sb.ToString(), vsb.ToString()); }
public void TryCopyTo_FailsWhenDestinationIsTooSmall_SucceedsWhenItsLargeEnough() { var vsb = new ValueStringBuilder(); const string Text = "expected text"; vsb.Append(Text); Assert.Equal(Text.Length, vsb.Length); Span <char> dst = new char[Text.Length - 1]; Assert.False(vsb.TryCopyTo(dst, out int charsWritten)); Assert.Equal(0, charsWritten); Assert.Equal(0, vsb.Length); }
public void TryCopyTo_ClearsBuilder_ThenReusable() { const string Text1 = "test"; var vsb = new ValueStringBuilder(); vsb.Append(Text1); Assert.Equal(Text1.Length, vsb.Length); Span <char> dst = new char[Text1.Length]; Assert.True(vsb.TryCopyTo(dst, out int charsWritten)); Assert.Equal(Text1.Length, charsWritten); Assert.Equal(Text1, new string(dst)); Assert.Equal(0, vsb.Length); Assert.Equal(string.Empty, vsb.ToString()); Assert.True(vsb.TryCopyTo(Span <char> .Empty, out _)); const string Text2 = "another test"; vsb.Append(Text2); Assert.Equal(Text2.Length, vsb.Length); Assert.Equal(Text2, vsb.ToString()); }
private static void BuildCommandLine(ProcessStartInfo startInfo, ref ValueStringBuilder commandLine) { // Construct a StringBuilder with the appropriate command line // to pass to CreateProcess. If the filename isn't already // in quotes, we quote it here. This prevents some security // problems (it specifies exactly which part of the string // is the file to execute). ReadOnlySpan <char> fileName = startInfo.FileName.AsSpan().Trim(); bool fileNameIsQuoted = fileName.Length > 0 && fileName[0] == '\"' && fileName[fileName.Length - 1] == '\"'; if (!fileNameIsQuoted) { commandLine.Append('"'); } commandLine.Append(fileName); if (!fileNameIsQuoted) { commandLine.Append('"'); } startInfo.AppendArgumentsTo(ref commandLine); }
public void Append_String_MatchesStringBuilder() { var sb = new StringBuilder(); var vsb = new ValueStringBuilder(); for (int i = 1; i <= 100; i++) { string s = i.ToString(); sb.Append(s); vsb.Append(s); } Assert.Equal(sb.Length, vsb.Length); Assert.Equal(sb.ToString(), vsb.ToString()); }
public void AppendTest4() { Span <char> buffer = stackalloc char[16]; using ValueStringBuilder sb = new ValueStringBuilder(buffer); ReadOnlySpan <char> str = "Hello World".AsSpan(); sb.Append(str); Assert.True(sb.Capacity >= 16); Assert.AreEqual(11, sb.Length); Assert.False(sb.IsEmpty); Assert.AreEqual("Hello World", sb.ToString()); }
public string ValueStringBuilder() { using var stringBuilder = new ValueStringBuilder(70 * Count); stringBuilder.Append("<p>"); stringBuilder.Append("<h2>"); for (int i = 0; i < Count; i++) { stringBuilder.Append("Generated guid \""); stringBuilder.Append(Guid.NewGuid()); stringBuilder.Append("\""); } stringBuilder.Append("</h2>"); stringBuilder.Append("</p>"); return(_cachedStringBuilder.ToString()); }
private string Format(string[] elements, string[] additionalHeaders) { // 200 is around the length of an average cookie-less entry var sb = new ValueStringBuilder(200); var firstElement = true; for (var i = 0; i < elements.Length; i++) { if (_loggingFields.HasFlag((W3CLoggingFields)(1 << i))) { if (!firstElement) { sb.Append(' '); } else { firstElement = false; } // If the element was not logged, or was the empty string, we log it as a dash if (string.IsNullOrEmpty(elements[i])) { sb.Append('-'); } else { sb.Append(elements[i]); } } } for (var i = 0; i < additionalHeaders.Length; i++) { if (!firstElement) { sb.Append(' '); } else { firstElement = false; } // If the element was not logged, or was the empty string, we log it as a dash if (string.IsNullOrEmpty(additionalHeaders[i])) { sb.Append('-'); } else { sb.Append(additionalHeaders[i]); } } return(sb.ToString()); }
public unsafe void Append_PtrInt_MatchesStringBuilder() { var sb = new StringBuilder(); var vsb = new ValueStringBuilder(); for (int i = 1; i <= 100; i++) { string s = i.ToString(); fixed(char *p = s) { sb.Append(p, s.Length); vsb.Append(p, s.Length); } } Assert.Equal(sb.Length, vsb.Length); Assert.Equal(sb.ToString(), vsb.ToString()); }
public void AsSpan_ReturnsCorrectValue_DoesntClearBuilder() { var sb = new StringBuilder(); var vsb = new ValueStringBuilder(); for (int i = 1; i <= 100; i++) { string s = i.ToString(); sb.Append(s); vsb.Append(s); } var resultString = new string(vsb.AsSpan()); Assert.Equal(sb.ToString(), resultString); Assert.NotEqual(0, sb.Length); Assert.Equal(sb.Length, vsb.Length); }
public static void HtmlEncode(string?value, TextWriter output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } if (string.IsNullOrEmpty(value)) { output.Write(value); return; } ReadOnlySpan <char> valueSpan = value.AsSpan(); // Don't create ValueStringBuilder if we don't have anything to encode int index = IndexOfHtmlEncodingChars(valueSpan); if (index == -1) { output.Write(value); return; } // For small inputs we allocate on the stack. In most cases a buffer three // times larger the original string should be sufficient as usually not all // characters need to be encoded. // For larger string we rent the input string's length plus a fixed // conservative amount of chars from the ArrayPool. Span <char> buffer = value.Length < 80 ? stackalloc char[256] : null; ValueStringBuilder sb = buffer != null ? new ValueStringBuilder(buffer) : new ValueStringBuilder(value.Length + 200); sb.Append(valueSpan.Slice(0, index)); HtmlEncode(valueSpan.Slice(index), ref sb); output.Write(sb.AsSpan()); sb.Dispose(); }
internal static unsafe char[] UnescapeString(char *pStr, int start, int end, char[] dest, ref int destPosition, char rsvd1, char rsvd2, char rsvd3, UnescapeMode unescapeMode, UriParser?syntax, bool isQuery) { ValueStringBuilder vsb = new ValueStringBuilder(dest.Length); vsb.Append(dest.AsSpan(0, destPosition)); UnescapeString(pStr, start, end, ref vsb, rsvd1, rsvd2, rsvd3, unescapeMode, syntax, isQuery); if (vsb.Length > dest.Length) { dest = vsb.AsSpan().ToArray(); } else { vsb.AsSpan(destPosition).TryCopyTo(dest.AsSpan(destPosition)); } destPosition = vsb.Length; vsb.Dispose(); return(dest); }
public static void HtmlDecode(string?value, TextWriter output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } if (string.IsNullOrEmpty(value)) { output.Write(value); return; } ReadOnlySpan <char> valueSpan = value.AsSpan(); int index = IndexOfHtmlDecodingChars(valueSpan); if (index == -1) { output.Write(value); return; } // In the worst case the decoded string has the same length. // For small inputs we use stack allocation. Span <char> buffer = value.Length <= 256 ? stackalloc char[256] : null; ValueStringBuilder sb = buffer != null ? new ValueStringBuilder(buffer) : new ValueStringBuilder(value.Length); sb.Append(valueSpan.Slice(0, index)); HtmlDecode(valueSpan.Slice(index), ref sb); output.Write(sb.AsSpan()); sb.Dispose(); }
internal string ReadASCII(int size) { EnsureSize(size); Span <char> span = stackalloc char[size]; ValueStringBuilder sb = new ValueStringBuilder(span); for (int i = 0; i < size; i++) { char c = (char)ReadByte(); if (c != 0) { sb.Append(c); } } string ss = sb.ToString(); sb.Dispose(); return(ss); }
// copied from CoreCLR's RuntimeMethodInfo public override string ToString() { if (toString == null) { var sbName = new ValueStringBuilder(MethodNameBufferSize); sbName.Append(ReturnType.FormatTypeName()); sbName.Append(' '); sbName.Append(Name); if (IsGenericMethod) sbName.Append(RuntimeMethodHandle.ConstructInstantiation(this, TypeNameFormatFlags.FormatBasic)); sbName.Append('('); AppendParameters(ref sbName, GetParameterTypes(), CallingConvention); sbName.Append(')'); toString = sbName.ToString(); } return toString; }
/// <summary> /// Given a Match, emits into the StringBuilder the evaluated /// substitution pattern. /// </summary> public void ReplacementImpl(ref ValueStringBuilder vsb, Match match) { for (int i = 0; i < _rules.Count; i++) { int r = _rules[i]; if (r >= 0) // string lookup { vsb.Append(_strings[r]); } else if (r < -Specials) // group lookup { vsb.Append(match.GroupToStringImpl(-Specials - 1 - r)); } else { switch (-Specials - 1 - r) { // special insertion patterns case LeftPortion: vsb.Append(match.GetLeftSubstring()); break; case RightPortion: vsb.Append(match.GetRightSubstring()); break; case LastGroup: vsb.Append(match.LastGroupToStringImpl()); break; case WholeString: vsb.Append(match.Text); break; } } } }
public override string ToString() { Span <char> charSpan = stackalloc char[128]; var sb = new ValueStringBuilder(charSpan); sb.Append(Name); if (Culture != null) { sb.Append(", culture=\""); sb.Append(Culture); sb.Append('"'); } sb.Append(", version=\""); sb.Append(Version.ToString()); sb.Append('"'); if (_publicKeyToken != null) { sb.Append(", publicKeyToken=\""); EncodeHexString(_publicKeyToken, ref sb); sb.Append('"'); } if (ProcessorArchitecture != null) { sb.Append(", processorArchitecture =\""); sb.Append(ProcessorArchitecture); sb.Append('"'); } return(sb.ToString()); }