Skip to content

Commit

Permalink
Add support for macro objects being UTF-8 string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
lithiumtoast committed Jan 3, 2025
1 parent 4e47ad8 commit 3677012
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-03 12:59:48 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2025-01-03 12:59:48 GMT-05:00)
// This code was generated by the following tool on 2025-01-03 14:27:37 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2025-01-03 14:27:37 GMT-05:00)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-03 12:59:48 GMT-05:00:
// This code was generated by the following tool on 2025-01-03 14:27:37 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-03 12:59:48 GMT-05:00:
// This code was generated by the following tool on 2025-01-03 14:27:37 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down Expand Up @@ -49,6 +49,8 @@ public static unsafe partial class my_c_library
[UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })]
public static partial void hw_pass_string(CString s);

public static readonly CString HW_STRING_POINTER = (CString)"Hello world using UTF-8 string literal from the C library's data segment!"u8;

public enum hw_my_enum_week_day : int
{
HW_MY_ENUM_WEEK_DAY_UNKNOWN = 0,
Expand Down
3 changes: 3 additions & 0 deletions src/cs/examples/helloworld/helloworld-app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ private static unsafe void Main()
// Only available in C# 11 (.NET 7+). See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#utf-8-string-literals
var cString1 = (CString)"Hello world from C# using UTF-8 string literal! No need to free this string!"u8;
hw_pass_string(cString1);

// NOTE: This is particularly useful if you have C defines to strings which are stored in the data segment of the loaded C library.
hw_pass_string(HW_STRING_POINTER);
#endif

// NOTE: If you don't apply the `u8` it's a UTF-16 string which needs to be converted to UTF-8 and allocated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define MY_C_LIBRARY_API_DECL extern __attribute__ ((visibility("default")))
#endif

#define HW_STRING_POINTER "Hello world using UTF-8 string literal from the C library's data segment!"

typedef enum hw_my_enum_week_day {
HW_MY_ENUM_WEEK_DAY_UNKNOWN,
HW_MY_ENUM_WEEK_DAY_MONDAY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ protected override string GenerateCode(
string nameCSharp, CodeGeneratorContext context, CMacroObject node)
{
var cSharpTypeName = context.NameMapper.GetTypeNameCSharp(node.Type);
var code = $"""

string code;
#pragma warning disable IDE0045
if (cSharpTypeName == "CString" && node.Value.StartsWith('"') && node.Value.EndsWith('"'))
#pragma warning restore IDE0045
{
code = $"""
public static readonly {cSharpTypeName} {nameCSharp} = ({cSharpTypeName}){node.Value}u8;
""";
}
else
{
code = $"""
public static readonly {cSharpTypeName} {nameCSharp} = ({cSharpTypeName}){node.Value};
""";
}

return code;
}
Expand Down

0 comments on commit 3677012

Please sign in to comment.