comparison dwtx/dwtxhelper/mangoicu/UString.d @ 91:11e8159caf7a

make the mango icu fork work.
author Frank Benoit <benoit@tionex.de>
date Mon, 07 Jul 2008 15:53:07 +0200
parents 040da1cb0d76
children f05207c07a98
comparison
equal deleted inserted replaced
90:7ffeace6c47f 91:11e8159caf7a
80 All trademarks and registered trademarks mentioned herein are the 80 All trademarks and registered trademarks mentioned herein are the
81 property of their respective owners. 81 property of their respective owners.
82 82
83 *******************************************************************************/ 83 *******************************************************************************/
84 84
85 module dwtx.dwthelper.mangoicu.UString; 85 module dwtx.dwtxhelper.mangoicu.UString;
86 86
87 private import dwtx.dwthelper.mangoicu.ICU, 87 private import dwtx.dwtxhelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.UChar, 88 dwtx.dwtxhelper.mangoicu.UChar,
89 dwtx.dwthelper.mangoicu.ULocale; 89 dwtx.dwtxhelper.mangoicu.ULocale;
90 90
91 /******************************************************************************* 91 /*******************************************************************************
92 92
93 *******************************************************************************/ 93 *******************************************************************************/
94 94
107 private interface ITextOther {} 107 private interface ITextOther {}
108 private interface IStringOther {} 108 private interface IStringOther {}
109 } 109 }
110 else 110 else
111 { 111 {
112 private import dwtx.dwthelper.mangoicu.UMango; 112 private import dwtx.dwtxhelper.mangoicu.UMango;
113 113
114 private import mango.io.model.IReader, 114 private import mango.io.model.IReader,
115 mango.io.model.IWriter; 115 mango.io.model.IWriter;
116 116
117 private interface ITextOther : IWritable {} 117 private interface ITextOther : IWritable {}
153 as if they pointed to an empty string. However, this is not the case 153 as if they pointed to an empty string. However, this is not the case
154 for char* parameters for charset names or other IDs. 154 for char* parameters for charset names or other IDs.
155 155
156 *******************************************************************************/ 156 *******************************************************************************/
157 157
158 class UString : UText, IStringOther 158 class UString : UStringView, IStringOther
159 { 159 {
160 alias opCat append; 160 alias opCat append;
161 alias opIndexAssign setCharAt; 161 alias opIndexAssign setCharAt;
162 162
163 /*********************************************************************** 163 /***********************************************************************
186 setTo (content, mutable); 186 setTo (content, mutable);
187 } 187 }
188 188
189 /*********************************************************************** 189 /***********************************************************************
190 190
191 Create a UString via the content of a UText. Note that the 191 Create a UString via the content of a UStringView. Note that the
192 default is to assume the content is immutable (read-only). 192 default is to assume the content is immutable (read-only).
193 193
194 ***********************************************************************/ 194 ***********************************************************************/
195 195
196 this (UText other, bool mutable = false) 196 this (UStringView other, bool mutable = false)
197 { 197 {
198 this (other.get, mutable); 198 this (other.get, mutable);
199 } 199 }
200 200
201 /*********************************************************************** 201 /***********************************************************************
293 catch the case where a slice starts at offset 0, 293 catch the case where a slice starts at offset 0,
294 which is where IBuffer slices may come from. 294 which is where IBuffer slices may come from.
295 295
296 To be safe, we ask the allocator in use whether 296 To be safe, we ask the allocator in use whether
297 the content it provided can be mutated or not. 297 the content it provided can be mutated or not.
298 Note that this is not necessary for UText, since 298 Note that this is not necessary for UStringView, since
299 that is a read-only construct. 299 that is a read-only construct.
300 300
301 ***************************************************************/ 301 ***************************************************************/
302 302
303 void read (IReader r) 303 void read (IReader r)
354 354
355 Append text to this UString 355 Append text to this UString
356 356
357 ***********************************************************************/ 357 ***********************************************************************/
358 358
359 UString opCat (UText other) 359 UString opCat (UStringView other)
360 { 360 {
361 return opCat (other.get); 361 return opCat (other.get);
362 } 362 }
363 363
364 /*********************************************************************** 364 /***********************************************************************
365 365
366 Append partial text to this UString 366 Append partial text to this UString
367 367
368 ***********************************************************************/ 368 ***********************************************************************/
369 369
370 UString opCat (UText other, uint start, uint len=uint.max) 370 UString opCat (UStringView other, uint start, uint len=uint.max)
371 { 371 {
372 other.pinIndices (start, len); 372 other.pinIndices (start, len);
373 return opCat (other.content [start..start+len]); 373 return opCat (other.content [start..start+len]);
374 } 374 }
375 375
401 401
402 ***********************************************************************/ 402 ***********************************************************************/
403 403
404 UString opCat (char[] chars) 404 UString opCat (char[] chars)
405 { 405 {
406 uint fmt (wchar* dst, uint len, inout Error e) 406 uint fmt (wchar* dst, uint len, inout UErrorCode e)
407 { 407 {
408 uint x; 408 uint x;
409 409
410 u_strFromUTF8 (dst, len, &x, chars.ptr, chars.length, e); 410 u_strFromUTF8 (dst, len, &x, chars.ptr, chars.length, e);
411 return x; 411 return x;
457 heap-space for the content until it is modified via one of 457 heap-space for the content until it is modified via one of
458 these methods. 458 these methods.
459 459
460 ***********************************************************************/ 460 ***********************************************************************/
461 461
462 UString setTo (UText other, bool mutable = true) 462 UString setTo (UStringView other, bool mutable = true)
463 { 463 {
464 return setTo (other.get, mutable); 464 return setTo (other.get, mutable);
465 } 465 }
466 466
467 /*********************************************************************** 467 /***********************************************************************
472 heap-space for the content until it is modified via one of 472 heap-space for the content until it is modified via one of
473 these methods. 473 these methods.
474 474
475 ***********************************************************************/ 475 ***********************************************************************/
476 476
477 UString setTo (UText other, uint start, uint len, bool mutable = true) 477 UString setTo (UStringView other, uint start, uint len, bool mutable = true)
478 { 478 {
479 other.pinIndices (start, len); 479 other.pinIndices (start, len);
480 return setTo (other.content [start..start+len], mutable); 480 return setTo (other.content [start..start+len], mutable);
481 } 481 }
482 482
619 This is used by many of the ICU wrappers to append content 619 This is used by many of the ICU wrappers to append content
620 into a UString. 620 into a UString.
621 621
622 ***********************************************************************/ 622 ***********************************************************************/
623 623
624 typedef uint delegate (wchar* dst, uint len, inout Error e) Formatter; 624 typedef uint delegate (wchar* dst, uint len, inout UErrorCode e) Formatter;
625 625
626 package final UString format (Formatter format, char[] msg) 626 package final UString format (Formatter format, char[] msg)
627 { 627 {
628 Error e; 628 UErrorCode e;
629 uint length; 629 uint length;
630 630
631 while (true) 631 while (true)
632 { 632 {
633 e = e.OK; 633 e = e.OK;
651 651
652 Immutable (read-only) text -- use UString for mutable strings. 652 Immutable (read-only) text -- use UString for mutable strings.
653 653
654 *******************************************************************************/ 654 *******************************************************************************/
655 655
656 class UText : ICU, ITextOther 656 class UStringView : ICU, ITextOther
657 { 657 {
658 alias opIndex charAt; 658 alias opIndex charAt;
659 659
660 // the core of the UText and UString attributes. The name 'len' 660 // the core of the UStringView and UString attributes. The name 'len'
661 // is used rather than the more obvious 'length' since there is 661 // is used rather than the more obvious 'length' since there is
662 // a collision with the silly array[length] syntactic sugar ... 662 // a collision with the silly array[length] syntactic sugar ...
663 package uint len; 663 package uint len;
664 package wchar[] content; 664 package wchar[] content;
665 665
666 // this should probably be in UString only, but there seems to 666 // this should probably be in UString only, but there seems to
667 // be a compiler bug where it doesn't get initialised correctly, 667 // be a compiler bug where it doesn't get initialised correctly,
668 // and it's perhaps useful to have here for when a UString is 668 // and it's perhaps useful to have here for when a UString is
669 // passed as a UText argument. 669 // passed as a UStringView argument.
670 private bool mutable; 670 private bool mutable;
671 671
672 // toFolded() argument 672 // toFolded() argument
673 public enum CaseOption 673 public enum CaseOption
674 { 674 {
713 } 713 }
714 } 714 }
715 715
716 /*********************************************************************** 716 /***********************************************************************
717 717
718 Return the valid content from this UText 718 Return the valid content from this UStringView
719 719
720 ***********************************************************************/ 720 ***********************************************************************/
721 721
722 final package wchar[] get () 722 final package wchar[] get ()
723 { 723 {
724 return content [0..len]; 724 return content [0..len];
725 } 725 }
726 726
727 /*********************************************************************** 727 /***********************************************************************
728 728
729 Is this UText equal to another? 729 Is this UStringView equal to another?
730 730
731 ***********************************************************************/ 731 ***********************************************************************/
732 732
733 final override int opEquals (Object o) 733 final override int opEquals (Object o)
734 { 734 {
735 UText other = cast(UText) o; 735 UStringView other = cast(UStringView) o;
736 736
737 if (other) 737 if (other)
738 return (other is this || compare (other) == 0); 738 return (other is this || compare (other) == 0);
739 return 0; 739 return 0;
740 } 740 }
741 741
742 /*********************************************************************** 742 /***********************************************************************
743 743
744 Compare this UText to another. 744 Compare this UStringView to another.
745 745
746 ***********************************************************************/ 746 ***********************************************************************/
747 747
748 final override int opCmp (Object o) 748 final override int opCmp (Object o)
749 { 749 {
750 UText other = cast(UText) o; 750 UStringView other = cast(UStringView) o;
751 751
752 if (other is this) 752 if (other is this)
753 return 0; 753 return 0;
754 else 754 else
755 if (other) 755 if (other)
757 return 1; 757 return 1;
758 } 758 }
759 759
760 /*********************************************************************** 760 /***********************************************************************
761 761
762 Hash this UText 762 Hash this UStringView
763 763
764 ***********************************************************************/ 764 ***********************************************************************/
765 765
766 final override uint toHash () 766 final override uint toHash ()
767 { 767 {
768 return typeid(wchar[]).getHash (&content[0..len]); 768 return typeid(wchar[]).getHash (&content[0..len]);
769 } 769 }
770 770
771 /*********************************************************************** 771 /***********************************************************************
772 772
773 Clone this UText into a UString 773 Clone this UStringView into a UString
774 774
775 ***********************************************************************/ 775 ***********************************************************************/
776 776
777 final UString copy () 777 final UString copy ()
778 { 778 {
779 return new UString (content); 779 return new UString (content);
780 } 780 }
781 781
782 /*********************************************************************** 782 /***********************************************************************
783 783
784 Clone a section of this UText into a UString 784 Clone a section of this UStringView into a UString
785 785
786 ***********************************************************************/ 786 ***********************************************************************/
787 787
788 final UString extract (uint start, uint len=uint.max) 788 final UString extract (uint start, uint len=uint.max)
789 { 789 {
857 supplementary code points because they are stored as 857 supplementary code points because they are stored as
858 pairs of surrogates which are at U+d800..U+dfff. 858 pairs of surrogates which are at U+d800..U+dfff.
859 859
860 ***********************************************************************/ 860 ***********************************************************************/
861 861
862 final int compare (UText other, bool codePointOrder=false) 862 final int compare (UStringView other, bool codePointOrder=false)
863 { 863 {
864 return compare (other.get, codePointOrder); 864 return compare (other.get, codePointOrder);
865 } 865 }
866 866
867 /*********************************************************************** 867 /***********************************************************************
893 supplementary code points because they are stored as 893 supplementary code points because they are stored as
894 pairs of surrogates which are at U+d800..U+dfff. 894 pairs of surrogates which are at U+d800..U+dfff.
895 895
896 ***********************************************************************/ 896 ***********************************************************************/
897 897
898 final int compareFolded (UText other, CaseOption option = CaseOption.Default) 898 final int compareFolded (UStringView other, CaseOption option = CaseOption.Default)
899 { 899 {
900 return compareFolded (other.content, option); 900 return compareFolded (other.content, option);
901 } 901 }
902 902
903 /*********************************************************************** 903 /***********************************************************************
918 return compareFolded (get, other, option); 918 return compareFolded (get, other, option);
919 } 919 }
920 920
921 /*********************************************************************** 921 /***********************************************************************
922 922
923 Does this UText start with specified string? 923 Does this UStringView start with specified string?
924 924
925 ***********************************************************************/ 925 ***********************************************************************/
926 926
927 final bool startsWith (UText other) 927 final bool startsWith (UStringView other)
928 { 928 {
929 return startsWith (other.get); 929 return startsWith (other.get);
930 } 930 }
931 931
932 /*********************************************************************** 932 /***********************************************************************
933 933
934 Does this UText start with specified string? 934 Does this UStringView start with specified string?
935 935
936 ***********************************************************************/ 936 ***********************************************************************/
937 937
938 final bool startsWith (wchar[] chars) 938 final bool startsWith (wchar[] chars)
939 { 939 {
942 return false; 942 return false;
943 } 943 }
944 944
945 /*********************************************************************** 945 /***********************************************************************
946 946
947 Does this UText end with specified string? 947 Does this UStringView end with specified string?
948 948
949 ***********************************************************************/ 949 ***********************************************************************/
950 950
951 final bool endsWith (UText other) 951 final bool endsWith (UStringView other)
952 { 952 {
953 return endsWith (other.get); 953 return endsWith (other.get);
954 } 954 }
955 955
956 /*********************************************************************** 956 /***********************************************************************
957 957
958 Does this UText end with specified string? 958 Does this UStringView end with specified string?
959 959
960 ***********************************************************************/ 960 ***********************************************************************/
961 961
962 final bool endsWith (wchar[] chars) 962 final bool endsWith (wchar[] chars)
963 { 963 {
993 surrogates stand alone in the text. Otherwise, the substring 993 surrogates stand alone in the text. Otherwise, the substring
994 edge units would be matched against halves of surrogate pairs. 994 edge units would be matched against halves of surrogate pairs.
995 995
996 ***********************************************************************/ 996 ***********************************************************************/
997 997
998 final uint indexOf (UText other, uint start=0) 998 final uint indexOf (UStringView other, uint start=0)
999 { 999 {
1000 return indexOf (other.get, start); 1000 return indexOf (other.get, start);
1001 } 1001 }
1002 1002
1003 /*********************************************************************** 1003 /***********************************************************************
1044 A surrogate code point is found only if its match in the 1044 A surrogate code point is found only if its match in the
1045 text is not part of a surrogate pair. 1045 text is not part of a surrogate pair.
1046 1046
1047 ***********************************************************************/ 1047 ***********************************************************************/
1048 1048
1049 final uint lastIndexOf (UText other, uint start=uint.max) 1049 final uint lastIndexOf (UStringView other, uint start=uint.max)
1050 { 1050 {
1051 return lastIndexOf (other.get, start); 1051 return lastIndexOf (other.get, start);
1052 } 1052 }
1053 1053
1054 /*********************************************************************** 1054 /***********************************************************************
1101 1101
1102 ***********************************************************************/ 1102 ***********************************************************************/
1103 1103
1104 final UString toLower (UString dst, inout ULocale locale) 1104 final UString toLower (UString dst, inout ULocale locale)
1105 { 1105 {
1106 uint lower (wchar* dst, uint length, inout Error e) 1106 uint lower (wchar* dst, uint length, inout UErrorCode e)
1107 { 1107 {
1108 return u_strToLower (dst, length, content.ptr, len, ICU.toString(locale.name), e); 1108 return u_strToLower (dst, length, content.ptr, len, ICU.toString(locale.name), e);
1109 } 1109 }
1110 1110
1111 dst.expand (len + 32); 1111 dst.expand (len + 32);
1141 1141
1142 ***********************************************************************/ 1142 ***********************************************************************/
1143 1143
1144 final UString toUpper (UString dst, inout ULocale locale) 1144 final UString toUpper (UString dst, inout ULocale locale)
1145 { 1145 {
1146 uint upper (wchar* dst, uint length, inout Error e) 1146 uint upper (wchar* dst, uint length, inout UErrorCode e)
1147 { 1147 {
1148 return u_strToUpper (dst, length, content.ptr, len, ICU.toString(locale.name), e); 1148 return u_strToUpper (dst, length, content.ptr, len, ICU.toString(locale.name), e);
1149 } 1149 }
1150 1150
1151 dst.expand (len + 32); 1151 dst.expand (len + 32);
1167 1167
1168 ***********************************************************************/ 1168 ***********************************************************************/
1169 1169
1170 final UString toFolded (UString dst, CaseOption option = CaseOption.Default) 1170 final UString toFolded (UString dst, CaseOption option = CaseOption.Default)
1171 { 1171 {
1172 uint fold (wchar* dst, uint length, inout Error e) 1172 uint fold (wchar* dst, uint length, inout UErrorCode e)
1173 { 1173 {
1174 return u_strFoldCase (dst, length, content.ptr, len, option, e); 1174 return u_strFoldCase (dst, length, content.ptr, len, option, e);
1175 } 1175 }
1176 1176
1177 dst.expand (len + 32); 1177 dst.expand (len + 32);
1193 ***********************************************************************/ 1193 ***********************************************************************/
1194 1194
1195 final char[] toUtf8 (char[] dst = null) 1195 final char[] toUtf8 (char[] dst = null)
1196 { 1196 {
1197 uint x; 1197 uint x;
1198 Error e; 1198 UErrorCode e;
1199 1199
1200 if (! cast(char*) dst) 1200 if (! cast(char*) dst)
1201 dst = new char[len * 4]; 1201 dst = new char[len * 4];
1202 1202
1203 u_strToUTF8 (dst.ptr, dst.length, &x, content.ptr, len, e); 1203 u_strToUTF8 (dst.ptr, dst.length, &x, content.ptr, len, e);
1205 return dst [0..x]; 1205 return dst [0..x];
1206 } 1206 }
1207 1207
1208 /*********************************************************************** 1208 /***********************************************************************
1209 1209
1210 Remove leading and trailing whitespace from this UText. 1210 Remove leading and trailing whitespace from this UStringView.
1211 Note that we slice the content to remove leading space. 1211 Note that we slice the content to remove leading space.
1212 1212
1213 ***********************************************************************/ 1213 ***********************************************************************/
1214 1214
1215 UText trim () 1215 UStringView trim ()
1216 { 1216 {
1217 wchar c; 1217 wchar c;
1218 uint i = len; 1218 uint i = len;
1219 1219
1220 // cut off trailing white space 1220 // cut off trailing white space
1408 1408
1409 ***********************************************************************/ 1409 ***********************************************************************/
1410 1410
1411 final private int compareFolded (wchar[] s1, wchar[] s2, CaseOption option = CaseOption.Default) 1411 final private int compareFolded (wchar[] s1, wchar[] s2, CaseOption option = CaseOption.Default)
1412 { 1412 {
1413 Error e; 1413 UErrorCode e;
1414 1414
1415 int x = u_strCaseCompare (s1.ptr, s1.length, s2.ptr, s2.length, option, e); 1415 int x = u_strCaseCompare (s1.ptr, s1.length, s2.ptr, s2.length, option, e);
1416 testError (e, "compareFolded failed"); 1416 testError (e, "compareFolded failed");
1417 return x; 1417 return x;
1418 } 1418 }
1437 wchar* function (wchar*, uint, wchar*, uint) u_strFindFirst; 1437 wchar* function (wchar*, uint, wchar*, uint) u_strFindFirst;
1438 wchar* function (wchar*, uint, wchar*, uint) u_strFindLast; 1438 wchar* function (wchar*, uint, wchar*, uint) u_strFindLast;
1439 wchar* function (wchar*, wchar, uint) u_memchr; 1439 wchar* function (wchar*, wchar, uint) u_memchr;
1440 wchar* function (wchar*, wchar, uint) u_memrchr; 1440 wchar* function (wchar*, wchar, uint) u_memrchr;
1441 int function (wchar*, uint, wchar*, uint, bool) u_strCompare; 1441 int function (wchar*, uint, wchar*, uint, bool) u_strCompare;
1442 int function (wchar*, uint, wchar*, uint, uint, inout Error) u_strCaseCompare; 1442 int function (wchar*, uint, wchar*, uint, uint, inout UErrorCode) u_strCaseCompare;
1443 dchar function (CharAt, uint*, uint, void*) u_unescapeAt; 1443 dchar function (CharAt, uint*, uint, void*) u_unescapeAt;
1444 uint function (wchar*, uint) u_countChar32; 1444 uint function (wchar*, uint) u_countChar32;
1445 uint function (wchar*, uint, wchar*, uint, char*, inout Error) u_strToUpper; 1445 uint function (wchar*, uint, wchar*, uint, char*, inout UErrorCode) u_strToUpper;
1446 uint function (wchar*, uint, wchar*, uint, char*, inout Error) u_strToLower; 1446 uint function (wchar*, uint, wchar*, uint, char*, inout UErrorCode) u_strToLower;
1447 uint function (wchar*, uint, wchar*, uint, uint, inout Error) u_strFoldCase; 1447 uint function (wchar*, uint, wchar*, uint, uint, inout UErrorCode) u_strFoldCase;
1448 wchar* function (wchar*, uint, uint*, char*, uint, inout Error) u_strFromUTF8; 1448 wchar* function (wchar*, uint, uint*, char*, uint, inout UErrorCode) u_strFromUTF8;
1449 char* function (char*, uint, uint*, wchar*, uint, inout Error) u_strToUTF8; 1449 char* function (char*, uint, uint*, wchar*, uint, inout UErrorCode) u_strToUTF8;
1450 } 1450 }
1451 1451
1452 /*********************************************************************** 1452 /***********************************************************************
1453 1453
1454 ***********************************************************************/ 1454 ***********************************************************************/