Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ env:

on:
push:
branches: '*'
pull_request:
branches: 'main'
branches: [ '*' ]
# pull_request:
# branches: [ 'main' ]

defaults:
run:
Expand All @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/AutoByte.Generators/AutoByteSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ private string GetStringMethodName(IPropertySymbol property, AutoByteFieldAttrib
}
}


throw new Exception($"Propertry {property.Name} require AutoByteString attribute.");
}

Expand Down
12 changes: 11 additions & 1 deletion src/AutoByte.Generators/AutoByteSourceGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamespaceDeclarationSyntax>().FirstOrDefault();

Expand All @@ -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<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault();

if (containingFileScopedNamespace != null)
{
// Get the namespace name from the file-scoped namespace node
return ((FileScopedNamespaceDeclarationSyntax)containingFileScopedNamespace).Name.ToString();
}

return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/AutoByte.Tests/AutoByte.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down
48 changes: 47 additions & 1 deletion src/AutoByte.Tests/ByteSlide_Must.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}




}
}
26 changes: 26 additions & 0 deletions src/AutoByte/ByteSlide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}




/// <summary>
/// Retrieves a byte array from the current pointer position and aligns the pointer to the specified value.
Expand Down