From ce1c011c0bdbc67f5179cd53e4a48b08bf342a44 Mon Sep 17 00:00:00 2001 From: GoldenCrystal Date: Sat, 15 Nov 2014 15:09:12 +0100 Subject: [PATCH] =?UTF-8?q?Add=20special=20handling=20for=20non-spacing=20?= =?UTF-8?q?marks=20and=20control=20codes=20U+0000=E2=80=A6U+0020.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CharacterInfoViewModel.cs | 14 +++ .../CharacterInspectorViewModel.cs | 88 +++++++++++-------- UnicodeCharacterInspector/MainWindow.xaml | 9 +- .../StringToUtf32Converter.cs | 26 ------ .../UnicodeCharacterInspector.csproj | 1 - UnicodeInformation/UnicodeCharInfo.cs | 4 +- UnicodeInformation/UnicodeInfo.cs | 21 +++++ 7 files changed, 93 insertions(+), 70 deletions(-) delete mode 100644 UnicodeCharacterInspector/StringToUtf32Converter.cs diff --git a/UnicodeCharacterInspector/CharacterInfoViewModel.cs b/UnicodeCharacterInspector/CharacterInfoViewModel.cs index 0b06f45..7d59723 100644 --- a/UnicodeCharacterInspector/CharacterInfoViewModel.cs +++ b/UnicodeCharacterInspector/CharacterInfoViewModel.cs @@ -11,6 +11,7 @@ namespace UnicodeCharacterInspector internal sealed class CharacterInfoViewModel : BindableObject { private string character; + private string displayText; private int codePoint; private UnicodeCharInfo characterInfo = UnicodeInfo.GetCharInfo(0); @@ -49,6 +50,7 @@ namespace UnicodeCharacterInspector } NotifyPropertyChanged(); + UpdateDisplayText(); NotifyPropertyChanged(nameof(CodePoint)); NotifyPropertyChanged(nameof(Name)); NotifyPropertyChanged(nameof(OldName)); @@ -77,6 +79,18 @@ namespace UnicodeCharacterInspector } } + private void UpdateDisplayText() + { + string oldValue = displayText; + + displayText = character != null ? UnicodeInfo.GetDisplayText(characterInfo) : null; + + if (displayText != oldValue) + NotifyPropertyChanged(nameof(DisplayText)); + } + + public string DisplayText { get { return displayText; } } + public int? CodePoint { get { return character != null ? codePoint : null as int?; } diff --git a/UnicodeCharacterInspector/CharacterInspectorViewModel.cs b/UnicodeCharacterInspector/CharacterInspectorViewModel.cs index 78dd3cf..e1a556d 100644 --- a/UnicodeCharacterInspector/CharacterInspectorViewModel.cs +++ b/UnicodeCharacterInspector/CharacterInspectorViewModel.cs @@ -12,7 +12,21 @@ namespace UnicodeCharacterInspector { internal sealed class CharacterInspectorViewModel : BindableObject { - private class CharacterCollection : INotifyCollectionChanged, IList + public sealed class CharacterViewModel + { + public int CodePoint { get; } + public string Character { get; } + public string DisplayText { get; } + + public CharacterViewModel(int codePoint) + { + CodePoint = codePoint; + Character = char.ConvertFromUtf32(codePoint); + DisplayText = UnicodeInfo.GetDisplayText(codePoint); + } + } + + private class CharacterCollection : INotifyCollectionChanged, IList { public event NotifyCollectionChangedEventHandler CollectionChanged { @@ -27,17 +41,17 @@ namespace UnicodeCharacterInspector this.owner = owner; } - public string this[int index] + public CharacterViewModel this[int index] { get { if (index < 0 || index >= owner.characterCount) throw new IndexOutOfRangeException(); - return owner.characters[index]; + return new CharacterViewModel(owner.codePoints[index]); } } - string IList.this[int index] + CharacterViewModel IList.this[int index] { get { return this[index]; } set { throw new NotSupportedException(); } @@ -45,28 +59,38 @@ namespace UnicodeCharacterInspector public int Count { get { return owner.characterCount; } } - bool ICollection.IsReadOnly { get { return true; } } + bool ICollection.IsReadOnly { get { return true; } } - void ICollection.Add(string item) { throw new NotSupportedException(); } - void IList.Insert(int index, string item) { throw new NotSupportedException(); } + void ICollection.Add(CharacterViewModel item) { throw new NotSupportedException(); } + void IList.Insert(int index, CharacterViewModel item) { throw new NotSupportedException(); } - bool ICollection.Remove(string item) { throw new NotSupportedException(); } - void IList.RemoveAt(int index) { throw new NotSupportedException(); } + bool ICollection.Remove(CharacterViewModel item) { throw new NotSupportedException(); } + void IList.RemoveAt(int index) { throw new NotSupportedException(); } - void ICollection.Clear() { throw new NotSupportedException(); } + void ICollection.Clear() { throw new NotSupportedException(); } - public int IndexOf(string item) { return Array.IndexOf(owner.characters, item); } - public bool Contains(string item) { return Array.IndexOf(owner.characters, item) >= 0; } + public int IndexOf(CharacterViewModel item) { return Array.IndexOf(owner.codePoints, item); } + public bool Contains(CharacterViewModel item) { return Array.IndexOf(owner.codePoints, item) >= 0; } - public void CopyTo(string[] array, int arrayIndex) { owner.characters.CopyTo(array, arrayIndex); } + public void CopyTo(CharacterViewModel[] array, int arrayIndex) + { + if (array == null) throw new ArgumentNullException("array"); + if (arrayIndex < 0) throw new ArgumentOutOfRangeException("arrayIndex"); + if (array.Length < arrayIndex + owner.codePoints.Length) throw new ArgumentException(); - public IEnumerator GetEnumerator() + for (int i = 0, j = arrayIndex; i < owner.codePoints.Length; ++i, ++j) + { + array[j] = new CharacterViewModel(owner.codePoints[i]); + } + } + + public IEnumerator GetEnumerator() { int length = owner.characterCount; - var array = owner.characters; + var array = owner.codePoints; for (int i = 0; i < length; ++i) - yield return array[i]; + yield return new CharacterViewModel(array[i]); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } @@ -76,7 +100,7 @@ namespace UnicodeCharacterInspector private string text; private int selectedCharacterIndex = -1; - private string[] characters = new string[1024]; + private int[] codePoints = new int[1024]; private int characterCount; private CharacterInfoViewModel selectedCharacterInfo = new CharacterInfoViewModel(); private readonly CharacterCollection characterCollection; @@ -121,18 +145,9 @@ namespace UnicodeCharacterInspector { foreach (int codePoint in value.AsCodePointEnumerable()) { - if (index >= characters.Length) Array.Resize(ref characters, characters.Length * 2); + if (index >= codePoints.Length) Array.Resize(ref codePoints, codePoints.Length * 2); - // We don't replace pre-existing elements if they already have the correct value. - // This should help a bit with GC, by making new string elements as short-lived as possible. - string oldCodePointValue = characters[index]; - - if (oldCodePointValue == null || char.ConvertToUtf32(oldCodePointValue, 0) != codePoint) - { - characters[index] = char.ConvertFromUtf32(codePoint); - } - - ++index; + codePoints[index++] = codePoint; } } characterCount = index; @@ -145,21 +160,21 @@ namespace UnicodeCharacterInspector NotifyCollectionChanged(NotifyCollectionChangedAction.Reset); if (!keepSelectedIndex) { - NotifyPropertyChanged("SelectedCharacterIndex"); - NotifyPropertyChanged("SelectedCharacter"); + NotifyPropertyChanged(nameof(SelectedCharacterIndex)); + NotifyPropertyChanged(nameof(SelectedCharacter)); selectedCharacterInfo.Character = null; } } } - public ICollection Characters { get { return characterCollection; } } + public ICollection Characters { get { return characterCollection; } } public int SelectedCharacterIndex { get { return selectedCharacterIndex; } set { - if (selectedCharacterIndex < -1 || selectedCharacterIndex >= characters.Length) + if (selectedCharacterIndex < -1 || selectedCharacterIndex >= codePoints.Length) throw new ArgumentOutOfRangeException("value"); if (value != selectedCharacterIndex) @@ -171,15 +186,18 @@ namespace UnicodeCharacterInspector string newSelectedCharacter = SelectedCharacter; NotifyPropertyChanged(); - if (newSelectedCharacter != oldSelectedCharacter) NotifyPropertyChanged("SelectedCharacter"); - selectedCharacterInfo.Character = newSelectedCharacter; + if (newSelectedCharacter != oldSelectedCharacter) + { + selectedCharacterInfo.Character = newSelectedCharacter; + NotifyPropertyChanged(nameof(SelectedCharacter)); + } } } } public string SelectedCharacter { - get { return selectedCharacterIndex >= 0 ? characters[selectedCharacterIndex] : null; } + get { return selectedCharacterIndex >= 0 ? char.ConvertFromUtf32(codePoints[selectedCharacterIndex]) : null; } } public CharacterInfoViewModel SelectedCharacterInfo diff --git a/UnicodeCharacterInspector/MainWindow.xaml b/UnicodeCharacterInspector/MainWindow.xaml index 012a9f1..8fbf293 100644 --- a/UnicodeCharacterInspector/MainWindow.xaml +++ b/UnicodeCharacterInspector/MainWindow.xaml @@ -13,9 +13,6 @@ - - -