Skip to content

Commit

Permalink
30% faster encode with a regex test for quotes-are-needed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Feb 7, 2019
1 parent 0a59b38 commit c5e877e
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions yaayaml.js
Expand Up @@ -125,6 +125,9 @@ Qyaml.prototype.encodeValue = function encodeValue( value ) {
var mustQuoteMap = new Array(128), mustQuoteChars = "'\" [] {} > | * & ! % # ` @ ,";
for (var i=0; i<mustQuoteChars.length; i++) mustQuoteMap[mustQuoteChars.charCodeAt(i)] = true;
Qyaml.prototype.mustBeQuoted = function mustBeQuoted( str ) {
// empty string, leading/trailing whitespace, leading special chars must be quote
return /^$|^[\s]|[\s]$|^[\s\'\"\[\{>|*!%#`@,]|[\x00-\x1f\n\":\x7f-\uffff]/.test(str);

if (str.length === 0 || mustQuoteMap[str.charCodeAt(0)]) return true;
for (var i = 0; i < str.length; i++) {
var ch = str.charCodeAt(i);
Expand Down

0 comments on commit c5e877e

Please sign in to comment.