Skip to content

string.Copy deprecation removes a necessary use case with TextRenderer.MeasureText #1564

Closed

Description

@MatthewBeardmore
  • .NET Core Version: 3.0 Preview 7
  • Have you experienced this same bug with .NET Framework?: No

Problem description:
string.Copy was deprecated in .NET Core 3.0 (for good reason), however, I had been using it with TextRenderer.MeasureText, which modifies the string when calling it if the string is too long for the width specified. Without string.Copy, I'm not sure how else you would use this API without string.Copy for now. With the warning showing that this API was removed as strings may be deduplicated in the future, I wasn't sure about how to properly change this code to be more future-proof, as I'm not sure it can be in its current state.

Minimal repro:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            string original = @"C:\New Folder\New Folder\New Folder\New Folder\New Folder\New Folder\text.txt";
            string result = GetCompactedString(original, 4);
            //Result is now correctly "...\text.txt"
            //Expectation is that original is still the very long file path, but it is the same as result
        }

        public static string GetCompactedString(string stringToCompact, int maxWidth)
        {
            //This line is necessary to not cause the bug shown below
            //string copy= string.Copy(stringToCompact);
            string copy = stringToCompact;

            var opts = TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString;
            TextRenderer.MeasureText(copy, new Font("Segoe UI", 9), new Size(maxWidth, 0), opts);

            // https://connect.microsoft.com/VisualStudio/feedback/details/620330
            int nullByteLoc = copy.IndexOf('\0');
            if (nullByteLoc != -1)
            {
                copy = copy.Substring(0, nullByteLoc);
            }

            return copy;
        }
    }
}

Activity

  1. weltkante commented on Aug 2, 2019

    @weltkante
    Contributor

    That sounds more like a bug in the MeasureText implementation than an intended feature, but nice catch anyways. I suspect fixing this needs a new overload though, I don't think the function should go around and break runtime invariants by writing into an immutable object.

  2. merriemcgaw commented on Aug 9, 2019

    @merriemcgaw
    Contributor

    I think we are beyond the timeframe where we can add an overload for 3.0 and 3.1 so we will probably need to fix this issue in 5.0. Moving to that milestone for now. We can always bring it back if we have the time to fix it.

  3. added
    tenet-compatibility
    on Aug 9, 2019
  4. added this to the 5.0.0 milestone on Aug 9, 2019
  5. zsd4yr commented on Aug 9, 2019

    @zsd4yr
    Contributor

    It is not as simple as string.Copy, but can you just?:

    string copy = new string(original.ToCharArray());

    or

    string copy= $"{original}";
  6. modified the milestones: 5.0 Previews 1-4, 5.0 on Apr 20, 2020
  7. self-assigned this
    on Jul 28, 2020
  8. JeremyKuhne commented on Jul 28, 2020

    @JeremyKuhne
    Member

    Need to validate we don't allow corrupting the immutability of strings.

  9. weltkante commented on Jul 28, 2020

    @weltkante
    Contributor

    Like said above TextRenderer should probably filter out or throw on passing the ModifyString flag and have a separate API for mutable strings (i.e. StringBuilder, ref string or mutable span)

  10. modified the milestones: 5.0, 6.0 on Sep 9, 2020
  11. added
    design-discussion
    on Sep 9, 2020
  12. JeremyKuhne commented on Sep 9, 2020

    @JeremyKuhne
    Member

    #3710 tracks replacing the existing TextRenderer API with a safe alternative.

  13. removed this from the 6.0 milestone on Sep 9, 2020
  14. ghost locked as resolved and limited conversation to collaborators on Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

design-discussiontenet-compatibility

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions