|
Revision 150, 1.4 kB
(checked in by Don Clugston, 9 months ago)
|
Strided array accesses are now parsed OK, and dimensionality errors are detected. (But there's no codegen support yet).
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
// Written in the D programming language 1.0 |
|---|
| 2 |
/** |
|---|
| 3 |
* General CTFE functions |
|---|
| 4 |
* BLADE 0.3Alpha -- Basic Linear Algebra D Expressions |
|---|
| 5 |
*/ |
|---|
| 6 |
|
|---|
| 7 |
module blade.BladeUtil; |
|---|
| 8 |
|
|---|
| 9 |
public: |
|---|
| 10 |
|
|---|
| 11 |
// Convert from integral type to string. |
|---|
| 12 |
char [] itoa(T)(T x) |
|---|
| 13 |
{ |
|---|
| 14 |
char [] s=""; |
|---|
| 15 |
static if (is(T==byte)||is(T==short)||is(T==int)||is(T==long)) { |
|---|
| 16 |
if (x<0) { |
|---|
| 17 |
return "-" ~ itoa(-x); |
|---|
| 18 |
} |
|---|
| 19 |
} |
|---|
| 20 |
do { |
|---|
| 21 |
s = cast(char)('0' + (x%10)) ~ s; |
|---|
| 22 |
x/=10; |
|---|
| 23 |
} while (x>0); |
|---|
| 24 |
return s; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
unittest { |
|---|
| 29 |
assert(blade.BladeUtil.itoa(-5)=="-5"); |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
/** Escape any quotes and backslashes inside the given string, |
|---|
| 33 |
* prefixing them with the given escape sequence. Use `\` to escape |
|---|
| 34 |
* once, `\\\` to escape twice. |
|---|
| 35 |
*/ |
|---|
| 36 |
char [] enquote(char [] instr, char [] escape=`\`) |
|---|
| 37 |
{ |
|---|
| 38 |
// This function is critical for compilation speed. |
|---|
| 39 |
// Need to minimise the number of allocations. |
|---|
| 40 |
// It's worth using copy-on-write even for CTFE. |
|---|
| 41 |
|
|---|
| 42 |
for(int i=0; i<instr.length; ++i) { |
|---|
| 43 |
if (instr[i]=='"' || instr[i]=='\\') { |
|---|
| 44 |
char [] str = instr[0..i] ~ escape; |
|---|
| 45 |
int m = i; |
|---|
| 46 |
foreach(int k, char c; instr[i+1..$]) { |
|---|
| 47 |
if (c=='"' || c=='\\') { |
|---|
| 48 |
str ~= instr[m..i+1+k] ~ escape; |
|---|
| 49 |
m = i+k+1; |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
return str ~ instr[m..$]; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
return instr; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
unittest { |
|---|
| 59 |
assert(enquote(`"a\"`)==`\"a\\\"`); |
|---|
| 60 |
assert(enquote(`abc`)==`abc`); |
|---|
| 61 |
} |
|---|