@@ -320,17 +320,23 @@ function caml_bytes_set(s, i, c) {
320320 return caml_bytes_unsafe_set ( s , i , c ) ;
321321}
322322
323+ //Provides: jsoo_text_encoder
324+ var jsoo_text_encoder = new TextEncoder ( ) ;
325+
326+ //Provides: jsoo_text_decoder
327+ var jsoo_text_decoder = new TextDecoder ( ) ;
328+
323329//Provides: caml_bytes_of_utf16_jsstring
324- //Requires: MlBytes
330+ //Requires: MlBytes, jsoo_text_encoder
325331function caml_bytes_of_utf16_jsstring ( s ) {
326- var e = new TextEncoder ( ) ;
327- var a = e . encode ( s ) ;
332+ var a = jsoo_text_encoder . encode ( s ) ;
328333 return new MlBytes ( 4 , a , a . length ) ;
329334}
330335
331336//Provides: MlBytes
332337//Requires: caml_convert_string_to_bytes, jsoo_is_ascii
333338//Requires: caml_uint8_array_of_bytes
339+ //Requires: jsoo_text_decoder
334340function MlBytes ( tag , contents , length ) {
335341 this . t = tag ; this . c = contents ; this . l = length ;
336342}
@@ -353,8 +359,7 @@ MlBytes.prototype.toString = function () {
353359MlBytes . prototype . toUtf16 = function ( ) {
354360 if ( this . t == 9 ) return this . c ;
355361 var a = caml_uint8_array_of_bytes ( this ) ;
356- let d = new TextDecoder ( ) ;
357- return d . decode ( a ) ;
362+ return jsoo_text_decoder . decode ( a ) ;
358363}
359364MlBytes . prototype . slice = function ( ) {
360365 var content = this . t == 4 ? this . c . slice ( ) : this . c ;
@@ -668,25 +673,32 @@ function caml_jsbytes_of_string(x) {
668673 return x ;
669674}
670675
676+ //Provides: jsoo_text_decoder_buff
677+ var jsoo_text_decoder_buff = new ArrayBuffer ( 1024 ) ;
678+
671679//Provides: caml_jsstring_of_string const
672680//Requires: jsoo_is_ascii
681+ //Requires: jsoo_text_decoder
682+ //Requires: jsoo_text_decoder_buff
673683//If: js-string
674684function caml_jsstring_of_string ( s ) {
675685 if ( jsoo_is_ascii ( s ) ) return s ;
676- var a = new Uint8Array ( s . length ) ;
686+ var a =
687+ ( s . length <= jsoo_text_decoder_buff . length )
688+ ? Uint8Array ( jsoo_text_decoder_buff , 0 , s . length )
689+ : ( new Uint8Array ( s . length ) ) ;
677690 for ( var i = 0 ; i < s . length ; i ++ ) {
678691 a [ i ] = s . charCodeAt ( i ) ;
679692 }
680- var d = new TextDecoder ( ) ;
681- return d . decode ( a ) ;
693+ return jsoo_text_decoder . decode ( a ) ;
682694}
683695
684696//Provides: caml_string_of_jsstring const
685697//Requires: caml_string_of_array
698+ //Requires: jsoo_text_encoder
686699//If: js-string
687700function caml_string_of_jsstring ( s ) {
688- var e = new TextEncoder ( ) ;
689- var a = e . encode ( s ) ;
701+ var a = jsoo_text_encoder . encode ( s ) ;
690702 return caml_string_of_array ( a ) ;
691703}
692704
0 commit comments