77
88class TextSegment :
99 def __init__ (self ):
10- self ._text : GraphemeString = GraphemeString ("" )
10+ self ._text : GlyphString = GlyphString ("" )
1111 self ._immediate_preceding_marker : UsfmMarkerType = UsfmMarkerType .NO_MARKER
1212 self ._markers_in_preceding_context : Set [UsfmMarkerType ] = set ()
1313 self .previous_segment : Optional [TextSegment ] = None
@@ -32,7 +32,7 @@ def __eq__(self, value):
3232 return True
3333
3434 @property
35- def text (self ) -> "GraphemeString " :
35+ def text (self ) -> "GlyphString " :
3636 return self ._text
3737
3838 @property
@@ -55,7 +55,7 @@ def is_last_segment_in_verse(self) -> bool:
5555 return self .index_in_verse == self .num_segments_in_verse - 1
5656
5757 def replace_substring (self , start_index : int , end_index : int , replacement : str ) -> None :
58- self ._text = GraphemeString (self .substring_before (start_index ) + replacement + self .substring_after (end_index ))
58+ self ._text = GlyphString (self .substring_before (start_index ) + replacement + self .substring_after (end_index ))
5959 if self ._usfm_token is not None :
6060 self ._usfm_token .text = str (self ._text )
6161
@@ -77,49 +77,49 @@ def set_usfm_token(self, token: UsfmToken) -> "TextSegment.Builder":
7777 return self
7878
7979 def set_text (self , text : str ) -> "TextSegment.Builder" :
80- self ._text_segment ._text = GraphemeString (text )
80+ self ._text_segment ._text = GlyphString (text )
8181 return self
8282
8383 def build (self ) -> "TextSegment" :
8484 return self ._text_segment
8585
8686
87- class GraphemeString :
87+ class GlyphString :
8888 def __init__ (self , string : str ) -> None :
8989 self ._string = string
90- self ._string_index_by_grapheme_index = {
91- grapheme_index : string_index
92- for grapheme_index , string_index in enumerate (
90+ self ._string_index_by_glyph_index = {
91+ glyph_index : string_index
92+ for glyph_index , string_index in enumerate (
9393 [i for i , c in enumerate (string ) if unicodedata .category (c ) not in ["Mc" , "Mn" ]]
9494 )
9595 }
9696
9797 def __len__ (self ) -> int :
98- return len (self ._string_index_by_grapheme_index )
98+ return len (self ._string_index_by_glyph_index )
9999
100100 def __str__ (self ):
101101 return self ._string
102102
103103 def __eq__ (self , other ) -> bool :
104- if not isinstance (other , GraphemeString ):
104+ if not isinstance (other , GlyphString ):
105105 return False
106106 return self ._string == other ._string
107107
108- def __getitem__ (self , key ) -> "GraphemeString " :
108+ def __getitem__ (self , key ) -> "GlyphString " :
109109 if isinstance (key , int ):
110- grapheme_start = self ._normalize_start_index (key )
111- grapheme_stop = self ._normalize_stop_index (grapheme_start + 1 )
112- string_start = self ._string_index_by_grapheme_index .get (grapheme_start , len (self ))
113- string_stop = self ._string_index_by_grapheme_index .get (grapheme_stop , None )
114- return GraphemeString (self ._string [string_start :string_stop ])
110+ glyph_start = self ._normalize_start_index (key )
111+ glyph_stop = self ._normalize_stop_index (glyph_start + 1 )
112+ string_start = self ._string_index_by_glyph_index .get (glyph_start , len (self ))
113+ string_stop = self ._string_index_by_glyph_index .get (glyph_stop , None )
114+ return GlyphString (self ._string [string_start :string_stop ])
115115 elif isinstance (key , slice ):
116116 if key .step is not None and key .step != 1 :
117- raise TypeError ("Steps are not allowed in _GraphemeString slices" )
118- grapheme_start = self ._normalize_start_index (key .start )
119- grapheme_stop = self ._normalize_stop_index (key .stop )
120- string_start = self ._string_index_by_grapheme_index .get (grapheme_start , len (self ))
121- string_stop = self ._string_index_by_grapheme_index .get (grapheme_stop , None )
122- return GraphemeString (self ._string [string_start :string_stop ])
117+ raise TypeError ("Steps are not allowed in _glyphString slices" )
118+ glyph_start = self ._normalize_start_index (key .start )
119+ glyph_stop = self ._normalize_stop_index (key .stop )
120+ string_start = self ._string_index_by_glyph_index .get (glyph_start , len (self ))
121+ string_stop = self ._string_index_by_glyph_index .get (glyph_stop , None )
122+ return GlyphString (self ._string [string_start :string_stop ])
123123 else :
124124 raise TypeError ("Indices must be integers or slices" )
125125
@@ -137,10 +137,10 @@ def _normalize_stop_index(self, index: Optional[int]) -> int:
137137 return len (self ) + index
138138 return index
139139
140- def string_index_to_grapheme_index (self , string_index : int ) -> int :
140+ def string_index_to_glyph_index (self , string_index : int ) -> int :
141141 if string_index == len (self ._string ):
142142 return len (self )
143- for g_index , s_index in self ._string_index_by_grapheme_index .items ():
143+ for g_index , s_index in self ._string_index_by_glyph_index .items ():
144144 if s_index == string_index :
145145 return g_index
146- raise ValueError (f"No corresponding grapheme index found for string index { string_index } ." )
146+ raise ValueError (f"No corresponding glyph index found for string index { string_index } ." )
0 commit comments