question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
  • .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;
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
JeremyKuhnecommented, Jul 28, 2020

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

0reactions
JeremyKuhnecommented, Sep 9, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why doesn't TextRenderer.MeasureText work properly?
I want to measure the height of the text given a certain width of available canvas. The text that I pass in is...
Read more >
TextRenderer.MeasureText Method (System.Windows. ...
Provides the size, in pixels, of the specified text when drawn with the specified font and formatting instructions, using the specified size to...
Read more >
Paint | Android Developers
Paint flag that enables the use of bitmap fonts when drawing text. ... The simplest case is when the string contains a single...
Read more >
graphics/java/android/graphics/Paint.java
* Paint flag that enables the use of bitmap fonts when drawing text. *. * <p>Disabling this flag will prevent text draw operations...
Read more >
Grant Winney - RSSing.com
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. Answer by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found