From 5b1e3766989b51a1f31e7dbe44337ea57b7e87c4 Mon Sep 17 00:00:00 2001 From: Brad Roberts Date: Mon, 10 Sep 2007 04:09:37 +0000 Subject: [PATCH] phobos 0.99 --- errno.c | 6 + internal/arraycat.d | 14 +- std/c/linux/linux.d | 1 - std/c/stdlib.d | 3 + std/c/windows/windows.d | 1 + std/file.d | 1 + std/mmfile.d | 1 + std/moduleinit.d | 19 +-- std/socket.d | 2 +- std/string.d | 299 +++++++++++++++++++++++++++++++++++++++- 10 files changed, 332 insertions(+), 15 deletions(-) diff --git a/errno.c b/errno.c index 7cfa224c6..2d3c93863 100644 --- a/errno.c +++ b/errno.c @@ -8,3 +8,9 @@ int getErrno() { return errno; } + +int setErrno(int newno) +{ + errno = newno; + return newno; +} diff --git a/internal/arraycat.d b/internal/arraycat.d index 1477d568c..2278d0254 100644 --- a/internal/arraycat.d +++ b/internal/arraycat.d @@ -95,7 +95,19 @@ bit[] _d_arraycopybit(bit[] from, bit[] to) if (cast(void *)to + nbytes <= cast(void *)from || cast(void *)from + nbytes <= cast(void *)to) { - memcpy(cast(void *)to, cast(void *)from, nbytes); + nbytes = to.length / 8; + if (nbytes) + memcpy(cast(void *)to, cast(void *)from, nbytes); + + if (to.length & 7) + { + /* Copy trailing bits. + */ + static ubyte[8] masks = [0,1,3,7,0x0F,0x1F,0x3F,0x7F]; + ubyte mask = masks[to.length & 7]; + (cast(ubyte*)to)[nbytes] &= ~mask; + (cast(ubyte*)to)[nbytes] |= (cast(ubyte*)from)[nbytes] & mask; + } } else { diff --git a/std/c/linux/linux.d b/std/c/linux/linux.d index 404be3a52..0a28dcc2a 100644 --- a/std/c/linux/linux.d +++ b/std/c/linux/linux.d @@ -110,7 +110,6 @@ extern (C) int lseek(int, int, int); int fstat(int, struct_stat*); int stat(char*, struct_stat*); - int getErrno(); int chdir(char*); int mkdir(char*, int); int rmdir(char*); diff --git a/std/c/stdlib.d b/std/c/stdlib.d index 57a560db4..9cc97cc57 100644 --- a/std/c/stdlib.d +++ b/std/c/stdlib.d @@ -36,3 +36,6 @@ enum int random(int num); void randomize(); + int getErrno(); + int setErrno(int); + diff --git a/std/c/windows/windows.d b/std/c/windows/windows.d index 0766aec35..728f7b8fc 100644 --- a/std/c/windows/windows.d +++ b/std/c/windows/windows.d @@ -263,6 +263,7 @@ enum } const HANDLE INVALID_HANDLE_VALUE = cast(HANDLE)-1; +const DWORD INVALID_SET_FILE_POINTER = cast(DWORD)-1; const DWORD INVALID_FILE_SIZE = cast(DWORD)0xFFFFFFFF; struct OVERLAPPED { diff --git a/std/file.d b/std/file.d index 7d2689429..cbe8d2402 100644 --- a/std/file.d +++ b/std/file.d @@ -24,6 +24,7 @@ module std.file; private import std.c.stdio; +private import std.c.stdlib; private import std.path; private import std.string; diff --git a/std/mmfile.d b/std/mmfile.d index e46ba3932..0d55ab52b 100644 --- a/std/mmfile.d +++ b/std/mmfile.d @@ -12,6 +12,7 @@ module mmfile; private import std.file; private import std.c.stdio; +private import std.c.stdlib; private import std.path; private import std.string; diff --git a/std/moduleinit.d b/std/moduleinit.d index b421e385f..1edb72df8 100644 --- a/std/moduleinit.d +++ b/std/moduleinit.d @@ -67,18 +67,19 @@ extern (C) int _fatexit(void *); extern (C) void _moduleCtor() { + debug printf("_moduleCtor()\n"); version (linux) { - int length = 0; + int len = 0; ModuleReference *mr; for (mr = _Dmodule_ref; mr; mr = mr.next) - length++; - _moduleinfo_array = new ModuleInfo[length]; - length = 0; + len++; + _moduleinfo_array = new ModuleInfo[len]; + len = 0; for (mr = _Dmodule_ref; mr; mr = mr.next) - { _moduleinfo_array[length] = mr.mod; - length++; + { _moduleinfo_array[len] = mr.mod; + len++; } } @@ -89,7 +90,7 @@ extern (C) void _moduleCtor() } _moduleinfo_dtors = new ModuleInfo[_moduleinfo_array.length]; - //printf("_moduleinfo_dtors = x%x\n", (void *)_moduleinfo_dtors); + debug printf("_moduleinfo_dtors = x%x\n", cast(void *)_moduleinfo_dtors); _moduleCtor2(_moduleinfo_array, 0); } @@ -100,7 +101,9 @@ void _moduleCtor2(ModuleInfo[] mi, int skip) { ModuleInfo m = mi[i]; -// debug printf("\tmodule[%d] = '%.*s'\n", i, m.name); + debug printf("\tmodule[%d] = '%p'\n", i, m); + if (!m) continue; + debug printf("\tmodule[%d] = '%.*s'\n", i, m.name); if (m.flags & MIctordone) continue; debug printf("\tmodule[%d] = '%.*s', m = x%x\n", i, m.name, m); diff --git a/std/socket.d b/std/socket.d index 018d63798..9e2d62789 100644 --- a/std/socket.d +++ b/std/socket.d @@ -48,7 +48,7 @@ const int SOCKET_ERROR = -1; private: -import std.string, std.stdint; +import std.string, std.stdint, std.c.stdlib; version(Win32) diff --git a/std/string.d b/std/string.d index d6797017b..d07089e2b 100644 --- a/std/string.d +++ b/std/string.d @@ -28,6 +28,7 @@ private import std.c.stdlib; private import std.utf; private import std.array; private import std.format; +private import std.ctype; extern (C) { @@ -265,10 +266,12 @@ int find(char[] s, dchar c) } // c is a universal character - char[4] buf; - char[] t; - t = std.utf.toUTF8(buf, c); - return find(s, t); + foreach (int i, dchar c2; s) + { + if (c == c2) + return i; + } + return -1; } unittest @@ -288,6 +291,63 @@ unittest } +/****************************************** + * Case insensitive version of find(). + */ + +int ifind(char[] s, dchar c) +{ + char* p; + + if (c <= 0x7F) + { // Plain old ASCII + char c1 = std.ctype.tolower(c); + + foreach (int i, char c2; s) + { + c2 = std.ctype.tolower(c2); + if (c1 == c2) + return i; + } + return -1; + } + + // c is a universal character + foreach (int i, dchar c2; s) + { + if (c == c2) + return i; + } + return -1; +} + +unittest +{ + debug(string) printf("string.ifind.unittest\n"); + + int i; + + i = ifind(null, cast(dchar)'a'); + assert(i == -1); + i = ifind("def", cast(dchar)'a'); + assert(i == -1); + i = ifind("Abba", cast(dchar)'a'); + assert(i == 0); + i = ifind("def", cast(dchar)'F'); + assert(i == 2); + + char[] sPlts = "Mars: the fourth Rock (Planet) from the Sun."; + + i = ifind("def", cast(char)'f'); + assert(i == 2); + + i = ifind(sPlts, cast(char)'P'); + assert(i == 23); + i = ifind(sPlts, cast(char)'R'); + assert(i == 2); +} + + /****************************************** * Find last occurrance of c in string s. * Return index in s where it is found. @@ -331,6 +391,62 @@ unittest assert(i == 2); } +/****************************************** + * Case insensitive version of rfind(). + */ + +int irfind(char[] s, dchar c) +{ + int i; + + if (c <= 0x7F) + { // Plain old ASCII + char c1 = std.ctype.tolower(c); + + for (i = s.length; i-- > 0;) + { char c2 = s[i]; + + c2 = std.ctype.tolower(c2); + if (c1 == c2) + break; + } + return i; + } + + // c is a universal character + char[4] buf; + char[] t; + t = std.utf.toUTF8(buf, c); + return irfind(s, t); +} + +unittest +{ + debug(string) printf("string.irfind.unittest\n"); + + int i; + + i = irfind(null, cast(dchar)'a'); + assert(i == -1); + i = irfind("def", cast(dchar)'a'); + assert(i == -1); + i = irfind("AbbA", cast(dchar)'a'); + assert(i == 3); + i = irfind("def", cast(dchar)'F'); + assert(i == 2); + + char[] sPlts = "Mars: the fourth Rock (Planet) from the Sun."; + + i = irfind("def", cast(char)'f'); + assert(i == 2); + + i = irfind(sPlts, cast(char)'M'); + assert(i == 34); + i = irfind(sPlts, cast(char)'S'); + assert(i == 40); +} + + /************************************* * Find first occurrance of sub[] in string s[]. * Return index in s[] where it is found. @@ -405,6 +521,105 @@ unittest assert(i == 6); } +/************************************* + * Case insensitive version of find(). + */ + +int ifind(char[] s, char[] sub) + out (result) + { + if (result == -1) + { + } + else + { + assert(0 <= result && result < s.length - sub.length + 1); + assert(icmp(s[result .. result + sub.length], sub) == 0); + } + } + body + { + int sublength = sub.length; + int i; + + if (sublength == 0) + return 0; + + if (s.length < sublength) + return -1; + + char c = sub[0]; + if (sublength == 1) + { + i = ifind(s, c); + } + else if (c <= 0x7F) + { + int imax = s.length - sublength + 1; + + // Remainder of sub[] + char[] subn = sub[1 .. sublength]; + + for (i = 0; i < imax; i++) + { + int j = ifind(s[i .. imax], c); + if (j == -1) + return -1; + i += j; + if (icmp(s[i + 1 .. i + sublength], subn) == 0) + break; + } + } + else + { + int imax = s.length - sublength; + + for (i = 0; i < imax; i++) + { + if (icmp(s[i .. i + sublength], sub) == 0) + break; + } + } + return i; + } + + +unittest +{ + debug(string) printf("string.ifind.unittest\n"); + + int i; + + i = ifind(null, "a"); + assert(i == -1); + i = ifind("def", "a"); + assert(i == -1); + i = ifind("abba", "a"); + assert(i == 0); + i = ifind("def", "f"); + assert(i == 2); + i = ifind("dfefffg", "fff"); + assert(i == 3); + i = ifind("dfeffgfff", "fff"); + assert(i == 6); + + char[] sPlts = "Mars: the fourth Rock (Planet) from the Sun."; + char[] sMars = "Who\'s \'My Favorite Maritian?\'"; + + i = ifind(sMars, "MY fAVe"); + assert(i == -1); + i = ifind(sMars, "mY fAVOriTe"); + assert(i == 7); + i = ifind(sPlts, "mArS:"); + assert(i == 0); + i = ifind(sPlts, "rOcK"); + assert(i == 17); + i = ifind(sPlts, "Un."); + assert(i == 41); + i = ifind(sPlts, sPlts); + assert(i == 0); +} + /************************************* * Find last occurrance of sub in string s. * Return index in s where it is found. @@ -447,6 +662,7 @@ unittest { int i; + debug(string) printf("string.rfind.unittest\n"); i = rfind("abcdefcdef", "c"); assert(i == 6); i = rfind("abcdefcdef", "cd"); @@ -459,6 +675,81 @@ unittest assert(i == 10); } + +/************************************* + * Case insensitive version of rfind(). + */ + +int irfind(char[] s, char[] sub) + out (result) + { + if (result == -1) + { + } + else + { + assert(0 <= result && result < s.length - sub.length + 1); + assert(icmp(s[result .. result + sub.length], sub) == 0); + } + } + body + { + char c; + + if (sub.length == 0) + return s.length; + c = sub[0]; + if (sub.length == 1) + return irfind(s, c); + c = std.ctype.tolower(c); + for (int i = s.length - sub.length; i >= 0; i--) + { + if (std.ctype.tolower(s[i]) == c) + { + if (icmp(s[i + 1 .. i + sub.length], sub[1 .. sub.length]) == 0) + return i; + } + } + return -1; + } + +unittest +{ + int i; + + debug(string) printf("string.irfind.unittest\n"); + i = irfind("abcdefCdef", "c"); + assert(i == 6); + i = irfind("abcdefCdef", "cD"); + assert(i == 6); + i = irfind("abcdefcdef", "x"); + assert(i == -1); + i = irfind("abcdefcdef", "xy"); + assert(i == -1); + i = irfind("abcdefcdef", ""); + assert(i == 10); + + char[] sPlts = "Mars: the fourth Rock (Planet) from the Sun."; + char[] sMars = "Who\'s \'My Favorite Maritian?\'"; + + i = irfind("abcdefcdef", "c"); + assert(i == 6); + i = irfind("abcdefcdef", "cd"); + assert(i == 6); + i = irfind( "abcdefcdef", "def" ); + assert(i == 7); + + i = irfind(sMars, "RiTE maR"); + assert(i == 14); + i = irfind(sPlts, "FOuRTh"); + assert(i == 10); + i = irfind(sMars, "whO\'s \'MY"); + assert(i == 0); + i = irfind(sMars, sMars); + assert(i == 0); +} + + /************************************ * Convert string to lower case. */