@@ -72,37 +72,19 @@ using v8::Value;
72
72
73
73
namespace i18n {
74
74
75
- const size_t kStorageSize = 1024 ;
76
-
77
- // TODO(jasnell): This could potentially become a member of MaybeStackBuffer
78
- // at some point in the future. Care would need to be taken with the
79
- // MaybeStackBuffer<UChar> variant below.
80
- MaybeLocal<Object> AsBuffer (Isolate* isolate,
81
- MaybeStackBuffer<char >* buf,
82
- size_t len) {
83
- if (buf->IsAllocated ()) {
84
- MaybeLocal<Object> ret = Buffer::New (isolate, buf->out (), len);
85
- if (!ret.IsEmpty ()) buf->Release ();
75
+ template <typename T>
76
+ MaybeLocal<Object> ToBufferEndian (Environment* env, MaybeStackBuffer<T>* buf) {
77
+ MaybeLocal<Object> ret = Buffer::New (env, buf);
78
+ if (ret.IsEmpty ())
86
79
return ret;
87
- }
88
- return Buffer::Copy (isolate, buf->out (), len);
89
- }
90
80
91
- MaybeLocal<Object> AsBuffer (Isolate* isolate,
92
- MaybeStackBuffer<UChar>* buf,
93
- size_t len) {
94
- char * dst = reinterpret_cast <char *>(**buf);
95
- MaybeLocal<Object> ret;
96
- if (buf->IsAllocated ()) {
97
- ret = Buffer::New (isolate, dst, len);
98
- if (!ret.IsEmpty ()) buf->Release ();
99
- } else {
100
- ret = Buffer::Copy (isolate, dst, len);
101
- }
102
- if (!ret.IsEmpty () && IsBigEndian ()) {
103
- SPREAD_BUFFER_ARG (ret.ToLocalChecked (), buf);
104
- SwapBytes16 (buf_data, buf_length);
81
+ static_assert (sizeof (T) == 1 || sizeof (T) == 2 ,
82
+ " Currently only one- or two-byte buffers are supported" );
83
+ if (sizeof (T) > 1 && IsBigEndian ()) {
84
+ SPREAD_BUFFER_ARG (ret.ToLocalChecked (), retbuf);
85
+ SwapBytes16 (retbuf_data, retbuf_length);
105
86
}
87
+
106
88
return ret;
107
89
}
108
90
@@ -138,14 +120,14 @@ void CopySourceBuffer(MaybeStackBuffer<UChar>* dest,
138
120
}
139
121
}
140
122
141
- typedef MaybeLocal<Object> (*TranscodeFunc)(Isolate* isolate ,
123
+ typedef MaybeLocal<Object> (*TranscodeFunc)(Environment* env ,
142
124
const char * fromEncoding,
143
125
const char * toEncoding,
144
126
const char * source,
145
127
const size_t source_length,
146
128
UErrorCode* status);
147
129
148
- MaybeLocal<Object> Transcode (Isolate* isolate ,
130
+ MaybeLocal<Object> Transcode (Environment* env ,
149
131
const char * fromEncoding,
150
132
const char * toEncoding,
151
133
const char * source,
@@ -162,12 +144,14 @@ MaybeLocal<Object> Transcode(Isolate* isolate,
162
144
ucnv_convertEx (to.conv , from.conv , &target, target + limit,
163
145
&source, source + source_length, nullptr , nullptr ,
164
146
nullptr , nullptr , true , true , status);
165
- if (U_SUCCESS (*status))
166
- ret = AsBuffer (isolate, &result, target - &result[0 ]);
147
+ if (U_SUCCESS (*status)) {
148
+ result.SetLength (target - &result[0 ]);
149
+ ret = ToBufferEndian (env, &result);
150
+ }
167
151
return ret;
168
152
}
169
153
170
- MaybeLocal<Object> TranscodeToUcs2 (Isolate* isolate ,
154
+ MaybeLocal<Object> TranscodeToUcs2 (Environment* env ,
171
155
const char * fromEncoding,
172
156
const char * toEncoding,
173
157
const char * source,
@@ -181,11 +165,11 @@ MaybeLocal<Object> TranscodeToUcs2(Isolate* isolate,
181
165
ucnv_toUChars (from.conv , *destbuf, length_in_chars,
182
166
source, source_length, status);
183
167
if (U_SUCCESS (*status))
184
- ret = AsBuffer (isolate , &destbuf, length_in_chars );
168
+ ret = ToBufferEndian (env , &destbuf);
185
169
return ret;
186
170
}
187
171
188
- MaybeLocal<Object> TranscodeFromUcs2 (Isolate* isolate ,
172
+ MaybeLocal<Object> TranscodeFromUcs2 (Environment* env ,
189
173
const char * fromEncoding,
190
174
const char * toEncoding,
191
175
const char * source,
@@ -200,37 +184,42 @@ MaybeLocal<Object> TranscodeFromUcs2(Isolate* isolate,
200
184
MaybeStackBuffer<char > destbuf (length_in_chars);
201
185
const uint32_t len = ucnv_fromUChars (to.conv , *destbuf, length_in_chars,
202
186
*sourcebuf, length_in_chars, status);
203
- if (U_SUCCESS (*status))
204
- ret = AsBuffer (isolate, &destbuf, len);
187
+ if (U_SUCCESS (*status)) {
188
+ destbuf.SetLength (len);
189
+ ret = ToBufferEndian (env, &destbuf);
190
+ }
205
191
return ret;
206
192
}
207
193
208
- MaybeLocal<Object> TranscodeUcs2FromUtf8 (Isolate* isolate ,
194
+ MaybeLocal<Object> TranscodeUcs2FromUtf8 (Environment* env ,
209
195
const char * fromEncoding,
210
196
const char * toEncoding,
211
197
const char * source,
212
198
const size_t source_length,
213
199
UErrorCode* status) {
214
200
*status = U_ZERO_ERROR;
215
- MaybeStackBuffer<UChar, kStorageSize > destbuf;
201
+ MaybeStackBuffer<UChar> destbuf;
216
202
int32_t result_length;
217
- u_strFromUTF8 (*destbuf, kStorageSize , &result_length,
203
+ u_strFromUTF8 (*destbuf, destbuf. capacity () , &result_length,
218
204
source, source_length, status);
219
205
MaybeLocal<Object> ret;
220
206
if (U_SUCCESS (*status)) {
221
- ret = AsBuffer (isolate, &destbuf, result_length * sizeof (**destbuf));
207
+ destbuf.SetLength (result_length);
208
+ ret = ToBufferEndian (env, &destbuf);
222
209
} else if (*status == U_BUFFER_OVERFLOW_ERROR) {
223
210
*status = U_ZERO_ERROR;
224
211
destbuf.AllocateSufficientStorage (result_length);
225
212
u_strFromUTF8 (*destbuf, result_length, &result_length,
226
213
source, source_length, status);
227
- if (U_SUCCESS (*status))
228
- ret = AsBuffer (isolate, &destbuf, result_length * sizeof (**destbuf));
214
+ if (U_SUCCESS (*status)) {
215
+ destbuf.SetLength (result_length);
216
+ ret = ToBufferEndian (env, &destbuf);
217
+ }
229
218
}
230
219
return ret;
231
220
}
232
221
233
- MaybeLocal<Object> TranscodeUtf8FromUcs2 (Isolate* isolate ,
222
+ MaybeLocal<Object> TranscodeUtf8FromUcs2 (Environment* env ,
234
223
const char * fromEncoding,
235
224
const char * toEncoding,
236
225
const char * source,
@@ -241,20 +230,21 @@ MaybeLocal<Object> TranscodeUtf8FromUcs2(Isolate* isolate,
241
230
const size_t length_in_chars = source_length / sizeof (UChar);
242
231
int32_t result_length;
243
232
MaybeStackBuffer<UChar> sourcebuf;
244
- MaybeStackBuffer<char , kStorageSize > destbuf;
233
+ MaybeStackBuffer<char > destbuf;
245
234
CopySourceBuffer (&sourcebuf, source, source_length, length_in_chars);
246
- u_strToUTF8 (*destbuf, kStorageSize , &result_length,
235
+ u_strToUTF8 (*destbuf, destbuf. capacity () , &result_length,
247
236
*sourcebuf, length_in_chars, status);
248
237
if (U_SUCCESS (*status)) {
249
- ret = AsBuffer (isolate, &destbuf, result_length);
238
+ destbuf.SetLength (result_length);
239
+ ret = ToBufferEndian (env, &destbuf);
250
240
} else if (*status == U_BUFFER_OVERFLOW_ERROR) {
251
241
*status = U_ZERO_ERROR;
252
242
destbuf.AllocateSufficientStorage (result_length);
253
243
u_strToUTF8 (*destbuf, result_length, &result_length, *sourcebuf,
254
244
length_in_chars, status);
255
245
if (U_SUCCESS (*status)) {
256
- ret = Buffer::New (isolate, * destbuf, result_length);
257
- destbuf. Release ( );
246
+ destbuf. SetLength ( result_length);
247
+ ret = ToBufferEndian (env, &destbuf );
258
248
}
259
249
}
260
250
return ret;
@@ -320,7 +310,7 @@ void Transcode(const FunctionCallbackInfo<Value>&args) {
320
310
ABORT ();
321
311
}
322
312
323
- result = tfn (isolate , EncodingName (fromEncoding), EncodingName (toEncoding),
313
+ result = tfn (env , EncodingName (fromEncoding), EncodingName (toEncoding),
324
314
ts_obj_data, ts_obj_length, &status);
325
315
} else {
326
316
status = U_ILLEGAL_ARGUMENT_ERROR;
@@ -431,7 +421,7 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
431
421
432
422
int32_t len = uidna_nameToUnicodeUTF8 (uidna,
433
423
input, length,
434
- **buf, buf->length (),
424
+ **buf, buf->capacity (),
435
425
&info,
436
426
&status);
437
427
@@ -440,13 +430,17 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
440
430
buf->AllocateSufficientStorage (len);
441
431
len = uidna_nameToUnicodeUTF8 (uidna,
442
432
input, length,
443
- **buf, buf->length (),
433
+ **buf, buf->capacity (),
444
434
&info,
445
435
&status);
446
436
}
447
437
448
- if (U_FAILURE (status))
438
+ if (U_FAILURE (status)) {
449
439
len = -1 ;
440
+ buf->SetLength (0 );
441
+ } else {
442
+ buf->SetLength (len);
443
+ }
450
444
451
445
uidna_close (uidna);
452
446
return len;
@@ -465,7 +459,7 @@ int32_t ToASCII(MaybeStackBuffer<char>* buf,
465
459
466
460
int32_t len = uidna_nameToASCII_UTF8 (uidna,
467
461
input, length,
468
- **buf, buf->length (),
462
+ **buf, buf->capacity (),
469
463
&info,
470
464
&status);
471
465
@@ -474,13 +468,17 @@ int32_t ToASCII(MaybeStackBuffer<char>* buf,
474
468
buf->AllocateSufficientStorage (len);
475
469
len = uidna_nameToASCII_UTF8 (uidna,
476
470
input, length,
477
- **buf, buf->length (),
471
+ **buf, buf->capacity (),
478
472
&info,
479
473
&status);
480
474
}
481
475
482
- if (U_FAILURE (status))
476
+ if (U_FAILURE (status)) {
483
477
len = -1 ;
478
+ buf->SetLength (0 );
479
+ } else {
480
+ buf->SetLength (len);
481
+ }
484
482
485
483
uidna_close (uidna);
486
484
return len;
0 commit comments