www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem using writeExact (std.stream)

reply Sebastián E. Peyrott <Sebastián_member pathlink.com> writes:
Hi, I've been playing around with D for some days, and I feel pretty    
compfortable ith the language, but there's a problem that I haven't been able

to sort out yet:    

Using writeExact to write to stdout using a Dynamic Array that has just been    
resized tends to truncate the output. Using printf to show the contents of the

buffer works OK.    

Example:    
1) Import std.cstream  
2) Put this code in your main.  
3) Redirect a ~2K file to stdio.  

for (i = 0; !din.eof; i++)   
{   
if (i == Buf_InputSequence.length)    
{ Buf_InputSequence.length = Buf_InputSequence.length * 2; }   
Buf_InputSequence ~= din.getc();   
}   
Buf_InputSequence.length = i + 1;   
dout.writeExact(Buf_InputSequence, Buf_InputSequence.length);   

If you manipulate the length passed to writeExact (2nd argument), it's  
possible to print all the contents from the buffer. It's necessary to increase  
the value to twice the length of the buffer for this example to work  
correctly.  
Using functions that depend on writeExact obviously behaves the same way  
(writeLine, etc).  
My guess is there is something wrong in the way size_t is defined in my  
system. I'm on Linux (64bits), using dmd 0.140 and linking with gcc 3.4.4 (in  
32bit mode). 

Any clues as to what may be the problem? Thanks! 
Jan 15 2006
parent reply Sebastián E. Peyrott <as7cf yahoo.com> writes:
OK, I've tried using some functions from std.regexp to work on the truncated 
buffer. RegExp.replace is not parsing the buffer at all, and ends up returning 
the original buffer. 
The only workaround I could come up with is to create another dynamic array 
and copy the original buffer there.
Am I missing something? Is this behaviour correct? Should I do something else
besides changing the value of the length property to resize the dynamic array?
Jan 15 2006
parent Sebastián E. Peyrott <as7cf yahoo.com> writes:
I found the error. Concatenation treats null characters as valid characters, so
they are not taken out each time Buf_InputSequence ~= din.getc() comes into
play. That must creates a sparse array of characters. From that point on, the
behaviour of different functions becomes undefined, some parse the array up to a
specific point (writeLine), others do nothing (RegExp.replace)...

In any case, I'm sorry for posting too early.
Jan 15 2006