Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 26, 2014 How do I translate this bit of code from C(using pointers) to D? | ||||
---|---|---|---|---|
| ||||
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 |
August 26, 2014 Re: How do I translate this bit of code from C(using pointers) to D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MacAsm | 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() call This 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 |
August 26, 2014 Re: How do I translate this bit of code from C(using pointers) to D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MacAsm | 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() call If 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 |
August 27, 2014 Re: How do I translate this bit of code from C(using pointers) to D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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:
>> 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() call
>
> This does not duplicate the string. You may find this article helpful:
>
> http://dlang.org/d-array-article.html
>
>
>
Thanks. This is exactly what I was worried about. If no string duplciation is done I think this D code is just fine...
|
Copyright © 1999-2021 by the D Language Foundation