From 110cfd9da72920f6a903b49d29212cbfa71c7515 Mon Sep 17 00:00:00 2001 From: Brad Roberts Date: Mon, 10 Sep 2007 05:04:40 +0000 Subject: [PATCH] phobos 0.148 --- internal/arraycast.d | 3 + internal/dmain2.d | 33 +- internal/match.d | 47 -- internal/object.d | 41 +- linux.mak | 20 +- object.d | 14 +- std/bitarray.d | 935 +++++++++++++++++++++++++++++++++++++++ std/boxer.d | 42 +- std/conv.d | 30 +- std/cover.d | 11 +- std/cstream.d | 6 +- std/demangle.d | 5 +- std/file.d | 1 + std/format.d | 14 +- std/intrinsic.d | 5 +- std/math.d | 4 +- std/math2.d | 10 +- std/mmfile.d | 2 +- std/path.d | 10 +- std/regexp.d | 87 ++-- std/socket.d | 28 +- std/socketstream.d | 4 +- std/stream.d | 44 +- std/string.d | 10 +- std/typeinfo/ti_Abit.d | 97 ---- std/typeinfo/ti_Aubyte.d | 7 + std/typeinfo/ti_bit.d | 43 -- std/typeinfo/ti_ubyte.d | 4 + std/utf.d | 2 +- unittest.d | 5 + win32.mak | 7 +- 31 files changed, 1179 insertions(+), 392 deletions(-) delete mode 100644 internal/match.d create mode 100644 std/bitarray.d delete mode 100644 std/typeinfo/ti_Abit.d delete mode 100644 std/typeinfo/ti_bit.d diff --git a/internal/arraycast.d b/internal/arraycast.d index 7998f23d6..f27a5a091 100644 --- a/internal/arraycast.d +++ b/internal/arraycast.d @@ -87,6 +87,8 @@ void[] _d_arraycast_frombit(uint tsize, void[] a) unittest { + version (D_Bits) + { bit[int.sizeof * 3 * 8] b; int[] i; short[] s; @@ -96,6 +98,7 @@ unittest s = cast(short[])b; assert(s.length == 6); + } } diff --git a/internal/dmain2.d b/internal/dmain2.d index e7d2a09a0..f5535f44d 100644 --- a/internal/dmain2.d +++ b/internal/dmain2.d @@ -20,6 +20,8 @@ extern (C) void _moduleCtor(); extern (C) void _moduleDtor(); extern (C) void _moduleUnitTests(); +extern (C) bool no_catch_exceptions = false; + /*********************************** * The D main() function supplied by the user's program */ @@ -56,7 +58,7 @@ extern (C) int main(int argc, char **argv) am = cast(char[] *) alloca(argc * (char[]).sizeof); } - try + if (no_catch_exceptions) { _moduleCtor(); _moduleUnitTests(); @@ -73,12 +75,33 @@ extern (C) int main(int argc, char **argv) _moduleDtor(); gc_term(); } - catch (Object o) + else { - printf("Error: "); - o.print(); - exit(EXIT_FAILURE); + try + { + _moduleCtor(); + _moduleUnitTests(); + + for (i = 0; i < argc; i++) + { + int len = strlen(argv[i]); + am[i] = argv[i][0 .. len]; + } + + args = am[0 .. argc]; + + result = main(args); + _moduleDtor(); + gc_term(); + } + catch (Object o) + { + printf("Error: "); + o.print(); + exit(EXIT_FAILURE); + } } + version (linux) { free(am); diff --git a/internal/match.d b/internal/match.d deleted file mode 100644 index 59f602b3d..000000000 --- a/internal/match.d +++ /dev/null @@ -1,47 +0,0 @@ - -/* - * Copyright (C) 2006 by Digital Mars, www.digitalmars.com - * Written by Walter Bright - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, in both source and binary form, subject to the following - * restrictions: - * - * o The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * o Altered source versions must be plainly marked as such, and must not - * be misrepresented as being the original software. - * o This notice may not be removed or altered from any source - * distribution. - */ - -module internal.match; - -import std.regexp; - -/****************************** - * Return handle to results if input[] matches regular expression pattern[], - * null if not. - */ - -extern (C) _Match* _d_match(char[] pattern, char[] input) -{ - return cast(_Match*)std.regexp.search(input, pattern); -} - -/****************************** - * Returns !=null for next match. - */ - -extern (C) _Match* _d_match_next(_Match* h) -{ - RegExp r = cast(RegExp)h; - return r.test() ? h : null; -} diff --git a/internal/object.d b/internal/object.d index 9cc48fe5c..f5b898f52 100644 --- a/internal/object.d +++ b/internal/object.d @@ -43,7 +43,7 @@ extern (C) } /// Standard boolean type. Implemented as a $(B bit) type. -alias bit bool; +alias bool bit; version (X86_64) { @@ -612,42 +612,3 @@ class Error : Exception //extern (C) int nullext = 0; -/* ***************************** _Match **************************** */ - -/* ** - * Default type for _match. - * Implemented as a proxy for RegExp, so that object doesn't pull in - * the entire std.regexp. - */ - -import std.regexp; - -struct _Match -{ - char[] match(size_t n) - { - return (cast(RegExp)this).match(n); - } - - _Match* opNext() - { - RegExp r = (cast(RegExp)this).opNext(); - if (r) - return cast(_Match*)this; - r = cast(RegExp)this; - delete r; - return null; - } - - char[] pre() - { - return (cast(RegExp)this).pre(); - } - - char[] post() - { - return (cast(RegExp)this).post(); - } -} - - diff --git a/linux.mak b/linux.mak index 76398a255..7a1611d93 100644 --- a/linux.mak +++ b/linux.mak @@ -57,18 +57,18 @@ OBJS= asserterror.o deh2.o switch.o complex.o gcstats.o \ process.o syserror.o \ socket.o socketstream.o stdarg.o stdio.o format.o \ perf.o openrj.o uni.o trace.o boxer.o \ - demangle.o cover.o mangle.o \ + demangle.o cover.o bitarray.o \ ti_wchar.o ti_uint.o ti_short.o ti_ushort.o \ ti_byte.o ti_ubyte.o ti_long.o ti_ulong.o ti_ptr.o \ ti_float.o ti_double.o ti_real.o ti_delegate.o \ ti_creal.o ti_ireal.o ti_cfloat.o ti_ifloat.o \ ti_cdouble.o ti_idouble.o \ ti_Aa.o ti_AC.o ti_Ag.o ti_Aubyte.o ti_Aushort.o ti_Ashort.o \ - ti_C.o ti_int.o ti_char.o ti_dchar.o ti_Adchar.o ti_bit.o \ + ti_C.o ti_int.o ti_char.o ti_dchar.o ti_Adchar.o \ ti_Aint.o ti_Auint.o ti_Along.o ti_Aulong.o ti_Awchar.o \ ti_Afloat.o ti_Adouble.o ti_Areal.o \ ti_Acfloat.o ti_Acdouble.o ti_Acreal.o \ - ti_Abit.o ti_void.o \ + ti_void.o \ date.o dateparse.o llmath.o math2.o Czlib.o Dzlib.o zip.o recls.o ZLIB_OBJS= etc/c/zlib/adler32.o etc/c/zlib/compress.o \ @@ -100,7 +100,7 @@ SRC_STD= std/zlib.d std/zip.d std/stdint.d std/conv.d std/utf.d std/uri.d \ std/regexp.d std/random.d std/stream.d std/process.d std/recls.d \ std/socket.d std/socketstream.d std/loader.d std/stdarg.d \ std/stdio.d std/format.d std/perf.d std/openrj.d std/uni.d \ - std/boxer.d std/cstream.d std/demangle.d std/cover.d + std/boxer.d std/cstream.d std/demangle.d std/cover.d std/bitarray.d SRC_STD_C= std/c/process.d std/c/stdlib.d std/c/time.d std/c/stdio.d \ std/c/math.d std/c/stdarg.d std/c/stddef.d @@ -110,7 +110,7 @@ SRC_TI= \ std/typeinfo/ti_short.d std/typeinfo/ti_ushort.d \ std/typeinfo/ti_byte.d std/typeinfo/ti_ubyte.d \ std/typeinfo/ti_long.d std/typeinfo/ti_ulong.d \ - std/typeinfo/ti_ptr.d std/typeinfo/ti_bit.d \ + std/typeinfo/ti_ptr.d \ std/typeinfo/ti_float.d std/typeinfo/ti_double.d \ std/typeinfo/ti_real.d std/typeinfo/ti_delegate.d \ std/typeinfo/ti_creal.d std/typeinfo/ti_ireal.d \ @@ -127,7 +127,7 @@ SRC_TI= \ std/typeinfo/ti_Areal.d \ std/typeinfo/ti_Acfloat.d std/typeinfo/ti_Acdouble.d \ std/typeinfo/ti_Acreal.d \ - std/typeinfo/ti_Abit.d std/typeinfo/ti_void.d \ + std/typeinfo/ti_void.d \ std/typeinfo/ti_Awchar.d std/typeinfo/ti_dchar.d SRC_INT= \ @@ -137,7 +137,7 @@ SRC_INT= \ internal/memset.d internal/arraycast.d internal/aaA.d internal/adi.d \ internal/dmain2.d internal/cast.d internal/qsort.d internal/deh2.d \ internal/cmath2.d internal/obj.d internal/mars.h internal/aApply.d \ - internal/object.d internal/trace.d internal/qsort2.d internal/match.d + internal/object.d internal/trace.d internal/qsort2.d SRC_STD_WIN= std/windows/registry.d \ std/windows/iunknown.d std/windows/charset.d @@ -438,9 +438,6 @@ invariant.o : internal/invariant.d llmath.o : internal/llmath.d $(DMD) -c $(DFLAGS) internal/llmath.d -match.o : internal/match.d - $(DMD) -c $(DFLAGS) internal/match.d - memset.o : internal/memset.d $(DMD) -c $(DFLAGS) internal/memset.d @@ -476,6 +473,9 @@ asserterror.o : std/asserterror.d base64.o : std/base64.d $(DMD) -c $(DFLAGS) std/base64.d +bitarray.o : std/bitarray.d + $(DMD) -c $(DFLAGS) std/bitarray.d + boxer.o : std/boxer.d $(DMD) -c $(DFLAGS) std/boxer.d diff --git a/object.d b/object.d index d7fc7de0b..ba2d92427 100644 --- a/object.d +++ b/object.d @@ -3,7 +3,8 @@ module object; -alias bit bool; +//alias bit bool; +alias bool bit; alias typeof(int.sizeof) size_t; alias typeof(cast(void*)0 - cast(void*)0) ptrdiff_t; @@ -129,14 +130,3 @@ class Error : Exception this(char[] msg, Error next); } -// Default type for _match - -struct _Match -{ - void* handle; - - char[] match(size_t n); - _Match* opNext(); - char[] pre(); - char[] post(); -} diff --git a/std/bitarray.d b/std/bitarray.d new file mode 100644 index 000000000..06ba4a18a --- /dev/null +++ b/std/bitarray.d @@ -0,0 +1,935 @@ +/*********************** + * Macros: + * WIKI = StdBitarray + */ + +module std.bitarray; + +//debug = bitarray; // uncomment to turn on debugging printf's + +private import std.intrinsic; + +/** + * An array of bits. + */ + +struct BitArray +{ + size_t len; + uint* ptr; + + size_t dim() + { + return (len + 31) / 32; + } + + size_t length() + { + return len; + } + + void length(size_t newlen) + { + if (newlen != len) + { + size_t olddim = dim(); + size_t newdim = (newlen + 31) / 32; + + if (newdim != olddim) + { + // Create a fake array so we can use D's realloc machinery + uint[] b = ptr[0 .. olddim]; + b.length = newdim; // realloc + ptr = b.ptr; + if (newdim & 31) + { // Set any pad bits to 0 + ptr[newdim - 1] &= ~(~0 << (newdim & 31)); + } + } + + len = newlen; + } + } + + /********************************************** + * Support for [$(I index)] operation for BitArray. + */ + bool opIndex(size_t i) + in + { + assert(i < len); + } + body + { + return cast(bool)bt(ptr, i); + } + + /** ditto */ + bool opIndexAssign(bool b, size_t i) + in + { + assert(i < len); + } + body + { + if (b) + bts(ptr, i); + else + btr(ptr, i); + return b; + } + + /********************************************** + * Support for array.dup property for BitArray. + */ + BitArray dup() + { + BitArray ba; + + uint[] b = ptr[0 .. dim].dup; + ba.len = len; + ba.ptr = b.ptr; + return ba; + } + + unittest + { + BitArray a; + BitArray b; + int i; + + debug(bitarray) printf("BitArray.dup.unittest\n"); + + a.length = 3; + a[0] = 1; a[1] = 0; a[2] = 1; + b = a.dup; + assert(b.length == 3); + for (i = 0; i < 3; i++) + { debug(bitarray) printf("b[%d] = %d\n", i, b[i]); + assert(b[i] == (((i ^ 1) & 1) ? true : false)); + } + } + + /********************************************** + * Support for foreach loops for BitArray. + */ + int opApply(int delegate(inout bool) dg) + { + int result; + + for (size_t i = 0; i < len; i++) + { bool b = opIndex(i); + result = dg(b); + if (result) + break; + } + return result; + } + + /** ditto */ + int opApply(int delegate(inout size_t, inout bool) dg) + { + int result; + + for (size_t i = 0; i < len; i++) + { bool b = opIndex(i); + result = dg(i, b); + if (result) + break; + } + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opApply unittest\n"); + + static bool[] ba = [1,0,1]; + + BitArray a; a.init(ba); + + int i; + foreach (b;a) + { + switch (i) + { case 0: assert(b == true); break; + case 1: assert(b == false); break; + case 2: assert(b == true); break; + } + i++; + } + + foreach (j,b;a) + { + switch (j) + { case 0: assert(b == true); break; + case 1: assert(b == false); break; + case 2: assert(b == true); break; + } + } + } + + + /********************************************** + * Support for array.reverse property for BitArray. + */ + + BitArray reverse() + out (result) + { + assert(result == *this); + } + body + { + if (len >= 2) + { + bool t; + size_t lo, hi; + + lo = 0; + hi = len - 1; + for (; lo < hi; lo++, hi--) + { + t = (*this)[lo]; + (*this)[lo] = (*this)[hi]; + (*this)[hi] = t; + } + } + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.reverse.unittest\n"); + + BitArray b; + static bool[5] data = [1,0,1,1,0]; + int i; + + b.init(data); + b.reverse; + for (i = 0; i < data.length; i++) + { + assert(b[i] == data[4 - i]); + } + } + + + /********************************************** + * Support for array.sort property for BitArray. + */ + + BitArray sort() + out (result) + { + assert(result == *this); + } + body + { + if (len >= 2) + { + size_t lo, hi; + + lo = 0; + hi = len - 1; + while (1) + { + while (1) + { + if (lo >= hi) + goto Ldone; + if ((*this)[lo] == true) + break; + lo++; + } + + while (1) + { + if (lo >= hi) + goto Ldone; + if ((*this)[hi] == false) + break; + hi--; + } + + (*this)[lo] = false; + (*this)[hi] = true; + + lo++; + hi--; + } + Ldone: + ; + } + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.sort.unittest\n"); + + static uint x = 0b1100011000; + static BitArray ba = { 10, &x }; + ba.sort; + for (size_t i = 0; i < 6; i++) + assert(ba[i] == false); + for (size_t i = 6; i < 10; i++) + assert(ba[i] == true); + } + + + /*************************************** + * Support for operators == and != for bit arrays. + */ + + int opEquals(BitArray a2) + { int i; + + if (this.length != a2.length) + return 0; // not equal + byte *p1 = cast(byte*)this.ptr; + byte *p2 = cast(byte*)a2.ptr; + uint n = this.length / 8; + for (i = 0; i < n; i++) + { + if (p1[i] != p2[i]) + return 0; // not equal + } + + ubyte mask; + + n = this.length & 7; + mask = (1 << n) - 1; + //printf("i = %d, n = %d, mask = %x, %x, %x\n", i, n, mask, p1[i], p2[i]); + return (mask == 0) || (p1[i] & mask) == (p2[i] & mask); + } + + unittest + { + debug(bitarray) printf("BitArray.opEquals unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1]; + static bool[] bc = [1,0,1,0,1,0,1]; + static bool[] bd = [1,0,1,1,1]; + static bool[] be = [1,0,1,0,1]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + BitArray c; c.init(bc); + BitArray d; d.init(bd); + BitArray e; e.init(be); + + assert(a != b); + assert(a != c); + assert(a != d); + assert(a == e); + } + + /*************************************** + * Implement comparison operators. + */ + + int opCmp(BitArray a2) + { + uint len; + uint i; + + len = this.length; + if (a2.length < len) + len = a2.length; + ubyte* p1 = cast(ubyte*)this.ptr; + ubyte* p2 = cast(ubyte*)a2.ptr; + uint n = len / 8; + for (i = 0; i < n; i++) + { + if (p1[i] != p2[i]) + break; // not equal + } + for (uint j = i * 8; j < len; j++) + { ubyte mask = 1 << j; + int c; + + c = cast(int)(p1[i] & mask) - cast(int)(p2[i] & mask); + if (c) + return c; + } + return cast(int)this.len - cast(int)a2.length; + } + + unittest + { + debug(bitarray) printf("BitArray.opCmp unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1]; + static bool[] bc = [1,0,1,0,1,0,1]; + static bool[] bd = [1,0,1,1,1]; + static bool[] be = [1,0,1,0,1]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + BitArray c; c.init(bc); + BitArray d; d.init(bd); + BitArray e; e.init(be); + + assert(a > b); + assert(a >= b); + assert(a < c); + assert(a <= c); + assert(a < d); + assert(a <= d); + assert(a == e); + assert(a <= e); + assert(a >= e); + } + + /*************************************** + * Set BitArray to contents of ba[] + */ + + void init(bool[] ba) + { + length = ba.length; + foreach (i, b; ba) + { + (*this)[i] = b; + } + } + + + /*************************************** + * Map BitArray onto v[], with numbits being the number of bits + * in the array. Does not copy the data. + * + * This is the inverse of opCast. + */ + void init(void[] v, size_t numbits) + in + { + assert(numbits <= v.length * 8); + assert((v.length & 3) == 0); + } + body + { + ptr = cast(uint*)v.ptr; + len = numbits; + } + + unittest + { + debug(bitarray) printf("BitArray.init unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + + BitArray a; a.init(ba); + BitArray b; + void[] v; + + v = cast(void[])a; + b.init(v, a.length); + + assert(b[0] == 1); + assert(b[1] == 0); + assert(b[2] == 1); + assert(b[3] == 0); + assert(b[4] == 1); + + a[0] = 0; + assert(b[0] == 0); + + assert(a == b); + } + + /*************************************** + * Convert to void[]. + */ + void[] opCast() + { + return cast(void[])ptr[0 .. dim]; + } + + unittest + { + debug(bitarray) printf("BitArray.opCast unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + + BitArray a; a.init(ba); + void[] v = cast(void[])a; + + assert(v.length == a.dim * uint.sizeof); + } + + /*************************************** + * Support for unary operator ~ for bit arrays. + */ + BitArray opCom() + { + auto dim = this.dim(); + + BitArray result; + + result.length = len; + for (size_t i = 0; i < dim; i++) + result.ptr[i] = ~this.ptr[i]; + if (len & 31) + result.ptr[dim - 1] &= ~(~0 << (len & 31)); + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opCom unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + + BitArray a; a.init(ba); + BitArray b = ~a; + + assert(b[0] == 0); + assert(b[1] == 1); + assert(b[2] == 0); + assert(b[3] == 1); + assert(b[4] == 0); + } + + + /*************************************** + * Support for binary operator & for bit arrays. + */ + BitArray opAnd(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + BitArray result; + + result.length = len; + for (size_t i = 0; i < dim; i++) + result.ptr[i] = this.ptr[i] & e2.ptr[i]; + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opAnd unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + BitArray c = a & b; + + assert(c[0] == 1); + assert(c[1] == 0); + assert(c[2] == 1); + assert(c[3] == 0); + assert(c[4] == 0); + } + + + /*************************************** + * Support for binary operator | for bit arrays. + */ + BitArray opOr(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + BitArray result; + + result.length = len; + for (size_t i = 0; i < dim; i++) + result.ptr[i] = this.ptr[i] | e2.ptr[i]; + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opOr unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + BitArray c = a | b; + + assert(c[0] == 1); + assert(c[1] == 0); + assert(c[2] == 1); + assert(c[3] == 1); + assert(c[4] == 1); + } + + + /*************************************** + * Support for binary operator ^ for bit arrays. + */ + BitArray opXor(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + BitArray result; + + result.length = len; + for (size_t i = 0; i < dim; i++) + result.ptr[i] = this.ptr[i] ^ e2.ptr[i]; + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opXor unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + BitArray c = a ^ b; + + assert(c[0] == 0); + assert(c[1] == 0); + assert(c[2] == 0); + assert(c[3] == 1); + assert(c[4] == 1); + } + + + /*************************************** + * Support for binary operator - for bit arrays. + * + * $(I a - b) for BitArrays means the same thing as $(I a & ~b). + */ + BitArray opSub(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + BitArray result; + + result.length = len; + for (size_t i = 0; i < dim; i++) + result.ptr[i] = this.ptr[i] & ~e2.ptr[i]; + return result; + } + + unittest + { + debug(bitarray) printf("BitArray.opSub unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + BitArray c = a - b; + + assert(c[0] == 0); + assert(c[1] == 0); + assert(c[2] == 0); + assert(c[3] == 0); + assert(c[4] == 1); + } + + + /*************************************** + * Support for operator &= bit arrays. + */ + BitArray opAndAssign(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + for (size_t i = 0; i < dim; i++) + ptr[i] &= e2.ptr[i]; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opAndAssign unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + a &= b; + assert(a[0] == 1); + assert(a[1] == 0); + assert(a[2] == 1); + assert(a[3] == 0); + assert(a[4] == 0); + } + + + /*************************************** + * Support for operator |= for bit arrays. + */ + BitArray opOrAssign(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + for (size_t i = 0; i < dim; i++) + ptr[i] |= e2.ptr[i]; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opOrAssign unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + a |= b; + assert(a[0] == 1); + assert(a[1] == 0); + assert(a[2] == 1); + assert(a[3] == 1); + assert(a[4] == 1); + } + + /*************************************** + * Support for operator ^= for bit arrays. + */ + BitArray opXorAssign(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + for (size_t i = 0; i < dim; i++) + ptr[i] ^= e2.ptr[i]; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opXorAssign unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + a ^= b; + assert(a[0] == 0); + assert(a[1] == 0); + assert(a[2] == 0); + assert(a[3] == 1); + assert(a[4] == 1); + } + + /*************************************** + * Support for operator -= for bit arrays. + * + * $(I a -= b) for BitArrays means the same thing as $(I a &= ~b). + */ + BitArray opSubAssign(BitArray e2) + in + { + assert(len == e2.length); + } + body + { + auto dim = this.dim(); + + for (size_t i = 0; i < dim; i++) + ptr[i] &= ~e2.ptr[i]; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opSubAssign unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + static bool[] bb = [1,0,1,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + + a -= b; + assert(a[0] == 0); + assert(a[1] == 0); + assert(a[2] == 0); + assert(a[3] == 0); + assert(a[4] == 1); + } + + /*************************************** + * Support for operator ~= for bit arrays. + */ + + BitArray opCatAssign(bool b) + { + length = len + 1; + (*this)[len - 1] = b; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opCatAssign unittest\n"); + + static bool[] ba = [1,0,1,0,1]; + + BitArray a; a.init(ba); + BitArray b; + + b = (a ~= true); + assert(a[0] == 1); + assert(a[1] == 0); + assert(a[2] == 1); + assert(a[3] == 0); + assert(a[4] == 1); + assert(a[5] == 1); + + assert(b == a); + } + + /*************************************** + * ditto + */ + + BitArray opCatAssign(BitArray b) + { + auto istart = len; + length = len + b.length; + for (auto i = istart; i < len; i++) + (*this)[i] = b[i - istart]; + return *this; + } + + unittest + { + debug(bitarray) printf("BitArray.opCatAssign unittest\n"); + + static bool[] ba = [1,0]; + static bool[] bb = [0,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + BitArray c; + + c = (a ~= b); + assert(a.length == 5); + assert(a[0] == 1); + assert(a[1] == 0); + assert(a[2] == 0); + assert(a[3] == 1); + assert(a[4] == 0); + + assert(c == a); + } + + /*************************************** + * Support for binary operator ~ for bit arrays. + */ + BitArray opCat(bool b) + { + BitArray r; + + r = this.dup; + r.length = len + 1; + r[len] = b; + return r; + } + + /** ditto */ + BitArray opCat_r(bool b) + { + BitArray r; + + r.length = len + 1; + r[0] = b; + for (size_t i = 0; i < len; i++) + r[1 + i] = (*this)[i]; + return r; + } + + /** ditto */ + BitArray opCat(BitArray b) + { + BitArray r; + + r = this.dup(); + r ~= b; + return r; + } + + unittest + { + debug(bitarray) printf("BitArray.opCat unittest\n"); + + static bool[] ba = [1,0]; + static bool[] bb = [0,1,0]; + + BitArray a; a.init(ba); + BitArray b; b.init(bb); + BitArray c; + + c = (a ~ b); + assert(c.length == 5); + assert(c[0] == 1); + assert(c[1] == 0); + assert(c[2] == 0); + assert(c[3] == 1); + assert(c[4] == 0); + + c = (a ~ true); + assert(c.length == 3); + assert(c[0] == 1); + assert(c[1] == 0); + assert(c[2] == 1); + + c = (false ~ a); + assert(c.length == 3); + assert(c[0] == 0); + assert(c[1] == 1); + assert(c[2] == 0); + } +} diff --git a/std/boxer.d b/std/boxer.d index 3bd76db45..b3493fd04 100644 --- a/std/boxer.d +++ b/std/boxer.d @@ -49,12 +49,12 @@ private import std.utf; * Finally, you can discover whether unboxing as a certain type is legal by * using the unboxable template or method: * - * bit unboxable!(T) (Box value); - * bit Box.unboxable(TypeInfo T); + * bool unboxable!(T) (Box value); + * bool Box.unboxable(TypeInfo T); */ /** Return the next type in an array typeinfo, or null if there is none. */ -private bit isArrayTypeInfo(TypeInfo type) +private bool isArrayTypeInfo(TypeInfo type) { char[] name = type.classinfo.name; return name.length >= 10 && name[9] == 'A' && name != "TypeInfo_AssociativeArray"; @@ -63,7 +63,8 @@ private bit isArrayTypeInfo(TypeInfo type) /** The type class returned from Box.findTypeClass; the order of entries is important. */ private enum TypeClass { - Bit, /**< bit */ + Bool, /**< bool */ + Bit = Bool, // for backwards compatibility Integer, /**< byte, ubyte, short, ushort, int, uint, long, ulong */ Float, /**< float, double, real */ Complex, /**< cfloat, cdouble, creal */ @@ -105,7 +106,8 @@ struct Box return TypeClass.Other; switch (type.classinfo.name[9]) { - case 'b': return TypeClass.Bit; + //case 'b': return TypeClass.Bit; + case 'x': return TypeClass.Bool; case 'g', 'h', 's', 't', 'i', 'k', 'l', 'm': return TypeClass.Integer; case 'f', 'd', 'e': return TypeClass.Float; case 'q', 'r', 'c': return TypeClass.Complex; @@ -118,7 +120,7 @@ struct Box /* Use the name returned from toString, which might (but hopefully doesn't) include an allocation. */ switch (type.toString) { - case "bit": return TypeClass.Bit; + case "bool": return TypeClass.Bool; case "byte", "ubyte", "short", "ushort", "int", "uint", "long", "ulong": return TypeClass.Integer; case "float", "real", "double": return TypeClass.Float; case "cfloat", "cdouble", "creal": return TypeClass.Complex; @@ -129,7 +131,7 @@ struct Box } /** Return whether this value could be unboxed as the given type without throwing. */ - bit unboxable(TypeInfo test) + bool unboxable(TypeInfo test) { if (type is test) return true; @@ -158,7 +160,7 @@ struct Box return (cast(TypeInfo_Pointer)type).next is (cast(TypeInfo_Pointer)test).next; if ((ta == tb && ta != TypeClass.Other) - || (ta == TypeClass.Bit && tb == TypeClass.Integer) + || (ta == TypeClass.Bool && tb == TypeClass.Integer) || (ta <= TypeClass.Integer && tb == TypeClass.Float) || (ta <= TypeClass.Imaginary && tb == TypeClass.Complex)) return true; @@ -207,7 +209,7 @@ struct Box return string; } - private bit opEqualsInternal(Box other, bit inverted) + private bool opEqualsInternal(Box other, bool inverted) { if (type != other.type) { @@ -238,16 +240,16 @@ struct Box assert (0); } - return cast(bit)type.equals(data, other.data); + return cast(bool)type.equals(data, other.data); } /** Implement the equals operator. */ - bit opEquals(Box other) + bool opEquals(Box other) { return opEqualsInternal(other, false); } - private float opCmpInternal(Box other, bit inverted) + private float opCmpInternal(Box other, bool inverted) { if (type != other.type) { @@ -434,8 +436,8 @@ private template unboxCastInteger(T) return cast(T) *cast(long*) value.data; if (value.type is typeid(ulong)) return cast(T) *cast(ulong*) value.data; - if (value.type is typeid(bit)) - return cast(T) *cast(bit*) value.data; + if (value.type is typeid(bool)) + return cast(T) *cast(bool*) value.data; if (value.type is typeid(byte)) return cast(T) *cast(byte*) value.data; if (value.type is typeid(ubyte)) @@ -602,7 +604,7 @@ template unbox(T : void*) */ template unboxable(T) { - bit unboxable(Box value) + bool unboxable(Box value) { return value.unboxable(typeid(T)); } @@ -614,7 +616,7 @@ private template unboxTest(T) T unboxTest(Box value) { T result; - bit unboxable = value.unboxable(typeid(T)); + bool unboxable = value.unboxable(typeid(T)); try result = unbox!(T) (value); catch (UnboxException error) @@ -641,7 +643,7 @@ unittest Box a, b; /* Call the function, catch UnboxException, return that it threw correctly. */ - bit fails(void delegate()func) + bool fails(void delegate()func) { try func(); catch (UnboxException error) @@ -719,7 +721,7 @@ unittest assert (box(4) > box(3.0)); assert (box(0+3i) < box(0+4i)); - /* Assert that casting from bit to int works. */ + /* Assert that casting from bool to int works. */ assert (1 == unboxTest!(int)(box(true))); assert (box(1) == box(true)); @@ -746,8 +748,8 @@ unittest assert (unboxTest!(void*)(box(p))); // int[] assert (unboxTest!(void*)(box(new A))); // Object - /* Assert that we can't unbox an integer as bit. */ - assert (!unboxable!(bit) (box(4))); + /* Assert that we can't unbox an integer as bool. */ + assert (!unboxable!(bool) (box(4))); /* Assert that we can't unbox a struct as another struct. */ SA sa; diff --git a/std/conv.d b/std/conv.d index ace01690a..cd1952b66 100644 --- a/std/conv.d +++ b/std/conv.d @@ -1,6 +1,6 @@ // Written by Walter Bright -// Copyright (c) 2002-2003 Digital Mars +// Copyright (c) 2002-2006 Digital Mars // All Rights Reserved // www.digitalmars.com // Some parts contributed by David L. Davis @@ -1174,7 +1174,7 @@ cfloat toCfloat(in char[] s) real r1; real r2; cfloat cf; - bit b = 0; + bool b = 0; char* endptr; if (!s.length) @@ -1255,7 +1255,7 @@ cdouble toCdouble(in char[] s) real r1; real r2; cdouble cd; - bit b = 0; + bool b = 0; char* endptr; if (!s.length) @@ -1332,7 +1332,7 @@ creal toCreal(in char[] s) real r1; real r2; creal cr; - bit b = 0; + bool b = 0; char* endptr; if (!s.length) @@ -1419,7 +1419,7 @@ unittest * Grammar: * ['+'|'-'] string floating-point digit {digit} */ -private bit getComplexStrings(in char[] s, out char[] s1, out char[] s2) +private bool getComplexStrings(in char[] s, out char[] s1, out char[] s2) { int len = s.length; @@ -1471,18 +1471,18 @@ private bit getComplexStrings(in char[] s, out char[] s1, out char[] s2) /**************************************** * Main function to compare reals with given precision */ -private bit feq(in real rx, in real ry, in real precision) +private bool feq(in real rx, in real ry, in real precision) { if (rx == ry) return 1; if (isnan(rx)) - return cast(bit)isnan(ry); + return cast(bool)isnan(ry); if (isnan(ry)) return 0; - return cast(bit)(fabs(rx - ry) <= precision); + return cast(bool)(fabs(rx - ry) <= precision); } /**************************************** @@ -1493,24 +1493,24 @@ private bit feq(in real rx, in real ry, in real precision) * 1 match * 0 nomatch */ -private bit feq(in real r1, in real r2) +private bool feq(in real r1, in real r2) { if (r1 == r2) return 1; if (isnan(r1)) - return cast(bit)isnan(r2); + return cast(bool)isnan(r2); if (isnan(r2)) return 0; - return cast(bit)(feq(r1, r2, 0.000001L)); + return cast(bool)(feq(r1, r2, 0.000001L)); } /**************************************** * compare ireals with given precision */ -private bit feq(in ireal r1, in ireal r2) +private bool feq(in ireal r1, in ireal r2) { real rx = cast(real)r1; real ry = cast(real)r2; @@ -1519,7 +1519,7 @@ private bit feq(in ireal r1, in ireal r2) return 1; if (isnan(rx)) - return cast(bit)isnan(ry); + return cast(bool)isnan(ry); if (isnan(ry)) return 0; @@ -1530,7 +1530,7 @@ private bit feq(in ireal r1, in ireal r2) /**************************************** * compare creals with given precision */ -private bit feq(in creal r1, in creal r2) +private bool feq(in creal r1, in creal r2) { real r1a = fabs(cast(real)r1.re - cast(real)r2.re); real r2b = fabs(cast(real)r1.im - cast(real)r2.im); @@ -1540,7 +1540,7 @@ private bit feq(in creal r1, in creal r2) return 1; if (isnan(r1a)) - return cast(bit)isnan(r2b); + return cast(bool)isnan(r2b); if (isnan(r2b)) return 0; diff --git a/std/cover.d b/std/cover.d index 9b2c378fb..110205a0b 100644 --- a/std/cover.d +++ b/std/cover.d @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005 by Digital Mars, www.digitalmars.com + * Copyright (C) 2005-2006 by Digital Mars, www.digitalmars.com * Written by Walter Bright * * This software is provided 'as-is', without any express or implied @@ -36,20 +36,21 @@ module std.cover; private import std.stdio; private import std.file; +private import std.bitarray; private { struct Cover { char[] filename; - bit[] valid; + BitArray valid; uint[] data; } Cover[] gdata; char[] srcpath; char[] dstpath; - bit merge; + bool merge; } /*********************************** @@ -78,12 +79,12 @@ void setDestDir(char[] pathname) * created. */ -void setMerge(bit flag) +void setMerge(bool flag) { merge = flag; } -extern (C) void _d_cover_register(char[] filename, bit[] valid, uint[] data) +extern (C) void _d_cover_register(char[] filename, BitArray valid, uint[] data) { //printf("_d_cover_register()\n"); //printf("\tfilename = '%.*s'\n", filename); diff --git a/std/cstream.d b/std/cstream.d index ae2267066..b3689eb15 100644 --- a/std/cstream.d +++ b/std/cstream.d @@ -16,8 +16,8 @@ class CFile : Stream { this(FILE* cfile, FileMode mode, bool seekable = false) { super(); this.file = cfile; - readable = cast(bit)(mode & FileMode.In); - writeable = cast(bit)(mode & FileMode.Out); + readable = cast(bool)(mode & FileMode.In); + writeable = cast(bool)(mode & FileMode.Out); this.seekable = seekable; } @@ -46,7 +46,7 @@ class CFile : Stream { } override size_t readBlock(void* buffer, size_t size) { size_t n = fread(buffer,1,size,cfile); - readEOF = cast(bit)(n == 0); + readEOF = cast(bool)(n == 0); return n; } override size_t writeBlock(void* buffer, size_t size) { diff --git a/std/demangle.d b/std/demangle.d index c4dfd5418..47b0f7ed4 100644 --- a/std/demangle.d +++ b/std/demangle.d @@ -40,7 +40,7 @@ import std.demangle; int main() { char[] buffer; - bit inword; + bool inword; int c; while ((c = fgetc(stdin)) != EOF) @@ -120,7 +120,7 @@ char[] demangle(char[] name) name[ni + 2] == 'T') { size_t nisave = ni; - bit err; + bool err; ni += 3; try { @@ -170,6 +170,7 @@ char[] demangle(char[] name) { case 'v': p = "void"; goto L1; case 'b': p = "bit"; goto L1; + case 'x': p = "bool"; goto L1; case 'g': p = "byte"; goto L1; case 'h': p = "ubyte"; goto L1; case 's': p = "short"; goto L1; diff --git a/std/file.d b/std/file.d index 7fe753b6b..65c6014e6 100644 --- a/std/file.d +++ b/std/file.d @@ -1164,6 +1164,7 @@ struct DirEntry { size_t len = std.string.strlen(fd.d_name); name = std.path.join(path, fd.d_name[0 .. len]); d_type = fd.d_type; + didstat = 0; } int isdir() diff --git a/std/format.d b/std/format.d index c8de273b9..6d23e5cc0 100644 --- a/std/format.d +++ b/std/format.d @@ -38,6 +38,8 @@ private import std.utf; private import std.c.stdlib; private import std.string; +alias bool bit; + version (Windows) { version (DigitalMars) @@ -81,6 +83,7 @@ enum Mangle : char { Tvoid = 'v', Tbit = 'b', + Tbool = 'x', Tbyte = 'g', Tubyte = 'h', Tshort = 's', @@ -130,6 +133,8 @@ private TypeInfo primitiveTypeInfo(Mangle m) ti = typeid(void);break; case Mangle.Tbit: ti = typeid(bit);break; + case Mangle.Tbool: + ti = typeid(bool);break; case Mangle.Tbyte: ti = typeid(byte);break; case Mangle.Tubyte: @@ -325,7 +330,7 @@ $(I FormatChar):
The corresponding argument is formatted in a manner consistent with its type:
-
$(B bit) +
$(B bool)
The result is 'true' or 'false'.
integral types
The $(B %d) format is used. @@ -351,7 +356,7 @@ $(I FormatChar): and is formatted as an integer. If the argument is a signed type and the $(I FormatChar) is $(B d) it is converted to a signed string of characters, otherwise it is treated as - unsigned. An argument of type $(B bit) is formatted as '1' + unsigned. An argument of type $(B bool) is formatted as '1' or '0'. The base used is binary for $(B b), octal for $(B o), decimal for $(B d), and hexadecimal for $(B x) or $(B X). @@ -456,7 +461,7 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, va_list argptr) void formatArg(char fc) { - bit vbit; + bool vbit; ulong vnumber; char vchar; dchar vdchar; @@ -604,7 +609,8 @@ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, va_list argptr) switch (m) { case Mangle.Tbit: - vbit = va_arg!(bit)(argptr); + case Mangle.Tbool: + vbit = va_arg!(bool)(argptr); if (fc != 's') { vnumber = vbit; goto Lnumber; diff --git a/std/intrinsic.d b/std/intrinsic.d index 3698af18f..fa11172c9 100644 --- a/std/intrinsic.d +++ b/std/intrinsic.d @@ -1,11 +1,10 @@ -// Copyright (c) 1999-2003 by Digital Mars -// All Rights Reserved // written by Walter Bright // www.digitalmars.com +// Placed into the public domain -/* These functions are built-in intrinsics to the compiler. +/** These functions are built-in intrinsics to the compiler. */ module std.intrinsic; diff --git a/std/math.d b/std/math.d index a228f8719..573bc160e 100644 --- a/std/math.d +++ b/std/math.d @@ -1677,13 +1677,13 @@ private int mfeq(real x, real y, real precision) } // Returns true if x is +0.0 (This function is used in unit tests) -bit isPosZero(real x) +bool isPosZero(real x) { return (x == 0) && (signbit(x) == 0); } // Returns true if x is -0.0 (This function is used in unit tests) -bit isNegZero(real x) +bool isNegZero(real x) { return (x == 0) && signbit(x); } diff --git a/std/math2.d b/std/math2.d index 487082669..de8336c6f 100644 --- a/std/math2.d +++ b/std/math2.d @@ -20,12 +20,12 @@ private import std.math, std.string, std.c.stdlib, std.c.stdio; * compare floats with given precision */ -bit feq(real a, real b) +bool feq(real a, real b) { return feq(a, b, 0.000001); } -bit feq(real a, real b, real eps) +bool feq(real a, real b, real eps) { return abs(a - b) <= eps; } @@ -761,7 +761,7 @@ real atof(char[] s) while (s[i] == '\t' || s[i] == ' ') if (++i >= s.length) return real.nan; - bit neg = false; + bool neg = false; if (s[i] == '-') { neg = true; @@ -771,7 +771,7 @@ real atof(char[] s) i++; if (i >= s.length) return real.nan; - bit hex; + bool hex; if (s[s.length - 1] == 'h') { hex = true; @@ -859,7 +859,7 @@ real atof(char[] s) } if (++i >= s.length) return real.nan; - bit eneg = false; + bool eneg = false; if (s[i] == '-') { eneg = true; diff --git a/std/mmfile.d b/std/mmfile.d index a022f19d4..d974a7141 100644 --- a/std/mmfile.d +++ b/std/mmfile.d @@ -259,7 +259,7 @@ class MmFile errNo(); } - data = p[0 .. size]; + data = p[0 .. initial_map]; } else { diff --git a/std/path.d b/std/path.d index 188d1c71b..154f07c09 100644 --- a/std/path.d +++ b/std/path.d @@ -3,8 +3,7 @@ * Macros: * WIKI = StdPath * Copyright: - * Copyright (c) 2001-2006 by Digital Mars - * All Rights Reserved + * Placed into public domain. * www.digitalmars.com * * Grzegorz Adam Hankiewicz added some documentation. @@ -537,7 +536,12 @@ int isabs(char[] path) { char[] d = getDrive(path); - return d.length < path.length && path[d.length] == sep[0]; + version (Windows) + { + return d.length && d.length < path.length && path[d.length] == sep[0]; + } + else + return d.length < path.length && path[d.length] == sep[0]; } unittest diff --git a/std/regexp.d b/std/regexp.d index ce621767f..b461bfeef 100644 --- a/std/regexp.d +++ b/std/regexp.d @@ -100,6 +100,7 @@ private import std.string; import std.ctype; import std.outbuffer; + import std.bitarray; } /** Regular expression to extract an _email address */ @@ -335,6 +336,21 @@ unittest * attributes = Regular expression attributes. * Returns: * corresponding RegExp if found, null if not. + * Example: + * --- + * import std.stdio; + * import std.regexp; + * + * void main() + * { + * if (m; std.regexp.search("abcdef", "c")) + * { + * writefln("%s[%s]%s", m.pre, m.match(0), m.post); + * } + * } + * // Prints: + * // ab[c]def + * --- */ RegExp search(char[] string, char[] pattern, char[] attributes = null) @@ -388,41 +404,51 @@ class RegExp return new RegExp(pattern, attributes); } - /********** - * Determine if there is an initial match with string[]. + /************************************ + * Set up for start of foreach loop. * Returns: - * $(B this) if there is a match, null if not + * search() returns instance of RegExp set up to _search string[]. * Example: - * This makes it possible - * to use RegExp's in a $(I MatchExpression): * --- - * if (RegExp("^abc") ~~ string) - * writefln("string starts with 'abc'"); + * import std.stdio; + * import std.regexp; + * + * void main() + * { + * foreach(m; RegExp("ab").search("abcabcabab")) + * { + * writefln("%s[%s]%s", m.pre, m.match(0), m.post); + * } + * } + * // Prints: + * // [ab]cabcabab + * // abc[ab]cabab + * // abcabc[ab]ab + * // abcabcab[ab] * --- */ - public RegExp opMatch(char[] string) + + public RegExp search(rchar[] string) { - return test(input, 0) ? this : null; + input = string; + pmatch[0].rm_eo = 0; + return this; } - /************ - * Determine next match in string. - * Returns: - * $(B this) if there is a match, null if not - * Example: - * This makes it possible, along with $(B opMatch) operator overload, - * to use RegExp's in a $(I WhileStatement): - * --- - * RegExp r = new RegExp("[a..c]"); - * writef("'"); - * while (r ~~ "abdd3cce") - * writef(_match.match(0)); - * writefln("'"); // writes 'abcc' - * --- - */ - public RegExp opNext() + /** ditto */ + public int opApply(int delegate(inout RegExp) dg) { - return test(input, pmatch[0].rm_eo) ? this : null; + int result; + RegExp r = this; + + while (test()) + { + result = dg(r); + if (result) + break; + } + + return result; } /****************** @@ -725,7 +751,7 @@ public int find(rchar[] string) return i; } -deprecated alias find search; +//deprecated alias find search; unittest { @@ -2090,7 +2116,7 @@ class Range uint maxb; OutBuffer buf; ubyte* base; - bit[] bits; + BitArray bits; this(OutBuffer buf) { @@ -2102,6 +2128,7 @@ class Range void setbitmax(uint u) { uint b; + //printf("setbitmax(x%x), maxc = x%x\n", u, maxc); if (u > maxc) { maxc = u; @@ -2113,8 +2140,10 @@ class Range buf.fill0(b - maxb + 1); base = &buf.data[u]; maxb = b + 1; - bits = (cast(bit*)this.base)[0 .. maxc + 1]; + //bits = (cast(bit*)this.base)[0 .. maxc + 1]; + bits.ptr = cast(uint*)this.base; } + bits.len = maxc + 1; } } diff --git a/std/socket.d b/std/socket.d index c8ba22cec..a6abf91a2 100644 --- a/std/socket.d +++ b/std/socket.d @@ -197,7 +197,7 @@ class Protocol } - bit getProtocolByName(char[] name) + bool getProtocolByName(char[] name) { protoent* proto; proto = getprotobyname(toStringz(name)); @@ -209,7 +209,7 @@ class Protocol // Same as getprotobynumber(). - bit getProtocolByType(ProtocolType type) + bool getProtocolByType(ProtocolType type) { protoent* proto; proto = getprotobynumber(type); @@ -269,7 +269,7 @@ class Service } - bit getServiceByName(char[] name, char[] protocolName) + bool getServiceByName(char[] name, char[] protocolName) { servent* serv; serv = getservbyname(toStringz(name), toStringz(protocolName)); @@ -281,7 +281,7 @@ class Service // Any protocol name will be matched. - bit getServiceByName(char[] name) + bool getServiceByName(char[] name) { servent* serv; serv = getservbyname(toStringz(name), null); @@ -292,7 +292,7 @@ class Service } - bit getServiceByPort(ushort port, char[] protocolName) + bool getServiceByPort(ushort port, char[] protocolName) { servent* serv; serv = getservbyport(port, toStringz(protocolName)); @@ -304,7 +304,7 @@ class Service // Any protocol name will be matched. - bit getServiceByPort(ushort port) + bool getServiceByPort(ushort port) { servent* serv; serv = getservbyport(port, null); @@ -411,7 +411,7 @@ class InternetHost } - bit getHostByName(char[] name) + bool getHostByName(char[] name) { hostent* he = gethostbyname(toStringz(name)); if(!he) @@ -422,7 +422,7 @@ class InternetHost } - bit getHostByAddr(uint addr) + bool getHostByAddr(uint addr) { uint x = htonl(addr); hostent* he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET); @@ -435,7 +435,7 @@ class InternetHost //shortcut - bit getHostByAddr(char[] addr) + bool getHostByAddr(char[] addr) { uint x = inet_addr(std.string.toStringz(addr)); hostent* he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET); @@ -922,7 +922,7 @@ class Socket AddressFamily _family; version(Win32) - bit _blocking = false; + bool _blocking = false; // For use with accepting(). @@ -972,7 +972,7 @@ class Socket } - bit blocking() // getter + bool blocking() // getter { version(Win32) { @@ -985,7 +985,7 @@ class Socket } - void blocking(bit byes) // setter + void blocking(bool byes) // setter { version(Win32) { @@ -1019,7 +1019,7 @@ class Socket } - bit isAlive() // getter + bool isAlive() // getter { int type, typesize = type.sizeof; return !getsockopt(sock, SOL_SOCKET, SO_TYPE, cast(char*)&type, &typesize); @@ -1439,7 +1439,7 @@ class Socket /+ - bit poll(events) + bool poll(events) { int WSAEventSelect(socket_t s, WSAEVENT hEventObject, int lNetworkEvents); // Winsock 2 ? int poll(pollfd* fds, int nfds, int timeout); // Unix ? diff --git a/std/socketstream.d b/std/socketstream.d index 48ac08abb..c29199821 100644 --- a/std/socketstream.d +++ b/std/socketstream.d @@ -62,7 +62,7 @@ class SocketStream: Stream return size; len = sock.receive(buffer[0 .. size]); - readEOF = cast(bit)(len == 0); + readEOF = cast(bool)(len == 0); if(len < 0) len = 0; return len; @@ -78,7 +78,7 @@ class SocketStream: Stream return size; len = sock.send(buffer[0 .. size]); - readEOF = cast(bit)(len == 0); + readEOF = cast(bool)(len == 0); if(len < 0) len = 0; return len; diff --git a/std/stream.d b/std/stream.d index 8e25a9392..9bfef7b25 100644 --- a/std/stream.d +++ b/std/stream.d @@ -380,15 +380,15 @@ class Stream : InputStream, OutputStream { private import std.string, crc32, std.c.stdlib, std.c.stdio; // stream abilities - bit readable = false; /// Indicates whether this stream can be read from. - bit writeable = false; /// Indicates whether this stream can be written to. - bit seekable = false; /// Indicates whether this stream can be seeked within. - protected bit isopen = true; /// Indicates whether this stream is open. + bool readable = false; /// Indicates whether this stream can be read from. + bool writeable = false; /// Indicates whether this stream can be written to. + bool seekable = false; /// Indicates whether this stream can be seeked within. + protected bool isopen = true; /// Indicates whether this stream is open. - protected bit readEOF = false; /// Indicates whether this stream is at eof + protected bool readEOF = false; /// Indicates whether this stream is at eof /// after the last read attempt. - protected bit prevCr = false; /// For a non-seekable stream indicates that + protected bool prevCr = false; /// For a non-seekable stream indicates that /// the last readLine or readLineW ended on a /// '\r' character. @@ -712,7 +712,7 @@ class Stream : InputStream, OutputStream { } if (fmt[i] == '%') { // a field i++; - bit suppress = false; + bool suppress = false; if (fmt[i] == '*') { // suppress assignment suppress = true; i++; @@ -745,7 +745,7 @@ class Stream : InputStream, OutputStream { c = getc(); count++; } - bit neg = false; + bool neg = false; if (c == '-') { neg = true; c = getc(); @@ -846,7 +846,7 @@ class Stream : InputStream, OutputStream { c = getc(); count++; } - bit neg = false; + bool neg = false; if (c == '-') { neg = true; c = getc(); @@ -881,7 +881,7 @@ class Stream : InputStream, OutputStream { c = getc(); count++; if (width) { - bit expneg = false; + bool expneg = false; if (c == '-') { expneg = true; width--; @@ -1249,7 +1249,7 @@ class Stream : InputStream, OutputStream { } // returns true if end of stream is reached, false otherwise - bit eof() { + bool eof() { // for unseekable streams we only know the end when we read it if (readEOF && !ungetAvailable()) return true; @@ -1366,7 +1366,7 @@ class FilterStream : Stream { /// Property indicating when this stream closes to close the source stream as /// well. /// Defaults to true. - bit nestClose = true; + bool nestClose = true; /// Construct a FilterStream for the given source. this(Stream source) { @@ -1455,7 +1455,7 @@ class BufferedStream : FilterStream { ubyte[] buffer; // buffer, if any uint bufferCurPos; // current position in buffer uint bufferLen; // amount of data in buffer - bit bufferDirty = false; + bool bufferDirty = false; uint bufferSourcePos; // position in buffer of source stream position ulong streamPos; // absolute position in source stream @@ -1796,8 +1796,8 @@ class File: Stream { this(HANDLE hFile, FileMode mode) { super(); this.hFile = hFile; - readable = cast(bit)(mode & FileMode.In); - writeable = cast(bit)(mode & FileMode.Out); + readable = cast(bool)(mode & FileMode.In); + writeable = cast(bool)(mode & FileMode.Out); version(Windows) { seekable = GetFileType(hFile) == 1; // FILE_TYPE_DISK } else { @@ -1831,8 +1831,8 @@ class File: Stream { int access, share, createMode; parseMode(mode, access, share, createMode); seekable = true; - readable = cast(bit)(mode & FileMode.In); - writeable = cast(bit)(mode & FileMode.Out); + readable = cast(bool)(mode & FileMode.In); + writeable = cast(bool)(mode & FileMode.Out); version (Win32) { if (std.file.useWfuncs) { hFile = CreateFileW(std.utf.toUTF16z(filename), access, share, @@ -2737,7 +2737,7 @@ class SliceStream : FilterStream { ulong pos; // our position relative to low ulong low; // low stream offset. ulong high; // high stream offset. - bit bounded; // upper-bounded by high. + bool bounded; // upper-bounded by high. } /*** @@ -2905,18 +2905,18 @@ class SliceStream : FilterStream { } // helper functions -private bit iswhite(char c) { +private bool iswhite(char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; } -private bit isdigit(char c) { +private bool isdigit(char c) { return c >= '0' && c <= '9'; } -private bit isoctdigit(char c) { +private bool isoctdigit(char c) { return c >= '0' && c <= '7'; } -private bit ishexdigit(char c) { +private bool ishexdigit(char c) { return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } diff --git a/std/string.d b/std/string.d index 233b1f5f6..abf4e93fa9 100644 --- a/std/string.d +++ b/std/string.d @@ -1730,6 +1730,8 @@ char[] replace(char[] s, char[] from, char[] to) int istart; //printf("replace('%.*s','%.*s','%.*s')\n", s, from, to); + if (from.length == 0) + return s; istart = 0; while (istart < s.length) { @@ -1759,6 +1761,10 @@ unittest r = replace(s, from, to); i = cmp(r, "This is a silly silly list"); assert(i == 0); + + r = replace(s, "", to); + i = cmp(r, "This is a foo foo list"); + assert(i == 0); } /***************************** @@ -2168,7 +2174,7 @@ char[] translate(char[] s, char[] transtab, char[] delchars) { char[] r; int count; - bit[256] deltab; + bool[256] deltab; deltab[] = false; foreach (char c; delchars) @@ -2220,7 +2226,7 @@ unittest * Convert to char[]. */ -char[] toString(bit b) +char[] toString(bool b) { return b ? "true" : "false"; } diff --git a/std/typeinfo/ti_Abit.d b/std/typeinfo/ti_Abit.d deleted file mode 100644 index 2a7d66269..000000000 --- a/std/typeinfo/ti_Abit.d +++ /dev/null @@ -1,97 +0,0 @@ - -module std.typeinfo.ti_Abit; - -private import std.string; - -// bit[] - -class TypeInfo_Ab : TypeInfo -{ - char[] toString() { return "bit[]"; } - - uint getHash(void *p) - { ubyte[] s = *cast(ubyte[]*)p; - size_t len = (s.length + 7) / 8; - ubyte *str = s; - uint hash = 0; - - while (1) - { - switch (len) - { - case 0: - return hash; - - case 1: - hash *= 9; - hash += *cast(ubyte *)str; - return hash; - - case 2: - hash *= 9; - hash += *cast(ushort *)str; - return hash; - - case 3: - hash *= 9; - hash += (*cast(ushort *)str << 8) + - (cast(ubyte *)str)[2]; - return hash; - - default: - hash *= 9; - hash += *cast(uint *)str; - str += 4; - len -= 4; - break; - } - } - - return hash; - } - - int equals(void *p1, void *p2) - { - bit[] s1 = *cast(bit[]*)p1; - bit[] s2 = *cast(bit[]*)p2; - - size_t len = s1.length; - - if (s2.length != len) - return 0;; - - // Woefully inefficient bit-by-bit comparison - for (size_t u = 0; u < len; u++) - { - if (s1[u] != s2[u]) - return 0; - } - return 1; - } - - int compare(void *p1, void *p2) - { - bit[] s1 = *cast(bit[]*)p1; - bit[] s2 = *cast(bit[]*)p2; - - size_t len = s1.length; - - if (s2.length < len) - len = s2.length; - - // Woefully inefficient bit-by-bit comparison - for (size_t u = 0; u < len; u++) - { - int result = s1[u] - s2[u]; - if (result) - return result; - } - return cast(int)s1.length - cast(int)s2.length; - } - - size_t tsize() - { - return (bit[]).sizeof; - } -} - diff --git a/std/typeinfo/ti_Aubyte.d b/std/typeinfo/ti_Aubyte.d index a6c7fed0d..b7c7df4b2 100644 --- a/std/typeinfo/ti_Aubyte.d +++ b/std/typeinfo/ti_Aubyte.d @@ -79,3 +79,10 @@ class TypeInfo_Av : TypeInfo_Ah { char[] toString() { return "void[]"; } } + +// bool[] + +class TypeInfo_Ax : TypeInfo_Ah +{ + char[] toString() { return "bool[]"; } +} diff --git a/std/typeinfo/ti_bit.d b/std/typeinfo/ti_bit.d deleted file mode 100644 index 327db6544..000000000 --- a/std/typeinfo/ti_bit.d +++ /dev/null @@ -1,43 +0,0 @@ - -// bit - -module std.typeinfo.ti_bit; - -class TypeInfo_b : TypeInfo -{ - char[] toString() { return "bit"; } - - uint getHash(void *p) - { - return *cast(bit *)p; - } - - int equals(void *p1, void *p2) - { - return *cast(bit *)p1 == *cast(bit *)p2; - } - - int compare(void *p1, void *p2) - { - if (*cast(bit*) p1 < *cast(bit*) p2) - return -1; - else if (*cast(bit*) p1 > *cast(bit*) p2) - return 1; - return 0; - } - - size_t tsize() - { - return bit.sizeof; - } - - void swap(void *p1, void *p2) - { - bit t; - - t = *cast(bit *)p1; - *cast(bit *)p1 = *cast(bit *)p2; - *cast(bit *)p2 = t; - } -} - diff --git a/std/typeinfo/ti_ubyte.d b/std/typeinfo/ti_ubyte.d index 023eee853..c9b678370 100644 --- a/std/typeinfo/ti_ubyte.d +++ b/std/typeinfo/ti_ubyte.d @@ -37,3 +37,7 @@ class TypeInfo_h : TypeInfo } } +class TypeInfo_x : TypeInfo_h +{ + char[] toString() { return "bool"; } +} diff --git a/std/utf.d b/std/utf.d index bf809124e..76d56501c 100644 --- a/std/utf.d +++ b/std/utf.d @@ -82,7 +82,7 @@ class UtfException : Exception * Returns: true if it is, false if not. */ -bit isValidDchar(dchar c) +bool isValidDchar(dchar c) { /* Note: FFFE and FFFF are specifically permitted by the * Unicode standard for application internal use, but are not diff --git a/unittest.d b/unittest.d index 5fbe5c3e6..fe492f6ed 100644 --- a/unittest.d +++ b/unittest.d @@ -47,6 +47,7 @@ import std.md5; import std.stdio; import std.conv; import std.boxer; +import std.bitarray; int main(char[][] args) { @@ -103,6 +104,10 @@ printf("test2\n"); std.demangle.demangle("hello"); + BitArray ba; // std.bitarray + ba.length = 3; + ba[0] = true; + printf("Success\n!"); return 0; } diff --git a/win32.mak b/win32.mak index 72892ca7e..87b7efc75 100644 --- a/win32.mak +++ b/win32.mak @@ -73,7 +73,7 @@ OBJS= asserterror.obj deh.obj switch.obj complex.obj gcstats.obj \ socket.obj socketstream.obj loader.obj stdarg.obj format.obj stdio.obj \ perf.obj openrj.obj uni.obj winsock.obj oldsyserror.obj \ errno.obj boxer.obj cstream.obj charset.obj \ - realtest.obj gamma.obj demangle.obj cover.obj match.obj \ + realtest.obj gamma.obj demangle.obj cover.obj \ ti_Aa.obj ti_Ag.obj ti_C.obj ti_int.obj ti_char.obj \ ti_wchar.obj ti_uint.obj ti_short.obj ti_ushort.obj \ ti_byte.obj ti_ubyte.obj ti_long.obj ti_ulong.obj ti_ptr.obj \ @@ -148,7 +148,7 @@ SRC_INT= \ internal\memset.d internal\arraycast.d internal\aaA.d internal\adi.d \ internal\dmain2.d internal\cast.d internal\qsort.d internal\deh2.d \ internal\cmath2.d internal\obj.d internal\mars.h internal\aApply.d \ - internal\object.d internal\trace.d internal\qsort2.d internal\match.d + internal\object.d internal\trace.d internal\qsort2.d SRC_STD_WIN= std\windows\registry.d \ std\windows\iunknown.d std\windows\syserror.d std\windows\charset.d @@ -427,9 +427,6 @@ gcstub.obj : internal\gc\gcstub.d invariant.obj : internal\invariant.d $(DMD) -c $(DFLAGS) internal\invariant.d -match.obj : internal\match.d - $(DMD) -c $(DFLAGS) internal\match.d - memset.obj : internal\memset.d $(DMD) -c $(DFLAGS) internal\memset.d