digitalmars.D.learn - Assigning to char[N]
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (13/13) Feb 01 2012 import std.stdio;
- bearophile (20/21) Feb 01 2012 In your code it's the definition too that throws an exception:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (31/52) Feb 01 2012 Of course! I need sleep. :( I also failed to mention that the rest of
- bearophile (5/16) Feb 01 2012 Try harder. Try to slice that a.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (15/31) Feb 01 2012 be required).
- Andrej Mitrovic (6/6) Feb 01 2012 OT: Just saw fill() by accident and something caught my eye:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/12) Feb 01 2012 Although "bla" is an array of char, it is a range of dchar; and dchar
- Steven Schveighoffer (13/28) Feb 06 2012 But this is the kind of unintuitive shit that treating char arrays not a...
import std.stdio; void main() { char[100] a = "old content"; a = "new content"; } The program above causes an exception to be thrown: object.Exception src/rt/arraycat.d(31): lengths don't match for array copy I admit that a fixed-length char array is not really a string. But assuming that the new content is legal UTF-8, what is the best way of modifying that array? Thank you, Ali
Feb 01 2012
Ali Çehreli:what is the best way of modifying that array?In your code it's the definition too that throws an exception: void main() { char[100] a = "old content"; } This works, but it's not nice: import std.stdio; void main() { char[100] a; string s1 = "old content"; a[0 .. s1.length] = s1; writeln(a); string s2 = "new content"; a[0 .. s2.length] = s2; writeln(a); a[0 .. "new content".length] = "new content"; writeln(a); } Bye, bearophile
Feb 01 2012
On 02/01/2012 03:14 PM, bearophile wrote:Ali Çehreli:Of course! I need sleep. :( I also failed to mention that the rest of the characters should be '\0' filled too but it's not a big deal.what is the best way of modifying that array?In your code it's the definition too that throws an exception:void main() { char[100] a = "old content"; } This works, but it's not nice: import std.stdio; void main() { char[100] a; string s1 = "old content"; a[0 .. s1.length] = s1; writeln(a); string s2 = "new content"; a[0 .. s2.length] = s2; writeln(a); a[0 .. "new content".length] = "new content"; writeln(a); } Bye, bearophileThe following is some of mine and the ones that did not work. import std.stdio; import std.algorithm; import std.string; import std.array; void main() { char[100] a; // explicit: foreach (i, c; "one") { a[i] = c; } a["one".length .. $] = '\0'; writeln(a); a = leftJustify("two", a.length, '\0'); writeln(a); // copy(a, "new content"); // fill(a, "new content"); // insertInPlace(a, "new content"); // // Those do not work with errors similar to this: // // Error: template std.algorithm.copy(Range1,Range2) if // (isInputRange!(Range1) && // isOutputRange!(Range2,ElementType!(Range1))) does not match any // function template declaration } Ali
Feb 01 2012
Ali:a["one".length .. $] = '\0';Is DMD able to optimize that string literal away? (Some assembly may be required).// copy(a, "new content"); // fill(a, "new content"); // insertInPlace(a, "new content"); // // Those do not work with errors similar to this: // // Error: template std.algorithm.copy(Range1,Range2) if // (isInputRange!(Range1) && // isOutputRange!(Range2,ElementType!(Range1))) does not match any // function template declarationTry harder. Try to slice that a. Bye, bearophile
Feb 01 2012
On 02/01/2012 04:34 PM, bearophile wrote:Ali:be required). I sure hope so. "one".length should be a compile time constant.a["one".length .. $] = '\0';Is DMD able to optimize that string literal away? (Some assembly maymatch any// copy(a, "new content"); // fill(a, "new content"); // insertInPlace(a, "new content"); // // Those do not work with errors similar to this: // // Error: template std.algorithm.copy(Range1,Range2) if // (isInputRange!(Range1)&& // isOutputRange!(Range2,ElementType!(Range1))) does notFirst of all, the parameters in my copy() call were swapped above. It should have been source then destination: copy("new content", a); But it won't work, because strings are ranges of Unicode code points (i.e. dchar ranges). Even narrow strings are exposed as dchar ranges. So the source is an InputRange in that call, and the destination is an output range, but their element types don't match. The following works because now all of the elements are dchar: dchar[100] a = '\0'; copy("new content"d, a[]); Ali// function template declarationTry harder. Try to slice that a. Bye, bearophile
Feb 01 2012
OT: Just saw fill() by accident and something caught my eye: char[100] a; fill(a[], "bla"); // fail, ok int[100] a; fill(a[], "bla"); // works It could be a constraint issue. To bugzilla?
Feb 01 2012
On 02/01/2012 04:24 PM, Andrej Mitrovic wrote:OT: Just saw fill() by accident and something caught my eye: char[100] a; fill(a[], "bla"); // fail, ok int[100] a; fill(a[], "bla"); // works It could be a constraint issue.Although "bla" is an array of char, it is a range of dchar; and dchar automatically converts to int.To bugzilla?I don't think so, because it's just dchar to int conversion. Ali
Feb 01 2012
On Thu, 02 Feb 2012 00:38:26 -0500, Ali =C3=87ehreli <acehreli yahoo.com=wrote:On 02/01/2012 04:24 PM, Andrej Mitrovic wrote: > OT: Just saw fill() by accident and something caught my eye: > > char[100] a; > fill(a[], "bla"); // fail, ok > > int[100] a; > fill(a[], "bla"); // works > > It could be a constraint issue. Although "bla" is an array of char, it is a range of dchar; and dchar ==automatically converts to int.But this is the kind of unintuitive shit that treating char arrays not a= s = arrays in *some* parts of D causes great confusion and silly limitations= . = The above should be the opposite, the first line should pass and the = second should fail.> To bugzilla? I don't think so, because it's just dchar to int conversion.You are likely to get an "invalid" response due to the strict views of = Andrei, but I think it's unacceptable behavior for fill to say it cannot= = fill a string with string data. -Steve
Feb 06 2012