Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_string.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2020 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_string.h"
10
11 #include "dng_assertions.h"
12 #include "dng_exceptions.h"
13 #include "dng_fingerprint.h"
14 #include "dng_flags.h"
15 #include "dng_mutex.h"
16 #include "dng_utils.h"
17 #include "dng_safe_arithmetic.h"
18
19 #if qMacOS
20 #include <TargetConditionals.h>
21 #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
22 #include <MobileCoreServices/MobileCoreServices.h>
23 #else
24 #include <CoreServices/CoreServices.h>
25 #endif
26 #include <CoreServices/CoreServices.h>
27 #endif
28
29 #if qWinOS
30
31 # include <windows.h>
32 # include <winnls.h>
33 # include <stringapiset.h>
34
35 # ifndef SORT_DIGITSASNUMBERS
36 # define SORT_DIGITSASNUMBERS 0x00000008 // use digits as numbers sort method
37 # endif
38
39 #endif
40
41 #if qiPhone || qAndroid || qLinux
42 #include <ctype.h> // for isdigit
43 #endif
44
45 /*****************************************************************************/
46
47 const uint32 kREPLACEMENT_CHARACTER = 0x0000FFFD;
48
49 /*****************************************************************************/
50
51 // Returns the length of the zero-terminated string 's'. Throws a dng_exception
52 // if the length of 's' is too large to be represented as a uint32.
53
54 static uint32 strlenAsUint32 (const char *s)
55 {
56
57 uint32 lengthAsUint32 = 0;
58
59 ConvertUnsigned (strlen (s), &lengthAsUint32);
60
61 return lengthAsUint32;
62
63 }
64
65 /*****************************************************************************/
66
67 #if qMacOS
68
69 static void Assign_Multibyte (dng_string &dngString,
70 const char *otherString,
71 TextEncoding encoding)
72 {
73
74 dng_safe_uint32 aSize (strlenAsUint32 (otherString));
75
76 if (aSize.Get () > 0)
77 {
78
79 dng_safe_uint32 aBufSize = aSize * 6u + 256u;
80
81 dng_memory_data aBuf (aBufSize + 1u);
82
83 UnicodeMapping aMapping;
84
85 aMapping.unicodeEncoding = ::CreateTextEncoding (kTextEncodingUnicodeV3_0,
86 kUnicodeNoSubset,
87 kUnicodeUTF8Format);
88
89 aMapping.otherEncoding = encoding;
90 aMapping.mappingVersion = kUnicodeUseLatestMapping;
91
92 TextToUnicodeInfo aInfo = NULL;
93
94 if (::CreateTextToUnicodeInfo (&aMapping, &aInfo) == noErr)
95 {
96
97 ByteCount aInput = 0;
98 ByteCount aOutput = 0;
99
100 ::ConvertFromTextToUnicode (aInfo,
101 aSize.Get (),
102 otherString,
103 kUnicodeUseFallbacksMask |
104 kUnicodeLooseMappingsMask,
105 0,
106 NULL,
107 NULL,
108 NULL,
109 aBufSize.Get (),
110 &aInput,
111 &aOutput,
112 (UniChar *) aBuf.Buffer ());
113
114 ::DisposeTextToUnicodeInfo (&aInfo);
115
116 if (aOutput > 0 && aOutput <= aBufSize.Get ())
117 {
118
119 char *aBufChar = aBuf.Buffer_char ();
120
121 aBufChar [aOutput] = 0;
122
123 dngString.Set (aBufChar);
124
125 return;
126
127 }
128
129 }
130
131 }
132
133 dngString.Clear ();
134
135 }
136
137 static uint32 Extract_Multibyte (const dng_string &dngString,
138 dng_memory_data &buffer,
139 TextEncoding encoding)
140 {
141
142 dng_safe_uint32 aSize (dngString.Length ());
143
144 if (aSize.Get () > 0)
145 {
146
147 dng_safe_uint32 aBufSize = aSize * 2u + 256u;
148
149 dng_memory_data tempBuffer (aBufSize);
150
151 UnicodeMapping aMapping;
152
153 aMapping.unicodeEncoding = ::CreateTextEncoding (kTextEncodingUnicodeV3_0,
154 kUnicodeNoSubset,
155 kUnicodeUTF8Format);
156
157 aMapping.otherEncoding = encoding;
158 aMapping.mappingVersion = kUnicodeUseLatestMapping;
159
160 UnicodeToTextInfo aInfo = NULL;
161
162 if (::CreateUnicodeToTextInfo (&aMapping, &aInfo) == noErr)
163 {
164
165 ByteCount aInput = 0;
166 ByteCount aOutput = 0;
167
168 ::ConvertFromUnicodeToText (aInfo,
169 aSize.Get (),
170 (const UniChar *) dngString.Get (),
171 kUnicodeUseFallbacksMask |
172 kUnicodeLooseMappingsMask |
173 kUnicodeDefaultDirectionMask,
174 0,
175 NULL,
176 NULL,
177 NULL,
178 aBufSize.Get (),
179 &aInput,
180 &aOutput,
181 tempBuffer.Buffer_char ());
182
183 ::DisposeUnicodeToTextInfo (&aInfo);
184
185 if (aOutput > 0)
186 {
187
188 uint32 aOutputAsUint32 = 0;
189
190 ConvertUnsigned (aOutput, &aOutputAsUint32);
191
192 buffer.Allocate (dng_safe_uint32 (aOutputAsUint32) + 1u);
193
194 memcpy (buffer.Buffer (),
195 tempBuffer.Buffer (),
196 aOutputAsUint32);
197
198 buffer.Buffer_char () [aOutputAsUint32] = 0;
199
200 return aOutputAsUint32;
201
202 }
203
204 }
205
206 }
207
208 buffer.Allocate (1);
209
210 buffer.Buffer_char () [0] = 0;
211
212 return 0;
213
214 }
215
216 static void Assign_SystemEncoding (dng_string &dngString,
217 const char *otherString)
218 {
219
220 TextEncoding aEncoding;
221
222 ::UpgradeScriptInfoToTextEncoding (smCurrentScript,
223 //smSystemScript,
224 //kTextScriptDontCare,
225 kTextLanguageDontCare,
226 kTextRegionDontCare,
227 NULL,
228 &aEncoding);
229
230 Assign_Multibyte (dngString,
231 otherString,
232 aEncoding);
233
234 }
235
236 static uint32 Extract_SystemEncoding (const dng_string &dngString,
237 dng_memory_data &buffer)
238 {
239
240 TextEncoding aEncoding;
241
242 ::UpgradeScriptInfoToTextEncoding (smCurrentScript,
243 //smSystemScript,
244 //kTextScriptDontCare,
245 kTextLanguageDontCare,
246 kTextRegionDontCare,
247 NULL,
248 &aEncoding);
249
250 return Extract_Multibyte (dngString,
251 buffer,
252 aEncoding);
253
254 }
255
256 static void Assign_JIS_X208_1990 (dng_string &dngString,
257 const char *otherString)
258 {
259
260 Assign_Multibyte (dngString,
261 otherString,
262 kTextEncodingJIS_X0208_90);
263
264 }
265
266 #endif
267
268 /*****************************************************************************/
269
270 #if qWinOS
271
272 static void Assign_Multibyte (dng_string &dngString,
273 const char *otherString,
274 UINT encoding)
275 {
276
277 DNG_ASSERT (sizeof (WCHAR) == 2, "WCHAR must be 2 bytes");
278
279 const dng_safe_uint32 otherStringLen (strlenAsUint32 (otherString));
280
281 const dng_safe_int32 aSize (otherStringLen);
282
283 if (aSize.Get () > 0)
284 {
285
286 dng_safe_uint32 aBufCharsUint32 = otherStringLen * 3u + 128u;
287
288 dng_safe_int32 aBufChars (aBufCharsUint32);
289
290 dng_safe_uint32 bytesToAllocate = (aBufCharsUint32 + 1u) * 2u;
291
292 dng_memory_data aBuf (bytesToAllocate);
293
294 int aResult = ::MultiByteToWideChar (encoding,
295 0,
296 otherString,
297 aSize.Get (),
298 (WCHAR *) aBuf.Buffer (),
299 aBufChars.Get ());
300
301 if (aResult > 0 && aResult <= aBufChars.Get ())
302 {
303
304 uint16 * aUTF16 = aBuf.Buffer_uint16 ();
305
306 aUTF16 [aResult] = 0;
307
308 dngString.Set_UTF16 (aUTF16);
309
310 return;
311
312 }
313
314 }
315
316 dngString.Clear ();
317
318 }
319
320 static uint32 Extract_Multibyte (const dng_string &dngString,
321 dng_memory_data &buffer,
322 UINT encoding)
323 {
324
325 DNG_ASSERT (sizeof (WCHAR) == 2, "WCHAR must be 2 bytes");
326
327 dng_memory_data sBuffer;
328
329 int aCount = dngString.Get_UTF16 (sBuffer);
330
331 if (aCount < 0)
332 {
333 return 0;
334 }
335
336 dng_safe_uint32 aCountAsUint32 (static_cast<uint32> (aCount));
337
338 dng_safe_uint32 dBufSize = aCountAsUint32 * 2u + 256u;
339
340 dng_memory_data dBuffer (dBufSize);
341
342 int aResult = ::WideCharToMultiByte (encoding,
343 0,
344 (WCHAR *) sBuffer.Buffer (),
345 aCount,
346 dBuffer.Buffer_char (),
347 dBufSize.Get (),
348 NULL,
349 NULL);
350
351 if (aResult < 0)
352 aResult = 0;
353
354 dng_safe_uint32 aResultAsUint32 (static_cast<uint32> (aResult));
355
356 buffer.Allocate (aResultAsUint32 + 1u);
357
358 memcpy (buffer.Buffer (),
359 dBuffer.Buffer (),
360 aResult);
361
362 buffer.Buffer_char () [aResult] = 0;
363
364 return aResultAsUint32.Get ();
365
366 }
367
368 static void Assign_SystemEncoding (dng_string &dngString,
369 const char *otherString)
370 {
371
372 Assign_Multibyte (dngString,
373 otherString,
374 ::GetACP ());
375
376 }
377
378 static uint32 Extract_SystemEncoding (const dng_string &dngString,
379 dng_memory_data &buffer)
380 {
381
382 return Extract_Multibyte (dngString,
383 buffer,
384 ::GetACP ());
385
386 }
387
388 static void Assign_JIS_X208_1990 (dng_string &dngString,
389 const char *otherString)
390 {
391
392 // From MSDN documentation: 20932 = JIS X 0208-1990 & 0121-1990
393
394 const UINT kJIS = 20932;
395
396 Assign_Multibyte (dngString,
397 otherString,
398 kJIS);
399
400 }
401
402 #endif
403
404 /*****************************************************************************/
405
406 static bool IsASCII (const char *s)
407 {
408
409 if (!s)
410 {
411
412 return true;
413
414 }
415
416 while (true)
417 {
418
419 uint8 c = (uint8) *(s++);
420
421 if (c == 0)
422 {
423
424 break;
425
426 }
427
428 if (c & 0x80)
429 {
430
431 return false;
432
433 }
434
435 }
436
437 return true;
438
439 }
440
441 /*****************************************************************************/
442
443 dng_string::dng_string ()
444
445 : fData ()
446
447 {
448
449 }
450
451 /*****************************************************************************/
452
453 dng_string::dng_string (const dng_string &s)
454
455 : fData (s.fData)
456
457 {
458
459 }
460
461 /*****************************************************************************/
462
463 dng_string::dng_string (const char *s)
464
465 : fData ()
466
467 {
468
469 Set (s);
470
471 }
472
473 /*****************************************************************************/
474
475 dng_string & dng_string::operator= (const dng_string &s)
476 {
477
478 if (this != &s)
479 {
480
481 fData = s.fData;
482
483 }
484
485 return *this;
486
487 }
488
489 /*****************************************************************************/
490
491 dng_string::~dng_string ()
492 {
493
494 }
495
496 /*****************************************************************************/
497
498 const char * dng_string::Get () const
499 {
500
501 if (fData.get ())
502 {
503
504 return fData->c_str ();
505
506 }
507
508 return "";
509
510 }
511
512 /*****************************************************************************/
513
514 bool dng_string::IsASCII () const
515 {
516
517 return ::IsASCII (Get ());
518
519 }
520
521 /*****************************************************************************/
522
523 void dng_string::Set (const char *s)
524 {
525
526 // Measure the new length.
527
528 uint32 newLen = (s != NULL ? strlenAsUint32 (s) : 0);
529
530 // If it is a NULL string, then clear the buffer.
531
532 if (newLen == 0)
533 {
534
535 fData.reset ();
536
537 }
538
539 // Else we need to copy the bytes.
540
541 else
542 {
543
544 if (!fData.get () || fData->compare (s) != 0)
545 {
546
547 fData.reset (new dng_std_string (s));
548
549 }
550
551 }
552
553 }
554
555 /*****************************************************************************/
556
557 void dng_string::Set_ASCII (const char *s)
558 {
559
560 if (::IsASCII (s))
561 {
562
563 Set (s);
564
565 }
566
567 else
568 {
569
570 Set_SystemEncoding (s);
571
572 }
573
574 }
575
576 /*****************************************************************************/
577
578 static void AppendUnicodeChar32 (dng_std_string &buffer, uint32 aChar)
579 {
580
581 if (aChar < 0x00000080)
582 {
583 buffer.push_back ((char) aChar);
584 }
585
586 else if (aChar < 0x00000800)
587 {
588 buffer.push_back ((char) ((aChar >> 6) | 0x000000C0));
589 buffer.push_back ((char) ((aChar & 0x0000003F) | 0x00000080));
590 }
591
592 else if (aChar < 0x00010000)
593 {
594 buffer.push_back ((char) ( (aChar >> 12) | 0x000000E0));
595 buffer.push_back ((char) (((aChar >> 6) & 0x0000003F) | 0x00000080));
596 buffer.push_back ((char) ( (aChar & 0x0000003F) | 0x00000080));
597 }
598
599 else if (aChar < 0x00200000)
600 {
601 buffer.push_back ((char) ( (aChar >> 18) | 0x000000F0));
602 buffer.push_back ((char) (((aChar >> 12) & 0x0000003F) | 0x00000080));
603 buffer.push_back ((char) (((aChar >> 6) & 0x0000003F) | 0x00000080));
604 buffer.push_back ((char) ( (aChar & 0x0000003F) | 0x00000080));
605 }
606
607 else if (aChar < 0x04000000)
608 {
609 buffer.push_back ((char) ( (aChar >> 24) | 0x000000F8));
610 buffer.push_back ((char) (((aChar >> 18) & 0x0000003F) | 0x00000080));
611 buffer.push_back ((char) (((aChar >> 12) & 0x0000003F) | 0x00000080));
612 buffer.push_back ((char) (((aChar >> 6) & 0x0000003F) | 0x00000080));
613 buffer.push_back ((char) ( (aChar & 0x0000003F) | 0x00000080));
614 }
615
616 else
617 {
618 buffer.push_back ((char) ( (aChar >> 30) | 0x000000FC));
619 buffer.push_back ((char) (((aChar >> 24) & 0x0000003F) | 0x00000080));
620 buffer.push_back ((char) (((aChar >> 18) & 0x0000003F) | 0x00000080));
621 buffer.push_back ((char) (((aChar >> 12) & 0x0000003F) | 0x00000080));
622 buffer.push_back ((char) (((aChar >> 6) & 0x0000003F) | 0x00000080));
623 buffer.push_back ((char) ( (aChar & 0x0000003F) | 0x00000080));
624 }
625
626 }
627
628 /*****************************************************************************/
629
630 void dng_string::Set_UTF8 (const char *s)
631 {
632
633 dng_safe_uint32 len (strlenAsUint32 (s));
634
635 const char *sEnd = s + len.Get ();
636
637 // Worst case expansion is 1-byte characters expanding to
638 // replacement character, which requires 3 bytes.
639
640 dng_safe_uint32 destBufferLength (len * 3u);
641
642 dng_std_string buffer;
643
644 buffer.reserve (destBufferLength.Get ());
645
646 while (s < sEnd)
647 {
648
649 uint32 aChar = DecodeUTF8 (s, (uint32) (sEnd - s));
650
651 if (aChar > 0x7FFFFFFF)
652 {
653 aChar = kREPLACEMENT_CHARACTER;
654 }
655
656 #if qDNGValidate
657
658 if (aChar == kREPLACEMENT_CHARACTER)
659 {
660 ReportWarning ("Expected UTF-8 value is not valid UTF-8 (or contains a kREPLACEMENT_CHARACTER)");
661 }
662
663 #endif
664
665 AppendUnicodeChar32 (buffer, aChar);
666
667 }
668
669 Set (buffer.c_str ());
670
671 }
672
673 /*****************************************************************************/
674
675 uint32 dng_string::Get_SystemEncoding (dng_memory_data &buffer) const
676 {
677
678 if (IsASCII ())
679 {
680
681 dng_safe_uint32 len (Length ());
682
683 const dng_safe_uint32 destBufferLength = len + 1u;
684
685 buffer.Allocate (destBufferLength);
686
687 memcpy (buffer.Buffer (), Get (), destBufferLength.Get ());
688
689 return len.Get ();
690
691 }
692
693 else
694 {
695
696 #if qMacOS || qWinOS
697
698 return Extract_SystemEncoding (*this, buffer);
699
700 #else
701
702 // Fallback logic to force the string to ASCII.
703
704 dng_string temp (*this);
705
706 temp.ForceASCII ();
707
708 return temp.Get_SystemEncoding (buffer);
709
710 #endif
711
712 }
713
714 }
715
716 /*****************************************************************************/
717
718 void dng_string::Set_SystemEncoding (const char *s)
719 {
720
721 if (::IsASCII (s))
722 {
723
724 Set (s);
725
726 }
727
728 else
729 {
730
731 #if qMacOS || qWinOS
732
733 Assign_SystemEncoding (*this, s);
734
735 #else
736
737 // Fallback logic that just grabs the ASCII characters and
738 // ignores the non-ASCII characters.
739
740 dng_safe_uint32 len = strlenAsUint32 (s);
741
742 dng_std_string buffer;
743
744 buffer.reserve (len.Get ());
745
746 while (*s)
747 {
748
749 uint8 c = (uint8) *(s++);
750
751 if ((c & 0x80) == 0)
752 {
753
754 buffer.push_back ((char) c);
755
756 }
757
758 }
759
760 Set (buffer.c_str ());
761
762 #endif
763
764 }
765
766 }
767
768 /*****************************************************************************/
769
770 bool dng_string::ValidSystemEncoding () const
771 {
772
773 if (IsASCII ())
774 {
775
776 return true;
777
778 }
779
780 dng_memory_data buffer;
781
782 Get_SystemEncoding (buffer);
783
784 dng_string temp;
785
786 temp.Set_SystemEncoding (buffer.Buffer_char ());
787
788 return (*this == temp);
789
790 }
791
792 /*****************************************************************************/
793
794 void dng_string::Set_JIS_X208_1990 (const char *s)
795 {
796
797 if (::IsASCII (s))
798 {
799
800 Set (s);
801
802 }
803
804 else
805 {
806
807 #if qMacOS || qWinOS
808
809 Assign_JIS_X208_1990 (*this, s);
810
811 #else
812
813 // Fallback to the ASCII extraction logic.
814
815 Set_SystemEncoding (s);
816
817 #endif
818
819 }
820
821 }
822
823 /*****************************************************************************/
824
825 uint32 dng_string::DecodeUTF8 (const char *&s,
826 uint32 maxBytes,
827 bool *isValid)
828 {
829
830 static const uint8 gUTF8Bytes [256] =
831 {
832 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
833 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
834 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
835 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
836 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
837 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
838 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
839 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,6,6
840 };
841
842 if (isValid)
843 {
844 *isValid = true;
845 }
846
847 const uint8 *nBuf = (const uint8 *) s;
848
849 uint32 aChar = nBuf [0];
850
851 uint32 aSize = gUTF8Bytes [aChar];
852
853 if (aSize > maxBytes)
854 {
855
856 s += maxBytes;
857
858 if (isValid)
859 {
860 *isValid = false;
861 }
862
863 return kREPLACEMENT_CHARACTER;
864
865 }
866
867 s += aSize;
868
869 for (uint32 extra = 1; extra < aSize; extra++)
870 {
871
872 if ((nBuf [extra] & 0xC0) != 0x80)
873 {
874
875 if (isValid)
876 {
877 *isValid = false;
878 }
879
880 return kREPLACEMENT_CHARACTER;
881
882 }
883
884 }
885
886 switch (aSize)
887 {
888
889 case 0:
890 {
891
892 s++; // Don't get stuck in infinite loop
893
894 if (isValid)
895 {
896 *isValid = false;
897 }
898
899 return kREPLACEMENT_CHARACTER;
900
901 }
902
903 case 1:
904 {
905
906 return aChar;
907
908 }
909
910 case 2:
911 {
912
913 aChar = ((aChar << 6) + nBuf [1]) - (uint32) 0x00003080UL;
914
915 break;
916
917 }
918
919 case 3:
920 {
921
922 aChar = ((((aChar << 6) + nBuf [1])
923 << 6) + nBuf [2]) - (uint32) 0x000E2080UL;
924
925 break;
926
927 }
928
929 case 4:
930 {
931
932 aChar = ((((((aChar << 6) + nBuf [1])
933 << 6) + nBuf [2])
934 << 6) + nBuf [3]) - (uint32) 0x03C82080UL;
935
936 break;
937
938 }
939
940 case 5:
941 {
942
943 aChar = ((((((((aChar << 6) + nBuf [1])
944 << 6) + nBuf [2])
945 << 6) + nBuf [3])
946 << 6) + nBuf [4]) - (uint32) 0xFA082080UL;
947
948 break;
949
950 }
951
952 case 6:
953 {
954
955 aChar = ((((((((((aChar << 6) + nBuf [1])
956 << 6) + nBuf [2])
957 << 6) + nBuf [3])
958 << 6) + nBuf [4])
959 << 6) + nBuf [5]) - (uint32) 0x82082080UL;
960
961 break;
962
963 }
964
965 }
966
967 if (aChar < 0x7F || aChar > 0x0010FFFF)
968 {
969
970 if (isValid)
971 {
972 *isValid = false;
973 }
974
975 return kREPLACEMENT_CHARACTER;
976
977 }
978
979 return aChar;
980
981 }
982
983 /*****************************************************************************/
984
985 bool dng_string::IsUTF8 (const char *s)
986 {
987
988 uint32 len = strlenAsUint32 (s);
989
990 const char *sEnd = s + len;
991
992 while (s < sEnd)
993 {
994
995 bool isValid = true;
996
997 (void) DecodeUTF8 (s, (uint32) (sEnd - s), &isValid);
998
999 if (!isValid)
1000 {
1001 return false;
1002 }
1003
1004 }
1005
1006 return true;
1007
1008 }
1009
1010 /*****************************************************************************/
1011
1012 void dng_string::Set_UTF8_or_System (const char *s)
1013 {
1014
1015 if (::IsASCII (s))
1016 {
1017
1018 Set (s);
1019
1020 }
1021
1022 else if (IsUTF8 (s))
1023 {
1024
1025 Set_UTF8 (s);
1026
1027 }
1028
1029 else
1030 {
1031
1032 Set_SystemEncoding (s);
1033
1034 }
1035
1036 }
1037
1038 /*****************************************************************************/
1039
1040 uint32 dng_string::Get_UTF16 (dng_memory_data &buffer) const
1041 {
1042
1043 // Count *exactly* how many 16-bit words required in buffer.
1044
1045 dng_safe_uint32 count = 0u;
1046
1047 const char *sPtr = Get ();
1048
1049 while (*sPtr)
1050 {
1051
1052 uint32 x = DecodeUTF8 (sPtr);
1053
1054 if (x <= 0x0000FFFF ||
1055 x > 0x0010FFFF)
1056 {
1057
1058 count += 1u;
1059
1060 }
1061
1062 else
1063 {
1064
1065 count += 2u;
1066
1067 }
1068
1069 }
1070
1071 const dng_safe_uint32 destBufferLength = count + 1u;
1072
1073 buffer.Allocate (destBufferLength.Get (),
1074 sizeof (uint16));
1075
1076 uint16 *dPtr = buffer.Buffer_uint16 ();
1077
1078 sPtr = Get ();
1079
1080 while (*sPtr)
1081 {
1082
1083 uint32 x = DecodeUTF8 (sPtr);
1084
1085 if (x <= 0x0000FFFF)
1086 {
1087
1088 *(dPtr++) = (uint16) x;
1089
1090 }
1091
1092 else if (x > 0x0010FFFF)
1093 {
1094
1095 *(dPtr++) = (uint16) kREPLACEMENT_CHARACTER;
1096
1097 }
1098
1099 else
1100 {
1101
1102 x -= 0x00010000;
1103
1104 *(dPtr++) = (uint16) ((x >> 10 ) + 0x0000D800);
1105 *(dPtr++) = (uint16) ((x & 0x000003FF) + 0x0000DC00);
1106
1107 }
1108
1109 }
1110
1111 *dPtr = 0;
1112
1113 return count.Get ();
1114
1115 }
1116
1117 /*****************************************************************************/
1118
1119 void dng_string::Set_UTF16 (const uint16 *s)
1120 {
1121
1122 if (!s)
1123 {
1124 Clear ();
1125 return;
1126 }
1127
1128 bool swap = false;
1129
1130 if (s [0] == 0xFFFE) // Swapped byte order marker
1131 {
1132 swap = true;
1133 s++;
1134 }
1135
1136 else if (s [0] == 0xFEFF) // Non-swapped byte order marker
1137 {
1138 s++;
1139 }
1140
1141 dng_safe_uint32 length16 (0u);
1142
1143 while (s [length16.Get ()] != 0)
1144 {
1145 length16 += 1u;
1146 }
1147
1148 const uint16 *sEnd = s + length16.Get ();
1149
1150 const dng_safe_uint32 destBufferSize = length16 * 6u;
1151
1152 dng_std_string buffer;
1153
1154 buffer.reserve (destBufferSize.Get ());
1155
1156 while (s < sEnd)
1157 {
1158
1159 uint32 aChar = *s++;
1160
1161 if (swap)
1162 {
1163 aChar = ((aChar << 8) | (aChar >> 8)) & 0x0000FFFF;
1164 }
1165
1166 if ((aChar >= 0x0000D800) && (aChar <= 0x0000DBFF) && (s < sEnd))
1167 {
1168
1169 uint32 aLow = *s;
1170
1171 if (swap)
1172 {
1173 aLow = ((aLow << 8) | (aLow >> 8)) & 0x0000FFFF;
1174 }
1175
1176 if ((aLow >= 0x0000DC00) && (aLow <= 0x0000DFFF))
1177 {
1178
1179 aChar = ((aChar - 0x0000D800) << 10) +
1180 (aLow - 0x0000DC00) +
1181 0x00010000;
1182
1183 s++;
1184
1185 }
1186
1187 }
1188
1189 if (aChar > 0x7FFFFFFF)
1190 {
1191 aChar = kREPLACEMENT_CHARACTER;
1192 }
1193
1194 AppendUnicodeChar32 (buffer, aChar);
1195
1196 }
1197
1198 Set (buffer.c_str ());
1199
1200 }
1201
1202 /*****************************************************************************/
1203
1204 void dng_string::Clear ()
1205 {
1206
1207 fData.reset ();
1208
1209 }
1210
1211 /*****************************************************************************/
1212
1213 void dng_string::Truncate (uint32 maxBytes)
1214 {
1215
1216 uint32 len = Length ();
1217
1218 if (len > maxBytes)
1219 {
1220
1221 dng_std_string newData (*fData);
1222
1223 uint8 *s = (uint8 *) newData.data ();
1224
1225 // Don't truncate on an extension character. Extensions characters
1226 // in UTF-8 have the 0x80 bit set and the 0x40 bit clear.
1227
1228 while (maxBytes > 0 && ((s [maxBytes]) & 0xC0) == 0x80)
1229 {
1230
1231 maxBytes--;
1232
1233 }
1234
1235 s [maxBytes] = 0;
1236
1237 Set (newData.c_str ());
1238
1239 }
1240
1241 }
1242
1243 /*****************************************************************************/
1244
1245 bool dng_string::TrimTrailingBlanks ()
1246 {
1247
1248 bool didTrim = false;
1249
1250 if (fData.get () && fData->back () == ' ')
1251 {
1252
1253 const char *s = fData->c_str ();
1254
1255 uint32 len = strlenAsUint32 (s);
1256
1257 while (len > 0 && s [len - 1] == ' ')
1258 {
1259 len--;
1260 didTrim = true;
1261 }
1262
1263 dng_std_string newData (*fData);
1264
1265 newData.erase (len);
1266
1267 Set (newData.c_str ());
1268
1269 }
1270
1271 return didTrim;
1272
1273 }
1274
1275 /*****************************************************************************/
1276
1277 bool dng_string::TrimLeadingBlanks ()
1278 {
1279
1280 bool didTrim = false;
1281
1282 const char *s = Get ();
1283
1284 while (*s == ' ')
1285 {
1286 s++;
1287 didTrim = true;
1288 }
1289
1290 if (didTrim)
1291 {
1292 Set (s);
1293 }
1294
1295 return didTrim;
1296
1297 }
1298
1299 /*****************************************************************************/
1300
1301 bool dng_string::IsEmpty () const
1302 {
1303
1304 return !fData.get ();
1305
1306 }
1307
1308 /*****************************************************************************/
1309
1310 uint32 dng_string::Length () const
1311 {
1312
1313 uint32 lengthAsUint32 = 0;
1314
1315 if (fData.get ())
1316 {
1317 ConvertUnsigned (fData->length (), &lengthAsUint32);
1318 }
1319
1320 return lengthAsUint32;
1321
1322 }
1323
1324 /*****************************************************************************/
1325
1326 bool dng_string::operator== (const dng_string &s) const
1327 {
1328
1329 if (fData.get ())
1330 {
1331 if (s.fData.get ())
1332 {
1333 return *fData == *s.fData;
1334 }
1335 else
1336 {
1337 return false;
1338 }
1339 }
1340 else
1341 {
1342 if (s.fData.get ())
1343 {
1344 return false;
1345 }
1346 else
1347 {
1348 return true;
1349 }
1350 }
1351
1352 }
1353
1354 /*****************************************************************************/
1355
1356 bool dng_string::Matches (const char *t,
1357 const char *s,
1358 bool case_sensitive)
1359 {
1360
1361 while (*s != 0)
1362 {
1363
1364 char c1 = *(s++);
1365 char c2 = *(t++);
1366
1367 if (!case_sensitive)
1368 {
1369 c1 = ForceUppercase (c1);
1370 c2 = ForceUppercase (c2);
1371 }
1372
1373 if (c1 != c2)
1374 {
1375 return false;
1376 }
1377
1378 }
1379
1380 return (*t == 0);
1381
1382 }
1383
1384 /*****************************************************************************/
1385
1386 bool dng_string::Matches (const char *s,
1387 bool case_sensitive) const
1388 {
1389
1390 return dng_string::Matches (Get (), s, case_sensitive);
1391
1392 }
1393
1394 /*****************************************************************************/
1395
1396 bool dng_string::StartsWith (const char *s,
1397 bool case_sensitive) const
1398 {
1399
1400 const char *t = Get ();
1401
1402 while (*s != 0)
1403 {
1404
1405 char c1 = *(s++);
1406 char c2 = *(t++);
1407
1408 if (!case_sensitive)
1409 {
1410 c1 = ForceUppercase (c1);
1411 c2 = ForceUppercase (c2);
1412 }
1413
1414 if (c1 != c2)
1415 {
1416 return false;
1417 }
1418
1419 }
1420
1421 return true;
1422
1423 }
1424
1425 /*****************************************************************************/
1426
1427 bool dng_string::EndsWith (const char *s,
1428 bool case_sensitive) const
1429 {
1430
1431 uint32 len1 = Length ();
1432
1433 uint32 len2 = strlenAsUint32 (s);
1434
1435 if (len1 < len2)
1436 {
1437 return false;
1438 }
1439
1440 const char *t = Get () + (len1 - len2);
1441
1442 while (*s != 0)
1443 {
1444
1445 char c1 = *(s++);
1446 char c2 = *(t++);
1447
1448 if (!case_sensitive)
1449 {
1450 c1 = ForceUppercase (c1);
1451 c2 = ForceUppercase (c2);
1452 }
1453
1454 if (c1 != c2)
1455 {
1456 return false;
1457 }
1458
1459 }
1460
1461 return true;
1462
1463 }
1464
1465 /*****************************************************************************/
1466
1467 bool dng_string::Contains (const char *s,
1468 bool case_sensitive,
1469 int32 *match_offset) const
1470 {
1471
1472 if (match_offset)
1473 {
1474 *match_offset = -1;
1475 }
1476
1477 uint32 len1 = Length ();
1478
1479 uint32 len2 = strlenAsUint32 (s);
1480
1481 if (len1 < len2)
1482 {
1483 return false;
1484 }
1485
1486 uint32 offsets = len1 - len2;
1487
1488 for (uint32 offset = 0; offset <= offsets; offset++)
1489 {
1490
1491 const char *ss = s;
1492 const char *tt = Get () + offset;
1493
1494 while (*ss != 0)
1495 {
1496
1497 char c1 = *(ss++);
1498 char c2 = *(tt++);
1499
1500 if (!case_sensitive)
1501 {
1502 c1 = ForceUppercase (c1);
1503 c2 = ForceUppercase (c2);
1504 }
1505
1506 if (c1 != c2)
1507 {
1508 goto tryNextOffset;
1509 }
1510
1511 }
1512
1513 if (match_offset)
1514 {
1515 *match_offset = offset;
1516 }
1517
1518 return true;
1519
1520 tryNextOffset: ;
1521
1522 }
1523
1524 return false;
1525
1526 }
1527
1528 /*****************************************************************************/
1529
1530 bool dng_string::Replace (const char *old_string,
1531 const char *new_string,
1532 bool case_sensitive)
1533 {
1534
1535 int32 match_offset = -1;
1536
1537 if (Contains (old_string,
1538 case_sensitive,
1539 &match_offset))
1540 {
1541
1542 uint32 len2 = strlenAsUint32 (old_string);
1543
1544 dng_std_string newData (*fData);
1545
1546 newData.replace (match_offset, len2, new_string);
1547
1548 Set (newData.c_str ());
1549
1550 return true;
1551
1552 }
1553
1554 return false;
1555
1556 }
1557
1558 /*****************************************************************************/
1559
1560 void dng_string::ReplaceChars (char oldChar,
1561 char newChar)
1562 {
1563
1564 if (fData.get ())
1565 {
1566
1567 // Find index of first character that needs to be changed.
1568
1569 const char *sPtr = fData->c_str ();
1570
1571 uint32 index = 0;
1572
1573 while (char c = sPtr [index])
1574 {
1575
1576 if (c == oldChar)
1577 {
1578 break;
1579 }
1580
1581 index++;
1582
1583 }
1584
1585 // Did we find one?
1586
1587 if (sPtr [index])
1588 {
1589
1590 // Allocate new copy.
1591
1592 dng_std_string * newData (new dng_std_string (*fData));
1593
1594 // Start fixing at index of first character to change.
1595
1596 while (char c = sPtr [index])
1597 {
1598
1599 if (c == oldChar)
1600 {
1601 (*newData) [index] = newChar;
1602 }
1603
1604 index++;
1605
1606 }
1607
1608 // Swap in new copy.
1609
1610 fData.reset (newData);
1611
1612 }
1613
1614 }
1615
1616 }
1617
1618 /*****************************************************************************/
1619
1620 bool dng_string::TrimLeading (const char *s,
1621 bool case_sensitive)
1622 {
1623
1624 if (StartsWith (s, case_sensitive))
1625 {
1626
1627 Set (Get () + strlenAsUint32 (s));
1628
1629 return true;
1630
1631 }
1632
1633 return false;
1634
1635 }
1636
1637 /*****************************************************************************/
1638
1639 void dng_string::Append (const char *s)
1640 {
1641
1642 if (*s)
1643 {
1644
1645 if (fData.get ())
1646 {
1647
1648 std::unique_ptr<dng_std_string> newData (new dng_std_string (*fData));
1649
1650 newData->append (s);
1651
1652 fData.reset (newData.release ());
1653
1654 }
1655
1656 else
1657 {
1658
1659 Set (s);
1660
1661 }
1662
1663 }
1664
1665 }
1666
1667 /*****************************************************************************/
1668
1669 void dng_string::SetUppercase ()
1670 {
1671
1672 if (fData.get ())
1673 {
1674
1675 // Find index of first character that needs to be changed.
1676
1677 const char *sPtr = fData->c_str ();
1678
1679 uint32 index = 0;
1680
1681 while (char c = sPtr [index])
1682 {
1683
1684 if (c >= 'a' && c <= 'z')
1685 {
1686 break;
1687 }
1688
1689 index++;
1690
1691 }
1692
1693 // Did we find one?
1694
1695 if (sPtr [index])
1696 {
1697
1698 // Allocate new copy.
1699
1700 dng_std_string * newData (new dng_std_string (*fData));
1701
1702 // Start fixing at index of first character to change.
1703
1704 while (char c = sPtr [index])
1705 {
1706
1707 if (c >= 'a' && c <= 'z')
1708 {
1709 (*newData) [index] = c - 'a' + 'A';
1710 }
1711
1712 index++;
1713
1714 }
1715
1716 // Swap in new copy.
1717
1718 fData.reset (newData);
1719
1720 }
1721
1722 }
1723
1724 }
1725
1726 /*****************************************************************************/
1727
1728 void dng_string::SetLowercase ()
1729 {
1730
1731 if (fData.get ())
1732 {
1733
1734 // Find index of first character that needs to be changed.
1735
1736 const char *sPtr = fData->c_str ();
1737
1738 uint32 index = 0;
1739
1740 while (char c = sPtr [index])
1741 {
1742
1743 if (c >= 'A' && c <= 'Z')
1744 {
1745 break;
1746 }
1747
1748 index++;
1749
1750 }
1751
1752 // Did we find one?
1753
1754 if (sPtr [index])
1755 {
1756
1757 // Allocate new copy.
1758
1759 dng_std_string * newData (new dng_std_string (*fData));
1760
1761 // Start fixing at index of first character to change.
1762
1763 while (char c = sPtr [index])
1764 {
1765
1766 if (c >= 'A' && c <= 'Z')
1767 {
1768 (*newData) [index] = c - 'A' + 'a';
1769 }
1770
1771 index++;
1772
1773 }
1774
1775 // Swap in new copy.
1776
1777 fData.reset (newData);
1778
1779 }
1780
1781 }
1782
1783 }
1784
1785 /*****************************************************************************/
1786
1787 void dng_string::SetLineEndings (char ending)
1788 {
1789
1790 DNG_ASSERT (ending == '\n' || ending == '\r',
1791 "Unexpected line ending in dng_string::SetLineEndings");
1792
1793 if (fData.get ())
1794 {
1795
1796 char otherEnding = (ending == '\n' ? '\r' : '\n');
1797
1798 if (fData->find (otherEnding) != dng_std_string::npos)
1799 {
1800
1801 dng_std_string newData;
1802
1803 newData.reserve (fData->length ());
1804
1805 const char *sPtr = fData->c_str ();
1806
1807 while (*sPtr)
1808 {
1809
1810 char c = *(sPtr++);
1811
1812 char nc = sPtr [0];
1813
1814 if ((c == '\r' && nc == '\n') ||
1815 (c == '\n' && nc == '\r'))
1816 {
1817
1818 sPtr++;
1819
1820 if (ending)
1821 {
1822 newData.push_back (ending);
1823 }
1824
1825 }
1826
1827 else if (c == '\n' ||
1828 c == '\r')
1829 {
1830
1831 if (ending)
1832 {
1833 newData.push_back (ending);
1834 }
1835
1836 }
1837
1838 else
1839 {
1840
1841 newData.push_back (c);
1842
1843 }
1844
1845 }
1846
1847 Set (newData.c_str ());
1848
1849 }
1850
1851 }
1852
1853 }
1854
1855 /*****************************************************************************/
1856
1857 void dng_string::StripLowASCII ()
1858 {
1859
1860 if (fData.get ())
1861 {
1862
1863 bool hasLowASCII = false;
1864
1865 const char *sPtr = fData->c_str ();
1866
1867 while (*sPtr)
1868 {
1869
1870 char c = *(sPtr++);
1871
1872 if (!(c == '\r' || c == '\n' || (uint8) c >= ' '))
1873 {
1874 hasLowASCII = true;
1875 break;
1876 }
1877
1878 }
1879
1880 if (hasLowASCII)
1881 {
1882
1883 dng_std_string newData;
1884
1885 newData.reserve (fData->length ());
1886
1887 sPtr = fData->c_str ();
1888
1889 while (*sPtr)
1890 {
1891
1892 char c = *(sPtr++);
1893
1894 if (c == '\r' || c == '\n' || (uint8) c >= ' ')
1895 {
1896
1897 newData.push_back (c);
1898
1899 }
1900
1901 }
1902
1903 Set (newData.c_str ());
1904
1905 }
1906
1907 }
1908
1909 }
1910
1911 /*****************************************************************************/
1912
1913 void dng_string::NormalizeAsCommaSeparatedNumbers ()
1914 {
1915
1916 if (fData.get ())
1917 {
1918
1919 dng_std_string newData;
1920
1921 newData.reserve (fData->length ());
1922
1923 const char *sPtr = fData->c_str ();
1924
1925 bool commaInserted = false;
1926
1927 while (*sPtr)
1928 {
1929
1930 uint32 c = DecodeUTF8 (sPtr);
1931
1932 // Support number formats such as "3", "+3.0", "-3.1416", "314.16e-2",
1933 // "0.31416E1", but no hex/octal number representations.
1934
1935 if (isdigit ((int) c) || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E')
1936 {
1937
1938 newData.push_back ((char) c);
1939
1940 if (commaInserted)
1941 {
1942
1943 commaInserted = false;
1944
1945 }
1946
1947 }
1948
1949 else if (!commaInserted)
1950 {
1951
1952 newData.push_back (',');
1953
1954 commaInserted = true;
1955
1956 }
1957
1958 }
1959
1960 Set (newData.c_str ());
1961
1962 }
1963
1964 }
1965
1966 /******************************************************************************/
1967
1968 // Unicode to low-ASCII strings table.
1969
1970 struct UnicodeToLowASCIIEntry
1971 {
1972 uint32 unicode;
1973 const char *ascii;
1974 };
1975
1976 static const UnicodeToLowASCIIEntry kUnicodeToLowASCII [] =
1977 {
1978 { 0x00A0, " " },
1979 { 0x00A1, "!" },
1980 { 0x00A9, "(C)" },
1981 { 0x00AA, "a" },
1982 { 0x00AB, "<<" },
1983 { 0x00AC, "!" },
1984 { 0x00AE, "(R)" },
1985 { 0x00B0, "dg" },
1986 { 0x00B1, "+-" },
1987 { 0x00B7, "." },
1988 { 0x00BA, "o" },
1989 { 0x00BB, ">>" },
1990 { 0x00BF, "?" },
1991 { 0x00C0, "A" },
1992 { 0x00C1, "A" },
1993 { 0x00C2, "A" },
1994 { 0x00C3, "A" },
1995 { 0x00C4, "A" },
1996 { 0x00C5, "A" },
1997 { 0x00C6, "AE" },
1998 { 0x00C7, "C" },
1999 { 0x00C8, "E" },
2000 { 0x00C9, "E" },
2001 { 0x00CA, "E" },
2002 { 0x00CB, "E" },
2003 { 0x00CC, "I" },
2004 { 0x00CD, "I" },
2005 { 0x00CE, "I" },
2006 { 0x00CF, "I" },
2007 { 0x00D1, "N" },
2008 { 0x00D2, "O" },
2009 { 0x00D3, "O" },
2010 { 0x00D4, "O" },
2011 { 0x00D5, "O" },
2012 { 0x00D6, "O" },
2013 { 0x00D8, "O" },
2014 { 0x00D9, "U" },
2015 { 0x00DA, "U" },
2016 { 0x00DB, "U" },
2017 { 0x00DC, "U" },
2018 { 0x00DD, "Y" },
2019 { 0x00E0, "a" },
2020 { 0x00E1, "a" },
2021 { 0x00E2, "a" },
2022 { 0x00E3, "a" },
2023 { 0x00E4, "a" },
2024 { 0x00E5, "a" },
2025 { 0x00E6, "ae" },
2026 { 0x00E7, "c" },
2027 { 0x00E8, "e" },
2028 { 0x00E9, "e" },
2029 { 0x00EA, "e" },
2030 { 0x00EB, "e" },
2031 { 0x00EC, "i" },
2032 { 0x00ED, "i" },
2033 { 0x00EE, "i" },
2034 { 0x00EF, "i" },
2035 { 0x00F1, "n" },
2036 { 0x00F2, "o" },
2037 { 0x00F3, "o" },
2038 { 0x00F4, "o" },
2039 { 0x00F5, "o" },
2040 { 0x00F6, "o" },
2041 { 0x00F7, "/" },
2042 { 0x00F8, "o" },
2043 { 0x00F9, "u" },
2044 { 0x00FA, "u" },
2045 { 0x00FB, "u" },
2046 { 0x00FC, "u" },
2047 { 0x00FD, "y" },
2048 { 0x00FF, "y" },
2049 { 0x0131, "i" },
2050 { 0x0152, "OE" },
2051 { 0x0153, "oe" },
2052 { 0x0178, "Y" },
2053 { 0x2013, "-" },
2054 { 0x2014, "-" },
2055 { 0x2018, "'" },
2056 { 0x2019, "'" },
2057 { 0x201A, "," },
2058 { 0x201C, "\"" },
2059 { 0x201D, "\"" },
2060 { 0x201E, ",," },
2061 { 0x2022, "." },
2062 { 0x2026, "..." },
2063 { 0x2039, "<" },
2064 { 0x203A, ">" },
2065 { 0x2044, "/" },
2066 { 0x2122, "TM" },
2067 { 0x2206, "d" },
2068 { 0x2211, "S" },
2069 { 0x2260, "!=" },
2070 { 0x2264, "<=" },
2071 { 0x2265, ">=" },
2072 { 0x2318, "#" },
2073 { 0xFB01, "fi" },
2074 { 0xFB02, "fl" }
2075 };
2076
2077 /******************************************************************************/
2078
2079 void dng_string::ForceASCII ()
2080 {
2081
2082 if (!IsASCII ())
2083 {
2084
2085 dng_safe_uint32 tempBufferSize = dng_safe_uint32 (Length ()) * 3u;
2086
2087 dng_std_string buffer;
2088
2089 buffer.reserve (tempBufferSize.Get ());
2090
2091 const char *sPtr = Get ();
2092
2093 while (*sPtr)
2094 {
2095
2096 uint32 x = DecodeUTF8 (sPtr);
2097
2098 if (x <= 0x007F)
2099 {
2100
2101 buffer.push_back ((char) x);
2102
2103 }
2104
2105 else
2106 {
2107
2108 const char *ascii = NULL;
2109
2110 const uint32 kTableEntrys = sizeof (kUnicodeToLowASCII ) /
2111 sizeof (kUnicodeToLowASCII [0]);
2112
2113 for (uint32 entry = 0; entry < kTableEntrys; entry++)
2114 {
2115
2116 if (kUnicodeToLowASCII [entry] . unicode == x)
2117 {
2118
2119 ascii = kUnicodeToLowASCII [entry] . ascii;
2120
2121 break;
2122
2123 }
2124
2125 }
2126
2127 if (ascii)
2128 {
2129
2130 buffer.append (ascii);
2131
2132 }
2133
2134 else
2135 {
2136
2137 buffer.push_back ('?');
2138
2139 }
2140
2141 }
2142
2143 }
2144
2145 Set (buffer.c_str ());
2146
2147 }
2148
2149 }
2150
2151 /******************************************************************************/
2152
2153 #if qMacOS
2154 static dng_std_mutex gProtectUCCalls;
2155 #endif
2156
2157 /******************************************************************************/
2158
2159 int32 dng_string::Compare (const dng_string &s,
2160 bool digitsAsNumber) const
2161 {
2162
2163 #if qMacOS
2164
2165 {
2166
2167 dng_memory_data aStrA;
2168 dng_memory_data aStrB;
2169
2170 uint32 aLenA = this->Get_UTF16 (aStrA);
2171 uint32 aLenB = s .Get_UTF16 (aStrB);
2172
2173 if (aLenA > 0)
2174 {
2175
2176 if (aLenB > 0)
2177 {
2178
2179 // For some Mac OS versions anyway, UCCompareTextDefault is not
2180 // thread safe.
2181
2182 dng_lock_std_mutex lockMutex (gProtectUCCalls);
2183
2184 UCCollateOptions aOptions = kUCCollateStandardOptions |
2185 kUCCollatePunctuationSignificantMask;
2186
2187 if (digitsAsNumber)
2188 {
2189
2190 aOptions |= kUCCollateDigitsOverrideMask |
2191 kUCCollateDigitsAsNumberMask;
2192
2193 }
2194
2195 SInt32 aOrder = -1;
2196
2197 Boolean aEqual = false;
2198
2199 OSStatus searchStatus = ::UCCompareTextDefault (aOptions,
2200 aStrA.Buffer_uint16 (),
2201 aLenA,
2202 aStrB.Buffer_uint16 (),
2203 aLenB,
2204 &aEqual,
2205 &aOrder);
2206
2207 if (searchStatus == noErr)
2208 {
2209
2210 if (aEqual || (aOrder == 0))
2211 {
2212 return 0;
2213 }
2214
2215 else
2216 {
2217 return (aOrder > 0) ? 1 : -1;
2218 }
2219
2220 }
2221
2222 else
2223 {
2224
2225 DNG_REPORT ("UCCompareTextDefault failed");
2226
2227 return -1;
2228
2229 }
2230
2231 }
2232
2233 else
2234 {
2235 return 1;
2236 }
2237
2238 }
2239
2240 else
2241 {
2242
2243 if (aLenB > 0)
2244 {
2245 return -1;
2246 }
2247
2248 else
2249 {
2250 return 0;
2251 }
2252
2253 }
2254
2255 }
2256
2257 #elif qWinOS
2258
2259 {
2260
2261 dng_memory_data aStrA;
2262 dng_memory_data aStrB;
2263
2264 uint32 aLenA = this->Get_UTF16 (aStrA);
2265 uint32 aLenB = s .Get_UTF16 (aStrB);
2266
2267 if (aLenA > 0)
2268 {
2269
2270 if (aLenB > 0)
2271 {
2272
2273 LCID locale = LOCALE_SYSTEM_DEFAULT;
2274
2275 DWORD aFlags = NORM_IGNOREWIDTH;
2276
2277 if (digitsAsNumber)
2278 {
2279 aFlags |= SORT_DIGITSASNUMBERS;
2280 }
2281
2282 int aOrder = ::CompareStringW (locale,
2283 aFlags,
2284 (const WCHAR *) aStrA.Buffer_uint16 (),
2285 aLenA,
2286 (const WCHAR *) aStrB.Buffer_uint16 (),
2287 aLenB);
2288
2289 if (aOrder == CSTR_EQUAL)
2290 {
2291 return 0;
2292 }
2293
2294 else if (aOrder == CSTR_GREATER_THAN)
2295 {
2296 return 1;
2297 }
2298
2299 else
2300 {
2301 return -1;
2302 }
2303
2304 }
2305
2306 else
2307 {
2308 return 1;
2309 }
2310
2311 }
2312
2313 else
2314 {
2315
2316 if (aLenB > 0)
2317 {
2318 return -1;
2319 }
2320 else
2321 {
2322 return 0;
2323 }
2324
2325 }
2326
2327 }
2328
2329 #else
2330
2331 // Fallback to a pure Unicode sort order.
2332
2333 {
2334
2335 for (uint32 pass = 0; pass < 2; pass++)
2336 {
2337
2338 const char *aPtr = Get ();
2339 const char *bPtr = s.Get ();
2340
2341 while (*aPtr || *bPtr)
2342 {
2343
2344 if (!bPtr)
2345 {
2346 return 1;
2347 }
2348
2349 else if (!aPtr)
2350 {
2351 return -1;
2352 }
2353
2354 uint32 a = DecodeUTF8 (aPtr);
2355 uint32 b = DecodeUTF8 (bPtr);
2356
2357 // Ignore case on first compare pass.
2358
2359 if (pass == 0)
2360 {
2361
2362 if (a >= (uint32) 'a' && a <= (uint32) 'z')
2363 {
2364 a = a - (uint32) 'a' + (uint32) 'A';
2365 }
2366
2367 if (b >= (uint32) 'a' && b <= (uint32) 'z')
2368 {
2369 b = b - (uint32) 'a' + (uint32) 'A';
2370 }
2371
2372 }
2373
2374 if (digitsAsNumber)
2375 {
2376
2377 uint32 aNumber = 0;
2378 uint32 aDigits = 0;
2379
2380 if (a >= (uint32) '0' && a <= (uint32) '9')
2381 {
2382
2383 aNumber = a - (uint32) '0';
2384 aDigits = 1;
2385
2386 while (aDigits < 6 && *aPtr >= '0' && *aPtr <= '9')
2387 {
2388 aNumber = aNumber * 10 + ((uint32) *aPtr -
2389 (uint32) '0');
2390 aDigits++;
2391 aPtr++;
2392 }
2393
2394 }
2395
2396 uint32 bNumber = 0;
2397 uint32 bDigits = 0;
2398
2399 if (b >= (uint32) '0' && b <= (uint32) '9')
2400 {
2401
2402 bNumber = b - (uint32) '0';
2403 bDigits = 1;
2404
2405 while (bDigits < 6 && *bPtr >= '0' && *bPtr <= '9')
2406 {
2407 bNumber = bNumber * 10 + ((uint32) *bPtr -
2408 (uint32) '0');
2409 bDigits++;
2410 bPtr++;
2411 }
2412
2413 }
2414
2415 if (aDigits > 0 && bDigits > 0)
2416 {
2417
2418 if (aNumber > bNumber)
2419 {
2420 return 1;
2421 }
2422
2423 if (aNumber < bNumber)
2424 {
2425 return -1;
2426 }
2427
2428 if (aDigits > bDigits)
2429 {
2430 return 1;
2431 }
2432
2433 if (aDigits < bDigits)
2434 {
2435 return -1;
2436 }
2437
2438 continue;
2439
2440 }
2441
2442 }
2443
2444 if (a > b)
2445 {
2446 return 1;
2447 }
2448
2449 else if (a < b)
2450 {
2451 return -1;
2452 }
2453
2454 }
2455
2456 }
2457
2458 }
2459
2460 #endif
2461
2462 return 0;
2463
2464 }
2465
2466 /*****************************************************************************/
2467
2468 size_t dng_string_hash::operator () (const dng_string &s) const
2469 {
2470
2471 dng_md5_printer printer;
2472
2473 printer.Process (s.Get ());
2474
2475 auto digest = printer.Result ();
2476
2477 return (size_t) digest.Collapse32 ();
2478
2479 }
2480
2481 /*****************************************************************************/