Skip to content

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.

csharp
public static class GraphemeHelper

Inheritance

ObjectGraphemeHelper

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

csharp
public static int DisplayColumnToIndex(string text, int column)

GetClusterAt(string, int)

Gets the grapheme cluster at the specified index.

Parameters:

Returns: String

csharp
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:

Returns: List<Int32>

csharp
public static List<int> GetClusterBoundaries(string text)

GetClusterCount(string)

Counts the number of grapheme clusters in the text.

Parameters:

Returns: Int32

csharp
public static int GetClusterCount(string text)

GetClusterDisplayWidth(string)

Gets the terminal display width of a single grapheme cluster.

Parameters:

Returns: Int32

csharp
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

csharp
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:

Returns: Int32

csharp
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:

  • text (String): The text to navigate
  • index (Int32): The current cursor position (0 to text.Length)

Returns: Int32

The index after the current grapheme cluster, or the current index if already at end

csharp
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:

  • text (String): The text to navigate
  • index (Int32): The current cursor position (0 to text.Length)

Returns: Int32

The index of the start of the next word, or text.Length if at end

csharp
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:

  • text (String): The text to navigate
  • index (Int32): The current cursor position (0 to text.Length)

Returns: Int32

The index of the start of the previous grapheme cluster, or the current index if already at start

csharp
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:

  • text (String): The text to navigate
  • index (Int32): The current cursor position (0 to text.Length)

Returns: Int32

The index of the start of the previous word, or 0 if at beginning

csharp
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

csharp
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

csharp
public static int SnapToClusterBoundary(string text, int index)

Released under the MIT License.