[your code here]

Timon Gehr timon.gehr at gmx.ch
Sun Jan 15 08:26:51 PST 2012


On 01/15/2012 03:23 PM, Jos van Uden wrote:
> import std.conv, std.traits, std.ascii;
>
> S rot13(S)(S s) if (isSomeString!S) {
> return rot(s, 13);
> }
>
> S rot(S)(S s, int key) if (isSomeString!S) {
> dchar[] r = new dchar[s.length];
>
> foreach (i, dchar c; s) {
> if (isLower(c))
> c = ((c - 'a' + key) % 26 + 'a');
> else if (isUpper(c))
> c = ((c - 'A' + key) % 26 + 'A');
> r[i] = c;
> }
> return to!S(r);
> }
>
>
> ---
>
> still learning this stuff, feel free to correct or improve
>
> Jos

Your code will fail for narrow strings containing Unicode.
dchar[] r = new dchar[s.length]; // creates a new dchar[] with as many 
elements as there are code units in the input string

You can use std.range.walkLength to get the number of code points.

Furthermore, that line mentions the type twice.

auto r = new dchar[s.walkLength()];


More information about the Digitalmars-d mailing list