diff --git a/src/Avalonia.Controls/Primitives/AccessText.cs b/src/Avalonia.Controls/Primitives/AccessText.cs
index 8a269287b2..15c44e8c12 100644
--- a/src/Avalonia.Controls/Primitives/AccessText.cs
+++ b/src/Avalonia.Controls/Primitives/AccessText.cs
@@ -76,26 +76,12 @@ namespace Avalonia.Controls.Primitives
rect.BottomLeft + offset,
rect.BottomRight + offset);
}
-
- internal static string RemoveAccessKeyMarker(string text)
- {
- if (!string.IsNullOrEmpty(text))
- {
- var accessKeyMarker = "_";
- var doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;
- int index = FindAccessKeyMarker(text);
- if (index >= 0 && index < text.Length - 1)
- text = text.Remove(index, 1);
- text = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
- }
- return text;
- }
}
///
protected override TextLayout CreateTextLayout(Size constraint, string text)
{
- return base.CreateTextLayout(constraint, StripAccessKey(text));
+ return base.CreateTextLayout(constraint, RemoveAccessKeyMarker(text));
}
///
@@ -127,23 +113,35 @@ namespace Avalonia.Controls.Primitives
return new NoneAutomationPeer(factory, this);
}
- ///
- /// Returns a string with the first underscore stripped.
- ///
- /// The text.
- /// The text with the first underscore stripped.
- private string StripAccessKey(string text)
+ internal static string RemoveAccessKeyMarker(string text)
{
- var position = text.IndexOf('_');
-
- if (position == -1)
+ if (!string.IsNullOrEmpty(text))
{
- return text;
+ var accessKeyMarker = "_";
+ var doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;
+ int index = FindAccessKeyMarker(text);
+ if (index >= 0 && index < text.Length - 1)
+ text = text.Remove(index, 1);
+ text = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
}
- else
+ return text;
+ }
+
+ private static int FindAccessKeyMarker(string text)
+ {
+ var length = text.Length;
+ var startIndex = 0;
+ while (startIndex < length)
{
- return text.Substring(0, position) + text.Substring(position + 1);
+ int index = text.IndexOf('_', startIndex);
+ if (index == -1)
+ return -1;
+ if (index + 1 < length && text[index + 1] != '_')
+ return index;
+ startIndex = index + 2;
}
+
+ return -1;
}
///