diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b1d6ad4..bd5d1bf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,9 +8,9 @@ env: on: push: - branches: '*' - pull_request: - branches: 'main' + branches: [ '*' ] +# pull_request: +# branches: [ 'main' ] defaults: run: @@ -30,15 +30,16 @@ jobs: - name: Setup uses: actions/setup-dotnet@v1 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json - name: Pack - run: dotnet pack -c:Release + run: dotnet pack -c:Release -o nuget - name: Test run: dotnet test - name: Push - if: github.ref == 'refs/heads/main' - run: dotnet nuget push "../bin/Release/*.nupkg" -k ${{ secrets.PACKAGE_REGISTRY_TOKEN }} -s https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json --skip-duplicate + # Create packages from every branch except pull requests + if: github.event_name == 'push' && contains(github.event.head_commit.message, 'prerelease') + run: dotnet nuget push "nuget/*.nupkg" -k ${{ secrets.NUGET_ORG_API_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate \ No newline at end of file diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index f4a491f..43afbd6 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -27,7 +27,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json - name: Pack diff --git a/src/AutoByte.Generators/AutoByteSourceGenerator.cs b/src/AutoByte.Generators/AutoByteSourceGenerator.cs index f57de21..ed1ce0d 100644 --- a/src/AutoByte.Generators/AutoByteSourceGenerator.cs +++ b/src/AutoByte.Generators/AutoByteSourceGenerator.cs @@ -189,7 +189,6 @@ private string GetStringMethodName(IPropertySymbol property, AutoByteFieldAttrib } } - throw new Exception($"Propertry {property.Name} require AutoByteString attribute."); } diff --git a/src/AutoByte.Generators/AutoByteSourceGeneratorExtensions.cs b/src/AutoByte.Generators/AutoByteSourceGeneratorExtensions.cs index d2f3fbe..1750dbc 100644 --- a/src/AutoByte.Generators/AutoByteSourceGeneratorExtensions.cs +++ b/src/AutoByte.Generators/AutoByteSourceGeneratorExtensions.cs @@ -21,7 +21,7 @@ public static bool IsPartial(this ClassDeclarationSyntax classDeclaration) public static string GetNamespace(this BaseTypeDeclarationSyntax syntax) { - // Get the syntax node for the containing namespace declaration + // Get the syntax node for the containing namespace declaration (traditional namespace) SyntaxNode containingNamespace = syntax.AncestorsAndSelf() .OfType().FirstOrDefault(); @@ -31,6 +31,16 @@ public static string GetNamespace(this BaseTypeDeclarationSyntax syntax) return ((NamespaceDeclarationSyntax)containingNamespace).Name.ToString(); } + // Check for file-scoped namespace declaration (C# 10+) + SyntaxNode containingFileScopedNamespace = syntax.AncestorsAndSelf() + .OfType().FirstOrDefault(); + + if (containingFileScopedNamespace != null) + { + // Get the namespace name from the file-scoped namespace node + return ((FileScopedNamespaceDeclarationSyntax)containingFileScopedNamespace).Name.ToString(); + } + return null; } diff --git a/src/AutoByte.Tests/AutoByte.Tests.csproj b/src/AutoByte.Tests/AutoByte.Tests.csproj index 9f0d529..d737b9c 100644 --- a/src/AutoByte.Tests/AutoByte.Tests.csproj +++ b/src/AutoByte.Tests/AutoByte.Tests.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable false true diff --git a/src/AutoByte.Tests/ByteSlide_Must.cs b/src/AutoByte.Tests/ByteSlide_Must.cs index ec11161..3939666 100644 --- a/src/AutoByte.Tests/ByteSlide_Must.cs +++ b/src/AutoByte.Tests/ByteSlide_Must.cs @@ -141,7 +141,53 @@ public void GetPascalString_ReturnsExcpectedStrings() } + [Fact] + public void GetPascalString_WithMaxLength_ReturnsExpectedStrings_AndConsumesAllBytes() + { + // "Hello World" (11) + "Matt Janda" (10) + "" (0) + byte[] PASCAL_STRINGS = + { + 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, + 0x0A, 0x4D, 0x61, 0x74, 0x74, 0x20, 0x4A, 0x61, 0x6E, 0x64, 0x61, + 0x00 + }; + + var slide = new ByteSlide(PASCAL_STRINGS); + + // Full read (maxLength >= length) + Assert.Equal("Hello World", slide.GetPascalString(Encoding.ASCII, maxLength: 11)); + Assert.Equal("Matt Janda", slide.GetPascalString(Encoding.ASCII, maxLength: 10)); + Assert.Equal("", slide.GetPascalString(Encoding.ASCII, maxLength: 100)); + + Assert.Equal(0, slide.Length); + } + + [Fact] + public void GetPascalString_WithMaxLength_TruncatesButStillSkipsRemainingBytes() + { + // "Hello World" (11) + "Matt Janda" (10) + "" (0) + byte[] PASCAL_STRINGS = + { + 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, + 0x0A, 0x4D, 0x61, 0x74, 0x74, 0x20, 0x4A, 0x61, 0x6E, 0x64, 0x61, + 0x00 + }; + + var slide = new ByteSlide(PASCAL_STRINGS); + + // Truncate first string to 5 bytes => "Hello" + Assert.Equal("Hello", slide.GetPascalString(Encoding.ASCII, maxLength: 5)); + + // Must still be aligned to next Pascal string (i.e., remaining 6 bytes were skipped) + Assert.Equal("Matt Janda", slide.GetPascalString(Encoding.ASCII, maxLength: 10)); + + // Empty + Assert.Equal("", slide.GetPascalString(Encoding.ASCII, maxLength: 1)); + + Assert.Equal(0, slide.Length); + } + + - } } diff --git a/src/AutoByte/ByteSlide.cs b/src/AutoByte/ByteSlide.cs index bb82b32..bae5034 100644 --- a/src/AutoByte/ByteSlide.cs +++ b/src/AutoByte/ByteSlide.cs @@ -258,6 +258,32 @@ public string GetCString(Encoding encoding, int maxLength) public string GetPascalString(Encoding encoding) => encoding.GetString(Slide(GetByte())); + public string GetPascalString(Encoding encoding, int maxLength) + { + var length = GetByte(); + + if (length == 0) + { + return string.Empty; + } + + // Enforce maxLength (byte-based) + var effectiveLength = Math.Min(length, maxLength); + + // Read only what we're allowed to decode + var bytes = Slide(effectiveLength); + + // Skip remaining bytes if original length was larger + if (length > effectiveLength) + { + Slide(length - effectiveLength); + } + + return encoding.GetString(bytes); + } + + + /// /// Retrieves a byte array from the current pointer position and aligns the pointer to the specified value.