Skip to content

Commit

Permalink
Merge pull request #5721 from WalterBright/purearray
Browse files Browse the repository at this point in the history
refactor: add pure and nothrow to root/array.d
  • Loading branch information
andralex committed Apr 29, 2016
2 parents 7829d3f + 279dc9a commit 7355b52
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 43 deletions.
39 changes: 20 additions & 19 deletions src/root/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private:
T[SMALLARRAYCAP] smallarray; // inline storage for small arrays

public:
~this()
~this() nothrow
{
if (data != &smallarray[0])
mem.xfree(data);
Expand Down Expand Up @@ -64,18 +64,18 @@ public:
}
}

void push(T ptr)
void push(T ptr) nothrow
{
reserve(1);
data[dim++] = ptr;
}

void append(typeof(this)* a)
void append(typeof(this)* a) nothrow
{
insert(dim, a);
}

void reserve(size_t nentries)
void reserve(size_t nentries) nothrow
{
//printf("Array::reserve: dim = %d, allocdim = %d, nentries = %d\n", (int)dim, (int)allocdim, (int)nentries);
if (allocdim - dim < nentries)
Expand Down Expand Up @@ -108,14 +108,14 @@ public:
}
}

void remove(size_t i)
void remove(size_t i) nothrow
{
if (dim - i - 1)
memmove(data + i, data + i + 1, (dim - i - 1) * (data[0]).sizeof);
dim--;
}

void insert(size_t index, typeof(this)* a)
void insert(size_t index, typeof(this)* a) nothrow
{
if (a)
{
Expand All @@ -128,15 +128,15 @@ public:
}
}

void insert(size_t index, T ptr)
void insert(size_t index, T ptr) nothrow
{
reserve(1);
memmove(data + index + 1, data + index, (dim - index) * (*data).sizeof);
data[index] = ptr;
dim++;
}

void setDim(size_t newdim)
void setDim(size_t newdim) nothrow
{
if (dim < newdim)
{
Expand All @@ -145,38 +145,38 @@ public:
dim = newdim;
}

ref inout(T) opIndex(size_t i) inout
ref inout(T) opIndex(size_t i) inout nothrow pure
{
return data[i];
}

inout(T)* tdata() inout
inout(T)* tdata() inout nothrow
{
return data;
}

Array!T* copy() const
Array!T* copy() const nothrow
{
auto a = new Array!T();
a.setDim(dim);
memcpy(a.data, data, dim * (void*).sizeof);
return a;
}

void shift(T ptr)
void shift(T ptr) nothrow
{
reserve(1);
memmove(data + 1, data, dim * (*data).sizeof);
data[0] = ptr;
dim++;
}

void zero()
void zero() nothrow pure
{
data[0 .. dim] = T.init;
}

T pop()
T pop() nothrow pure
{
return data[--dim];
}
Expand All @@ -200,12 +200,12 @@ public:
assert(0);
}

extern (D) inout(T)[] opSlice() inout
extern (D) inout(T)[] opSlice() inout nothrow pure
{
return data[0 .. dim];
}

