bencbartlett / screeps-packrat

Lightning-fast and memory-efficient serialization of Screeps IDs, Coords, and RoomPositions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String.fromCharCode does not work for surrogate characters range d800 to dfff

GDennis opened this issue · comments

return String.fromCharCode(parseInt(id.substr(0, 4), 16)) +

Include it in the unit tests:
String.fromCharCode is a problem for surrogate characters range d800 to dfff.

If such character is output into console or Memory and taken back, then it's replaced with the replacement character.
But this is also a problem in Memory.

It has the potential to corrupt the data randomly if for instance an id happen to contain a surrogate character.

A test scenario to try:
Memory.test=String.fromCharCode(0xd811) + String.fromCharCode(0xd8ab)
Then after one tick after the next reset, Memory will be rebuilt with JSON.parse by the screeps engine. For some reason these surrogate characters will be replaced by 0xfffd instead.
Then try:
Memory.test.charCodeAt(0).toString(16) + ' ' + Memory.test.charCodeAt(1).toString(16)
And you'll get fffd fffd, the replacement character. Expected would be d811 d8ab