Skip to content

Commit

Permalink
feat: use ; as a delimiter for _box
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas950 authored and porsager committed Apr 5, 2023
1 parent 62a23bb commit 26d08c8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cjs/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return;
const parser = options.parsers[oid]
options.shared.typeArrayMap[oid] = typarray
options.parsers[typarray] = (xs) => arrayParser(xs, parser)
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
options.parsers[typarray].array = true
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options)
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
}

function tryNext(x, xs) {
Expand Down
20 changes: 12 additions & 8 deletions cjs/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,19 @@ function arrayEscape(x) {
.replace(escapeQuote, '\\"')
}

const arraySerializer = module.exports.arraySerializer = function arraySerializer(xs, serializer, options) {
const arraySerializer = module.exports.arraySerializer = function arraySerializer(xs, serializer, options, typarray) {
if (Array.isArray(xs) === false)
return xs

if (!xs.length)
return '{}'

const first = xs[0]
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','

if (Array.isArray(first) && !first.type)
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'

return '{' + xs.map(x => {
if (x === undefined) {
Expand All @@ -257,7 +259,7 @@ const arraySerializer = module.exports.arraySerializer = function arraySerialize
return x === null
? 'null'
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
}).join(',') + '}'
}).join(delimiter) + '}'
}

const arrayParserState = {
Expand All @@ -268,13 +270,15 @@ const arrayParserState = {
last: 0
}

const arrayParser = module.exports.arrayParser = function arrayParser(x, parser) {
const arrayParser = module.exports.arrayParser = function arrayParser(x, parser, typarray) {
arrayParserState.i = arrayParserState.last = 0
return arrayParserLoop(arrayParserState, x, parser)
return arrayParserLoop(arrayParserState, x, parser, typarray)
}

function arrayParserLoop(s, x, parser) {
function arrayParserLoop(s, x, parser, typarray) {
const xs = []
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','
for (; s.i < x.length; s.i++) {
s.char = x[s.i]
if (s.quoted) {
Expand All @@ -292,13 +296,13 @@ function arrayParserLoop(s, x, parser) {
s.quoted = true
} else if (s.char === '{') {
s.last = ++s.i
xs.push(arrayParserLoop(s, x, parser))
xs.push(arrayParserLoop(s, x, parser, typarray))
} else if (s.char === '}') {
s.quoted = false
s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
break
} else if (s.char === ',' && s.p !== '}' && s.p !== '"') {
} else if (s.char === delimiter && s.p !== '}' && s.p !== '"') {
xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
}
Expand Down
4 changes: 2 additions & 2 deletions deno/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return;
const parser = options.parsers[oid]
options.shared.typeArrayMap[oid] = typarray
options.parsers[typarray] = (xs) => arrayParser(xs, parser)
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
options.parsers[typarray].array = true
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options)
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
}

function tryNext(x, xs) {
Expand Down
20 changes: 12 additions & 8 deletions deno/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,19 @@ function arrayEscape(x) {
.replace(escapeQuote, '\\"')
}

export const arraySerializer = function arraySerializer(xs, serializer, options) {
export const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {
if (Array.isArray(xs) === false)
return xs

if (!xs.length)
return '{}'

const first = xs[0]
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','

if (Array.isArray(first) && !first.type)
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'

return '{' + xs.map(x => {
if (x === undefined) {
Expand All @@ -258,7 +260,7 @@ export const arraySerializer = function arraySerializer(xs, serializer, options)
return x === null
? 'null'
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
}).join(',') + '}'
}).join(delimiter) + '}'
}

const arrayParserState = {
Expand All @@ -269,13 +271,15 @@ const arrayParserState = {
last: 0
}

export const arrayParser = function arrayParser(x, parser) {
export const arrayParser = function arrayParser(x, parser, typarray) {
arrayParserState.i = arrayParserState.last = 0
return arrayParserLoop(arrayParserState, x, parser)
return arrayParserLoop(arrayParserState, x, parser, typarray)
}

function arrayParserLoop(s, x, parser) {
function arrayParserLoop(s, x, parser, typarray) {
const xs = []
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','
for (; s.i < x.length; s.i++) {
s.char = x[s.i]
if (s.quoted) {
Expand All @@ -293,13 +297,13 @@ function arrayParserLoop(s, x, parser) {
s.quoted = true
} else if (s.char === '{') {
s.last = ++s.i
xs.push(arrayParserLoop(s, x, parser))
xs.push(arrayParserLoop(s, x, parser, typarray))
} else if (s.char === '}') {
s.quoted = false
s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
break
} else if (s.char === ',' && s.p !== '}' && s.p !== '"') {
} else if (s.char === delimiter && s.p !== '}' && s.p !== '"') {
xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
}
Expand Down
4 changes: 2 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return;
const parser = options.parsers[oid]
options.shared.typeArrayMap[oid] = typarray
options.parsers[typarray] = (xs) => arrayParser(xs, parser)
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
options.parsers[typarray].array = true
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options)
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
}

function tryNext(x, xs) {
Expand Down
20 changes: 12 additions & 8 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,19 @@ function arrayEscape(x) {
.replace(escapeQuote, '\\"')
}

export const arraySerializer = function arraySerializer(xs, serializer, options) {
export const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {
if (Array.isArray(xs) === false)
return xs

if (!xs.length)
return '{}'

const first = xs[0]
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','

if (Array.isArray(first) && !first.type)
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'

return '{' + xs.map(x => {
if (x === undefined) {
Expand All @@ -257,7 +259,7 @@ export const arraySerializer = function arraySerializer(xs, serializer, options)
return x === null
? 'null'
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
}).join(',') + '}'
}).join(delimiter) + '}'
}

const arrayParserState = {
Expand All @@ -268,13 +270,15 @@ const arrayParserState = {
last: 0
}

export const arrayParser = function arrayParser(x, parser) {
export const arrayParser = function arrayParser(x, parser, typarray) {
arrayParserState.i = arrayParserState.last = 0
return arrayParserLoop(arrayParserState, x, parser)
return arrayParserLoop(arrayParserState, x, parser, typarray)
}

function arrayParserLoop(s, x, parser) {
function arrayParserLoop(s, x, parser, typarray) {
const xs = []
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','
for (; s.i < x.length; s.i++) {
s.char = x[s.i]
if (s.quoted) {
Expand All @@ -292,13 +296,13 @@ function arrayParserLoop(s, x, parser) {
s.quoted = true
} else if (s.char === '{') {
s.last = ++s.i
xs.push(arrayParserLoop(s, x, parser))
xs.push(arrayParserLoop(s, x, parser, typarray))
} else if (s.char === '}') {
s.quoted = false
s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
break
} else if (s.char === ',' && s.p !== '}' && s.p !== '"') {
} else if (s.char === delimiter && s.p !== '}' && s.p !== '"') {
xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
}
Expand Down

0 comments on commit 26d08c8

Please sign in to comment.