Optimize performance and GC for string parsing in PDT.

This commit is contained in:
2023-11-29 14:51:49 +08:00
parent 5ef6e2b4a3
commit ed6ae7ad8a

View File

@@ -4,7 +4,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Text;
using CMath = System.Math;
namespace Cryville.Common.Pdt {
@@ -64,6 +64,8 @@ namespace Cryville.Common.Pdt {
/// The current position in the string being parsed by the interpreter.
/// </summary>
public int Position { get; protected set; }
readonly StringBuilder _sb = new StringBuilder();
#pragma warning disable IDE1006
/// <summary>
/// The character at the current position.
@@ -139,13 +141,15 @@ namespace Cryville.Common.Pdt {
/// <returns>A string.</returns>
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
protected string GetString() {
int sp = Position;
do {
if (cc == '\\') Position++;
Position++;
} while (ct != 0x0100);
_sb.Clear();
Position++;
return Regex.Replace(Source.Substring(sp + 1, Position - sp - 2), @"\\(.)", "$1");
while (ct != CharCategory.StringDelimiter) {
if (cc == '\\') Position++;
_sb.Append(cc);
Position++;
}
Position++;
return _sb.ToString();
}
/// <summary>
/// Reads an expression.