How do I translate this bit of code from C(using pointers) to D?

MacAsm via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 26 13:58:13 PDT 2014


It maybe trivial to most of you but I don't know how do this 
avoiding string duplications. Can someone help me?

char *str = "foobaa";
char *p = str;
char *prev = NULL;

int go(void)
{
    int ch;

    ch = top();
    move(1);
    return ch;
}

int top(void)
{
   retun *p;
}

void back(void)
{
    p = prev;
}

void move(int n)
{
    prev = p;
    p += n;
}

in D, I can wrote as following:

string str = "foobaa";
string prev;

dchar go()
{
   dchar ch = top();
   move(1);
   return ch;
}

dchar top()
{
   return str.front;
}

void move(int n)
{
   prev = str; // <- I want to avoid this string duplication did 
in every move() call
   foreach(i; 0 .. n) { // <- improve this is welcome too.
       str.popFront();
    }
}

void back()
{
   str = prev;
}


Here's (for better code reading readability) C and D codes on 
pastebin,respectively :

http://pastebin.com/J3vzP3UK

http://pastebin.com/C0NyNjY0



More information about the Digitalmars-d mailing list