extern (D) inout(T)[] opSlice(size_t a, size_t b) inout
extern (D) inout(T)[] opSlice(size_t a, size_t b) inout nothrow pure
{
assert(a <= b && b <= dim);
return data[a .. b];
Expand All @@ -214,7 +214,8 @@ public:

struct BitArray
{
size_t length() const
nothrow:
size_t length() const pure
{
return len;
}
Expand All @@ -229,15 +230,15 @@ struct BitArray
len = nlen;
}

bool opIndex(size_t idx) const
bool opIndex(size_t idx) const pure
{
import core.bitop : bt;

assert(idx < length);
return !!bt(ptr, idx);
}

void opIndexAssign(bool val, size_t idx)
void opIndexAssign(bool val, size_t idx) pure
{
import core.bitop : btc, bts;

Expand Down
3 changes: 2 additions & 1 deletion src/root/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct File
size_t len; // amount of data in buffer[]
const(FileName)* name; // name of our file

nothrow:
extern (D) this(const(char)* n)
{
_ref = 0;
Expand Down Expand Up @@ -69,7 +70,7 @@ struct File
}
}

extern (C++) const(char)* toChars()
extern (C++) const(char)* toChars() pure
{
return name.toChars();
}
Expand Down
30 changes: 17 additions & 13 deletions src/root/filename.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import ddmd.root.outbuffer;
import ddmd.root.rmem;
import ddmd.root.rootobject;

nothrow
{
version (Windows) extern (C) int mkdir(const char*);
version (Windows) alias _mkdir = mkdir;
version (Posix) extern (C) char* canonicalize_file_name(const char*);
version (Windows) extern (C) int stricmp(const char*, const char*);
version (Windows) extern (C) int stricmp(const char*, const char*) pure;
version (Windows) extern (Windows) DWORD GetFullPathNameA(LPCSTR lpFileName, DWORD nBufferLength, LPSTR lpBuffer, LPSTR* lpFilePart);
}

alias Strings = Array!(const(char)*);
alias Files = Array!(File*);
Expand All @@ -37,29 +40,30 @@ alias Files = Array!(File*);
*/
struct FileName
{
nothrow:
const(char)* str;

extern (D) this(const(char)* str)
{
this.str = mem.xstrdup(str);
}

extern (C++) bool equals(const RootObject obj) const
extern (C++) bool equals(const RootObject obj) const pure
{
return compare(obj) == 0;
}

extern (C++) static bool equals(const(char)* name1, const(char)* name2)
extern (C++) static bool equals(const(char)* name1, const(char)* name2) pure
{
return compare(name1, name2) == 0;
}

extern (C++) int compare(const RootObject obj) const
extern (C++) int compare(const RootObject obj) const pure
{
return compare(str, (cast(FileName*)obj).str);
}

extern (C++) static int compare(const(char)* name1, const(char)* name2)
extern (C++) static int compare(const(char)* name1, const(char)* name2) pure
{
version (Windows)
{
Expand All @@ -78,7 +82,7 @@ struct FileName
* Returns:
* true if absolute path name.
*/
extern (C++) static bool absolute(const(char)* name)
extern (C++) static bool absolute(const(char)* name) pure
{
version (Windows)
{
Expand All @@ -103,7 +107,7 @@ struct FileName
* Points past '.' of extension.
* If there isn't one, return null.
*/
extern (C++) static const(char)* ext(const(char)* str)
extern (C++) static const(char)* ext(const(char)* str) pure
{
size_t len = strlen(str);
const(char)* e = str + len;
Expand Down Expand Up @@ -135,7 +139,7 @@ struct FileName
}
}

extern (C++) const(char)* ext() const
extern (C++) const(char)* ext() const pure
{
return ext(str);
}
Expand Down Expand Up @@ -164,7 +168,7 @@ struct FileName
/********************************
* Return filename name excluding path (read-only).
*/
extern (C++) static const(char)* name(const(char)* str)
extern (C++) static const(char)* name(const(char)* str) pure
{
size_t len = strlen(str);
const(char)* e = str + len;
Expand Down Expand Up @@ -203,7 +207,7 @@ struct FileName
assert(0);
}

extern (C++) const(char)* name() const
extern (C++) const(char)* name() const pure
{
return name(str);
}
Expand Down Expand Up @@ -436,7 +440,7 @@ struct FileName
return defaultExt(name, ext); // doesn't have one
}

extern (C++) static bool equalsExt(const(char)* name, const(char)* ext)
extern (C++) static bool equalsExt(const(char)* name, const(char)* ext) pure
{
const(char)* e = FileName.ext(name);
if (!e && !ext)
Expand All @@ -449,7 +453,7 @@ struct FileName
/******************************
* Return !=0 if extensions match.
*/
extern (C++) bool equalsExt(const(char)* ext) const
extern (C++) bool equalsExt(const(char)* ext) const pure
{
return equalsExt(str, ext);
}
Expand Down Expand Up @@ -711,7 +715,7 @@ struct FileName
mem.xfree(cast(void*)str);
}

extern (C++) const(char)* toChars() const
extern (C++) const(char)* toChars() const pure
{
return str;
}
Expand Down
4 changes: 2 additions & 2 deletions src/root/rmem.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ version (GC)
}
}

extern (C++) __gshared Mem mem;
extern (C++) const __gshared Mem mem;
}
else
{
Expand Down Expand Up @@ -122,7 +122,7 @@ else
}
}

extern (C++) __gshared Mem mem;
extern (C++) const __gshared Mem mem;

enum CHUNK_SIZE = (256 * 4096 - 64);

Expand Down

0 comments on commit 7355b52

Please sign in to comment.