-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathllstring.cpp
More file actions
2057 lines (1832 loc) · 60.4 KB
/
llstring.cpp
File metadata and controls
2057 lines (1832 loc) · 60.4 KB
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file llstring.cpp
* @brief String utility functions and the std::string class.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* Alchemy Viewer Source Code
* Copyright (C) 2026, Rye <rye@alchemyviewer.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llstring.h"
#include "llerror.h"
#include "llfasttimer.h"
#include "llsd.h"
#include <vector>
#include <simdutf.h>
#if LL_WINDOWS
#include "llwin32headers.h"
#endif
namespace
{
// Tolerantly convert src (SrcCh* of length len) using simdutf's fast path on
// valid runs, substituting `replacement` for each rejected code unit. Mirrors
// the "best-effort with LL_UNKNOWN_CHAR" semantics of the previous hand-rolled
// decoders while letting valid segments ride the SIMD path.
template<typename SrcCh, typename DstCh>
std::basic_string<DstCh> utf_convert_with_replacement(
const SrcCh* src, size_t len,
simdutf::result (*validate)(const SrcCh*, size_t),
size_t (*out_len_from)(const SrcCh*, size_t),
size_t (*convert_valid)(const SrcCh*, size_t, DstCh*),
DstCh replacement)
{
std::basic_string<DstCh> out;
if (len == 0 || !src) return out;
while (len > 0)
{
const auto r = validate(src, len);
const size_t valid = (r.error == simdutf::error_code::SUCCESS) ? len : r.count;
if (valid > 0)
{
const size_t pos = out.size();
out.resize(pos + out_len_from(src, valid));
convert_valid(src, valid, out.data() + pos);
src += valid;
len -= valid;
}
if (r.error != simdutf::error_code::SUCCESS)
{
out.push_back(replacement);
++src;
--len;
}
}
return out;
}
} // anonymous namespace
std::string ll_safe_string(const char* in)
{
if(in) return std::string(in);
return std::string();
}
std::string ll_safe_string(const char* in, S32 maxlen)
{
if(in && maxlen > 0 ) return std::string(in, maxlen);
return std::string();
}
bool is_char_hex(char hex)
{
if((hex >= '0') && (hex <= '9'))
{
return true;
}
else if((hex >= 'a') && (hex <='f'))
{
return true;
}
else if((hex >= 'A') && (hex <='F'))
{
return true;
}
return false; // uh - oh, not hex any more...
}
U8 hex_as_nybble(char hex)
{
if((hex >= '0') && (hex <= '9'))
{
return (U8)(hex - '0');
}
else if((hex >= 'a') && (hex <='f'))
{
return (U8)(10 + hex - 'a');
}
else if((hex >= 'A') && (hex <='F'))
{
return (U8)(10 + hex - 'A');
}
return 0; // uh - oh, not hex any more...
}
bool iswindividual(llwchar elem)
{
U32 cur_char = (U32)elem;
bool result = false;
if (0x2E80<= cur_char && cur_char <= 0x9FFF)
{
result = true;
}
else if (0xAC00<= cur_char && cur_char <= 0xD7A0 )
{
result = true;
}
else if (0xF900<= cur_char && cur_char <= 0xFA60 )
{
result = true;
}
return result;
}
bool _read_file_into_string(std::string& str, const std::string& filename)
{
llifstream ifs(filename.c_str(), llifstream::binary);
if (!ifs.is_open())
{
LL_INFOS() << "Unable to open file " << filename << LL_ENDL;
return false;
}
std::ostringstream oss;
oss << ifs.rdbuf();
str = oss.str();
ifs.close();
return true;
}
// See http://www.unicode.org/Public/BETA/CVTUTF-1-2/ConvertUTF.c
// for the Unicode implementation - this doesn't match because it was written before finding
// it.
std::ostream& operator<<(std::ostream &s, const LLWString &wstr)
{
std::string utf8_str = wstring_to_utf8str(wstr);
s << utf8_str;
return s;
}
std::string rawstr_to_utf8(const std::string& raw)
{
LLWString wstr(utf8str_to_wstring(raw));
return wstring_to_utf8str(wstr);
}
std::ptrdiff_t wchar_to_utf8chars(llwchar in_char, char* outchars)
{
U32 cur_char = (U32)in_char;
char* base = outchars;
if (cur_char < 0x80)
{
*outchars++ = (U8)cur_char;
}
else if (cur_char < 0x800)
{
*outchars++ = 0xC0 | (cur_char >> 6);
*outchars++ = 0x80 | (cur_char & 0x3F);
}
else if (cur_char < 0x10000)
{
*outchars++ = 0xE0 | (cur_char >> 12);
*outchars++ = 0x80 | ((cur_char >> 6) & 0x3F);
*outchars++ = 0x80 | (cur_char & 0x3F);
}
else if (cur_char < 0x200000)
{
*outchars++ = 0xF0 | (cur_char >> 18);
*outchars++ = 0x80 | ((cur_char >> 12) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 6) & 0x3F);
*outchars++ = 0x80 | (cur_char & 0x3F);
}
else if (cur_char < 0x4000000)
{
*outchars++ = 0xF8 | (cur_char >> 24);
*outchars++ = 0x80 | ((cur_char >> 18) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 12) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 6) & 0x3F);
*outchars++ = 0x80 | (cur_char & 0x3F);
}
else if (cur_char < 0x80000000)
{
*outchars++ = 0xFC | (cur_char >> 30);
*outchars++ = 0x80 | ((cur_char >> 24) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 18) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 12) & 0x3F);
*outchars++ = 0x80 | ((cur_char >> 6) & 0x3F);
*outchars++ = 0x80 | (cur_char & 0x3F);
}
else
{
LL_WARNS() << "Invalid Unicode character " << cur_char << "!" << LL_ENDL;
*outchars++ = LL_UNKNOWN_CHAR;
}
return outchars - base;
}
llutf16string wstring_to_utf16str(const llwchar* utf32str, size_t len)
{
return utf_convert_with_replacement<char32_t, char16_t>(
utf32str, len,
&simdutf::validate_utf32_with_errors,
&simdutf::utf16_length_from_utf32,
&simdutf::convert_valid_utf32_to_utf16le,
static_cast<char16_t>(LL_UNKNOWN_CHAR));
}
LLWString utf16str_to_wstring(const char16_t* utf16str, size_t len)
{
return utf_convert_with_replacement<char16_t, char32_t>(
utf16str, len,
&simdutf::validate_utf16le_with_errors,
&simdutf::utf32_length_from_utf16le,
&simdutf::convert_valid_utf16le_to_utf32,
static_cast<char32_t>(LL_UNKNOWN_CHAR));
}
// Length in utf16string (UTF-16) of wlen wchars beginning at woffset.
S32 wstring_utf16_length(const LLWString &wstr, const S32 woffset, const S32 wlen)
{
const S32 end = llmin((S32)wstr.length(), woffset + wlen);
if (end <= woffset) return 0;
return (S32)simdutf::utf16_length_from_utf32(wstr.data() + woffset, end - woffset);
}
// Given a wstring and an offset in it, returns the length as wstring (i.e.,
// number of llwchars) of the longest substring that starts at the offset
// and whose equivalent utf-16 string does not exceeds the given utf16_length.
S32 wstring_wstring_length_from_utf16_length(const LLWString & wstr, const S32 woffset, const S32 utf16_length, bool *unaligned)
{
const auto end = wstr.length();
bool u{ false };
S32 n = woffset + utf16_length;
S32 i = woffset;
while (i < end)
{
if (wstr[i] >= 0x10000)
{
--n;
}
if (i >= n)
{
u = (i > n);
break;
}
i++;
}
if (unaligned)
{
*unaligned = u;
}
return i - woffset;
}
S32 wchar_utf8_length(const llwchar wc)
{
if (wc < 0x80)
{
return 1;
}
else if (wc < 0x800)
{
return 2;
}
else if (wc < 0x10000)
{
return 3;
}
else if (wc < 0x200000)
{
return 4;
}
else if (wc < 0x4000000)
{
return 5;
}
else
{
return 6;
}
}
std::string wchar_utf8_preview(const llwchar wc)
{
std::ostringstream oss;
oss << std::hex << std::uppercase << (U32)wc;
U8 out_bytes[8];
U32 size = (U32)wchar_to_utf8chars(wc, (char*)out_bytes);
if (size > 1)
{
oss << " [";
for (U32 i = 0; i < size; ++i)
{
if (i)
{
oss << ", ";
}
oss << (int)out_bytes[i];
}
oss << "]";
}
return oss.str();
}
S32 wstring_utf8_length(const LLWString& wstr)
{
return (S32)simdutf::utf8_length_from_utf32(wstr.data(), wstr.size());
}
LLWString utf8str_to_wstring(const char* utf8str, size_t len)
{
return utf_convert_with_replacement<char, char32_t>(
utf8str, len,
&simdutf::validate_utf8_with_errors,
&simdutf::utf32_length_from_utf8,
&simdutf::convert_valid_utf8_to_utf32,
static_cast<char32_t>(LL_UNKNOWN_CHAR));
}
std::string wstring_to_utf8str(const llwchar* utf32str, size_t len)
{
return utf_convert_with_replacement<char32_t, char>(
utf32str, len,
&simdutf::validate_utf32_with_errors,
&simdutf::utf8_length_from_utf32,
&simdutf::convert_valid_utf32_to_utf8,
static_cast<char>(LL_UNKNOWN_CHAR));
}
std::string utf16str_to_utf8str(const char16_t* utf16str, size_t len)
{
return utf_convert_with_replacement<char16_t, char>(
utf16str, len,
&simdutf::validate_utf16le_with_errors,
&simdutf::utf8_length_from_utf16le,
&simdutf::convert_valid_utf16le_to_utf8,
static_cast<char>(LL_UNKNOWN_CHAR));
}
std::u8string str_to_u8str(const char* str, size_t len)
{
if (!str || len == 0) return {};
// We treat std::string as utf8 in this codebase so pass through
std::string_view str_view(str, len);
return std::u8string(str_view.begin(), str_view.end());
}
std::string u8str_to_str(const char8_t* u8str, size_t len)
{
if (!u8str || len == 0) return {};
// We treat std::string as utf8 in this codebase so pass through
std::u8string_view u8str_view(u8str, len);
return std::string(u8str_view.begin(), u8str_view.end());
}
std::string utf8str_trim(const std::string& utf8str)
{
LLWString wstr = utf8str_to_wstring(utf8str);
LLWStringUtil::trim(wstr);
return wstring_to_utf8str(wstr);
}
std::string utf8str_tolower(const std::string& utf8str)
{
LLWString out_str = utf8str_to_wstring(utf8str);
LLWStringUtil::toLower(out_str);
return wstring_to_utf8str(out_str);
}
S32 utf8str_compare_insensitive(const std::string& lhs, const std::string& rhs)
{
LLWString wlhs = utf8str_to_wstring(lhs);
LLWString wrhs = utf8str_to_wstring(rhs);
return LLWStringUtil::compareInsensitive(wlhs, wrhs);
}
std::string utf8str_truncate(const std::string& utf8str, const S32 max_len)
{
if (0 == max_len) return std::string();
if ((S32)utf8str.length() <= max_len) return utf8str;
return utf8str.substr(0,
simdutf::trim_partial_utf8(utf8str.data(), (size_t)max_len));
}
// [RLVa:KB] - Checked: RLVa-2.1.0
std::string utf8str_substr(const std::string& utf8str, const S32 index, const S32 max_len)
{
if (0 == max_len) return std::string();
if (utf8str.length() - index <= (size_t)max_len)
{
return utf8str.substr(index, max_len);
}
return utf8str.substr(index,
simdutf::trim_partial_utf8(utf8str.data() + index, (size_t)max_len));
}
void utf8str_split(std::list<std::string>& split_list, const std::string& utf8str, size_t maxlen, char split_token)
{
split_list.clear();
std::string::size_type lenMsg = utf8str.length(), lenIt = 0;
const char* pstrIt = utf8str.c_str(); std::string strTemp;
while (lenIt < lenMsg)
{
if (lenIt + maxlen < lenMsg)
{
// Find the last split character
const char* pstrTemp = pstrIt + maxlen;
while ( (pstrTemp > pstrIt) && (*pstrTemp != split_token) )
pstrTemp--;
if (pstrTemp > pstrIt)
strTemp = utf8str.substr(lenIt, pstrTemp - pstrIt);
else
strTemp = utf8str_substr(utf8str, narrow(lenIt), narrow(maxlen));
}
else
{
strTemp = utf8str.substr(lenIt, std::string::npos);
}
split_list.push_back(strTemp);
lenIt += strTemp.length();
pstrIt = utf8str.c_str() + lenIt;
if (*pstrIt == split_token)
lenIt++;
}
}
// [/RLVa:KB]
std::string utf8str_symbol_truncate(const std::string& utf8str, const S32 symbol_len)
{
if (0 == symbol_len)
{
return std::string();
}
if ((S32)utf8str.length() <= symbol_len)
{
return utf8str;
}
int symbols = 0;
size_t byteIndex = 0;
const size_t origSize = utf8str.size();
while (byteIndex < origSize)
{
if ((utf8str[byteIndex] & 0xc0) != 0x80)
{
if (symbols == symbol_len)
break;
++symbols;
}
++byteIndex;
}
return utf8str.substr(0, byteIndex);
}
std::string utf8str_substChar(
const std::string& utf8str,
const llwchar target_char,
const llwchar replace_char)
{
LLWString wstr = utf8str_to_wstring(utf8str);
LLWStringUtil::replaceChar(wstr, target_char, replace_char);
//wstr = wstring_substChar(wstr, target_char, replace_char);
return wstring_to_utf8str(wstr);
}
std::string utf8str_makeASCII(const std::string& utf8str)
{
LLWString wstr = utf8str_to_wstring(utf8str);
LLWStringUtil::_makeASCII(wstr);
return wstring_to_utf8str(wstr);
}
std::string mbcsstring_makeASCII(const std::string& wstr)
{
// Replace non-ASCII chars with replace_char
std::string out_str = wstr;
for (S32 i = 0; i < (S32)out_str.length(); i++)
{
if ((U8)out_str[i] > 0x7f)
{
out_str[i] = LL_UNKNOWN_CHAR;
}
}
return out_str;
}
std::string utf8str_removeCRLF(const std::string& utf8str)
{
if (0 == utf8str.length())
{
return std::string();
}
const char CR = 13;
std::string out;
out.reserve(utf8str.length());
const S32 len = (S32)utf8str.length();
for( S32 i = 0; i < len; i++ )
{
if( utf8str[i] != CR )
{
out.push_back(utf8str[i]);
}
}
return out;
}
// Only used by utf8str_showBytesUTF8 below. Kept file-local after the simdutf
// migration (no external callers).
static llwchar utf8str_to_wchar(const std::string& utf8str, size_t offset, size_t length)
{
switch (length)
{
case 2:
return ((utf8str[offset] & 0x1F) << 6) +
(utf8str[offset + 1] & 0x3F);
case 3:
return ((utf8str[offset] & 0x0F) << 12) +
((utf8str[offset + 1] & 0x3F) << 6) +
(utf8str[offset + 2] & 0x3F);
case 4:
return ((utf8str[offset] & 0x07) << 18) +
((utf8str[offset + 1] & 0x3F) << 12) +
((utf8str[offset + 2] & 0x3F) << 6) +
(utf8str[offset + 3] & 0x3F);
case 5:
return ((utf8str[offset] & 0x03) << 24) +
((utf8str[offset + 1] & 0x3F) << 18) +
((utf8str[offset + 2] & 0x3F) << 12) +
((utf8str[offset + 3] & 0x3F) << 6) +
(utf8str[offset + 4] & 0x3F);
case 6:
return ((utf8str[offset] & 0x01) << 30) +
((utf8str[offset + 1] & 0x3F) << 24) +
((utf8str[offset + 2] & 0x3F) << 18) +
((utf8str[offset + 3] & 0x3F) << 12) +
((utf8str[offset + 4] & 0x3F) << 6) +
(utf8str[offset + 5] & 0x3F);
case 7:
return ((utf8str[offset + 1] & 0x03) << 30) +
((utf8str[offset + 2] & 0x3F) << 24) +
((utf8str[offset + 3] & 0x3F) << 18) +
((utf8str[offset + 4] & 0x3F) << 12) +
((utf8str[offset + 5] & 0x3F) << 6) +
(utf8str[offset + 6] & 0x3F);
}
return LL_UNKNOWN_CHAR;
}
std::string utf8str_showBytesUTF8(const std::string& utf8str)
{
std::string result;
bool in_sequence = false;
size_t sequence_size = 0;
size_t byte_index = 0;
size_t source_length = utf8str.size();
auto open_sequence = [&]()
{
if (!result.empty() && result.back() != '\n')
result += '\n'; // Use LF as a separator before new UTF-8 sequence
result += '[';
in_sequence = true;
};
auto close_sequence = [&]()
{
llwchar unicode = utf8str_to_wchar(utf8str, byte_index - sequence_size, sequence_size);
if (unicode != LL_UNKNOWN_CHAR)
{
result += llformat("+%04X", unicode);
}
result += ']';
in_sequence = false;
sequence_size = 0;
};
while (byte_index < source_length)
{
U8 byte = utf8str[byte_index];
if (byte >= 0x80) // Part of an UTF-8 sequence
{
if (!in_sequence) // Start new UTF-8 sequence
{
open_sequence();
}
else if (byte >= 0xC0) // Start another UTF-8 sequence
{
close_sequence();
open_sequence();
}
else // Continue the same UTF-8 sequence
{
result += '.';
}
result += llformat("%02X", byte); // The byte is represented in hexadecimal form
++sequence_size;
}
else // ASCII symbol is represented as a character
{
if (in_sequence) // End of UTF-8 sequence
{
close_sequence();
if (byte != '\n')
{
result += '\n'; // Use LF as a separator between UTF-8 and ASCII
}
}
result += byte;
}
++byte_index;
}
if (in_sequence) // End of UTF-8 sequence
{
close_sequence();
}
return result;
}
// Search for any emoji symbol, return true if found
bool wstring_has_emoji(LLWStringView wstr)
{
for (const llwchar& wch : wstr)
{
if (LLStringOps::isEmoji(wch))
return true;
}
return false;
}
// Strip every emoji-cluster the cluster walker identifies, plus any isolated
// astral-emoji codepoint (LLStringOps::isEmoji-true) that the walker excludes
// because it shapes correctly via the 1:1 path. Sharing the cluster walker
// here means this function and wstring_find_emoji_clusters can never disagree
// on cluster bounds — historical ad-hoc state machines diverged on tag chars,
// VS-15, BMP-base ZWJ sequences, and keycaps and produced visibly broken
// output (orphan ZWJ, leftover tag bytes) in those cases.
//
// "Cluster" here means anything HarfBuzz would itemize as a single emoji
// glyph — keycaps, BMP-base sequences (heart-on-fire, isolated heart+VS16),
// subdivision flags. Earlier the function used the narrower
// LLStringOps::isEmoji predicate which let composed glyphs survive partial
// stripping; the broader contract matches the visual intent of "remove
// emojis" for input fields like usernames and search.
bool wstring_remove_emojis(LLWString& wstr)
{
const auto clusters = wstring_find_emoji_clusters(wstr);
bool found = false;
size_t read = 0, write = 0;
auto cluster_it = clusters.begin();
while (read < wstr.size())
{
if (cluster_it != clusters.end() && read == cluster_it->first)
{
read = cluster_it->second;
++cluster_it;
found = true;
continue;
}
if (LLStringOps::isEmoji(wstr[read]))
{
++read;
found = true;
continue;
}
wstr[write++] = wstr[read++];
}
if (found)
wstr.resize(write);
return found;
}
// Cut emoji symbols if exist
bool utf8str_remove_emojis(std::string& utf8str)
{
LLWString wstr = utf8str_to_wstring(utf8str);
if (!wstring_remove_emojis(wstr))
return false;
utf8str = wstring_to_utf8str(wstr);
return true;
}
// Codepoints that can act as a ZWJ/VS emoji-sequence base. Broader than
// LLStringOps::isEmoji (which is restricted to the "genuine" astral emoji
// range so font fallback only routes genuine emoji to the colour face) —
// BMP pictographs like ❤ (U+2764), ©, ®, and the various symbol blocks in
// U+2000..U+32FF are eligible sequence bases per UAX #51, and HarfBuzz
// compositions like ❤️🔥 (U+2764 U+FE0F U+200D U+1F525) require they be
// detected here even though they are not "genuine" emoji.
// (Defined as LLStringOps::isPictographBase so the same predicate is
// available to llrender's shape-itemizer and font-fallback walkers without
// duplicating the range list.)
// True if position i begins an emoji sequence that the 1:1 codepoint->glyph
// path cannot render correctly — i.e., the next codepoint transforms the base
// (ZWJ, VS15/16, skin-tone, keycap combiner, tag character, or regional
// indicator pair), or we're sitting on a keycap starter (digit/#/* + FE0F +
// 20E3). Isolated emoji are excluded: they render fine through FreeType alone.
static bool is_shaping_starter(const llwchar* p, size_t n, size_t i)
{
const llwchar c = p[i];
// Keycap sequence: digit/#/* + VS16 + COMBINING ENCLOSING KEYCAP.
// shapeRun itemises these into per-face sub-runs (digit on the text
// font, combining mark on the emoji font) so we can treat keycap as
// one cluster for cursor/grapheme purposes without losing the mark's
// natural overlay on the base.
if ((c == '#' || c == '*' || (c >= '0' && c <= '9'))
&& i + 2 < n && p[i + 1] == 0xFE0F && p[i + 2] == 0x20E3)
{
return true;
}
if (!LLStringOps::isPictographBase(c) || i + 1 >= n)
return false;
const llwchar next = p[i + 1];
if (next == 0x200D || LLStringOps::isEmojiClusterExtender(next))
return true;
// Regional indicator pair (flag).
return c >= 0x1F1E6 && c <= 0x1F1FF
&& next >= 0x1F1E6 && next <= 0x1F1FF;
}
// Greedy forward walk from a confirmed shaping-starter position, returning the
// one-past-end index of the sequence.
static size_t advance_shaping_run(const llwchar* p, size_t n, size_t start)
{
const llwchar base = p[start];
size_t r = start + 1;
// Keycap: always exactly 3 codepoints.
if ((base == '#' || base == '*' || (base >= '0' && base <= '9'))
&& r + 1 < n && p[r] == 0xFE0F && p[r + 1] == 0x20E3)
{
return r + 2;
}
// Regional indicator pair: exactly one trailing RI.
if (base >= 0x1F1E6 && base <= 0x1F1FF
&& r < n && p[r] >= 0x1F1E6 && p[r] <= 0x1F1FF)
{
return r + 1;
}
// General case: ZWJ joins another base, then any number of plain
// extenders (VS, skin-tone, keycap mark, tag chars).
while (r < n)
{
const llwchar c = p[r];
if (c == 0x200D)
{
// Accept any pictograph base after the joiner, including BMP
// pictographs like 🔥's partner heart in ❤️🔥 where the base
// sits outside the astral emoji range.
if (r + 1 < n && LLStringOps::isPictographBase(p[r + 1]))
{
r += 2;
continue;
}
break; // orphan ZWJ
}
if (LLStringOps::isEmojiClusterExtender(c))
{
++r;
continue;
}
break;
}
return r;
}
EmojiClusterList wstring_find_emoji_clusters(LLWStringView wstr)
{
EmojiClusterList runs;
const llwchar* p = wstr.data();
const size_t n = wstr.size();
size_t i = 0;
while (i < n)
{
if (is_shaping_starter(p, n, i))
{
const size_t end = advance_shaping_run(p, n, i);
// is_shaping_starter accepts a base when the *next* codepoint is
// an extender, but advance_shaping_run can still bail (e.g. a
// ZWJ at end of string with nothing after it, or a ZWJ followed
// by something that isn't a pictograph base). That leaves us
// with a degenerate length-1 "cluster" that's really just the
// base alone with a dangling extender. Skip those — isolated
// single-codepoint emoji shape correctly via the 1:1 path and
// are intentionally absent from the cluster list.
if (end > i + 1)
runs.emplace_back(i, end);
i = end;
}
else
{
++i;
}
}
return runs;
}
size_t wstring_step_grapheme_forward(LLWStringView wstr, size_t pos,
const EmojiClusterList& clusters)
{
const size_t n = wstr.size();
if (pos >= n)
return n;
const size_t next = pos + 1;
// Clusters are sorted by start, so stop scanning once we pass `next`.
for (const auto& run : clusters)
{
if (next <= run.first)
break;
if (run.first < next && next < run.second)
return run.second;
}
return next;
}
size_t wstring_step_grapheme_forward(LLWStringView wstr, size_t pos)
{
return wstring_step_grapheme_forward(wstr, pos, wstring_find_emoji_clusters(wstr));
}
size_t wstring_step_grapheme_backward(LLWStringView wstr, size_t pos,
const EmojiClusterList& clusters)
{
if (pos == 0)
return 0;
const size_t prev = pos - 1;
for (const auto& run : clusters)
{
if (prev < run.first)
break;
if (run.first < prev && prev < run.second)
return run.first;
}
return prev;
}
size_t wstring_step_grapheme_backward(LLWStringView wstr, size_t pos)
{
return wstring_step_grapheme_backward(wstr, pos, wstring_find_emoji_clusters(wstr));
}
size_t wstring_grapheme_align_backward(LLWStringView wstr, size_t pos,
const EmojiClusterList& clusters)
{
if (pos == 0 || pos >= wstr.size())
return pos;
for (const auto& run : clusters)
{
if (pos <= run.first)
break;
if (run.first < pos && pos < run.second)
return run.first;
}
return pos;
}
size_t wstring_grapheme_align_backward(LLWStringView wstr, size_t pos)
{
return wstring_grapheme_align_backward(wstr, pos, wstring_find_emoji_clusters(wstr));
}
size_t wstring_grapheme_align_forward(LLWStringView wstr, size_t pos,
const EmojiClusterList& clusters)
{
if (pos >= wstr.size())
return wstr.size();
for (const auto& run : clusters)
{
if (pos <= run.first)
break;
if (run.first < pos && pos < run.second)
return run.second;
}
return pos;
}
size_t wstring_grapheme_align_forward(LLWStringView wstr, size_t pos)
{
return wstring_grapheme_align_forward(wstr, pos, wstring_find_emoji_clusters(wstr));
}
std::pair<size_t, size_t> wstring_emoji_range_at(LLWStringView wstr, size_t pos,
const EmojiClusterList& clusters)
{
if (pos >= wstr.size())
return { pos, pos };
for (const auto& run : clusters)
{
if (pos < run.first)
break;
if (pos < run.second)
return run;
}
// Single-codepoint pictographs are intentionally absent from the cluster
// list (they shape via the 1:1 path), but tooltip lookup still wants
// their bounds. Use isPictographBase rather than isEmoji so BMP
// pictographs (©, ®, ☦, ⚓, ❤, …) get a range; isPictographBase already
// excludes extenders (ZWJ, VS-15/16, keycap combiner, skin-tone mods,
// tag chars) which have no business being a standalone tooltip target.
return LLStringOps::isPictographBase(wstr[pos])
? std::make_pair(pos, pos + 1)
: std::make_pair(pos, pos);
}
std::pair<size_t, size_t> wstring_emoji_range_at(LLWStringView wstr, size_t pos)
{
return wstring_emoji_range_at(wstr, pos, wstring_find_emoji_clusters(wstr));
}
#if LL_WINDOWS
unsigned int ll_wstring_default_code_page()
{
return CP_UTF8;
}
std::string ll_convert_wide_to_string(const wchar_t* in, size_t len_in, unsigned int code_page)
{
std::string out;
if(in)
{
int len_out = WideCharToMultiByte(
code_page,
0,
in,
static_cast<int>(len_in),
NULL,
0,
0,
0);
// We will need two more bytes for the double NULL ending
// created in WideCharToMultiByte().
char* pout = new char [len_out + 2];
memset(pout, 0, len_out + 2);
if(pout)
{
WideCharToMultiByte(
code_page,
0,