1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) 2024-2025 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use Debug;
use SegIndex;
use crate::;
/// `Seg` represents a grapheme cluster segment within a [`super::GCString`].
///
/// A Unicode "grapheme" is a user-perceived character.
/// - For `UTF-8` encoded text, a grapheme can be a single byte or up to 4 bytes.
/// - A "grapheme cluster" can be multiple graphemes or code points or Unicode scalar
/// values.
///
/// - For example, the `๐` emoji is a single grapheme cluster, also represented by a
/// single code point.
/// - However, the `๐๐ฝ` emoji is a jumbo emoji that is an amalgamation of multiple code
/// points `'๐' + '๐ฝ'`.
/// - The single letter "A" (U+0041) is a grapheme cluster consisting of one code point.
/// - The letter "รก" can be represented as a single code point (U+00E1) or as a
/// combination of "a" (U+0061) followed by a combining acute accent (U+0301). In the
/// latter case, the grapheme cluster is the combination of the two code points "a" +
/// "ยด".
/// - Emoji like "๐จโ๐ฉโ๐งโ๐ฆ" (family) are often represented by a sequence of multiple code points
/// (in this case, several characters joined by zero-width joiners). The entire sequence
/// forms a single grapheme cluster.
///
/// Let's take the example of `๐๐ฝ`. This is a jumbo emoji that is an amalgamation of
/// multiple code points.
/// - If you use [`str::chars()`] to parse this, you would get two separate [char]: `'๐'`
/// + `'๐ฝ'`.
/// - However, [`unicode_segmentation::UnicodeSegmentation`] represents this as a single
/// grapheme cluster. This is why we use [`unicode_segmentation::UnicodeSegmentation`]
/// to handle grapheme clusters.
///
/// # Performance, memory latency, access, allocation
///
/// 1. This struct does not allocate anything and is [Copy].
/// 2. The [`super::GCString`] owns the memory, and this struct is a "view" into parts of
/// it, where each part is a grapheme cluster, and each of them is represented by this
/// struct.
///
/// This struct provides information about a single grapheme cluster, including its byte
/// indices within the original string, its display width, its logical index within the
/// [`super::GCString`], its byte size, and its starting display column index.
///
/// ## Fields
///
/// - `start_byte_index`: The starting byte index of the grapheme cluster within the
/// original string.
/// - `end_byte_index`: The ending byte index of the grapheme cluster within the original
/// string.
/// - `display_width`: The display width of the grapheme cluster, as calculated by
/// [`unicode_width::UnicodeWidthChar`].
/// - `seg_index`: The index of this grapheme cluster within the
/// [`super::GCString::segments`] vector.
/// - `bytes_size`: The number of bytes this grapheme cluster occupies in the original
/// string.
/// - `start_display_col_index`: The display column index at which this grapheme cluster
/// starts in the original string.
///
/// ## Purpose
///
/// The `Seg` struct is used to efficiently represent and manipulate grapheme clusters
/// within a [`super::GCString`]. It allows for easy access to the underlying string
/// slice, as well as information about its display width and position.
///
/// # UTF-8 is variable length encoding
///
/// Rust uses `UTF-8` to represent text in [String]. `UTF-8` is a variable width encoding,
/// so each character can take up a different number of bytes, between 1 and 4, and 1 byte
/// is 8 bits; this is why we use [Vec] of [u8] to represent a [String].
///
/// For example, the character `H` takes up 1 byte. `UTF-8` is also backward compatible
/// with `ASCII`, meaning that the first 128 characters (the ASCII characters) are
/// represented using the same single byte as in ASCII. So the character `H` is
/// represented by the same byte value in `UTF-8` as it is in `ASCII`. This is why `UTF-8`
/// is so popular, as it allows for the representation of all the characters in the
/// Unicode standard, while still being able to represent `ASCII` characters in the same
/// way.
///
/// A grapheme cluster is a user-perceived character. Grapheme clusters can take up many
/// more bytes, e.g., 4 bytes or 2 or 3, etc. Here are some examples:
/// - `๐` takes up 4 bytes.
/// - `๐ฆ` also takes up 4 bytes.
/// - `๐๐ฝ` takes up 4 bytes, but it is a compound grapheme cluster.
/// - `H` takes up only 1 byte.
///
/// Videos:
///
/// - [Live coding video on Rust String](https://youtu.be/7I11degAElQ?si=xPDIhITDro7Pa_gq)
/// - [UTF-8 encoding video](https://youtu.be/wIVmDPc16wA?si=D9sTt_G7_mBJFLmc)
///
/// ## Usage
///
/// This struct is primarily used internally by the [`super::GCString`] struct. However,
/// it can also be used directly to access information about individual grapheme clusters.
///
/// ## Example
///
/// ```
/// use r3bl_tui::{GCString, GCStringExt, ch, col, width, seg_index};
/// let u_str = "๐ฆ๐๐ฝ".grapheme_string();
/// if let Some(segment) = u_str.segments.first() {
/// assert_eq!(segment.start_byte_index, ch(0));
/// assert_eq!(segment.end_byte_index, ch(4));
/// assert_eq!(segment.display_width, width(2));
/// assert_eq!(segment.seg_index, seg_index(0));
/// assert_eq!(segment.bytes_size, 4);
/// assert_eq!(segment.start_display_col_index, col(0));
/// }
/// ```
/// Pretty print for [`crate::Seg`] that is compact and easier to read. The default
/// implementation takes up too much space and makes it difficult to debug.