From d99c1e3aa686da0c4670739a02adcdf001c24098 Mon Sep 17 00:00:00 2001 From: Nitload Date: Mon, 23 Feb 2026 19:54:21 +0800 Subject: [PATCH] add unicode figure --- .../Collections/CapacityFixedList.cs | 25 +++--- .../Collections/CapacityFixedStack.cs | 10 ++- .../Collections/NotEnumerableList.cs | 55 +++++++------ StgSharp.Common/Counter.cs | 37 +++------ StgSharp.Native/StgSharp.Native.vcxproj | 6 +- .../TextRegex/RegexTokenReader.cs | 80 +++++++++++++------ 6 files changed, 119 insertions(+), 94 deletions(-) diff --git a/StgSharp.Common/Collections/CapacityFixedList.cs b/StgSharp.Common/Collections/CapacityFixedList.cs index 13849c5e..d1531f60 100644 --- a/StgSharp.Common/Collections/CapacityFixedList.cs +++ b/StgSharp.Common/Collections/CapacityFixedList.cs @@ -1,9 +1,9 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="CapacityFixedList" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -47,11 +47,13 @@ namespace StgSharp.Collections { private T[] _values; - private int capacity, _index; + private readonly int _capacity; + private int _index; public CapacityFixedList(int size) { _values = new T[size]; + _capacity = size; } public CapacityFixedList(ReadOnlySpan values) @@ -62,16 +64,7 @@ namespace StgSharp.Collections public T this[int index] { get => index < _index ? _values[index] : throw new ArgumentOutOfRangeException(nameof(index)); - set - { - if (index < _index) - { - _values[index] = value; - } else - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - } + set => _values[index] = index < _index ? value : throw new ArgumentOutOfRangeException(nameof(index)); } public bool IsReadOnly => false; @@ -80,7 +73,7 @@ namespace StgSharp.Collections public void Add(T item) { - if (_index < capacity - 1) + if (_index < _capacity - 1) { _index++; this[_index] = item; @@ -89,7 +82,7 @@ namespace StgSharp.Collections public void Clear() { - _values = new T[capacity]; + _values = new T[_capacity]; _index = 0; } diff --git a/StgSharp.Common/Collections/CapacityFixedStack.cs b/StgSharp.Common/Collections/CapacityFixedStack.cs index a44b6fde..64f7b127 100644 --- a/StgSharp.Common/Collections/CapacityFixedStack.cs +++ b/StgSharp.Common/Collections/CapacityFixedStack.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="CapacityFixedStack" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -57,7 +57,11 @@ namespace StgSharp.Collections public int Capacity => _values.Length; - public ReadOnlySpan AsSpan() => _values.AsSpan(0, _index); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlySpan AsSpan() + { + return _values.AsSpan(0, _index); + } public void Clear() { diff --git a/StgSharp.Common/Collections/NotEnumerableList.cs b/StgSharp.Common/Collections/NotEnumerableList.cs index 151c09ca..d1589176 100644 --- a/StgSharp.Common/Collections/NotEnumerableList.cs +++ b/StgSharp.Common/Collections/NotEnumerableList.cs @@ -1,27 +1,30 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="NotEnumerableList" +// file="NotEnumerableList.cs" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -113,7 +116,10 @@ namespace StgSharp.Collections /// Returns a span over the live portion of the bag (0..Count-1). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span AsSpan() => new Span(_items, 0, Count); + public Span AsSpan() + { + return new Span(_items, 0, Count); + } /// /// Clears all elements and zeroes out references to avoid retention. Count becomes 0. @@ -130,7 +136,10 @@ namespace StgSharp.Collections /// released. Use only when you are certain old references can remain. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FastClear() => Count = 0; + public void FastClear() + { + Count = 0; + } /// /// Removes the first occurrence of value (unordered). Does nothing if not found. diff --git a/StgSharp.Common/Counter.cs b/StgSharp.Common/Counter.cs index f37c0ebe..aaf36c39 100644 --- a/StgSharp.Common/Counter.cs +++ b/StgSharp.Common/Counter.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="Counter" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -30,7 +30,7 @@ using System.Numerics; namespace StgSharp { - public class Counter where T : unmanaged, INumber + public class Counter where T: unmanaged, INumber { internal dynamic _value; @@ -39,11 +39,7 @@ namespace StgSharp internal readonly T _defaultIncrement; internal readonly T _defaultValue; - public Counter( - T defaultValue, - T defaultIncrement, - T defaultDecrement - ) + public Counter(T defaultValue, T defaultIncrement, T defaultDecrement) { _value = defaultValue; _defaultValue = defaultValue; @@ -63,9 +59,7 @@ namespace StgSharp set => _value = value; } - public void Add( - T value - ) + public void Add(T value) { _value += value; } @@ -85,34 +79,29 @@ namespace StgSharp _value = _defaultValue; } - public void Sub( - T value - ) + public void Sub(T value) { _value -= value; } - public override string ToString() => Value.ToString(); + public override string ToString() + { + return Value.ToString() ?? string.Empty; + } - public static Counter operator --( - Counter counter - ) + public static Counter operator --(Counter counter) { counter?.QuickSub(); return counter; } - public static Counter operator ++( - Counter counter - ) + public static Counter operator ++(Counter counter) { counter?.QuickAdd(); return counter; } - public static implicit operator T( - Counter counter - ) + public static implicit operator T(Counter counter) { return counter switch { diff --git a/StgSharp.Native/StgSharp.Native.vcxproj b/StgSharp.Native/StgSharp.Native.vcxproj index 30f8fefc..5c24176a 100644 --- a/StgSharp.Native/StgSharp.Native.vcxproj +++ b/StgSharp.Native/StgSharp.Native.vcxproj @@ -28,21 +28,21 @@ true Unicode false - v143 + v145 DynamicLibrary false true Unicode - v143 + v145 DynamicLibrary false true Unicode - v143 + v145 diff --git a/StgSharp.RegularAnalysis/TextRegex/RegexTokenReader.cs b/StgSharp.RegularAnalysis/TextRegex/RegexTokenReader.cs index 3856649e..813c6f2b 100644 --- a/StgSharp.RegularAnalysis/TextRegex/RegexTokenReader.cs +++ b/StgSharp.RegularAnalysis/TextRegex/RegexTokenReader.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="RegexTokenReader" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -45,9 +45,7 @@ namespace StgSharp.RegularAnalysis.TextRegex public RegexTokenReader() { } - public RegexTokenReader( - string source - ) + public RegexTokenReader(string source) { _source = source; } @@ -63,9 +61,50 @@ namespace StgSharp.RegularAnalysis.TextRegex if (pos + 1 == _source.Length) { throw new InvalidOperationException("Unclosed char"); } - position = pos + 2; - return new Token(_source[(pos)..(pos + 2)], 0, pos, - RegexElementLabel.UNIT_SET); + if (_source[pos + 1] == 'x') + { + string value = _source[(pos + 1)..(pos + 3)]; + pos += 4; + if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result)) + { + return new Token(result.ToString(), 0, pos, RegexElementLabel.UNIT); + } else + { + throw new InvalidCastException($"Failed to convert {value} to a valid ascii code point."); + } + } else if (_source[pos + 1] == 'u') + { + string value = _source[(pos + 1)..(pos + 5)]; + pos += 6; + if (uint.TryParse(value, out uint code) && Rune.TryCreate(code, out Rune result)) + { + return new Token(result.ToString(), 0, pos, RegexElementLabel.UNIT); + } else + { + throw new InvalidCastException($"Failed to convert {value} to a valid unicode code point."); + } + } else if (_source[pos + 1] == 'p') + { + int i = pos + 2; + if (_source[i] != '{') { + throw new InvalidOperationException("Invalid Unicode property escape sequence."); + } + for (; i < _source.Length; i++) + { + if (_source[i] == '}') + { + position = i + 1; + return new Token(_source[(pos)..(i + 1)], 0, pos, + RegexElementLabel.UNIT_SET); + } + } + throw new InvalidOperationException("Unclosed Unicode property escape sequence."); + } else + { + position = pos + 2; + return new Token(_source[(pos)..(pos + 2)], 0, pos, + RegexElementLabel.UNIT_SET); + } case '[': for (int i = pos + 1; i < _source.Length; i++) { @@ -73,24 +112,21 @@ namespace StgSharp.RegularAnalysis.TextRegex { int length = (i - pos - 1); position = i + 1; - return new Token(_source[(pos)..(i + 1)], 0, - position, + return new Token(_source[(pos)..(i + 1)], 0, position, RegexElementLabel.UNIT_SET); } } throw new InvalidOperationException("Unclosed character set"); case '|': position = pos + 1; - return new Token(_source[(pos)..(pos + 1)], 0, pos, - RegexElementLabel.ALT); + return new Token(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.ALT); case '(': // very comlex here return ReadGroupBeginToken(); case ')': position = pos + 1; - return new Token(_source[(pos)..(pos + 1)], 0, pos, - RegexElementLabel.GROUP_END); + return new Token(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.GROUP_END); case '{': for (int i = pos + 1; i < _source.Length; i++) { @@ -105,25 +141,19 @@ namespace StgSharp.RegularAnalysis.TextRegex case '*' or '+': int count = _source[pos + 1] == '?' ? 2 : 1; position = pos + count; - return new Token(_source[(pos)..(pos + count)], 0, pos, - RegexElementLabel.COUNT); + return new Token(_source[(pos)..(pos + count)], 0, pos, RegexElementLabel.COUNT); case '?': position = pos + 1; - return new Token(_source[(pos)..(pos + 1)], 0, pos, - RegexElementLabel.COUNT); + return new Token(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.COUNT); case '.': - return new Token(_source[(pos)..(pos + 1)], 0, pos, - RegexElementLabel.UNIT); + return new Token(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT); default: position = pos + 1; - return new Token(_source[(pos)..(pos + 1)], 0, pos, - RegexElementLabel.UNIT); + return new Token(_source[(pos)..(pos + 1)], 0, pos, RegexElementLabel.UNIT); } } - public bool TryReadToken( - out Token t - ) + public bool TryReadToken(out Token t) { if (IsEmpty) {