Using arrays with functions taking ranges
Mu
invalid_email at address.int
Thu Dec 13 05:52:29 PST 2012
The code below works as is with memory mapped files' casted
opSlice's.
However, I can't figure out how to test it with simple arrays.
Its unittest fails.
Question:
How to implement the function correctly?
Otherwise what is the correct way to test it?
Thank you.
Code:
----------------
Range2 caesarCipher(Range1, Range2)(Range1 input, Range2 output,
int shift)
if (isInputRange!Range1 && isOutputRange!(Range2, ubyte))
{
auto rotAb = lowercase.dtext.dup; // rotated alphabet
shift %= lowercase.length.to!int; // bring the shift within the
length of the alphabet
if (shift < 0)
bringToFront(rotAb[0 .. $ + shift], rotAb[$ + shift .. $]);
else
bringToFront(rotAb[0 .. shift], rotAb[shift .. $]);
foreach (i, ref o; output)
{
const char c = input[i];
if (isAlpha(c))
if (isUpper(c))
o = toUpper(rotAb[lowercase.countUntil(toLower(c))]).to!ubyte;
else
o = rotAb[lowercase.countUntil(c)].to!ubyte;
else
o = c;
}
return output;
}
unittest
{
ubyte[] uba;
assert(caesarCipher("Exxego ex srgi!", uba, -56).to!string ==
"Attack at once!");
}
More information about the Digitalmars-d-learn
mailing list