Mercurial > projects > dwt-win
annotate dwt/dwthelper/System.d @ 225:cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 19 May 2008 22:19:44 +0200 |
parents | ab60f3309436 |
children | d10ff1f47f84 |
rev | line source |
---|---|
23 | 1 /** |
0 | 2 * Authors: Frank Benoit <keinfarbton@googlemail.com> |
3 */ | |
4 module dwt.dwthelper.System; | |
5 | |
6 import tango.core.Exception; | |
84
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
7 import tango.time.Clock; |
33 | 8 import tango.stdc.stdlib : exit; |
0 | 9 |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
197
diff
changeset
|
10 import dwt.dwthelper.utils; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
197
diff
changeset
|
11 |
0 | 12 template SimpleType(T) { |
13 debug{ | |
14 static void validCheck(uint SrcLen, uint DestLen, uint copyLen){ | |
15 if(SrcLen < copyLen || DestLen < copyLen|| SrcLen < 0 || DestLen < 0){ | |
23 | 16 //Util.trace("Error : SimpleType.arraycopy(), out of bounds."); |
0 | 17 assert(0); |
18 } | |
19 } | |
20 } | |
21 | |
22 static void remove(inout T[] items, int index) { | |
23 if(items.length == 0) | |
24 return; | |
25 | |
26 if(index < 0 || index >= items.length){ | |
27 throw new ArrayBoundsException(__FILE__, __LINE__); | |
28 } | |
29 | |
30 T element = items[index]; | |
31 | |
32 int length = items.length; | |
33 if(length == 1){ | |
34 items.length = 0; | |
35 return;// element; | |
36 } | |
37 | |
38 if(index == 0) | |
39 items = items[1 .. $]; | |
40 else if(index == length - 1) | |
41 items = items[0 .. index]; | |
42 else | |
43 items = items[0 .. index] ~ items[index + 1 .. $]; | |
44 } | |
45 | |
46 static void insert(inout T[] items, T item, int index = -1) { | |
47 if(index == -1) | |
48 index = items.length; | |
49 | |
50 if(index < 0 || index > items.length ){ | |
51 throw new ArrayBoundsException(__FILE__, __LINE__); | |
52 } | |
53 | |
54 if(index == items.length){ | |
55 items ~= item; | |
56 }else if(index == 0){ | |
57 T[] newVect; | |
58 newVect ~= item; | |
59 items = newVect ~ items; | |
60 }else if(index < items.length ){ | |
61 T[] arr1 = items[0 .. index]; | |
62 T[] arr2 = items[index .. $]; | |
63 | |
64 // Important : if you write like the following commented, | |
65 // you get wrong data | |
66 // code: T[] arr1 = items[0..index]; | |
67 // T[] arr2 = items[index..$]; | |
68 // items = arr1 ~ item; // error, !!! | |
69 // items ~= arr2; // item replace the arrr2[0] here | |
70 items = arr1 ~ item ~ arr2; | |
71 } | |
72 } | |
73 | |
74 static void arraycopy(T[] src, uint srcPos, T[] dest, uint destPos, uint len) | |
75 { | |
76 if(len == 0) return; | |
77 | |
78 assert(src); | |
79 assert(dest); | |
80 debug{validCheck(src.length - srcPos, dest.length - destPos, len);} | |
81 | |
82 if(src is dest){ | |
225
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
83 if( destPos < srcPos ){ |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
84 for(int i=0; i<len; ++i){ |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
85 dest[destPos+i] = src[srcPos+i]; |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
86 } |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
87 } |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
88 else{ |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
89 for(int i=len-1; i>=0; --i){ |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
90 dest[destPos+i] = src[srcPos+i]; |
cba62ee36290
Fix bug in System.arraycopy when called for overlapping array ranges
Frank Benoit <benoit@tionex.de>
parents:
212
diff
changeset
|
91 } |
0 | 92 } |
93 }else{ | |
94 dest[destPos..(len+destPos)] = src[srcPos..(len+srcPos)]; | |
95 } | |
96 } | |
97 } | |
98 | |
99 | |
100 class System { | |
101 | |
102 alias SimpleType!(int).arraycopy arraycopy; | |
103 alias SimpleType!(byte).arraycopy arraycopy; | |
104 alias SimpleType!(double).arraycopy arraycopy; | |
105 alias SimpleType!(float).arraycopy arraycopy; | |
106 alias SimpleType!(short).arraycopy arraycopy; | |
107 alias SimpleType!(long).arraycopy arraycopy; | |
108 alias SimpleType!(uint).arraycopy arraycopy; | |
109 alias SimpleType!(ushort).arraycopy arraycopy; | |
110 alias SimpleType!(ubyte).arraycopy arraycopy; | |
111 alias SimpleType!(ulong).arraycopy arraycopy; | |
112 alias SimpleType!(char).arraycopy arraycopy; | |
113 alias SimpleType!(wchar).arraycopy arraycopy; | |
114 alias SimpleType!(Object).arraycopy arraycopy; | |
115 alias SimpleType!(void*).arraycopy arraycopy; | |
116 | |
117 alias SimpleType!(int[]).arraycopy arraycopy; | |
118 alias SimpleType!(byte[]).arraycopy arraycopy; | |
119 alias SimpleType!(double[]).arraycopy arraycopy; | |
120 alias SimpleType!(float[]).arraycopy arraycopy; | |
121 alias SimpleType!(short[]).arraycopy arraycopy; | |
122 alias SimpleType!(long[]).arraycopy arraycopy; | |
123 alias SimpleType!(uint[]).arraycopy arraycopy; | |
124 alias SimpleType!(ushort[]).arraycopy arraycopy; | |
125 alias SimpleType!(ubyte[]).arraycopy arraycopy; | |
126 alias SimpleType!(ulong[]).arraycopy arraycopy; | |
127 alias SimpleType!(char[]).arraycopy arraycopy; | |
128 alias SimpleType!(wchar[]).arraycopy arraycopy; | |
129 alias SimpleType!(Object[]).arraycopy arraycopy; | |
130 alias SimpleType!(void*[]).arraycopy arraycopy; | |
131 alias SimpleType!(void*[]).arraycopy arraycopy; | |
132 | |
133 static long currentTimeMillis(){ | |
84
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
33
diff
changeset
|
134 return Clock.now().ticks() / 10000; |
0 | 135 } |
136 | |
33 | 137 static void exit( int code ){ |
138 .exit(code); | |
139 } | |
197 | 140 public static int identityHashCode(Object x){ |
141 if( x is null ){ | |
142 return 0; | |
143 } | |
144 return (*cast(Object *)&x).toHash(); | |
145 } | |
33 | 146 |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
197
diff
changeset
|
147 public static String getProperty( String key ){ |
197 | 148 switch( key ){ |
149 case "os.name": return "windows"; | |
150 default: return null; | |
151 } | |
152 } | |
0 | 153 } |
154 |