Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to interpolated strings #45438

Closed
gewarren opened this issue Mar 19, 2025 · 0 comments · Fixed by #45441
Closed

Convert to interpolated strings #45438

gewarren opened this issue Mar 19, 2025 · 0 comments · Fixed by #45441
Assignees
Labels
in-pr This issue will be closed (fixed) by an active pull request. resolved-by-customer

Comments

@gewarren
Copy link
Contributor

gewarren commented Mar 19, 2025

Use this console app generated by GitHub Copilot to convert to interpolated strings:

using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string directory = "c:\\repos\\docs\\docs\\fundamentals\\runtime-libraries";
        if (!Directory.Exists(directory))
        {
            Console.WriteLine($"Directory '{directory}' does not exist.");
            return;
        }

        foreach (string file in Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories))
        {
            ConvertFileToInterpolatedStrings(file);
        }
    }

    static void ConvertFileToInterpolatedStrings(string filePath)
    {
        string fileContent = File.ReadAllText(filePath);
        string updatedContent = ConvertToInterpolatedStrings(fileContent);

        if (fileContent != updatedContent)
        {
            File.WriteAllText(filePath, updatedContent);
            Console.WriteLine($"Updated file: {filePath}");
        }
    }

    static string ConvertToInterpolatedStrings(string content)
    {
        string pattern = @"Console\.WriteLine\(([^;]*)\);";
        return Regex.Replace(content, pattern, match =>
        {
            string arguments = match.Groups[1].Value;
            string[] parts = SplitArgument(arguments);

            if (parts.Length < 2)
                return match.Value;

            string formatString = parts[0].Trim();
            string[] formatArguments = parts[1..].Select(arg => arg.Trim()).ToArray();

            if (!formatString.StartsWith('"') || !formatString.EndsWith('"'))
                return match.Value;

            formatString = formatString[1..^1]; // Remove quotes

            string interpolatedString = "Console.WriteLine($\"" + Regex.Replace(formatString, @"\{(\d+)(,[^}]*)?(:[^}]*)?\}", m =>
            {
                int index = int.Parse(m.Groups[1].Value);
                string alignment = m.Groups[2].Value;
                string formatSpecifier = m.Groups[3].Value;

                if (index < formatArguments.Length)
                {
                    return "{" + formatArguments[index].Trim() + alignment + formatSpecifier + "}";
                }
                return m.Value;
            }) + "\");";

            return interpolatedString;
        });
    }

    static string[] SplitArgument(string argument)
    {
       var parts = new List<string>();
        int bracketCount = 0;
        int start = 0;

        for (int i = 0; i < argument.Length; i++)
        {
            if (argument[i] == ',' && bracketCount == 0)
            {
                parts.Add(argument[start..i].Trim());
                start = i + 1;
            }
            else if (argument[i] == '(')
            {
                bracketCount++;
            }
            else if (argument[i] == ')')
            {
                bracketCount--;
            }
        }

        parts.Add(argument[start..].Trim());
        return [.. parts];
    }
}
@gewarren gewarren self-assigned this Mar 19, 2025
@dotnetrepoman dotnetrepoman bot added the 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. label Mar 19, 2025
@gewarren gewarren moved this from 🔖 Ready to 🏗 In progress in dotnet/docs March 2025 sprint project Mar 19, 2025
@dotnet-policy-service dotnet-policy-service bot added ⌚ Not Triaged Not triaged and removed 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. labels Mar 19, 2025
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Mar 19, 2025
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in dotnet/docs March 2025 sprint project Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-pr This issue will be closed (fixed) by an active pull request. resolved-by-customer
Projects
Development

Successfully merging a pull request may close this issue.

1 participant