Skip to content

Commit cbcb6df

Browse files
konardclaude
andcommitted
Fix readonly to const transformation for string types
- Updated regex pattern on line 127 to include 'const' in the replacement string - This ensures that both 'readonly' and 'const' C# keywords are converted to 'const' in C++ - Added test cases in experiments/ folder to verify the transformation works correctly - All existing tests continue to pass Resolves issue #56 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bc810a5 commit cbcb6df

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

csharp/Platform.RegularExpressions.Transformer.CSharpToCpp/CSharpToCppTransformer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class CSharpToCppTransformer : TextTransformer
124124
(new Regex(@"(?<access>(private|protected|public): )?static readonly (?<type>[a-zA-Z0-9]+(<[a-zA-Z0-9]+>)?) (?<name>[a-zA-Z0-9_]+) = new \k<type>\((?<arguments>[^\n]+)\);"), "${access}inline static ${type} ${name} = ${type}(${arguments});", 0),
125125
// public: static readonly string ExceptionContentsSeparator = "---";
126126
// public: inline static std::string ExceptionContentsSeparator = "---";
127-
(new Regex(@"(?<access>(private|protected|public): )?(const|static readonly) string (?<name>[a-zA-Z0-9_]+) = ""(?<string>(\\""|[^""\r\n])+)"";"), "${access}inline static std::string ${name} = \"${string}\";", 0),
127+
(new Regex(@"(?<access>(private|protected|public): )?(const|static readonly) string (?<name>[a-zA-Z0-9_]+) = ""(?<string>(\\""|[^""\r\n])+)"";"), "${access}inline static const std::string ${name} = \"${string}\";", 0),
128128
// private: const int MaxPath = 92;
129129
// private: inline static const int MaxPath = 92;
130130
(new Regex(@"(?<access>(private|protected|public): )?(const|static readonly) (?<type>[a-zA-Z0-9]+) (?<name>[_a-zA-Z0-9]+) = (?<value>[^;\r\n]+);"), "${access}inline static const ${type} ${name} = ${value};", 0),

experiments/Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using Platform.RegularExpressions.Transformer.CSharpToCpp;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
string inputCode = @"// Test cases for readonly transformations
10+
public class TestClass
11+
{
12+
// Test string readonly
13+
public static readonly string ExceptionContentsSeparator = ""---"";
14+
private static readonly string PrivateMessage = ""error"";
15+
16+
// Test other types readonly
17+
public static readonly int MaxPath = 92;
18+
private static readonly bool IsEnabled = true;
19+
20+
// Test const (should already work)
21+
private const int ConstValue = 42;
22+
public const string ConstString = ""test"";
23+
}";
24+
25+
Console.WriteLine("Original code:");
26+
Console.WriteLine(inputCode);
27+
Console.WriteLine("\n" + new string('=', 50) + "\n");
28+
29+
var transformer = new CSharpToCppTransformer();
30+
string transformed = transformer.Transform(inputCode);
31+
Console.WriteLine("Transformed code:");
32+
Console.WriteLine(transformed);
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="../csharp/Platform.RegularExpressions.Transformer.CSharpToCpp/Platform.RegularExpressions.Transformer.CSharpToCpp.csproj" />
8+
</ItemGroup>
9+
</Project>

experiments/readonly_test.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test cases for readonly transformations
2+
public class TestClass
3+
{
4+
// Test string readonly
5+
public static readonly string ExceptionContentsSeparator = "---";
6+
private static readonly string PrivateMessage = "error";
7+
8+
// Test other types readonly
9+
public static readonly int MaxPath = 92;
10+
private static readonly bool IsEnabled = true;
11+
12+
// Test const (should already work)
13+
private const int ConstValue = 42;
14+
public const string ConstString = "test";
15+
}

0 commit comments

Comments
 (0)