Browse Source

refactor: update ReadByteAsync to ReadStringAsync and simplify Seek method

Akeit0 7 months ago
parent
commit
95e1c80b1f

+ 14 - 4
src/Lua/IO/ILuaFileSystem.cs

@@ -1,4 +1,6 @@
-namespace Lua.IO;
+using Lua.Internal;
+
+namespace Lua.IO;
 
 
 public interface ILuaFileSystem
 public interface ILuaFileSystem
 {
 {
@@ -29,7 +31,7 @@ public interface IStreamReader : IDisposable
 {
 {
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
-    public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken);
+    public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken);
 }
 }
 
 
 public interface IStreamWriter : IDisposable
 public interface IStreamWriter : IDisposable
@@ -94,9 +96,17 @@ public sealed class StreamReaderWrapper(StreamReader streamReader) : IStreamRead
         return new(streamReader.ReadToEnd());
         return new(streamReader.ReadToEnd());
     }
     }
 
 
-    public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken)
+    public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken)
     {
     {
-        return new(streamReader.Read());
+        using var byteBuffer = new PooledArray<char>(count);
+        var span = byteBuffer.AsSpan();
+        var ret=streamReader.Read(span);
+        if(ret != span.Length)
+        {
+            return new(default(string));
+        }
+
+        return new(span.ToString());
     }
     }
 
 
     public void Dispose()
     public void Dispose()

+ 1 - 1
src/Lua/Runtime/LuaThreadAccessExtensions.cs

@@ -183,7 +183,7 @@ public static class LuaThreadAccessAccessExtensions
         {
         {
             if (luaTable.TryGetValue(key, out var value))
             if (luaTable.TryGetValue(key, out var value))
             {
             {
-                return new(value);
+                return (value);
             }
             }
         }
         }
 
 

+ 9 - 23
src/Lua/Standard/FileHandle.cs

@@ -69,9 +69,9 @@ public class FileHandle : ILuaUserData
         return reader!.ReadToEndAsync(cancellationToken);
         return reader!.ReadToEndAsync(cancellationToken);
     }
     }
 
 
-    public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken)
+    public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken)
     {
     {
-        return reader!.ReadByteAsync(cancellationToken);
+        return reader!.ReadStringAsync(count,cancellationToken);
     }
     }
 
 
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
@@ -79,28 +79,14 @@ public class FileHandle : ILuaUserData
         return writer!.WriteAsync(buffer, cancellationToken);
         return writer!.WriteAsync(buffer, cancellationToken);
     }
     }
 
 
-    public long Seek(string whence, long offset)
-    {
-        if (whence != null)
+    public long Seek(string whence, long offset) =>
+        whence switch
         {
         {
-            switch (whence)
-            {
-                case "set":
-                    stream.Seek(offset, SeekOrigin.Begin);
-                    break;
-                case "cur":
-                    stream.Seek(offset, SeekOrigin.Current);
-                    break;
-                case "end":
-                    stream.Seek(offset, SeekOrigin.End);
-                    break;
-                default:
-                    throw new ArgumentException($"Invalid option '{whence}'");
-            }
-        }
-
-        return stream.Position;
-    }
+            "set" => stream.Seek(offset, SeekOrigin.Begin),
+            "cur" => stream.Seek(offset, SeekOrigin.Current),
+            "end" => stream.Seek(offset, SeekOrigin.End),
+            _ => throw new ArgumentException($"Invalid option '{whence}'")
+        };
 
 
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     {
     {

+ 9 - 14
src/Lua/Standard/Internal/IOHelper.cs

@@ -125,22 +125,17 @@ internal static class IOHelper
                 }
                 }
                 else if (format.TryRead<int>(out var count))
                 else if (format.TryRead<int>(out var count))
                 {
                 {
-                    using var byteBuffer = new PooledArray<byte>(count);
-
-                    for (int j = 0; j < count; j++)
+                    var ret = await file.ReadStringAsync(count, cancellationToken);
+                    if (ret == null)
                     {
                     {
-                        var b = await file.ReadByteAsync(cancellationToken);
-                        if (b == -1)
-                        {
-                            stack.PopUntil(top);
-                            stack.Push(LuaValue.Nil);
-                            return 1;
-                        }
-
-                        byteBuffer[j] = (byte)b;
+                        stack.PopUntil(top);
+                        stack.Push(default);
+                        return 1;
+                    }
+                    else
+                    {
+                        stack.Push(ret);
                     }
                     }
-
-                    stack.Push(Encoding.UTF8.GetString(byteBuffer.AsSpan()));
                 }
                 }
                 else
                 else
                 {
                 {