@ -690,6 +690,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
enum encoding encoding ) {
EscapableHandleScope scope ( isolate ) ;
CHECK_NE ( encoding , UCS2 ) ;
CHECK_LE ( buflen , Buffer : : kMaxLength ) ;
if ( ! buflen & & encoding ! = BUFFER )
return scope . Escape ( String : : Empty ( isolate ) ) ;
@ -747,32 +748,6 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
break ;
}
case UCS2 : {
// Node's "ucs2" encoding expects LE character data inside a
// Buffer, so we need to reorder on BE platforms. See
// http://nodejs.org/api/buffer.html regarding Node's "ucs2"
// encoding specification. Note that we have to make a copy
// to avoid pointer aliasing and unaligned access, something
// that we can't guarantee by just reinterpret_casting |buf|.
size_t srclen = buflen / 2 ;
uint16_t * src = new uint16_t [ srclen ] ;
for ( size_t i = 0 , k = 0 ; i < srclen ; i + = 1 , k + = 2 ) {
const uint8_t lo = static_cast < uint8_t > ( buf [ k + 0 ] ) ;
const uint8_t hi = static_cast < uint8_t > ( buf [ k + 1 ] ) ;
src [ i ] = lo | hi < < 8 ;
}
if ( buflen < EXTERN_APEX ) {
val = String : : NewFromTwoByte ( isolate ,
src ,
String : : kNormalString ,
srclen ) ;
} else {
val = ExternTwoByteString : : NewFromCopy ( isolate , src , srclen ) ;
}
delete [ ] src ;
break ;
}
case HEX : {
size_t dlen = buflen * 2 ;
char * dst = new char [ dlen ] ;
@ -796,4 +771,39 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
return scope . Escape ( val ) ;
}
Local < Value > StringBytes : : Encode ( Isolate * isolate ,
const uint16_t * buf ,
size_t buflen ) {
const uint16_t * src = buf ;
// Node's "ucs2" encoding expects LE character data inside a
// Buffer, so we need to reorder on BE platforms. See
// http://nodejs.org/api/buffer.html regarding Node's "ucs2"
// encoding specification.
if ( IsBigEndian ( ) ) {
// Inefficient, see StringSlice<UCS2>() in src/node_buffer.cc;
// this is potentially the second copy of the actual input.
uint16_t * copy = new uint16_t [ buflen ] ;
for ( size_t i = 0 ; i < buflen ; i + = 1 )
copy [ i ] = buf [ i ] < < 8 | buf [ i ] > > 8 ;
src = copy ;
}
Local < String > val ;
if ( buflen < EXTERN_APEX ) {
val = String : : NewFromTwoByte ( isolate ,
src ,
String : : kNormalString ,
buflen ) ;
} else {
val = ExternTwoByteString : : NewFromCopy ( isolate , src , buflen ) ;
}
if ( src ! = buf )
delete [ ] src ;
return val ;
}
} // namespace node