From b4b506a4b6c7c1e90b5b0980e4cfe0460e7546a2 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 8 Jun 2024 21:54:52 -0400 Subject: some minor touchups --- lib/Utils/src/ArgumentList.cs | 26 ++++++++++++++++------ lib/Utils/src/IO/ArrayPoolStreamBuffer.cs | 26 +++++++++------------- lib/Utils/src/IO/VnStreamWriter.cs | 36 +++++++++++-------------------- 3 files changed, 43 insertions(+), 45 deletions(-) (limited to 'lib/Utils/src') diff --git a/lib/Utils/src/ArgumentList.cs b/lib/Utils/src/ArgumentList.cs index c02ebee..7226b27 100644 --- a/lib/Utils/src/ArgumentList.cs +++ b/lib/Utils/src/ArgumentList.cs @@ -41,18 +41,16 @@ namespace VNLib.Utils /// /// The array of arguments to clone /// - public ArgumentList(string[] args) - { - ArgumentNullException.ThrowIfNull(args); - _args = args.ToList(); - } + [Obsolete("Deprecated in preference to ctor(IEnumerable)")] + public ArgumentList(string[] args) : this(args as IEnumerable) + { } /// /// Initalizes the argument parser by copying the given argument list /// /// The argument list to clone /// - public ArgumentList(IReadOnlyList args) + public ArgumentList(IEnumerable args) { ArgumentNullException.ThrowIfNull(args); _args = args.ToList(); @@ -102,5 +100,21 @@ namespace VNLib.Utils /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + /// Captures the command line arguments from the currently executing process + /// and returns them as an ArgumentList + /// + /// The containing the current process's argument list + public static ArgumentList CaptureCurrentArgs() + { + /* + * Capture the current command line arguments and + * pop the first argument which is always the program + * name + */ + string[] strings = Environment.GetCommandLineArgs(); + return new ArgumentList(strings.Skip(1)); + } } } \ No newline at end of file diff --git a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs index b62412f..a943a5b 100644 --- a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs +++ b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -28,7 +28,13 @@ using System.Buffers; namespace VNLib.Utils.IO { - internal class ArrayPoolStreamBuffer : ISlindingWindowBuffer + /// + /// Creates a new from the + /// given array instance and it came from. + /// + /// The rented array to use + /// The pool to return the array to when completed + internal class ArrayPoolStreamBuffer(T[] array, ArrayPool pool) : ISlindingWindowBuffer { /// /// The shared instance to allocate buffers @@ -36,20 +42,8 @@ namespace VNLib.Utils.IO /// public static IStreamBufferFactory Shared { get; } = new DefaultFactory(); - private readonly ArrayPool _pool; - private T[] _buffer; - - /// - /// Creates a new from the - /// given array instance and it came from. - /// - /// The rented array to use - /// The pool to return the array to when completed - public ArrayPoolStreamBuffer(T[] array, ArrayPool pool) - { - _pool = pool; - _buffer = array; - } + private readonly ArrayPool _pool = pool; + private T[] _buffer = array; /// public int WindowStartPos { get; set; } diff --git a/lib/Utils/src/IO/VnStreamWriter.cs b/lib/Utils/src/IO/VnStreamWriter.cs index ddebc07..7d43f48 100644 --- a/lib/Utils/src/IO/VnStreamWriter.cs +++ b/lib/Utils/src/IO/VnStreamWriter.cs @@ -41,21 +41,29 @@ namespace VNLib.Utils.IO /// Provides a memory optimized implementation. Optimized for writing /// to network streams /// - public class VnStreamWriter : TextWriter + /// + /// Creates a new that writes encoded data to the base stream + /// and uses the specified buffer. + /// + /// The underlying stream to write data to + /// The to use when writing to the stream + /// The internal to use + /// + public class VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer buffer) : TextWriter { - private readonly Encoder Enc; + private readonly Encoder Enc = encoding.GetEncoder(); - private readonly ISlindingWindowBuffer _buffer; + private readonly ISlindingWindowBuffer _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); private bool closed; /// /// Gets the underlying stream that interfaces with the backing store /// - public virtual Stream BaseStream { get; } + public virtual Stream BaseStream { get; } = baseStream ?? throw new ArgumentNullException(nameof(buffer)); /// - public override Encoding Encoding { get; } + public override Encoding Encoding { get; } = encoding ?? throw new ArgumentNullException(nameof(encoding)); /// /// Line termination to use when writing lines to the output @@ -97,24 +105,6 @@ namespace VNLib.Utils.IO { } - /// - /// Creates a new that writes encoded data to the base stream - /// and uses the specified buffer. - /// - /// The underlying stream to write data to - /// The to use when writing to the stream - /// The internal to use - /// - public VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer buffer) - { - BaseStream = baseStream ?? throw new ArgumentNullException(nameof(buffer)); - Encoding = encoding ?? throw new ArgumentNullException(nameof(encoding)); - _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); - - //Get an encoder - Enc = encoding.GetEncoder(); - } - /// public void Write(byte value) { -- cgit