FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
livecode/libfoundation/src/foundation-text.cpp at develop · livecode/livecode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
livecode
/
livecode
Public archive
Notifications
You must be signed in to change notification settings
Fork
223
Star
517
Code
Pull requests
189
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-text.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
500 lines (411 loc) · 14.9 KB
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-text.cpp
Copy path
File metadata and controls
500 lines (411 loc) · 14.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>.
*/
#
include
"
foundation-text.h
"
#
include
"
foundation-auto.h
"
void
MCTextFilter::MarkText
()
{
//
All filters other than the first just pass on the message
m_Prev->
MarkText
();
}
uindex_t
MCTextFilter::GetMarkedLength
()
const
{
//
All filters other than the first just pass on the message
return
m_Prev->
GetMarkedLength
();
}
MCTextFilter::~MCTextFilter
()
{
if
(m_Next)
{
m_Next->
m_Prev
= nil;
delete
m_Next;
}
if
(m_Prev)
{
m_Prev->
m_Next
= nil;
delete
m_Prev;
}
}
bool
MCTextFilter::PlaceBefore
(MCTextFilter* p_filter)
{
p_filter->
m_Prev
=
this
;
m_Next = p_filter;
return
true
;
}
bool
MCTextFilter::PlaceAfter
(MCTextFilter* p_filter)
{
p_filter->
m_Next
=
this
;
m_Prev = p_filter;
return
true
;
}
MCTextFilter*
MCTextFilter::NextFilter
()
const
{
return
m_Next;
}
MCTextFilter*
MCTextFilter::PrevFilter
()
const
{
return
m_Prev;
}
MCTextFilter::MCTextFilter
()
: m_Next(nil), m_Prev(nil)
{
;
}
bool
MCTextFilter_Encoder::PlaceBefore
(MCTextFilter*)
{
return
false
;
}
bool
MCTextFilter_Decoder::PlaceAfter
(MCTextFilter*)
{
return
false
;
}
codepoint_t
MCTextFilter_DecodeUTF16::GetNextCodepoint
()
{
if
(!m_Reverse)
{
//
Read the first codeunit and check whether it is a leading surrogate half
unichar_t
t_lead = (m_ReadIndex < m_DataLength) ? m_Data[m_ReadIndex] : -
1
;
if
(t_lead <
0xD800
|| t_lead >=
0xDC00
)
return
t_lead;
//
Read another codeunit to check whether it is an trailing surrogate half
unichar_t
t_trail = (m_ReadIndex +
1
< m_DataLength) ? m_Data[m_ReadIndex+
1
] : -
1
;
if
(t_trail <
0xDC00
|| t_trail >=
0xE000
)
return
t_lead;
//
Valid surrogate pair
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(t_lead, t_trail);
}
//
Read the first codeunit from end and check whether it is a trailing surrogate half
unichar_t
t_trail = (m_ReadIndex < m_DataLength) ? m_Data[m_DataLength - m_ReadIndex -
1
] : -
1
;
if
(t_trail <
0xDC00
|| t_trail >=
0xE000
)
return
t_trail;
//
Read another codeunit to check whether it is an trailing surrogate half
unichar_t
t_lead = (m_ReadIndex +
1
< m_DataLength) ? m_Data[m_DataLength - m_ReadIndex -
2
] : -
1
;
if
(t_lead <
0xD800
|| t_lead >=
0xDC00
)
return
t_trail;
//
Valid surrogate pair
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(t_lead, t_trail);
}
bool
MCTextFilter_DecodeUTF16::AdvanceCursor
()
{
if
(m_surrogate)
m_ReadIndex +=
2
;
else
m_ReadIndex +=
1
;
m_surrogate =
false
;
return
m_ReadIndex < m_DataLength;
}
bool
MCTextFilter_DecodeUTF16::HasData
()
const
{
return
m_ReadIndex < m_DataLength;
}
void
MCTextFilter_DecodeUTF16::MarkText
()
{
m_AcceptedIndex = m_ReadIndex;
}
uindex_t
MCTextFilter_DecodeUTF16::GetMarkedLength
()
const
{
return
m_AcceptedIndex +
1
;
}
MCTextFilter_DecodeUTF16::MCTextFilter_DecodeUTF16
(
const
unichar_t
*p_text,
uindex_t
p_length,
bool
p_from_end)
: m_surrogate(
false
), m_AcceptedIndex(-
1
), m_ReadIndex(
0
), m_Data(p_text), m_DataLength(p_length), m_Reverse(p_from_end)
{
;
}
MCTextFilter_DecodeUTF16::~MCTextFilter_DecodeUTF16
()
{
;
}
codepoint_t
MCTextFilter_DecodeNative::GetNextCodepoint
()
{
//
Don't read beyond the end of the input if no data remains
if
(m_ReadIndex >= m_DataLength)
return
0
;
if
(m_Reverse)
return
MCUnicodeMapFromNative
(m_Data[m_DataLength - m_ReadIndex -
1
]);
return
MCUnicodeMapFromNative
(m_Data[m_ReadIndex]);
}
bool
MCTextFilter_DecodeNative::AdvanceCursor
()
{
return
++m_ReadIndex < m_DataLength;
}
bool
MCTextFilter_DecodeNative::HasData
()
const
{
return
m_ReadIndex < m_DataLength;
}
void
MCTextFilter_DecodeNative::MarkText
()
{
m_AcceptedIndex = m_ReadIndex;
}
uindex_t
MCTextFilter_DecodeNative::GetMarkedLength
()
const
{
return
m_AcceptedIndex +
1
;
}
MCTextFilter_DecodeNative::MCTextFilter_DecodeNative
(
const
char_t
*p_text,
uindex_t
p_length,
bool
p_from_end)
: m_Data(p_text), m_DataLength(p_length), m_AcceptedIndex(-
1
), m_ReadIndex(
0
), m_Reverse(p_from_end)
{
;
}
MCTextFilter_DecodeNative::~MCTextFilter_DecodeNative
()
{
;
}
codepoint_t
MCTextFilter_SimpleCaseFold::GetNextCodepoint
()
{
//
Get a codepoint from the preceding filter
codepoint_t
t_raw;
t_raw =
PrevFilter
()->
GetNextCodepoint
();
//
Case fold the codepoint
codepoint_t
t_folded;
t_folded =
MCUnicodeGetCharacterProperty
(t_raw,
kMCUnicodePropertySimpleCaseFolding
);
return
t_folded;
}
bool
MCTextFilter_SimpleCaseFold::AdvanceCursor
()
{
return
PrevFilter
()->
AdvanceCursor
();
}
bool
MCTextFilter_SimpleCaseFold::HasData
()
const
{
return
PrevFilter
()->
HasData
();
}
MCTextFilter_SimpleCaseFold::MCTextFilter_SimpleCaseFold
()
{
;
}
MCTextFilter_SimpleCaseFold::~MCTextFilter_SimpleCaseFold
()
{
;
}
codepoint_t
MCTextFilter_NormalizeNFC::GetNextCodepoint
()
{
if
(m_Reverse)
return
GetNextCodepointReverse
();
//
If possible, return a codepoint from the cached state
if
(m_ReadIndex < m_StateLength)
{
//
AL-2014-10-23: [[ Bug 13762 ]] Update the mark point when fetching codepoints from the state
m_MarkPoint++;
//
Check whether we have a surrogate pair
//
We are sure to have the following trail surrogate if we find a lead surrogate
if
(m_State[m_ReadIndex] >
0xD800
&& m_State[m_ReadIndex] <
0xDBFF
)
{
m_MarkPoint++;
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(m_State[m_ReadIndex], m_State[m_ReadIndex +
1
]);
}
return
m_State[m_ReadIndex];
}
PrevFilter
()->
MarkText
();
m_MarkPoint =
PrevFilter
()->
GetMarkedLength
();
//
Otherwise, the state needs to be refreshed. Loop until we get to a
//
normalisation boundary (i.e a base character)
codepoint_t
t_cp;
m_StateLength =
0
;
while
(
PrevFilter
()->
HasData
())
{
t_cp =
PrevFilter
()->
GetNextCodepoint
();
//
The first character is always added to the state
if
(m_StateLength ==
0
)
{
//
Check whether the codepoint we got is a surrogate pair
if
(
MCUnicodeCodepointToSurrogates
(t_cp, m_State[m_StateLength],
m_State[m_StateLength +
1
]))
m_StateLength++;
m_StateLength++;
PrevFilter
()->
AdvanceCursor
();
}
//
Non-first grapheme base characters terminate the run
else
if
(
MCUnicodeGetBinaryProperty
(t_cp,
kMCUnicodePropertyGraphemeBase
) ||
MCUnicodeGetBinaryProperty
(t_cp,
kMCUnicodePropertyWhiteSpace
))
{
break
;
}
//
All other characters are appended to the state
else
{
//
Check whether the codepoint we got is a surrogate pair
if
(
MCUnicodeCodepointToSurrogates
(t_cp, m_State[m_StateLength],
m_State[m_StateLength+
1
]))
m_StateLength++;
m_StateLength++;
PrevFilter
()->
AdvanceCursor
();
}
//
Abort if our arbitrary limit has been reached
if
(m_StateLength ==
kMCTextFilterMaxNormLength
)
break
;
}
//
Normalise the state
unichar_t
*t_norm;
uindex_t
t_norm_length;
MCUnicodeNormaliseNFC
(m_State, m_StateLength, t_norm, t_norm_length);
//
Copy the normalised state to the internal state (note: we assume that
//
composing will never create a longer string)
MCMemoryCopy
(m_State, t_norm, t_norm_length *
sizeof
(
unichar_t
));
m_StateLength = t_norm_length;
m_ReadIndex =
0
;
free
(t_norm);
//
All done
if
(m_State[
0
] >
0xD800
&& m_State[
0
] <
0xDBFF
)
{
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(m_State[
0
], m_State[
1
]);
}
else
return
m_State[
0
];
}
codepoint_t
MCTextFilter_NormalizeNFC::GetNextCodepointReverse
()
{
//
If possible, return a codepoint from the cached state
if
(m_ReadIndex < m_StateLength)
{
//
AL-2014-10-23: [[ Bug 13762 ]] Update the mark point when fetching codepoints from the state
m_MarkPoint++;
//
We are sure to have the preceding lead surrogate if we find a trail surrogate
if
(m_State[
kMCTextFilterMaxNormLength
- m_ReadIndex -
1
] >=
0xDC00
&& m_State[
kMCTextFilterMaxNormLength
- m_ReadIndex -
1
] <
0xE000
)
{
m_MarkPoint++;
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(m_State[
kMCTextFilterMaxNormLength
- m_ReadIndex -
2
], m_State[
kMCTextFilterMaxNormLength
- m_ReadIndex -
1
]);
}
return
m_State[
kMCTextFilterMaxNormLength
- m_ReadIndex -
1
];
}
PrevFilter
()->
MarkText
();
m_MarkPoint =
PrevFilter
()->
GetMarkedLength
();
//
Otherwise, the state needs to be refreshed. Loop until we get to a
//
normalisation boundary (i.e a base character)
codepoint_t
t_cp;
m_StateLength =
0
;
while
(
PrevFilter
()->
HasData
())
{
t_cp =
PrevFilter
()->
GetNextCodepoint
();
//
The first character is always added to the state
if
(m_StateLength ==
0
)
{
//
Check whether the codepoint we got is a surrogate pair
if
(
MCUnicodeCodepointToSurrogates
(t_cp,
m_State[
kMCTextFilterMaxNormLength
-
2
],
m_State[
kMCTextFilterMaxNormLength
-
1
]))
m_StateLength++;
else
m_State[
kMCTextFilterMaxNormLength
-
1
] = m_State[
kMCTextFilterMaxNormLength
-
2
];
m_StateLength++;
PrevFilter
()->
AdvanceCursor
();
}
//
All other characters are appended to the state
else
{
//
Check whether the codepoint we got is a surrogate pair
if
(
MCUnicodeCodepointToSurrogates
(t_cp,
m_State[
kMCTextFilterMaxNormLength
- m_StateLength -
2
],
m_State[
kMCTextFilterMaxNormLength
- m_StateLength -
1
]))
m_StateLength++;
else
m_State[
kMCTextFilterMaxNormLength
- m_StateLength -
1
] = m_State[
kMCTextFilterMaxNormLength
- m_StateLength -
2
];
m_StateLength++;
PrevFilter
()->
AdvanceCursor
();
}
//
The backwards run is terminated after a grapheme base.
if
(
MCUnicodeGetBinaryProperty
(t_cp,
kMCUnicodePropertyGraphemeBase
) ||
MCUnicodeGetBinaryProperty
(t_cp,
kMCUnicodePropertyWhiteSpace
))
{
break
;
}
//
Abort if our arbitrary limit has been reached
if
(m_StateLength ==
kMCTextFilterMaxNormLength
)
break
;
}
//
Normalise the state
unichar_t
*t_norm;
uindex_t
t_norm_length;
MCUnicodeNormaliseNFC
(m_State +
kMCTextFilterMaxNormLength
- m_StateLength, m_StateLength, t_norm, t_norm_length);
//
Copy the normalised state to the internal state (note: we assume that
//
composing will never create a longer string)
uindex_t
i =
0
;
while
(t_norm_length)
m_State[
kMCTextFilterMaxNormLength
- t_norm_length--] = t_norm[i++];
m_StateLength = i;
m_ReadIndex =
0
;
free
(t_norm);
//
All done
if
(m_StateLength >
1
&& m_State[
kMCTextFilterMaxNormLength
-
2
] >
0xD800
&& m_State[
kMCTextFilterMaxNormLength
-
2
] <
0xDBFF
)
{
m_surrogate =
true
;
return
MCUnicodeSurrogatesToCodepoint
(m_State[
kMCTextFilterMaxNormLength
-
2
], m_State[
kMCTextFilterMaxNormLength
-
1
]);
}
else
return
m_State[
kMCTextFilterMaxNormLength
-
1
];
}
bool
MCTextFilter_NormalizeNFC::AdvanceCursor
()
{
m_ReadIndex++;
if
(m_surrogate)
{
m_ReadIndex++;
m_surrogate =
false
;
}
return
HasData
();
}
bool
MCTextFilter_NormalizeNFC::HasData
()
const
{
return
m_ReadIndex < m_StateLength ||
PrevFilter
()->
HasData
();
}
void
MCTextFilter_NormalizeNFC::MarkText
()
{
//
Only mark on run boundaries
m_MarkedLength = m_MarkPoint;
}
uindex_t
MCTextFilter_NormalizeNFC::GetMarkedLength
()
const
{
return
m_MarkedLength;
}
MCTextFilter_NormalizeNFC::MCTextFilter_NormalizeNFC
(
bool
p_from_end)
: m_StateLength(
0
), m_ReadIndex(
0
), m_MarkedLength(
0
), m_MarkPoint(
0
), m_surrogate(
false
), m_Reverse(p_from_end)
{
;
}
MCTextFilter_NormalizeNFC::~MCTextFilter_NormalizeNFC
()
{
;
}
MCTextFilter*
MCTextFilterCreate
(MCStringRef p_string, MCStringOptions p_options)
{
if
(
MCStringIsNative
(p_string))
return
MCTextFilterCreate
(
MCStringGetNativeCharPtr
(p_string),
MCStringGetLength
(p_string),
kMCStringEncodingNative
, p_options,
false
);
return
MCTextFilterCreate
(
MCStringGetCharPtr
(p_string),
MCStringGetLength
(p_string),
kMCStringEncodingUTF16
, p_options,
false
);
}
MCTextFilter*
MCTextFilterCreate
(MCDataRef p_data, MCStringEncoding p_encoding, MCStringOptions p_options)
{
return
MCTextFilterCreate
(
MCDataGetBytePtr
(p_data),
MCDataGetLength
(p_data), p_encoding, p_options,
false
);
}
MCTextFilter*
MCTextFilterCreate
(
const
void
*p_data,
uindex_t
p_length, MCStringEncoding p_encoding, MCStringOptions p_options,
bool
p_from_end)
{
MCTextFilter *t_chain = nil;
//
Choose the decoder based on the encoding
if
(p_encoding ==
kMCStringEncodingUTF16
)
t_chain =
new
(nothrow)
MCTextFilter_DecodeUTF16
(
reinterpret_cast
<
const
unichar_t
*>(p_data), p_length, p_from_end);
else
t_chain =
new
(nothrow)
MCTextFilter_DecodeNative
(
reinterpret_cast
<
const
char_t
*>(p_data), p_length, p_from_end);
//
Add filters based on the options given
if
(p_options ==
kMCStringOptionCompareCaseless
|| p_options ==
kMCStringOptionCompareFolded
)
{
MCTextFilter *t_filter;
t_filter =
new
(nothrow)
MCTextFilter_SimpleCaseFold
();
t_chain->
PlaceBefore
(t_filter);
t_chain = t_filter;
}
if
(p_encoding ==
kMCStringEncodingUTF16
&& (p_options ==
kMCStringOptionCompareCaseless
|| p_options ==
kMCStringOptionCompareNonliteral
))
{
MCTextFilter *t_filter;
t_filter =
new
(nothrow)
MCTextFilter_NormalizeNFC
(p_from_end);
t_chain->
PlaceBefore
(t_filter);
t_chain = t_filter;
}
return
t_chain;
}
Back
|
FazBrowse Home
|
New Git URL