OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 7535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7546 | 7546 |
7547 | 7547 |
7548 THREADED_TEST(StringWrite) { | 7548 THREADED_TEST(StringWrite) { |
7549 LocalContext context; | 7549 LocalContext context; |
7550 v8::HandleScope scope(context->GetIsolate()); | 7550 v8::HandleScope scope(context->GetIsolate()); |
7551 v8::Handle<String> str = v8_str("abcde"); | 7551 v8::Handle<String> str = v8_str("abcde"); |
7552 // abc<Icelandic eth><Unicode snowman>. | 7552 // abc<Icelandic eth><Unicode snowman>. |
7553 v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203"); | 7553 v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203"); |
7554 v8::Handle<String> str3 = v8::String::NewFromUtf8( | 7554 v8::Handle<String> str3 = v8::String::NewFromUtf8( |
7555 context->GetIsolate(), "abc\0def", v8::String::kNormalString, 7); | 7555 context->GetIsolate(), "abc\0def", v8::String::kNormalString, 7); |
| 7556 // "ab" + lead surrogate + "cd" + trail surrogate + "ef" |
| 7557 uint16_t orphans[8] = { 0x61, 0x62, 0xd800, 0x63, 0x64, 0xdc00, 0x65, 0x66 }; |
| 7558 v8::Handle<String> orphans_str = v8::String::NewFromTwoByte( |
| 7559 context->GetIsolate(), orphans, v8::String::kNormalString, 8); |
| 7560 // single lead surrogate |
| 7561 uint16_t lead[1] = { 0xd800 }; |
| 7562 v8::Handle<String> lead_str = v8::String::NewFromTwoByte( |
| 7563 context->GetIsolate(), lead, v8::String::kNormalString, 1); |
| 7564 // single trail surrogate |
| 7565 uint16_t trail[1] = { 0xdc00 }; |
| 7566 v8::Handle<String> trail_str = v8::String::NewFromTwoByte( |
| 7567 context->GetIsolate(), trail, v8::String::kNormalString, 1); |
7556 const int kStride = 4; // Must match stride in for loops in JS below. | 7568 const int kStride = 4; // Must match stride in for loops in JS below. |
7557 CompileRun( | 7569 CompileRun( |
7558 "var left = '';" | 7570 "var left = '';" |
7559 "for (var i = 0; i < 0xd800; i += 4) {" | 7571 "for (var i = 0; i < 0xd800; i += 4) {" |
7560 " left = left + String.fromCharCode(i);" | 7572 " left = left + String.fromCharCode(i);" |
7561 "}"); | 7573 "}"); |
7562 CompileRun( | 7574 CompileRun( |
7563 "var right = '';" | 7575 "var right = '';" |
7564 "for (var i = 0; i < 0xd800; i += 4) {" | 7576 "for (var i = 0; i < 0xd800; i += 4) {" |
7565 " right = String.fromCharCode(i) + right;" | 7577 " right = String.fromCharCode(i) + right;" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7619 CHECK_EQ(3, len); | 7631 CHECK_EQ(3, len); |
7620 CHECK_EQ(3, charlen); | 7632 CHECK_EQ(3, charlen); |
7621 CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4)); | 7633 CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4)); |
7622 | 7634 |
7623 memset(utf8buf, 0x1, 1000); | 7635 memset(utf8buf, 0x1, 1000); |
7624 len = str2->WriteUtf8(utf8buf, 2, &charlen); | 7636 len = str2->WriteUtf8(utf8buf, 2, &charlen); |
7625 CHECK_EQ(2, len); | 7637 CHECK_EQ(2, len); |
7626 CHECK_EQ(2, charlen); | 7638 CHECK_EQ(2, charlen); |
7627 CHECK_EQ(0, strncmp(utf8buf, "ab\1", 3)); | 7639 CHECK_EQ(0, strncmp(utf8buf, "ab\1", 3)); |
7628 | 7640 |
| 7641 // allow orphan surrogates by default |
| 7642 memset(utf8buf, 0x1, 1000); |
| 7643 len = orphans_str->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen); |
| 7644 CHECK_EQ(13, len); |
| 7645 CHECK_EQ(8, charlen); |
| 7646 CHECK_EQ(0, strcmp(utf8buf, "ab\355\240\200cd\355\260\200ef")); |
| 7647 |
| 7648 // replace orphan surrogates with unicode replacement via flag |
| 7649 memset(utf8buf, 0x1, 1000); |
| 7650 len = orphans_str->WriteUtf8(utf8buf, |
| 7651 sizeof(utf8buf), |
| 7652 &charlen, |
| 7653 String::DISALLOW_INVALID_UTF8); |
| 7654 CHECK_EQ(13, len); |
| 7655 CHECK_EQ(8, charlen); |
| 7656 CHECK_EQ(0, strcmp(utf8buf, "ab\357\277\275cd\357\277\275ef")); |
| 7657 |
| 7658 // replace single lead surrogate with unicode replacement character |
| 7659 memset(utf8buf, 0x1, 1000); |
| 7660 len = lead_str->WriteUtf8(utf8buf, |
| 7661 sizeof(utf8buf), |
| 7662 &charlen, |
| 7663 String::DISALLOW_INVALID_UTF8); |
| 7664 CHECK_EQ(4, len); |
| 7665 CHECK_EQ(1, charlen); |
| 7666 CHECK_EQ(0, strcmp(utf8buf, "\357\277\275")); |
| 7667 |
| 7668 // replace single trail surrogate with unicode replacement character |
| 7669 memset(utf8buf, 0x1, 1000); |
| 7670 len = trail_str->WriteUtf8(utf8buf, |
| 7671 sizeof(utf8buf), |
| 7672 &charlen, |
| 7673 String::DISALLOW_INVALID_UTF8); |
| 7674 CHECK_EQ(4, len); |
| 7675 CHECK_EQ(1, charlen); |
| 7676 CHECK_EQ(0, strcmp(utf8buf, "\357\277\275")); |
| 7677 |
7629 memset(utf8buf, 0x1, sizeof(utf8buf)); | 7678 memset(utf8buf, 0x1, sizeof(utf8buf)); |
7630 len = GetUtf8Length(left_tree); | 7679 len = GetUtf8Length(left_tree); |
7631 int utf8_expected = | 7680 int utf8_expected = |
7632 (0x80 + (0x800 - 0x80) * 2 + (0xd800 - 0x800) * 3) / kStride; | 7681 (0x80 + (0x800 - 0x80) * 2 + (0xd800 - 0x800) * 3) / kStride; |
7633 CHECK_EQ(utf8_expected, len); | 7682 CHECK_EQ(utf8_expected, len); |
7634 len = left_tree->WriteUtf8(utf8buf, utf8_expected, &charlen); | 7683 len = left_tree->WriteUtf8(utf8buf, utf8_expected, &charlen); |
7635 CHECK_EQ(utf8_expected, len); | 7684 CHECK_EQ(utf8_expected, len); |
7636 CHECK_EQ(0xd800 / kStride, charlen); | 7685 CHECK_EQ(0xd800 / kStride, charlen); |
7637 CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[utf8_expected - 3])); | 7686 CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[utf8_expected - 3])); |
7638 CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[utf8_expected - 2])); | 7687 CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[utf8_expected - 2])); |
(...skipping 13436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21075 } | 21124 } |
21076 for (int i = 0; i < runs; i++) { | 21125 for (int i = 0; i < runs; i++) { |
21077 Local<String> expected; | 21126 Local<String> expected; |
21078 if (i != 0) { | 21127 if (i != 0) { |
21079 CHECK_EQ(v8_str("escape value"), values[i]); | 21128 CHECK_EQ(v8_str("escape value"), values[i]); |
21080 } else { | 21129 } else { |
21081 CHECK(values[i].IsEmpty()); | 21130 CHECK(values[i].IsEmpty()); |
21082 } | 21131 } |
21083 } | 21132 } |
21084 } | 21133 } |
OLD | NEW |