daily update on 20250210 by nitload youmu
This commit is contained in:
@@ -37,28 +37,71 @@ using System.Text.RegularExpressions;
|
||||
|
||||
namespace StgSharp.Script
|
||||
{
|
||||
/// <summary>
|
||||
/// Transmit a continuous part of script code to interpreter or compiler.
|
||||
/// This transmitter is designed for single-thread-write and single-thread
|
||||
/// read. Any multi thread operation will cause undefined behaviour.
|
||||
/// </summary>
|
||||
public class ScriptSourceTransmitter : IScriptSourceProvider
|
||||
{
|
||||
private bool _isWriting = true;
|
||||
private ConcurrentQueue<string> cache = new ConcurrentQueue<string>();
|
||||
private int _location;
|
||||
private List<string> _cache;
|
||||
private string[] _buffer;
|
||||
private int _begin, _index;
|
||||
private readonly string _completeMark;
|
||||
|
||||
public ScriptSourceTransmitter( string endMark, int location )
|
||||
public ScriptSourceTransmitter( string endMark, int start )
|
||||
{
|
||||
_cache = new List<string>();
|
||||
_completeMark = endMark;
|
||||
_location = location;
|
||||
_begin = start;
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
public int Location => _location;
|
||||
public ScriptSourceTransmitter(string endMark, int start, int capacity)
|
||||
{
|
||||
_cache = new List<string>(capacity);
|
||||
_completeMark = endMark;
|
||||
_begin = start;
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
public bool IsEmpty => cache.IsEmpty;
|
||||
internal ScriptSourceTransmitter(string[] buffer, int start)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_begin = start;
|
||||
_isWriting = false;
|
||||
}
|
||||
|
||||
public bool IsEmpty => _isWriting ?
|
||||
(_cache.Count == 0) :
|
||||
(_buffer.Length == 0) || (_index >= _buffer.Length - 1);
|
||||
|
||||
public int Location => _begin + _index;
|
||||
|
||||
public bool IsFrozen => !_isWriting;
|
||||
|
||||
public string ReadLine()
|
||||
{
|
||||
cache.TryDequeue( out string line );
|
||||
_location++;
|
||||
return line;
|
||||
if (_isWriting)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot read before all writing operation.");
|
||||
}
|
||||
var str = _buffer[_index];
|
||||
_index++;
|
||||
return str;
|
||||
}
|
||||
|
||||
public string ReadLine(out int position)
|
||||
{
|
||||
if (_isWriting)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot read before all writing operation.");
|
||||
}
|
||||
position = _begin + _index;
|
||||
var str = _buffer[_index];
|
||||
_index++;
|
||||
return str;
|
||||
}
|
||||
|
||||
public void WriteLine( string line )
|
||||
@@ -67,11 +110,31 @@ namespace StgSharp.Script
|
||||
return;
|
||||
}
|
||||
if( string.Equals( _completeMark, line ) ) {
|
||||
_isWriting = false;
|
||||
EndWriting();
|
||||
return;
|
||||
}
|
||||
cache.Enqueue( line );
|
||||
_cache.Add( line );
|
||||
}
|
||||
|
||||
|
||||
public void EndWriting()
|
||||
{
|
||||
if (!_isWriting)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_isWriting = false;
|
||||
_buffer = _cache.ToArray();
|
||||
_cache = null!;
|
||||
}
|
||||
|
||||
public IScriptSourceProvider Slice(int location, int count)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(location, _begin);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(
|
||||
location + count,_begin + _buffer.Length);
|
||||
return new ScriptSourceTransmitter(
|
||||
_buffer.AsSpan().Slice( location, count ).ToArray(),
|
||||
location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user