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

Side by Side Diff: src/json.js

Issue 5443001: Move quoting of a JSON string to a specialized runtime function. (Closed)
Patch Set: Remove TODO, add counters to track behavior. Created 10 years 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 | « no previous file | src/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 function JSONParse(text, reviver) { 60 function JSONParse(text, reviver) {
61 var unfiltered = ParseJSONUnfiltered(text); 61 var unfiltered = ParseJSONUnfiltered(text);
62 if (IS_FUNCTION(reviver)) { 62 if (IS_FUNCTION(reviver)) {
63 return Revive({'': unfiltered}, '', reviver); 63 return Revive({'': unfiltered}, '', reviver);
64 } else { 64 } else {
65 return unfiltered; 65 return unfiltered;
66 } 66 }
67 } 67 }
68 68
69 var characterQuoteCache = {
70 '\b': '\\b', // ASCII 8, Backspace
71 '\t': '\\t', // ASCII 9, Tab
72 '\n': '\\n', // ASCII 10, Newline
73 '\f': '\\f', // ASCII 12, Formfeed
74 '\r': '\\r', // ASCII 13, Carriage Return
75 '\"': '\\"',
76 '\\': '\\\\'
77 };
78
79 function QuoteSingleJSONCharacter(c) {
80 if (c in characterQuoteCache) {
81 return characterQuoteCache[c];
82 }
83 var charCode = c.charCodeAt(0);
84 var result;
85 if (charCode < 16) result = '\\u000';
86 else if (charCode < 256) result = '\\u00';
87 else if (charCode < 4096) result = '\\u0';
88 else result = '\\u';
89 result += charCode.toString(16);
90 characterQuoteCache[c] = result;
91 return result;
92 }
93
94 function QuoteJSONString(str) { 69 function QuoteJSONString(str) {
95 var quotable = /[\\\"\x00-\x1f]/g; 70 var quoted_str = %QuoteJSONString(str);
96 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; 71 if (IS_STRING(quoted_str)) return quoted_str;
72 return '"' + str + '"';
97 } 73 }
98 74
99 function StackContains(stack, val) { 75 function StackContains(stack, val) {
100 var length = stack.length; 76 var length = stack.length;
101 for (var i = 0; i < length; i++) { 77 for (var i = 0; i < length; i++) {
102 if (stack[i] === val) { 78 if (stack[i] === val) {
103 return true; 79 return true;
104 } 80 }
105 } 81 }
106 return false; 82 return false;
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 232 }
257 233
258 function SetupJSON() { 234 function SetupJSON() {
259 InstallFunctions($JSON, DONT_ENUM, $Array( 235 InstallFunctions($JSON, DONT_ENUM, $Array(
260 "parse", JSONParse, 236 "parse", JSONParse,
261 "stringify", JSONStringify 237 "stringify", JSONStringify
262 )); 238 ));
263 } 239 }
264 240
265 SetupJSON(); 241 SetupJSON();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698