4 * Part of the D programming language runtime library.
5 * Dynamic array property support routines
9 * Copyright (C) 2000-2006 by Digital Mars, www.digitalmars.com
10 * Written by Walter Bright
12 * This software is provided 'as-is', without any express or implied
13 * warranty. In no event will the authors be held liable for any damages
14 * arising from the use of this software.
16 * Permission is granted to anyone to use this software for any purpose,
17 * including commercial applications, and to alter it and redistribute it
18 * freely, in both source and binary form, subject to the following
21 * o The origin of this software must not be misrepresented; you must not
22 * claim that you wrote the original software. If you use this software
23 * in a product, an acknowledgment in the product documentation would be
24 * appreciated but is not required.
25 * o Altered source versions must be plainly marked as such, and must not
26 * be misrepresented as being the original software.
27 * o This notice may not be removed or altered from any source
32 * Modified by Sean Kelly for use with the D Runtime Project
37 //debug=adi; // uncomment to turn on debugging printf's
41 debug(adi) import stdc.stdio;
48 FINALIZE = 0b0000_0001,
49 NO_SCAN = 0b0000_0010,
50 NO_MOVE = 0b0000_0100,
51 ALL_BITS = 0b1111_1111
54 extern (C) void* gc_malloc( size_t sz, uint ba = 0 );
55 extern (C) void* gc_calloc( size_t sz, uint ba = 0 );
56 extern (C) void gc_free( void* p );
66 /**********************************************
67 * Reverse array of chars.
68 * Handled separately because embedded multibyte encodings should not be
72 extern (C) long _adReverseChar(char[] a)
79 char* hi = &a[length - 1];
85 debug(adi) printf("lo = %d, hi = %d\n", lo, hi);
86 if (clo <= 0x7F && chi <= 0x7F)
88 debug(adi) printf("\tascii\n");
96 uint stridelo = UTF8stride[clo];
99 while ((chi & 0xC0) == 0x80)
108 debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi);
109 if (stridelo == stridehi)
112 memcpy(tmp.ptr, lo, stridelo);
113 memcpy(lo, hi, stridelo);
114 memcpy(hi, tmp.ptr, stridelo);
120 /* Shift the whole array. This is woefully inefficient
122 memcpy(tmp.ptr, hi, stridehi);
123 memcpy(tmplo.ptr, lo, stridelo);
124 memmove(lo + stridehi, lo + stridelo , (hi - lo) - stridelo);
125 memcpy(lo, tmp.ptr, stridehi);
126 memcpy(hi + stridehi - stridelo, tmplo.ptr, stridelo);
129 hi = hi - 1 + (stridehi - stridelo);
132 return *cast(long*)(&a);
139 auto r = a.dup.reverse;
143 a = "a\u1235\u1234c";
147 assert(r == "c\u1234\u1235a");
153 assert(r == "c\u1234ba");
155 a = "\u3026\u2021\u3061\n";
157 assert(r == "\n\u3061\u2021\u3026");
161 /**********************************************
162 * Reverse array of wchars.
163 * Handled separately because embedded multiword encodings should not be
167 extern (C) long _adReverseWchar(wchar[] a)
173 wchar* hi = &a[length - 1];
179 if ((clo < 0xD800 || clo > 0xDFFF) &&
180 (chi < 0xD800 || chi > 0xDFFF))
189 int stridelo = 1 + (clo >= 0xD800 && clo <= 0xDBFF);
192 if (chi >= 0xDC00 && chi <= 0xDFFF)
201 if (stridelo == stridehi)
204 assert(stridelo == 2);
205 assert(stmp.sizeof == 2 * (*lo).sizeof);
206 stmp = *cast(int*)lo;
207 *cast(int*)lo = *cast(int*)hi;
208 *cast(int*)hi = stmp;
214 /* Shift the whole array. This is woefully inefficient
216 memcpy(tmp.ptr, hi, stridehi * wchar.sizeof);
217 memcpy(hi + stridehi - stridelo, lo, stridelo * wchar.sizeof);
218 memmove(lo + stridehi, lo + stridelo , (hi - (lo + stridelo)) * wchar.sizeof);
219 memcpy(lo, tmp.ptr, stridehi * wchar.sizeof);
222 hi = hi - 1 + (stridehi - stridelo);
225 return *cast(long*)(&a);
236 a = "a\U00012356\U00012346c";
238 assert(r == "c\U00012346\U00012356a");
242 assert(r == "c\U00012345ba");
246 /**********************************************
247 * Support for array.reverse property.
250 extern (C) long _adReverse(Array a, size_t szelem)
253 assert(result is *cast(long*)(&a));
263 void* hi = a.ptr + (a.length - 1) * szelem;
269 tmp = cast(byte*) alloca(szelem);
271 //tmp = gc_malloc(szelem);
274 for (; lo < hi; lo += szelem, hi -= szelem)
276 memcpy(tmp, lo, szelem);
277 memcpy(lo, hi, szelem);
278 memcpy(hi, tmp, szelem);
287 // BUG: bad code is generate for delete pointer, tries
292 return *cast(long*)(&a);
297 debug(adi) printf("array.reverse.unittest\n");
299 int[] a = new int[5];
303 for (i = 0; i < 5; i++)
307 for (i = 0; i < 5; i++)
308 assert(a[i] == 4 - i);
311 { // More than 16 bytes in size
316 X20[] c = new X20[5];
319 for (i = 0; i < 5; i++)
325 for (i = 0; i < 5; i++)
327 assert(c[i].a == 4 - i);
328 assert(c[i].e == 10);
332 /**********************************************
333 * Sort array of chars.
336 extern (C) long _adSortChar(char[] a)
340 dchar[] da = toUTF32(a);
343 foreach (dchar d; da)
345 auto t = toUTF8(buf, d);
346 a[i .. i + t.length] = t[];
351 return *cast(long*)(&a);
354 /**********************************************
355 * Sort array of wchars.
358 extern (C) long _adSortWchar(wchar[] a)
362 dchar[] da = toUTF32(a);
365 foreach (dchar d; da)
367 auto t = toUTF16(buf, d);
368 a[i .. i + t.length] = t[];
373 return *cast(long*)(&a);
376 /***************************************
377 * Support for array equality test.
383 extern (C) int _adEq(Array a1, Array a2, TypeInfo ti)
385 debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length);
386 if (a1.length != a2.length)
387 return 0; // not equal
388 auto sz = ti.tsize();
393 // We should really have a ti.isPOD() check for this
394 return (memcmp(p1, p2, a1.length) == 0);
396 for (size_t i = 0; i < a1.length; i++)
398 if (!ti.equals(p1 + i * sz, p2 + i * sz))
399 return 0; // not equal
404 extern (C) int _adEq2(Array a1, Array a2, TypeInfo ti)
406 debug(adi) printf("_adEq2(a1.length = %d, a2.length = %d)\n", a1.length, a2.length);
407 if (a1.length != a2.length)
408 return 0; // not equal
409 if (!ti.equals(&a1, &a2))
415 debug(adi) printf("array.Eq unittest\n");
420 assert(a != "helloo");
421 assert(a != "betty");
422 assert(a == "hello");
423 assert(a != "hxxxx");
426 /***************************************
427 * Support for array compare test.
430 extern (C) int _adCmp(Array a1, Array a2, TypeInfo ti)
432 debug(adi) printf("adCmp()\n");
433 auto len = a1.length;
436 auto sz = ti.tsize();
441 { // We should really have a ti.isPOD() check for this
442 auto c = memcmp(p1, p2, len);
448 for (size_t i = 0; i < len; i++)
450 auto c = ti.compare(p1 + i * sz, p2 + i * sz);
455 if (a1.length == a2.length)
457 return (a1.length > a2.length) ? 1 : -1;
460 extern (C) int _adCmp2(Array a1, Array a2, TypeInfo ti)
462 debug(adi) printf("_adCmp2(a1.length = %d, a2.length = %d)\n", a1.length, a2.length);
463 return ti.compare(&a1, &a2);
467 debug(adi) printf("array.Cmp unittest\n");
473 assert(a < "helloo");
474 assert(a <= "helloo");
476 assert(a >= "betty");
477 assert(a == "hello");
478 assert(a <= "hello");
479 assert(a >= "hello");
482 /***************************************
483 * Support for array compare test.
486 extern (C) int _adCmpChar(Array a1, Array a2)
496 mov ESI,a1+4[4+ESP] ;
497 mov EDI,a2+4[4+ESP] ;
511 // Do alignment if neither is dword aligned
518 mov AL,[ESI] ; //align ESI to dword bounds
534 // do multiple of 4 bytes at a time
547 // if still equal and not end of string, do up to 3 bytes slightly
600 debug(adi) printf("adCmpChar()\n");
604 c = memcmp(cast(char *)a1.ptr, cast(char *)a2.ptr, len);
606 c = cast(int)a1.length - cast(int)a2.length;
613 debug(adi) printf("array.CmpChar unittest\n");
619 assert(a < "helloo");
620 assert(a <= "helloo");
622 assert(a >= "betty");
623 assert(a == "hello");
624 assert(a <= "hello");
625 assert(a >= "hello");