forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-string.cpp
More file actions
executable file
·5568 lines (4485 loc) · 174 KB
/
Copy pathfoundation-string.cpp
File metadata and controls
executable file
·5568 lines (4485 loc) · 174 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
/* Copyright (C) 2003-2013 Runtime Revolution 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.h>
#include <foundation-auto.h>
#include <foundation-unicode.h>
#include "foundation-private.h"
#include "foundation-bidi.h"
#ifdef __LINUX__
#include <errno.h>
#include <iconv.h>
#include <langinfo.h>
#include <locale.h>
#endif
#ifdef __WINDOWS__
#include <windows.h>
#endif
////////////////////////////////////////////////////////////////////////////////
// CAVEAT!
//
// The currently implementation of the string value is native-only. Although
// the main (Unicode) methods will work, the input will get coerced to native
// encoding.
//
// When all the work is done to change the engine to use MCValueRef exclusively,
// gaining full transparent Unicode *should* just be a case of reimplementing
// the string value!
// NOTE!
//
// As it stands the underlying (native) char buffer always has an implicit NUL
// terminator added on. This is merely an aid to integration until all the uses
// of the legacy 'NativeCString' method is eliminated from use in the engine
// (which is pre-requisite for unicodification).
////////////////////////////////////////////////////////////////////////////////
// This method ensures there is 'count' chars of empty space starting at 'at'
// in 'string'. It returns false if allocation fails.
static bool __MCStringExpandAt(MCStringRef string, uindex_t at, uindex_t count);
// This method removes 'count' chars from string starting at 'at'.
static void __MCStringShrinkAt(MCStringRef string, uindex_t at, uindex_t count);
// This method clamps the given range to the valid limits for the string.
static void __MCStringClampRange(MCStringRef string, MCRange& x_range);
// This method forces a nativization of a string even if there is already a native char ptr.
static uindex_t __MCStringNativize(MCStringRef string);
// This method ensures there is a unichar ptr.
static void __MCStringUnnativize(MCStringRef self);
// This method marks the string as changed.
static void __MCStringChanged(MCStringRef string, uindex_t simple = kMCStringFlagNoChange, uindex_t combined = kMCStringFlagNoChange, uindex_t native = kMCStringFlagNoChange);
// Creates an indirect mutable string with contents.
static bool __MCStringCreateIndirect(__MCString *contents, __MCString*& r_string);
// Returns true if the string is indirect.
static bool __MCStringIsIndirect(__MCString *self);
static bool __MCStringMakeImmutable(__MCString *self);
// Creates an immutable string from this one, changing 'self' to indirect.
static bool __MCStringMakeIndirect(__MCString *self);
// Ensures the given mutable but indirect string is direct.
static bool __MCStringResolveIndirect(__MCString *self);
// Makes direct mutable string indirect, referencing r_new_string.
static bool __MCStringCopyMutable(__MCString *self, __MCString*& r_new_string);
////////////////////////////////////////////////////////////////////////////////
// AL-2015-02-06: [[ Bug 14504 ]] Add wrappers for string flag and length checking,
// for internal use when a string is known to be direct.
static bool __MCStringIsNative(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return (self -> flags & kMCStringFlagIsNotNative) == 0;
}
static bool __MCStringIsChecked(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return (self -> flags & kMCStringFlagIsChecked) != 0;
}
static bool __MCStringIsUncombined(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return (self -> flags & kMCStringFlagIsUncombined) != 0;
}
static bool __MCStringIsSimple(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return (self -> flags & kMCStringFlagIsSimple) != 0;
}
static bool __MCStringCanBeNative(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return (self -> flags & kMCStringFlagIsNotNative) == 0 || (self -> flags & kMCStringFlagCanBeNative) != 0;
}
static bool __MCStringCantBeNative(MCStringRef self, MCStringOptions p_options)
{
return ((!__MCStringCanBeNative(self) && (p_options == kMCStringOptionCompareExact || p_options == kMCStringOptionCompareFolded))
|| (__MCStringIsChecked(self) && __MCStringIsUncombined(self)));
}
static uindex_t __MCStringGetLength(MCStringRef self)
{
MCAssert(!__MCStringIsIndirect(self));
return self -> char_count;
}
static bool __MCStringIsEmpty(MCStringRef string)
{
return string == nil || __MCStringGetLength(string) == 0;
}
////////////////////////////////////////////////////////////////////////////////
// This method creates a 'constant' MCStringRef from the given c-string. At some
// point we'll make it work 'magically' at compile/build time. For now, uniquing
// and returning that has a similar effect (if slightly slower).
MCStringRef MCSTR(const char *p_cstring)
{
MCStringRef t_string;
/* UNCHECKED */ MCStringCreateWithNativeChars((const char_t *)p_cstring, strlen(p_cstring), t_string);
MCValueRef t_unique_string;
/* UNCHECKED */ MCValueInter(t_string, t_unique_string);
MCValueRelease(t_string);
return (MCStringRef)t_unique_string;
}
////////////////////////////////////////////////////////////////////////////////
bool MCStringCreateWithCString(const char* p_cstring, MCStringRef& r_string)
{
return MCStringCreateWithNativeChars((const char_t*)p_cstring, p_cstring == nil ? 0 : strlen(p_cstring), r_string);
}
bool MCStringCreateWithCStringAndRelease(char* p_cstring, MCStringRef& r_string)
{
if (MCStringCreateWithNativeChars((const char_t *)p_cstring, p_cstring == nil ? 0 : strlen((const char*)p_cstring), r_string))
{
delete p_cstring;
return true;
}
return false;
}
const char *MCStringGetCString(MCStringRef p_string)
{
if (p_string == nil)
return nil;
MCStringNativize(p_string);
const char *t_cstring;
t_cstring = (const char *)MCStringGetNativeCharPtr(p_string);
MCAssert(t_cstring != nil);
return t_cstring;
}
bool MCStringIsEqualToCString(MCStringRef p_string, const char *p_cstring, MCStringOptions p_options)
{
return MCStringIsEqualToNativeChars(p_string, (const char_t *)p_cstring, strlen(p_cstring), p_options);
}
// Create an immutable string from the given bytes, interpreting them using
// the specified encoding.
bool MCStringCreateWithBytes(const byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
{
MCAssert(!p_is_external_rep);
switch (p_encoding)
{
case kMCStringEncodingASCII:
case kMCStringEncodingNative:
return MCStringCreateWithNativeChars(p_bytes, p_byte_count, r_string);
case kMCStringEncodingUTF16:
return MCStringCreateWithChars((unichar_t *)p_bytes, p_byte_count / 2, r_string);
// AL-2014-31-03: [[ Bug 12067 ]] Fix conversion from little endian bytes.
case kMCStringEncodingUTF16LE:
case kMCStringEncodingUTF16BE:
{
unichar_t *t_buffer;
uindex_t t_length = p_byte_count / 2;
MCMemoryAllocate(t_length * sizeof(unichar_t), t_buffer);
for (uindex_t i = 0; i < t_length; i++)
{
if (p_encoding == kMCStringEncodingUTF16BE)
t_buffer[i] = (unichar_t)MCSwapInt16BigToHost(((unichar_t *)p_bytes)[i]);
else
t_buffer[i] = (unichar_t)MCSwapInt16LittleToHost(((unichar_t *)p_bytes)[i]);
}
return MCStringCreateWithCharsAndRelease(t_buffer, t_length, r_string);
}
case kMCStringEncodingUTF8:
{
unichar_t *t_chars;
uindex_t t_char_count;
t_char_count = MCUnicodeCharsMapFromUTF8(p_bytes, p_byte_count, nil, 0);
if (!MCMemoryNewArray(t_char_count, t_chars))
return false;
MCUnicodeCharsMapFromUTF8(p_bytes, p_byte_count, t_chars, t_char_count);
if (!MCStringCreateWithCharsAndRelease(t_chars, t_char_count, r_string))
{
MCMemoryDeleteArray(t_chars);
return false;
}
return true;
}
break;
case kMCStringEncodingUTF32:
case kMCStringEncodingUTF32LE:
case kMCStringEncodingUTF32BE:
{
// Round the byte count to a multiple of UTF-32 units
p_byte_count = ((p_byte_count + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1));
// Convert the string to UTF-16 first. The UTF-16 string cannot be longer than the UTF-32 string
MCAutoArray<unichar_t> t_buffer;
t_buffer.Extend(p_byte_count / sizeof(uint32_t));
uindex_t t_in_offset;
uindex_t t_out_offset = 0;
for (t_in_offset = 0; t_in_offset < p_byte_count; t_in_offset += sizeof(uint32_t))
{
// BMP characters are output unchanged, non-BMP requires surrogate pairs
codepoint_t t_codepoint;
t_codepoint = *(uint32_t*)&p_bytes[t_in_offset];
if (p_encoding == kMCStringEncodingUTF32BE)
t_codepoint = MCSwapInt32BigToHost(t_codepoint);
else if (p_encoding == kMCStringEncodingUTF32LE)
t_codepoint = MCSwapInt32LittleToHost(t_codepoint);
if (t_codepoint < 0x10000)
{
t_buffer[t_out_offset] = unichar_t(t_codepoint);
t_out_offset += 1;
}
else
{
// Split to surrogate pairs
unichar_t t_lead, t_trail;
t_lead = unichar_t((t_codepoint - 0x10000) >> 10) + 0xD800;
t_trail = unichar_t((t_codepoint - 0x10000) & 0x3FF) + 0xDC00;
t_buffer[t_out_offset] = t_lead;
t_buffer[t_out_offset + 1] = t_trail;
t_out_offset += 2;
}
}
return MCStringCreateWithChars(t_buffer.Ptr(), t_out_offset, r_string);
}
break;
#if !defined(__LINUX__) && !defined(__ANDROID__)
case kMCStringEncodingISO8859_1:
break;
#endif
#ifndef __WINDOWS__
case kMCStringEncodingWindows1252:
break;
#endif
#if !defined(__MAC__) && !defined(__IOS__)
case kMCStringEncodingMacRoman:
break;
#endif
}
return false;
}
bool MCStringCreateWithBytesAndRelease(byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
{
MCStringRef t_string;
t_string = nil;
switch (p_encoding)
{
case kMCStringEncodingASCII:
case kMCStringEncodingNative:
break;
default:
if (!MCStringCreateWithBytes(p_bytes, p_byte_count, p_encoding, p_is_external_rep, t_string))
return false;
r_string = t_string;
free(p_bytes);
return true;
}
bool t_success;
t_success = true;
if (p_byte_count == 0 && kMCEmptyString != nil)
{
r_string = MCValueRetain(kMCEmptyString);
free(p_bytes);
return true;
}
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, t_string);
if (t_success)
t_success = MCMemoryReallocate(p_bytes, p_byte_count + 1, p_bytes);
if (t_success)
{
p_bytes[p_byte_count] = '\0';
t_string -> native_chars = p_bytes;
t_string -> char_count = p_byte_count;
r_string = t_string;
}
else
MCMemoryDelete(t_string);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
bool MCStringCreateWithChars(const unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
{
if (p_char_count == 0 && kMCEmptyString != nil)
{
r_string = MCValueRetain(kMCEmptyString);
return true;
}
bool t_success;
t_success = true;
__MCString *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
bool t_not_native;
t_not_native = false;
if (t_success)
t_success = MCMemoryNewArray(p_char_count + 1, self -> native_chars);
if (t_success)
{
uindex_t i;
for(i = 0; i < p_char_count; i++)
if (!MCUnicodeCharMapToNative(p_chars[i], self -> native_chars[i]))
{
t_not_native = true;
break;
}
if (t_not_native)
{
MCMemoryDeleteArray(self -> native_chars);
t_success = MCMemoryNewArray(p_char_count + 1, self -> chars);
}
}
if (t_success)
{
if (t_not_native)
{
MCStrCharsMapFromUnicode(p_chars, p_char_count, self -> chars, self -> char_count);
self -> flags |= kMCStringFlagIsNotNative;
}
else
self -> char_count = p_char_count;
r_string = self;
}
else
{
if (self != nil)
MCMemoryDeleteArray(self -> chars);
MCMemoryDelete(self);
}
return t_success;
}
bool MCStringCreateWithCharsAndRelease(unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
{
if (MCStringCreateWithChars(p_chars, p_char_count, r_string))
{
free(p_chars);
return true;
}
return false;
}
bool MCStringCreateWithWString(const unichar_t *p_wstring, MCStringRef& r_string)
{
uindex_t t_length;
for(t_length = 0; p_wstring[t_length] != 0; t_length++)
;
return MCStringCreateWithChars(p_wstring, t_length, r_string);
}
bool MCStringCreateWithWStringAndRelease(unichar_t* p_wstring, MCStringRef& r_string)
{
if (MCStringCreateWithWString(p_wstring, r_string))
{
free(p_wstring);
return true;
}
return false;
}
bool MCStringCreateWithNativeChars(const char_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
{
bool t_success;
t_success = true;
if (p_char_count == 0 && kMCEmptyString != nil)
{
r_string = MCValueRetain(kMCEmptyString);
return true;
}
__MCString *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
if (t_success)
t_success = MCMemoryNewArray(p_char_count + 1, self -> native_chars);
if (t_success)
MCMemoryCopy(self -> native_chars, p_chars, p_char_count);
else
{
if (self != nil)
MCMemoryDeleteArray(self -> native_chars);
MCMemoryDelete(self);
}
if (t_success)
{
self -> char_count = p_char_count;
r_string = self;
}
return t_success;
}
bool MCStringCreateWithNativeCharsAndRelease(char_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
{
bool t_success;
t_success = true;
if (p_char_count == 0 && kMCEmptyString != nil)
{
r_string = MCValueRetain(kMCEmptyString);
MCMemoryDeallocate(p_chars);
return true;
}
__MCString *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
if (t_success)
t_success = MCMemoryReallocate(p_chars, p_char_count + 1, p_chars);
if (t_success)
{
p_chars[p_char_count] = '\0';
self -> native_chars = p_chars;
self -> char_count = p_char_count;
r_string = self;
}
else
MCMemoryDelete(self);
return t_success;
}
static bool MCStringCreateMutableUnicode(uindex_t p_initial_capacity, MCStringRef& r_string)
{
bool t_success;
t_success = true;
__MCString *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
if (t_success)
{
self -> flags |= kMCStringFlagIsNotNative;
t_success = __MCStringExpandAt(self, 0, p_initial_capacity);
}
if (t_success)
{
self -> flags |= kMCStringFlagIsMutable;
self->char_count = 0;
r_string = self;
}
else
{
MCValueRelease (self);
}
return t_success;
}
bool MCStringCreateMutable(uindex_t p_initial_capacity, MCStringRef& r_string)
{
bool t_success;
t_success = true;
__MCString *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
if (t_success)
t_success = __MCStringExpandAt(self, 0, p_initial_capacity);
if (t_success)
{
self -> flags |= kMCStringFlagIsMutable;
self->char_count = 0;
r_string = self;
}
else
{
MCValueRelease (self);
}
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
bool MCStringEncode(MCStringRef p_string, MCStringEncoding p_encoding, bool p_is_external_rep, MCDataRef& r_data)
{
byte_t *t_bytes;
uindex_t t_byte_count;
if (!MCStringConvertToBytes(p_string, p_encoding, p_is_external_rep, t_bytes, t_byte_count))
return false;
if (!MCDataCreateWithBytesAndRelease(t_bytes, t_byte_count, r_data))
{
free(t_bytes);
return false;
}
return true;
}
bool MCStringEncodeAndRelease(MCStringRef p_string, MCStringEncoding p_encoding, bool p_is_external_rep, MCDataRef& r_data)
{
MCDataRef t_data;
if (!MCStringEncode(p_string, p_encoding, p_is_external_rep, t_data))
return false;
MCValueRelease(p_string);
r_data = t_data;
return true;
}
bool MCStringDecode(MCDataRef p_data, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
{
return MCStringCreateWithBytes(MCDataGetBytePtr(p_data), MCDataGetLength(p_data), p_encoding, p_is_external_rep, r_string);
}
bool MCStringDecodeAndRelease(MCDataRef p_data, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
{
MCStringRef t_string;
if (!MCStringDecode(p_data, p_encoding, p_is_external_rep, t_string))
return false;
MCValueRelease(p_data);
r_string = t_string;
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool __MCStringFormatSupportedForUnicode(const char *p_format)
{
while(*p_format != '\0')
{
if (*p_format == '%' &&
(p_format[1] != 's' && p_format[1] != 'd' && p_format[1] != '@'))
return false;
if (*p_format == '\\' &&
(p_format[1] != 'n' && p_format[1] != '"'))
return false;
p_format++;
}
return true;
}
#if defined(__32_BIT__)
#define FORMAT_ARG_32_BIT 1
#define FORMAT_ARG_64_BIT 2
#elif defined(__64_BIT__)
#define FORMAT_ARG_32_BIT 1
#define FORMAT_ARG_64_BIT 1
#endif
bool MCStringFormatV(MCStringRef& r_string, const char *p_format, va_list p_args)
{
MCStringRef t_buffer;
if (!MCStringCreateMutable(0, t_buffer))
return false;
bool t_success;
t_success = true;
const char *t_format_ptr;
t_format_ptr = p_format;
while(t_success && *t_format_ptr != '\0')
{
const char *t_format_start_ptr;
t_format_start_ptr = t_format_ptr;
bool t_has_range;
t_has_range = false;
int t_arg_count;
t_arg_count = 0;
while(*t_format_ptr != '\0')
{
if (*t_format_ptr == '%')
{
// AL-2014-09-19: Flush chars between format strings
if (t_format_ptr != t_format_start_ptr)
break;
t_format_ptr++;
if (*t_format_ptr == '@')
break;
// AL-2014-11-19: [[ Bug 14059 ]] Add support for variable length zero padding
if (*t_format_ptr == '0')
t_format_ptr++;
if (*t_format_ptr == '*')
{
t_arg_count += FORMAT_ARG_32_BIT;
t_format_ptr++;
if (*t_format_ptr == '@')
{
t_format_start_ptr = t_format_ptr;
t_has_range = true;
break;
}
}
else
{
while(*t_format_ptr != '\0' && isdigit(*t_format_ptr))
t_format_ptr++;
}
if (*t_format_ptr == '.')
{
t_format_ptr++;
if (*t_format_ptr == '*')
{
t_arg_count += FORMAT_ARG_32_BIT;
t_format_ptr++;
}
else
{
while(*t_format_ptr != '\0' && isdigit(*t_format_ptr))
t_format_ptr++;
}
}
// MW-2014-10-23: [[ Bug 13757 ]] Make sure we process the VS specific 'I64d' format
// as 64-bit.
if (strncmp(t_format_ptr, "lld", 3) == 0 ||
strncmp(t_format_ptr, "llu", 3) == 0 ||
strncmp(t_format_ptr, "lf", 2) == 0 ||
strncmp(t_format_ptr, "f", 1) == 0 ||
strncmp(t_format_ptr, "g", 1) == 0 ||
strncmp(t_format_ptr, "I64d", 4) == 0)
t_arg_count += FORMAT_ARG_64_BIT;
// SN-2015-01-05: [[ Bug 14304 ]] There is no argument to be popped from the list
// if we are considering a "%%" sequence
else if (*t_format_ptr != '%')
t_arg_count += FORMAT_ARG_32_BIT;
}
t_format_ptr += 1;
}
if (t_format_start_ptr != t_format_ptr)
{
char *t_format;
uint32_t t_format_size;
// [[ vsnprintf ]] On Linux, the trailing '%' from '%@' placeholder causes vsprintf to fail
// and return -1 in MCNativeCharsFormat (and thus creates a 0-byte sized array).
if (*t_format_ptr == '@' && *(t_format_ptr - 1) == '%')
t_format_size = t_format_ptr - t_format_start_ptr - 1;
else
t_format_size = t_format_ptr - t_format_start_ptr;
/* UNCHECKED */ t_format = (char *)malloc(t_format_size + 1);
if (t_format == nil)
t_success = false;
char_t *t_string;
uindex_t t_size;
t_string = nil;
if (t_success)
{
memcpy(t_format, t_format_start_ptr, t_format_size);
t_format[t_format_size] = '\0';
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
t_success = MCNativeCharsFormatV(t_string, t_size, t_format, p_args);
#else
va_list t_args;
va_copy(t_args, p_args);
t_success = MCNativeCharsFormatV(t_string, t_size, t_format, t_args);
va_end(t_args);
#endif
}
if (t_success)
t_success = MCStringAppendNativeChars(t_buffer, t_string, t_size);
if (t_success)
while(t_arg_count > 0)
{
va_arg(p_args, uintptr_t);
t_arg_count -= 1;
}
MCMemoryDeallocate(t_string);
free(t_format);
}
if (t_success && *t_format_ptr == '@')
{
t_format_ptr += 1;
const MCRange *t_range;
if (t_has_range)
t_range = va_arg(p_args, const MCRange *);
else
t_range = nil;
MCValueRef t_value;
t_value = va_arg(p_args, MCValueRef);
MCAutoStringRef t_string;
if (MCValueGetTypeCode(t_value) == kMCValueTypeCodeString)
t_string = (MCStringRef)t_value;
else
/* UNCHECKED */ MCValueCopyDescription (t_value, &t_string);
if (t_range == nil)
t_success = MCStringAppend(t_buffer, *t_string);
else
t_success = MCStringAppendSubstring(t_buffer, *t_string, *t_range);
}
}
if (t_success)
t_success = MCStringCopyAndRelease(t_buffer, r_string);
if (!t_success)
MCValueRelease (t_buffer);
return t_success;
}
bool MCStringFormat(MCStringRef& r_string, const char *p_format, ...)
{
bool t_success;
va_list t_args;
va_start(t_args, p_format);
t_success = MCStringFormatV(r_string, p_format, t_args);
va_end(t_args);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
static bool __MCStringCloneBuffer(MCStringRef self, unichar_t*& chars, uindex_t& char_count)
{
MCAssert(!__MCStringIsIndirect(self));
if (MCMemoryNewArray(self -> char_count + 1, chars))
{
MCStrCharsMapFromUnicode(self -> chars, self -> char_count, chars, char_count);
return true;
}
return false;
}
static bool __MCStringCloneNativeBuffer(MCStringRef self, char_t*& chars, uindex_t& char_count)
{
MCAssert(!__MCStringIsIndirect(self));
if (MCMemoryNewArray(self -> char_count + 1, chars))
{
MCMemoryCopy(chars, self -> native_chars, self -> char_count);
char_count = self -> char_count;
return true;
}
return false;
}
bool MCStringCopy(MCStringRef self, MCStringRef& r_new_string)
{
// If the string is immutable we can just bump the reference count.
if (!MCStringIsMutable(self))
{
r_new_string = self;
MCValueRetain(self);
return true;
}
// If it is mutable and indirect then retain the referenced string
if (__MCStringIsIndirect(self))
{
r_new_string = MCValueRetain(self -> string);
return true;
}
// Otherwise make the mutable direct string immutable,
// assign the new string as that, and make self indirect
// referencing it.
return __MCStringCopyMutable(self, r_new_string);
}
bool MCStringCopyAndRelease(MCStringRef self, MCStringRef& r_new_string)
{
// If the string is immutable we just pass it through (as we are releasing the string).
if (!MCStringIsMutable(self))
{
r_new_string = self;
return true;
}
// If the string is indirect then retain its reference, and release
if (__MCStringIsIndirect(self))
{
r_new_string = MCValueRetain(self -> string);
MCValueRelease(self);
return true;
}
// If the reference count is one, then shrink the buffer and mark as immutable.
if (self -> references == 1)
{
__MCStringMakeImmutable(self);
self -> flags &= ~kMCStringFlagIsMutable;
self -> capacity = 0;
r_new_string = self;
return true;
}
// Otherwise, make a new indirect string
if (!__MCStringMakeIndirect(self))
return false;
// Reduce reference count
self -> references -= 1;
// And return a copy of the string
r_new_string = MCValueRetain(self -> string);
return true;
}
bool MCStringMutableCopy(MCStringRef self, MCStringRef& r_new_string)
{
// If self is immutable, then the new mutable string will be indirect
// referencing it.
if (!MCStringIsMutable(self))
return __MCStringCreateIndirect(self, r_new_string);
// If the string is already indirect, we just create a new reference to its string
if (__MCStringIsIndirect(self))
return __MCStringCreateIndirect(self -> string, r_new_string);
// If the string is mutable, we make it indirect and share
// the indirect copy.
if (!__MCStringMakeIndirect(self))
return false;
return __MCStringCreateIndirect(self -> string, r_new_string);
}
bool MCStringMutableCopyAndRelease(MCStringRef self, MCStringRef& r_new_string)
{
if (self -> references == 1)
{
if (!MCStringIsMutable(self))
{
self -> flags |= kMCStringFlagIsMutable;
//self -> capacity = self -> char_count;
}
r_new_string = self;
return true;
}
if (!MCStringMutableCopy(self, r_new_string))
return false;
self -> references -= 1;
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool MCStringCopySubstring(MCStringRef self, MCRange p_range, MCStringRef& r_substring)
{
if (__MCStringIsIndirect(self))
self = self -> string;
// Avoid copying in case the substring is actually the whole string
if (p_range . offset == 0 && self -> char_count < p_range . length)
return MCStringCopy(self, r_substring);
__MCStringClampRange(self, p_range);
if (__MCStringIsNative(self))
return MCStringCreateWithNativeChars(self -> native_chars + p_range . offset, p_range . length, r_substring);
return MCStringCreateWithChars(self -> chars + p_range . offset, p_range . length, r_substring);
}
bool MCStringCopySubstringAndRelease(MCStringRef self, MCRange p_range, MCStringRef& r_substring)
{
if (MCStringCopySubstring(self, p_range, r_substring))
{
MCValueRelease(self);
return true;
}
return false;