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

Fix bisearch array bounds #44

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions NStack/unicode/Rune.ColumnWidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ public partial struct Rune
{ 0xE0100, 0xE01EF }
};

static int bisearch (uint rune, uint [,] table, int max)
static int bisearch (uint rune, uint [,] table)
{
int min = 0;
int max = table.GetLength (0) - 1;
int mid;


if (rune < table [0, 0] || rune > table [max, 1])
return 0;
while (max >= min) {
Expand All @@ -82,8 +84,8 @@ static int bisearch (uint rune, uint [,] table, int max)
/// <summary>
/// Number of column positions of a wide-character code. This is used to measure runes as displayed by text-based terminals.
/// </summary>
/// <returns>The width in columns, 0 if the argument is the null character, -1 if the value is not printable, otherwise the number of columsn that the rune occupies.</returns>
/// <param name="r">The red component.</param>
/// <returns>The width in columns, 0 if the argument is the null character, -1 if the value is not printable, otherwise the number of columns that the rune occupies.</returns>
/// <param name="rune">The rune to measure</param>
public static int ColumnWidth (Rune rune)
{
uint irune = (uint)rune;
Expand All @@ -94,7 +96,7 @@ public static int ColumnWidth (Rune rune)
if (irune >= 0x7f && irune <= 0xa0)
return 0;
/* binary search in table of non-spacing characters */
if (bisearch (irune, combining, combining.GetLength (0)-1) != 0)
if (bisearch (irune, combining) != 0)
return 0;
/* if we arrive here, ucs is not a combining or C0/C1 control character */
return 1 +
Expand Down
10 changes: 10 additions & 0 deletions NStackTests/UnicodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ public void TestIsUpper ()

};

[Test]
public void TestColumnWidth ()
{
// unicode chars taken from https://github.com/migueldeicaza/gui.cs/issues/146
foreach (var c in new string[] { "\u2261", "\u2302", "\u2191", "\u2193", "\u2026"}) {
(var rune, var size) = Utf8.DecodeRune (c);
Assert.AreEqual (Rune.ColumnWidth (rune), 1);
}
}

[Test]
public void TestTo ()
{
Expand Down