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
use crate::byte_cursor::{byte_offset_cursor_to_byte_offset, char_byte_offset_to_cursor};
use cosmic_text::{Affinity, Cursor};
#[test]
pub fn test_char_byte_offset_to_cursor() {
// Test with a simple Latin string
let text = "Hello World";
// Test byte offset at the beginning of the string
let cursor = char_byte_offset_to_cursor(text, 0);
assert_eq!(
cursor,
Some(Cursor {
line: 0,
index: 0,
affinity: Affinity::Before,
})
);
// Test byte offset at the beginning of the string
let cursor = char_byte_offset_to_cursor(text, 1);
assert_eq!(
cursor,
Some(Cursor {
line: 0,
index: 1,
affinity: Affinity::Before,
})
);
// Test byte offset in the middle of the string
let cursor = char_byte_offset_to_cursor(text, 5);
assert_eq!(
cursor,
Some(Cursor {
line: 0,
index: 5,
affinity: Affinity::Before,
})
);
// Test byte offset at the end of the string
let cursor = char_byte_offset_to_cursor(text, 11);
assert_eq!(
cursor,
Some(Cursor {
line: 0,
index: 11,
affinity: Affinity::Before
})
);
// Test byte offset beyond the end of the string
let cursor = char_byte_offset_to_cursor(text, 12);
assert_eq!(cursor, None);
// Test with a multi-line string
let text = "Hello\nWorld";
// Test byte offset at the beginning of the second line
let cursor = char_byte_offset_to_cursor(text, 6);
assert_eq!(
cursor,
Some(Cursor {
line: 1,
index: 0,
affinity: Affinity::Before,
})
);
// Test byte offset in the middle of the second line
let cursor = char_byte_offset_to_cursor(text, 8);
assert_eq!(
cursor,
Some(Cursor {
line: 1,
index: 2,
affinity: Affinity::Before,
})
);
// Test byte offset at the end of the second line
let cursor = char_byte_offset_to_cursor(text, 11);
assert_eq!(
cursor,
Some(Cursor {
line: 1,
index: 5,
affinity: Affinity::Before,
})
);
// Test byte offset at the end of the second line
let cursor = char_byte_offset_to_cursor(text, 12);
assert_eq!(cursor, None);
// Test with an invalid byte offset (beyond the end of the string)
let cursor = char_byte_offset_to_cursor(text, 20);
assert_eq!(cursor, None);
}
#[test]
pub fn test_byte_offset_cursor_to_byte_offset() {
// Test with a simple Latin string
let text = "Hello World";
// Test cursor at the beginning of the string
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 0,
index: 0,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, Some(0));
// Test cursor at the second character of the string
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 0,
index: 1,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, Some(1));
// Test cursor in the middle of the string
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 0,
index: 5,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, Some(5));
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 0,
index: 4,
affinity: Affinity::After,
},
);
// Foe the byte offset, we don't really care about the affinity, so we can use either Before or After
assert_eq!(byte_offset, Some(4));
// Test with a multi-line string
let text = "Hello\nWorld";
// Test cursor at the beginning of the second line
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 1,
index: 0,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, Some(6));
// Test cursor in the middle of the second line
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 1,
index: 2,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, Some(8));
// Test with an invalid cursor (line beyond available lines)
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 2,
index: 0,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, None);
// Test with an invalid cursor (index beyond line length)
let byte_offset = byte_offset_cursor_to_byte_offset(
text,
Cursor {
line: 0,
index: 20,
affinity: Affinity::Before,
},
);
assert_eq!(byte_offset, None);
}