4
4
// found in the LICENSE file.
5
5
6
6
#include < stdio.h>
7
- #include <string.h >
7
+ #include < string>
8
8
#include < emscripten.h>
9
- #include <assert.h >
9
+ #include < cassert >
10
10
#include < wchar.h>
11
11
12
12
typedef unsigned int utf32;
@@ -19,24 +19,23 @@ int main() {
19
19
// U+2603 is snowman,
20
20
// U+20AC is the Euro sign,
21
21
// U+2007C is a Chinese Han character that looks like three raindrops.
22
- wchar_t wstr [] = L"abc\u2603\u20AC\U0002007C123 --- abc\u2603\u20AC\U0002007C123" ;
23
- size_t wstr_len = wcslen (wstr );
22
+ std::wstring wstr = L" abc\u2603\u20AC\U0002007C 123 --- abc\u2603\u20AC\U0002007C 123" ;
24
23
25
24
printf (" sizeof(wchar_t): %d.\n " , (int )sizeof (wchar_t ));
26
25
27
26
if (sizeof (wchar_t ) == 4 ) {
28
- utf32 * memory = malloc ( wstr_len * sizeof ( utf32 )) ;
27
+ utf32 *memory = new utf32[wstr. length ()+ 1 ] ;
29
28
30
29
EM_ASM ({
31
30
var str = UTF32ToString ($0 );
32
31
out (str);
33
32
var numBytesWritten = stringToUTF32 (str, $1 , Number ($2 ));
34
33
if (numBytesWritten != 23 *4 ) throw ' stringToUTF32 wrote an invalid length ' + numBytesWritten;
35
- }, wstr , memory , (wstr_len + 1 )* sizeof (utf32 ));
34
+ }, wstr. c_str () , memory, (wstr. length () +1 )*sizeof (utf32));
36
35
37
36
// Compare memory to confirm that the string is intact after taking a route
38
37
// through JS side.
39
- const utf32 * srcPtr = ( const utf32 * ) (wstr );
38
+ const utf32 *srcPtr = reinterpret_cast < const utf32 *> (wstr. c_str () );
40
39
for (int i = 0 ;; ++i) {
41
40
assert (memory[i] == srcPtr[i]);
42
41
if (srcPtr[i] == 0 )
@@ -48,24 +47,24 @@ int main() {
48
47
out (str);
49
48
var numBytesWritten = stringToUTF32 (str, $1 , Number ($2 ));
50
49
if (numBytesWritten != 5 *4 ) throw ' stringToUTF32 wrote an invalid length ' + numBytesWritten;
51
- }, wstr , memory , 6 * sizeof (utf32 ));
50
+ }, wstr. c_str () , memory, 6 *sizeof (utf32));
52
51
assert (memory[5 ] == 0 );
53
52
54
- free ( memory ) ;
53
+ delete[] memory;
55
54
} else {
56
55
// sizeof(wchar_t) == 2, and we're building with -fshort-wchar.
57
- utf16 * memory = malloc (( 2 * wstr_len + 1 ) * sizeof ( utf16 )) ;
56
+ utf16 *memory = new utf16[ 2 *wstr. length ()+ 1 ] ;
58
57
59
58
EM_ASM ({
60
59
var str = UTF16ToString ($0 );
61
60
out (str);
62
61
var numBytesWritten = stringToUTF16 (str, $1 , $2 );
63
62
if (numBytesWritten != 25 *2 ) throw ' stringToUTF16 wrote an invalid length ' + numBytesWritten;
64
- }, wstr , memory , (2 * wstr_len + 1 )* sizeof (utf16 ));
63
+ }, wstr. c_str () , memory, (2 *wstr. length () +1 )*sizeof (utf16));
65
64
66
65
// Compare memory to confirm that the string is intact after taking a route
67
66
// through JS side.
68
- const utf16 * srcPtr = ( const utf16 * ) (wstr );
67
+ const utf16 *srcPtr = reinterpret_cast < const utf16 *> (wstr. c_str () );
69
68
for (int i = 0 ;; ++i) {
70
69
assert (memory[i] == srcPtr[i]);
71
70
if (srcPtr[i] == 0 )
@@ -77,10 +76,10 @@ int main() {
77
76
out (str);
78
77
var numBytesWritten = stringToUTF16 (str, $1 , $2 );
79
78
if (numBytesWritten != 5 *2 ) throw ' stringToUTF16 wrote an invalid length ' + numBytesWritten;
80
- }, wstr , memory , 6 * sizeof (utf16 ));
79
+ }, wstr. c_str () , memory, 6 *sizeof (utf16));
81
80
assert (memory[5 ] == 0 );
82
81
83
- free ( memory ) ;
82
+ delete[] memory;
84
83
}
85
84
86
85
printf (" OK.\n " );
0 commit comments