mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
phobos 0.99
This commit is contained in:
parent
f86a362105
commit
5b1e376698
10 changed files with 332 additions and 15 deletions
|
@ -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*);
|
||||
|
|
|
@ -36,3 +36,6 @@ enum
|
|||
int random(int num);
|
||||
void randomize();
|
||||
|
||||
int getErrno();
|
||||
int setErrno(int);
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
299
std/string.d
299
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.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue