-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1694 from glopesdev/issue-1692
Improve detection of word boundaries in node names
- Loading branch information
Showing
5 changed files
with
95 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Bonsai.Editor.Tests | ||
{ | ||
[TestClass] | ||
public class WordSeparationTests | ||
{ | ||
[DataTestMethod] | ||
[DataRow("")] | ||
[DataRow("Point.X", "Point.", "X")] | ||
[DataRow("Source/1", "Source/", "1")] | ||
[DataRow("Source/Path", "Source/", "Path")] | ||
[DataRow("State Space", "State ", "Space")] | ||
[DataRow("TimeStep.ElapsedTime", "Time", "Step.", "Elapsed", "Time")] | ||
[DataRow("UpdateVRState", "Update", "VR", "State")] | ||
[DataRow("Get-DisplayLED", "Get-", "Display", "LED")] | ||
[DataRow("A_LARGE_NAME", "A_", "LARGE_", "NAME")] | ||
[DataRow("Get-LatestI2CReading", "Get-", "Latest", "I2C", "Reading")] | ||
[DataRow("ObserveOnTaskPool", "Observe", "On", "Task", "Pool")] | ||
public void SplitOnWordBoundaries_ExpectedWordSequence(string text, params string[] expectedWords) | ||
{ | ||
var words = text.SplitOnWordBoundaries(); | ||
CollectionAssert.AreEqual(expectedWords, words); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentNullException))] | ||
public void SplitOnWordBoundaries_NullString_ThrowsArgumentNullException() | ||
{ | ||
StringExtensions.SplitOnWordBoundaries(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace Bonsai.Editor | ||
{ | ||
internal static class StringExtensions | ||
{ | ||
static bool IsLowerToUpperCase(char a, char b) | ||
{ | ||
return char.IsLower(a) && char.IsUpper(b); | ||
} | ||
|
||
static bool IsUpperToLowerCase(char a, char b) | ||
{ | ||
return char.IsUpper(a) && char.IsLower(b); | ||
} | ||
|
||
static bool IsWordSeparator(char c) | ||
{ | ||
return char.IsSeparator(c) || char.IsPunctuation(c); | ||
} | ||
|
||
static bool IsWordBoundary(string text, int index, char current) | ||
{ | ||
var previous = text[index - 1]; | ||
return IsLowerToUpperCase(previous, current) | ||
|| IsWordSeparator(previous) | ||
|| index < text.Length - 1 && IsUpperToLowerCase(current, text[index + 1]); | ||
} | ||
|
||
public static string[] SplitOnWordBoundaries(this string text) | ||
{ | ||
if (text == null) | ||
{ | ||
throw new ArgumentNullException(nameof(text)); | ||
} | ||
|
||
var wordCount = 0; | ||
var words = new string[text.Length]; | ||
var builder = new StringBuilder(text.Length); | ||
for (int i = 0; i < text.Length; i++) | ||
{ | ||
var c = text[i]; | ||
if (i > 0 && IsWordBoundary(text, i, c)) | ||
{ | ||
words[wordCount++] = builder.ToString(); | ||
builder.Clear(); | ||
} | ||
|
||
builder.Append(c); | ||
} | ||
|
||
if (builder.Length > 0) words[wordCount++] = builder.ToString(); | ||
Array.Resize(ref words, wordCount); | ||
return words; | ||
} | ||
} | ||
} |