Skip to content

Tabular output is not displayed correctly for non-Latin chars - #3922

Merged
vlada-shubina merged 26 commits into
mainfrom
dev/bozturk/wcwidth
Oct 4, 2021
Merged

vlada-shubina merged 26 commits into
mainfrom
dev/bozturk/wcwidth

Conversation

@bekir-ozturk

Copy link
Copy Markdown

Problem

Fixes #3288

Solution

Take into account that a single character may not always occupy 1 space in the console.

Checks:

  • Added unit tests
  • Added #nullable enable to all the modified files ?

@bekir-ozturk
bekir-ozturk requested a review from a team as a code owner September 17, 2021 08:28
Comment thread src/dotnet-new3/Program.cs Resolved
@vlada-shubina vlada-shubina removed their assignment Sep 29, 2021

@DavidKarlas DavidKarlas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK, I suggested few improvements and maybe some comments explaining things.


int desiredLength = maxLength - ShrinkReplacement.Length;
int possibleLength = 1;
while (text.Substring(0, possibleLength).GetUnicodeLength() <= desiredLength)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make sense performance wise, to start with desiredLength and do possibleLength-- and go towards zero...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this to remove allocations. Starting from the beginning and moving one by one, we also calculate each character's length only once now, so it should be much better performance wise.

{
internal static int GetUnicodeLength (this string s)
{
return s.Sum(ch => Wcwidth.UnicodeCalculator.GetWidth((int)ch));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: For some reason I'm not impressed with using lambda here because perf, maybe I'm wrong... We can also improve Utf16->32 story here by doing:

var sum = 0;
foreach (Rune rune in s.EnumerateRunes())
{
    sum += Wcwidth.UnicodeCalculator.GetWidth(rune.Value);
}
return sum;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rune's are not available in .NET Standard, unfortunately, which we target.
Replaced the Sum with a for loop.

for (int j = 0; j < columnIndexes.Length; j++)
{
parsedRow.Add(lines[i].Substring(columnsIndexes[j], columnsIndexes[j + 1] - columnsIndexes[j]).Trim());
int unfilledColumnWidth = (j == columnIndexes.Length - 1 ? line.Length : columnIndexes[j + 1] ) - columnIndexes[j];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this part dealing with cutting code into helper static method?

@vlada-shubina
vlada-shubina merged commit 48f5e83 into main Oct 4, 2021
@vlada-shubina
vlada-shubina deleted the dev/bozturk/wcwidth branch October 4, 2021 12:33
vlada-shubina added a commit to vlada-shubina/templating that referenced this pull request Oct 4, 2021
…#3922)

* Reference Wcwidth.Sources package
* Default to Utf8 encoding if used as sub-process (https://dev.azure.com/devdiv/DevDiv/_wiki/wikis/DevDiv.wiki/4236/Character-Encoding-Issues?anchor=.net)
* Disable stylecop rules for source package references
Co-authored-by: Vlada Shubina <[email protected]>
# Conflicts:
#	eng/Versions.props
vlada-shubina added a commit that referenced this pull request Oct 4, 2021
* Reference Wcwidth.Sources package
* Default to Utf8 encoding if used as sub-process (https://dev.azure.com/devdiv/DevDiv/_wiki/wikis/DevDiv.wiki/4236/Character-Encoding-Issues?anchor=.net)
* Disable stylecop rules for source package references
Co-authored-by: Vlada Shubina <[email protected]>
# Conflicts:
#	eng/Versions.props
verdie-g pushed a commit to verdie-g/sdk that referenced this pull request Jul 16, 2026
…/templating#3922)

* Reference Wcwidth.Sources package
* Default to Utf8 encoding if used as sub-process (https://dev.azure.com/devdiv/DevDiv/_wiki/wikis/DevDiv.wiki/4236/Character-Encoding-Issues?anchor=.net)
* Disable stylecop rules for source package references
Co-authored-by: Vlada Shubina <[email protected]>

Commit migrated from dotnet/templating@48f5e83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tabular output is not displayed correctly for non-Latin chars

3 participants