July 02, 2006 Re: String convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niklas Ulvinge | "Niklas Ulvinge" <Niklas_member@pathlink.com> wrote in message news:e86qp9$1e3t$1@digitaldaemon.com... > Thanks for all replies, now I understand most of what I wanted to know. (although the Q about the internal structure of dynamic arrays still remains...) OK. This is the definition for a char[]. This is the same for ALL array types in D, not just strings; just replace "char" with any other type, and it's the same. struct CharArray { private size_t _length = 0; private char* _ptr = null; public size_t length(size_t l) { if(_ptr is null && l > 0) _ptr = malloc(char.sizeof * l); else { if(l > _length) _ptr = realloc(_ptr, char.sizeof * l); } _length = l; return _length; } public size_t length() { return _length; } public char* ptr() { return _ptr; } } There are other methods, such as .dup and .sort, but I won't list them. When you write char[] s; You get CharArray s; It has length 0 and pointer null. So you set its length: s.length = 5; This actually means "s.length(5)". This is because of the property syntax in D. So it allocates enough space for 5 characters. Is that satisfactory? > The foreach statemente as an example. > In D, the compiler handles the implementation. > I want to know how it is implemented. You worry too much about things that you shouldn't care about. But if you really must know, foreach is implemented as a nested function for the actual foreach body. You can see kind of how it works by looking up how to overload opApply. > In languages where "a" + "b" = "ab" works there could be programmers who > doesn't > see that concating is much more complex than adding a couple of numbers. > In D, this is a little better, becouse it's hard to find the concating > char (I > don't have it now, becouse of an odd bug in firefox). > In C/C++ this is better, becouse it was a func, wich indicated how hard it > was > to do. Not for std::string; that used + for string concatenation. Sure hides the implementation details. > Some programmers may instead of using: > writef(a,b,c) > concate them. Wich would be very bad. Unless you really _need_ to concatenate strings, such as to store in a new string. |
July 03, 2006 Re: String convention | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niklas Ulvinge | Niklas Ulvinge wrote: > Thanks for all replies, now I understand most of what I wanted to know. > (although the Q about the internal structure of dynamic arrays still remains...) > > > In article <op.tb03wsb06b8z09@ginger.vic.bigpond.net.au>, Derek Parnell says... >> But you as a coder don't need to worry about this because the compiler = >> >> handles all the manipulation for you. >> > > I think as 'real programmers' ;) : > "Real programmers can write assembly langauge in any language" > > This is very hard to do in D, but really easy in C. > > The foreach statemente as an example. > In D, the compiler handles the implementation. > I want to know how it is implemented. > > > In languages where "a" + "b" = "ab" works there could be programmers who doesn't > see that concating is much more complex than adding a couple of numbers. > In D, this is a little better, becouse it's hard to find the concating char (I > don't have it now, becouse of an odd bug in firefox). > In C/C++ this is better, becouse it was a func, wich indicated how hard it was > to do. A crucial difference between C++ and D, is that the compiler understands the concept of concatenation. This means that you can concatenate at compile time. const char [] str1 = "a"; const char [] str2 = str1 ~ "b" creates str2 as a compile-time constant. In this case the concatenation has zero run-time cost. You can use it anywhere that a compile-time string literal can be used. You couldn't do this with function calls. > Some programmers may instead of using: > writef(a,b,c) > concate them. Wich would be very bad. |
Copyright © 1999-2021 by the D Language Foundation