Thread overview
Segfault using std.algorithm.map
Oct 16, 2010
klickverbot
Oct 16, 2010
klickverbot
Oct 16, 2010
klickverbot
October 16, 2010
As you might know from the main D NG, I have just started to solve a few little programming puzzles with D2/Phobos to test how well especially the algorithm/range stuff in Phobos works out in more-or-less real world use cases.

To solve a Caesar-cipher related challenge (from hacker.org, by the way), I wrote up the following simple program:

---
import std.algorithm;
import std.stdio;

enum INPUT = "cqrb lryqna rb fjh, fjh qjamna cqjw axc cqracnnw. qnan, hxd wnena twxf qxf oja cx bqroc! xq kh cqn fjh, cqn jwbfna rb mnjmvjwblqnbc.";

void main() {
   foreach ( offset; 0..25 ) {
      writeln( map!( ( dchar c ) {
         if ( c < 'a' ) return c;
         if ( 'z' < c ) return c;
         return cast( dchar )( ( ( ( c - 'a' ) + offset ) % 26 ) + 'a' );
      } )( INPUT ) );
   }
}
---

Seems reasonable, right? Using DMD 2.049, however, it segfaults in the delegate literal. Why this? Have I missed something obvious?

If you don't want to compile it yourself, I have posted the backtrace here: http://gist.github.com/630201

David
October 16, 2010
On 10/16/10 10:13 PM, klickverbot wrote:
> ---
> import std.algorithm;
> import std.stdio;
>
> enum INPUT = "cqrb lryqna rb fjh, fjh qjamna cqjw axc cqracnnw. qnan,
> hxd wnena twxf qxf oja cx bqroc! xq kh cqn fjh, cqn jwbfna rb
> mnjmvjwblqnbc.";
>
> void main() {
> foreach ( offset; 0..25 ) {
> writeln( map!( ( dchar c ) {
> if ( c < 'a' ) return c;
> if ( 'z' < c ) return c;
> return cast( dchar )( ( ( ( c - 'a' ) + offset ) % 26 ) + 'a' );
> } )( INPUT ) );
> }
> }
> ---

Okay, after experimenting for a bit, I found that this works:

      writeln( map!( ( c ){
         return offset;
      } )( INPUT ) );

While this segfaults:

      writeln( map!( ( c ){
         return cast( dchar )( offset );
      } )( INPUT ) );

I guess this should go to bugzilla, right?
October 16, 2010
Forget it, I took this directly to bugzilla:
http://d.puremagic.com/issues/show_bug.cgi?id=5064