Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Buffer#write adds trailing 0x00 for utf-8 but not ascii #2446

Closed
felixge opened this issue Dec 31, 2011 · 3 comments
Closed

Buffer#write adds trailing 0x00 for utf-8 but not ascii #2446

felixge opened this issue Dec 31, 2011 · 3 comments
Labels

Comments

@felixge
Copy link

felixge commented Dec 31, 2011

I'd love to fix this (make it consistent), but first I'm wondering if anybody has comments on this:

When writing out a 'utf-8' string, it gets a terminating 0x00 (C-Style String):

> buffer = new Buffer([255, 255, 255])
<Buffer ff ff ff>
> buffer.write('Ö', 'utf-8')
2
> buffer
<Buffer c3 96 00>

However, when writing out a string using 'ascii' encoding, no trailing 0x00 is added:

> buffer = new Buffer([255, 255, 255])
<Buffer ff ff ff>
> buffer.write('Oe', 'ascii')
2
> buffer
<Buffer 4f 65 ff>

IMO this is very inconsistent. Personally I'd favor never adding a trailing 0x00 since the underlaying assumption (everything is stored as a 0x00-terminated string) is not true.

@bnoordhuis
Copy link
Member

What version of Node are you seeing this with, Felix? v0.6 and master both use NO_NULL_TERMINATION. To wit:

$ v0.6/out/Debug/node 
> buffer = new Buffer([255, 255, 255])
<Buffer ff ff ff>
> buffer.write('Ö', 'utf-8')
2
> buffer
<Buffer c3 96 ff>

@koichik
Copy link

koichik commented Jan 5, 2012

Yes, this has fixed in 6a72e52. See also #394 and #1902.
Closing.

@koichik koichik closed this as completed Jan 5, 2012
@felixge
Copy link
Author

felixge commented Jan 5, 2012

What version of Node are you seeing this with, Felix? v0.6 and master both use NO_NULL_TERMINATION. To wit:

Yeah, sorry I was testing with node 0.4.12, it's fixed in 0.6. I can work around it in 0.4.x so I guess it doesn't need backporting.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants