www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Output-Range and char

reply Mafi <mafi example.org> writes:
Hi there,
every time I want to use output-ranges again they seem to be 
broken in a different way (e.g. value/reference semantics). This 
time it is char types and encoding.

How do I make formattedWrite work with a char buffer? I tried the 
following code and it fails with a *static assert*; it is 
itentionally disabled. I suspects it is again something about 
autodecoding but in this case I don't understand it.

https://dpaste.dzfl.pl/c68b3e3529f6

```
import std.format, std.stdio;

void main() {
	char[20] buffer;
	formattedWrite(buffer[], "Long string %s\n", "more more more");
	write(buffer);
}
```

Compilation output:

/opt/compilers/dmd2/include/std/range/primitives.d(351): Error: 
static assert  "Cannot put a char into a char[]."
/opt/compilers/dmd2/include/std/format.d(2647):        
instantiated from here: put!(char[], char)
/opt/compilers/dmd2/include/std/format.d(2297):        
instantiated from here: formatRange!(char[], string, char)
/opt/compilers/dmd2/include/std/format.d(3778):        
instantiated from here: formatValue!(char[], string, char)
/opt/compilers/dmd2/include/std/format.d(460):        
instantiated from here: formatGeneric!(char[], string, char)
/d446/f470.d(5):        instantiated from here: 
formattedWrite!(char[], char, string)
Apr 23 2017
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/23/2017 04:17 AM, Mafi wrote:

 /opt/compilers/dmd2/include/std/range/primitives.d(351): Error: static
 assert  "Cannot put a char into a char[]."
Appender recommended: import std.format, std.stdio, std.array; void main() { auto sink = appender!(char[])(); formattedWrite(sink, "Long string %s\n", "more more more"); write(sink.data); } Of course appender!string is more natural but I just wanted to see that it works with char[] as well. Ali
Apr 23 2017
parent Mafi <mafi example.org> writes:
On Sunday, 23 April 2017 at 12:03:58 UTC, Ali Çehreli wrote:
 On 04/23/2017 04:17 AM, Mafi wrote:

 /opt/compilers/dmd2/include/std/range/primitives.d(351): 
 Error: static
 assert  "Cannot put a char into a char[]."
Appender recommended: import std.format, std.stdio, std.array; void main() { auto sink = appender!(char[])(); formattedWrite(sink, "Long string %s\n", "more more more"); write(sink.data); } Of course appender!string is more natural but I just wanted to see that it works with char[] as well. Ali
Thank you. I see. But I would really like not to allocate ever. Instead I want to fill a given buffer, then stop formatting. I probably have to implement my own output-range for this use case. It would fill the buffer and then ignore further 'put'-calls. I can live with this. I do have a follow-up question though. Why does formattedWrite take the output-range by value? It just doesn't make sense for value-type output-ranges; especially with consecutive calls. One has to pass the pointer of the output-range (this does work because of auto-dereferencing). Forgetting it one place, lets the code still compile, with very strange semantics which most probably were not intended.
Apr 23 2017
prev sibling parent Jonathan Marler <johnnymarler gmail.com> writes:
On Sunday, 23 April 2017 at 11:17:37 UTC, Mafi wrote:
 Hi there,
 every time I want to use output-ranges again they seem to be 
 broken in a different way (e.g. value/reference semantics). 
 This time it is char types and encoding.

 [...]
Use sformat: import std.format, std.stdio; void main() { char[20] buffer; sformat(buffer, "Long string %s\n", "more more more"); write(buffer); } Note: the buffer is not large enough to hold the entire string in your example so this will cause a stack buffer overflow.
Apr 23 2017