www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - create fixed length string of characters

reply Bruce <bruce technisolve.co.za> writes:
Is there an easy way to create a 60 character string in D?
Like in Python...
ul = '-'*60
Aug 15
next sibling parent Olivier Pisano <olivier.pisano laposte.net> writes:
On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 Is there an easy way to create a 60 character string in D?
 Like in Python...
 ul = '-'*60
You can use the repeat() function in std.range to create a range of N consecutive elements. If you need to assign this range to a string, you'll have to use the array() function. import std.range; import std.stdio; void main() { string s = '-'.repeat(60).array; writeln(s); }
Aug 15
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 Is there an easy way to create a 60 character string in D?
 Like in Python...
 ul = '-'*60
2 ways: ```d // use a fixed array: immutable char[60] a = '-'; string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); // allocate mutable array on heap char[] b = new char[60]; b[] = '-'; // if b is unique and no longer used s = cast(string)b; // not safe s.writeln(); ``` `a` is initialized from a single element which is copied over each of its elements. `b` uses an array operation to assign a single element to each of its elements. I think the 2nd way may be more efficient, but it requires an unsafe cast.
Aug 16
next sibling parent reply IchorDev <zxinsworld gmail.com> writes:
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
Aug 16
next sibling parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:
 On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique. https://dlang.org/spec/function.html#pure-factory-functions So perhaps `idup` is no longer needed.
Aug 16
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, August 16, 2024 9:58:31 AM MDT Nick Treleaven via Digitalmars-d-
learn wrote:
 On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:
 On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique. https://dlang.org/spec/function.html#pure-factory-functions So perhaps `idup` is no longer needed.
Well, you if you use dup, you're asking for a mutable array, whereas if you use idup, you're asking for an immutable array. Whether the result of dup is then able to be implicitly converted to immutable based on whether the operation is pure depends on the element type and where the result is used. So, dup may very well work in this particular case, but in the general case, you really want to be using idup if you want immutable. And in this particular example, all it would take to make the result not immutable would be to use auto instead of string. - Jonathan M Davis
Aug 16
parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote:
 Well, you if you use dup, you're asking for a mutable array, 
 whereas if you use idup, you're asking for an immutable array.
Yes, `idup` may be needed e.g. for overloads varying on mutability.
 Whether the result of dup is then able to be implicitly 
 converted to immutable based on whether the operation is pure 
 depends on the element type and where the result is used.
If it can't be converted to immutable then `idup` won't work either.
 So, dup may very well work in this particular case, but in the 
 general case, you really want to be using idup if you want 
 immutable. And in this particular example, all it would take to 
 make the result not immutable would be to use auto instead of 
 string.
Because `idup` exists, sure use that. But writing `immutable s = a.dup;` does the same thing.
Aug 16
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, August 16, 2024 10:37:45 AM MDT Nick Treleaven via Digitalmars-d-
learn wrote:
 On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote:
 Whether the result of dup is then able to be implicitly
 converted to immutable based on whether the operation is pure
 depends on the element type and where the result is used.
If it can't be converted to immutable then `idup` won't work either.
Yes, but if you use idup, then the result is always immutable, whereas if you use dup, it's mutable unless it's used in a context where it has to be immutable. So, in the general case, you need to use idup if you want to be sure that you get immutable. You can still get immutable in specific cases, but there will be plenty of cases where you won't, and even if you do right now, that could change when refactoring. Something as simple as a function being changed from taking string to taking a range of characters could then change the type that you get. So, it's probably better practice to just always use idup when you want immutable, but obviously, developers can choose to do otherwise and make it work just fine so long as they're consistently using the result of dup in a context which requires immutable. - Jonathan M Davis
Aug 16
prev sibling parent IchorDev <zxinsworld gmail.com> writes:
On Friday, 16 August 2024 at 15:58:31 UTC, Nick Treleaven wrote:
 On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:
 On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven 
 wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique.
Oh, that’s awesome I didn’t know pure stuff can do that.
 https://dlang.org/spec/function.html#pure-factory-functions

 So perhaps `idup` is no longer needed.
It’s more explicit so I think still generally preferable.
Aug 16
prev sibling parent reply Renato Athaydes <renato athaydes.com> writes:
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:
 On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
I found the docs of `dup` here: https://dlang.org/library/std/container/array/array.html But not `idup`. Where does that come from?
Aug 16
parent Renato Athaydes <renato athaydes.com> writes:
On Friday, 16 August 2024 at 18:00:25 UTC, Renato Athaydes wrote:
 On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:
 On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven 
 wrote:
 On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
 ```d
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
 ```
I think you meant `idup`? `dup` will make it mutable.
I found the docs of `dup` here: https://dlang.org/library/std/container/array/array.html But not `idup`. Where does that come from?
Ah nevermind, I was looking at the container `Array` not the built-in array type, which is documented here: https://dlang.org/spec/arrays.html#array-properties which does mention `idup`!
Aug 16
prev sibling parent reply Bruce <bruce technisolve.co.za> writes:
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:

 ```d
 // use a fixed array:
 immutable char[60] a = '-';
 string s = a.dup; // copy to heap, assuming you need the data 
 to escape (use a[] otherwise)
 s.writeln();
This seems to work without having to make a string dup. I just wanted to write a separator line in my output. Is there anything wrong with using the char[60] directly? import std.stdio; immutable char[60] a = '-'; void main() { a.writeln(); }
Aug 16
parent IchorDev <zxinsworld gmail.com> writes:
On Saturday, 17 August 2024 at 06:03:09 UTC, Bruce wrote:
Is there anything wrong
 with using
 the char[60] directly?
In this case it’s fine to.
Aug 17