From 9b078eddcca5020367a2b09055fcb4d81f0bd5ae Mon Sep 17 00:00:00 2001 From: Max Amanshauser Date: Fri, 10 Aug 2012 04:06:49 +0200 Subject: [PATCH 1/3] Fixed bytea decode and added 'hex' for pg >= 9.0. --- lib/textParsers.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/textParsers.js b/lib/textParsers.js index f4e35dc5..bbb0df7e 100644 --- a/lib/textParsers.js +++ b/lib/textParsers.js @@ -119,9 +119,32 @@ var parseInterval = function(val) { }; var parseByteA = function(val) { - return new Buffer(val.replace(/\\([0-7]{3})/g, function (full_match, code) { - return String.fromCharCode(parseInt(code, 8)); - }).replace(/\\\\/g, "\\"), "binary"); + if(val.match(/^\\x/)){ + // new 'hex' style response (pg >9.0) + return new Buffer(val.replace(/^\\x/,''), 'hex'); + }else{ + out = "" + i = 0 + while(i < val.length){ + if(val[i] != "\\"){ + out += val[i] + ++i + }else{ + if(val.substring(i+1, i+4).match(/([0-7]){3}/)){ + out += String.fromCharCode(parseInt(val.substring(i+1,i+4),8)) + i += 4 + }else{ + backslashes = 1 + while(i+backslashes < val.length && val[i+backslashes] == "\\") + backslashes++ + for(k=0; k Date: Sat, 18 Aug 2012 13:17:10 +0200 Subject: [PATCH 2/3] Slightly more efficient hex bytea decode --- lib/textParsers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/textParsers.js b/lib/textParsers.js index bbb0df7e..0a584015 100644 --- a/lib/textParsers.js +++ b/lib/textParsers.js @@ -119,9 +119,9 @@ var parseInterval = function(val) { }; var parseByteA = function(val) { - if(val.match(/^\\x/)){ + if(/^\\x/.test(val)){ // new 'hex' style response (pg >9.0) - return new Buffer(val.replace(/^\\x/,''), 'hex'); + return new Buffer(val.substr(2), 'hex'); }else{ out = "" i = 0 From 430f1079fca0eb5aca6297f3c9a4c38f6cd48d7e Mon Sep 17 00:00:00 2001 From: Max Amanshauser Date: Sat, 18 Aug 2012 13:48:06 +0200 Subject: [PATCH 3/3] Tidy up --- lib/textParsers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/textParsers.js b/lib/textParsers.js index 0a584015..c8811bec 100644 --- a/lib/textParsers.js +++ b/lib/textParsers.js @@ -130,8 +130,8 @@ var parseByteA = function(val) { out += val[i] ++i }else{ - if(val.substring(i+1, i+4).match(/([0-7]){3}/)){ - out += String.fromCharCode(parseInt(val.substring(i+1,i+4),8)) + if(val.substr(i+1,3).match(/[0-7]{3}/)){ + out += String.fromCharCode(parseInt(val.substr(i+1,3),8)) i += 4 }else{ backslashes = 1