GraphemeHelper
Namespace: Hex1b
Assembly: Hex1b.dll
Helper methods for working with grapheme clusters (user-perceived characters).
A grapheme cluster is what users perceive as a single "character" but may be composed of multiple Unicode code points (and thus multiple C# chars).
This class provides methods to navigate text by grapheme cluster boundaries, ensuring operations like delete, cursor movement, and selection work correctly with emojis, combining characters, and other complex Unicode sequences.
public static class GraphemeHelperInheritance
Object → GraphemeHelper
Methods
DisplayColumnToIndex(string, int)
Converts a display column position to a string index (cursor position). Returns the index at or after the specified column.
Parameters:
Returns: Int32
public static int DisplayColumnToIndex(string text, int column)GetClusterAt(string, int)
Gets the grapheme cluster at the specified index.
Parameters:
Returns: String
public static string GetClusterAt(string text, int index)GetClusterBoundaries(string)
Gets all grapheme cluster boundary positions in the text. This includes 0 (start) and text.Length (end), plus the index after each cluster.
Parameters:
text(String):
Returns: List<Int32>
public static List<int> GetClusterBoundaries(string text)GetClusterCount(string)
Counts the number of grapheme clusters in the text.
Parameters:
text(String):
Returns: Int32
public static int GetClusterCount(string text)GetClusterDisplayWidth(string)
Gets the terminal display width of a single grapheme cluster.
Parameters:
grapheme(String):
Returns: Int32
public static int GetClusterDisplayWidth(string grapheme)GetClusterLength(string, int)
Gets the length (in chars) of the grapheme cluster that starts at the given index.
Parameters:
Returns: Int32
public static int GetClusterLength(string text, int index)GetDisplayWidth(string)
Gets the terminal display width of the text (accounting for wide characters like emoji and CJK).
Parameters:
text(String):
Returns: Int32
public static int GetDisplayWidth(string text)GetNextClusterBoundary(string, int)
Gets the string index of the end of the grapheme cluster that starts at or after the given index. Used for "move right" and "delete forward" operations.
Parameters:
Returns: Int32
The index after the current grapheme cluster, or the current index if already at end
public static int GetNextClusterBoundary(string text, int index)GetNextWordBoundary(string, int)
Gets the string index of the start of the next word. Used for Ctrl+Right navigation. Skips forward over word chars, then non-word chars.
Parameters:
Returns: Int32
The index of the start of the next word, or text.Length if at end
public static int GetNextWordBoundary(string text, int index)GetPreviousClusterBoundary(string, int)
Gets the string index of the start of the grapheme cluster that ends at or before the given index. Used for "move left" and "delete backward" operations.
Parameters:
Returns: Int32
The index of the start of the previous grapheme cluster, or the current index if already at start
public static int GetPreviousClusterBoundary(string text, int index)GetPreviousWordBoundary(string, int)
Gets the string index of the start of the previous word. Used for Ctrl+Left navigation. Skips backward over non-word chars, then word chars.
Parameters:
Returns: Int32
The index of the start of the previous word, or 0 if at beginning
public static int GetPreviousWordBoundary(string text, int index)IndexToDisplayColumn(string, int)
Converts a string index (cursor position) to a display column position.
Parameters:
Returns: Int32
public static int IndexToDisplayColumn(string text, int index)SnapToClusterBoundary(string, int)
Snaps a cursor position to the nearest valid grapheme cluster boundary. If the position is already at a boundary, returns it unchanged. Otherwise, snaps to the start of the containing cluster.
Parameters:
Returns: Int32
public static int SnapToClusterBoundary(string text, int index)