digitalmars.D - How do I translate this bit of code from C(using pointers) to D?
- MacAsm (54/54) Aug 26 2014 It maybe trivial to most of you but I don't know how do this
- H. S. Teoh via Digitalmars-d (8/14) Aug 26 2014 [...]
- MacAsm (4/19) Aug 27 2014 Thanks. This is exactly what I was worried about. If no string
- anonymous (18/25) Aug 26 2014 If you're worried that this copies the string data, it doesn't. A
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
Aug 26 2014
On Tue, Aug 26, 2014 at 08:58:13PM +0000, MacAsm via Digitalmars-d wrote:It maybe trivial to most of you but I don't know how do this avoiding string duplications. Can someone help me?[...]in D, I can wrote as following:[...]void move(int n) { prev = str; // <- I want to avoid this string duplication did in every move() callThis does not duplicate the string. You may find this article helpful: http://dlang.org/d-array-article.html T -- Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard
Aug 26 2014
On Tuesday, 26 August 2014 at 21:29:04 UTC, H. S. Teoh via Digitalmars-d wrote:On Tue, Aug 26, 2014 at 08:58:13PM +0000, MacAsm via Digitalmars-d wrote:Thanks. This is exactly what I was worried about. If no string duplciation is done I think this D code is just fine...It maybe trivial to most of you but I don't know how do this avoiding string duplications. Can someone help me?[...]in D, I can wrote as following:[...]void move(int n) { prev = str; // <- I want to avoid this string duplication did in every move() callThis does not duplicate the string. You may find this article helpful: http://dlang.org/d-array-article.html
Aug 27 2014
On Tuesday, 26 August 2014 at 20:58:15 UTC, MacAsm wrote:string str = "foobaa"; string prev;[...]prev = str; // <- I want to avoid this string duplication did in every move() callIf you're worried that this copies the string data, it doesn't. A dynamic array (which string is) is a pair of a pointer and a length, and only those are copied.foreach(i; 0 .. n) { // <- improve this is welcome too. str.popFront(); }You can use std.range.popFrontN: str.popFrontN(n); Also, there are two (maybe more) subtle differences between the D code and the C code, of which you may not be aware: 1) The D version assumes UTF8. A char is a UTF8 code unit in D, whereas in C it's just a byte. In D that would be (u)byte. But char is fine when you're indeed working on UTF8. 2) The D version decodes to code points (dchar). This means e.g., that the D version potentially reaches the end of the string in fewer `go` calls than the C version. Finally, the "learn" board [1] would have been a better place for this post. No turning back now, and it's not a big deal, but next time maybe post there. [1] http://forum.dlang.org/group/digitalmars.D.learn
Aug 26 2014