The capacity is the size of the memory allocated, the length is the number of 
`char` that are not 0x00 .

Basically : 
    
    
    a.len() # Maximum number of char you can write in the string memory
    a.cstring.len() # Number of char before the first char(0) character
    
    
    Run

That is because C consider the string to end at the first null character while 
Nim doesn't have this limitation.

Since you allocated a string that is bigger than what C write you end up with a 
Nim string with a lot of trailing char(0) that you need to remove.

Which is why we do :
    
    
    
    mystring = $(mystring.cstring) # My solution
    setLen(mystring, mystring.cstring.len) # PMunch's solution, that is 
(arguably) cleaner
    
    
    Run

Reply via email to