Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: test/cctest/test-api.cc

Issue 121173009: String:WriteUtf8: Add REPLACE_INVALID_UTF8 option (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/unicode-inl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 7596 matching lines...) Expand 10 before | Expand all | Expand 10 after
7607 7607
7608 7608
7609 THREADED_TEST(StringWrite) { 7609 THREADED_TEST(StringWrite) {
7610 LocalContext context; 7610 LocalContext context;
7611 v8::HandleScope scope(context->GetIsolate()); 7611 v8::HandleScope scope(context->GetIsolate());
7612 v8::Handle<String> str = v8_str("abcde"); 7612 v8::Handle<String> str = v8_str("abcde");
7613 // abc<Icelandic eth><Unicode snowman>. 7613 // abc<Icelandic eth><Unicode snowman>.
7614 v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203"); 7614 v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203");
7615 v8::Handle<String> str3 = v8::String::NewFromUtf8( 7615 v8::Handle<String> str3 = v8::String::NewFromUtf8(
7616 context->GetIsolate(), "abc\0def", v8::String::kNormalString, 7); 7616 context->GetIsolate(), "abc\0def", v8::String::kNormalString, 7);
7617 // "ab" + lead surrogate + "cd" + trail surrogate + "ef"
7618 uint16_t orphans[8] = { 0x61, 0x62, 0xd800, 0x63, 0x64, 0xdc00, 0x65, 0x66 };
7619 v8::Handle<String> orphans_str = v8::String::NewFromTwoByte(
7620 context->GetIsolate(), orphans, v8::String::kNormalString, 8);
7621 // single lead surrogate
7622 uint16_t lead[1] = { 0xd800 };
7623 v8::Handle<String> lead_str = v8::String::NewFromTwoByte(
7624 context->GetIsolate(), lead, v8::String::kNormalString, 1);
7625 // single trail surrogate
7626 uint16_t trail[1] = { 0xdc00 };
7627 v8::Handle<String> trail_str = v8::String::NewFromTwoByte(
7628 context->GetIsolate(), trail, v8::String::kNormalString, 1);
7629 // surrogate pair
7630 uint16_t pair[2] = { 0xd800, 0xdc00 };
7631 v8::Handle<String> pair_str = v8::String::NewFromTwoByte(
7632 context->GetIsolate(), pair, v8::String::kNormalString, 2);
7617 const int kStride = 4; // Must match stride in for loops in JS below. 7633 const int kStride = 4; // Must match stride in for loops in JS below.
7618 CompileRun( 7634 CompileRun(
7619 "var left = '';" 7635 "var left = '';"
7620 "for (var i = 0; i < 0xd800; i += 4) {" 7636 "for (var i = 0; i < 0xd800; i += 4) {"
7621 " left = left + String.fromCharCode(i);" 7637 " left = left + String.fromCharCode(i);"
7622 "}"); 7638 "}");
7623 CompileRun( 7639 CompileRun(
7624 "var right = '';" 7640 "var right = '';"
7625 "for (var i = 0; i < 0xd800; i += 4) {" 7641 "for (var i = 0; i < 0xd800; i += 4) {"
7626 " right = String.fromCharCode(i) + right;" 7642 " right = String.fromCharCode(i) + right;"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
7680 CHECK_EQ(3, len); 7696 CHECK_EQ(3, len);
7681 CHECK_EQ(3, charlen); 7697 CHECK_EQ(3, charlen);
7682 CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4)); 7698 CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4));
7683 7699
7684 memset(utf8buf, 0x1, 1000); 7700 memset(utf8buf, 0x1, 1000);
7685 len = str2->WriteUtf8(utf8buf, 2, &charlen); 7701 len = str2->WriteUtf8(utf8buf, 2, &charlen);
7686 CHECK_EQ(2, len); 7702 CHECK_EQ(2, len);
7687 CHECK_EQ(2, charlen); 7703 CHECK_EQ(2, charlen);
7688 CHECK_EQ(0, strncmp(utf8buf, "ab\1", 3)); 7704 CHECK_EQ(0, strncmp(utf8buf, "ab\1", 3));
7689 7705
7706 // allow orphan surrogates by default
7707 memset(utf8buf, 0x1, 1000);
7708 len = orphans_str->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen);
7709 CHECK_EQ(13, len);
7710 CHECK_EQ(8, charlen);
7711 CHECK_EQ(0, strcmp(utf8buf, "ab\355\240\200cd\355\260\200ef"));
7712
7713 // replace orphan surrogates with unicode replacement character
7714 memset(utf8buf, 0x1, 1000);
7715 len = orphans_str->WriteUtf8(utf8buf,
7716 sizeof(utf8buf),
7717 &charlen,
7718 String::REPLACE_INVALID_UTF8);
7719 CHECK_EQ(13, len);
7720 CHECK_EQ(8, charlen);
7721 CHECK_EQ(0, strcmp(utf8buf, "ab\357\277\275cd\357\277\275ef"));
7722
7723 // replace single lead surrogate with unicode replacement character
7724 memset(utf8buf, 0x1, 1000);
7725 len = lead_str->WriteUtf8(utf8buf,
7726 sizeof(utf8buf),
7727 &charlen,
7728 String::REPLACE_INVALID_UTF8);
7729 CHECK_EQ(4, len);
7730 CHECK_EQ(1, charlen);
7731 CHECK_EQ(0, strcmp(utf8buf, "\357\277\275"));
7732
7733 // replace single trail surrogate with unicode replacement character
7734 memset(utf8buf, 0x1, 1000);
7735 len = trail_str->WriteUtf8(utf8buf,
7736 sizeof(utf8buf),
7737 &charlen,
7738 String::REPLACE_INVALID_UTF8);
7739 CHECK_EQ(4, len);
7740 CHECK_EQ(1, charlen);
7741 CHECK_EQ(0, strcmp(utf8buf, "\357\277\275"));
7742
7743 // do not replace / write anything if surrogate pair does not fit the buffer
7744 // space
7745 memset(utf8buf, 0x1, 1000);
7746 len = pair_str->WriteUtf8(utf8buf,
7747 3,
7748 &charlen,
7749 String::REPLACE_INVALID_UTF8);
7750 CHECK_EQ(0, len);
7751 CHECK_EQ(0, charlen);
7752
7690 memset(utf8buf, 0x1, sizeof(utf8buf)); 7753 memset(utf8buf, 0x1, sizeof(utf8buf));
7691 len = GetUtf8Length(left_tree); 7754 len = GetUtf8Length(left_tree);
7692 int utf8_expected = 7755 int utf8_expected =
7693 (0x80 + (0x800 - 0x80) * 2 + (0xd800 - 0x800) * 3) / kStride; 7756 (0x80 + (0x800 - 0x80) * 2 + (0xd800 - 0x800) * 3) / kStride;
7694 CHECK_EQ(utf8_expected, len); 7757 CHECK_EQ(utf8_expected, len);
7695 len = left_tree->WriteUtf8(utf8buf, utf8_expected, &charlen); 7758 len = left_tree->WriteUtf8(utf8buf, utf8_expected, &charlen);
7696 CHECK_EQ(utf8_expected, len); 7759 CHECK_EQ(utf8_expected, len);
7697 CHECK_EQ(0xd800 / kStride, charlen); 7760 CHECK_EQ(0xd800 / kStride, charlen);
7698 CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[utf8_expected - 3])); 7761 CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[utf8_expected - 3]));
7699 CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[utf8_expected - 2])); 7762 CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[utf8_expected - 2]));
(...skipping 14052 matching lines...) Expand 10 before | Expand all | Expand 10 after
21752 context->Global()->Set(v8_str("P"), templ->NewInstance()); 21815 context->Global()->Set(v8_str("P"), templ->NewInstance());
21753 CompileRun( 21816 CompileRun(
21754 "function C1() {" 21817 "function C1() {"
21755 " this.x = 23;" 21818 " this.x = 23;"
21756 "};" 21819 "};"
21757 "C1.prototype = P;" 21820 "C1.prototype = P;"
21758 "for (var i = 0; i < 4; i++ ) {" 21821 "for (var i = 0; i < 4; i++ ) {"
21759 " new C1();" 21822 " new C1();"
21760 "}"); 21823 "}");
21761 } 21824 }
OLDNEW
« no previous file with comments | « src/unicode-inl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698