Skip to content

Commit

Permalink
Update to latest rdf-canon N-Quads canonical form.
Browse files Browse the repository at this point in the history
**BREAKING**: Use latest rdf-canon N-Quads canonical form. This can
change the canonical output! There is an expanded set of control
characters that are escaped as an `ECHAR` or `UCHAR` instead of using a
native representation.
  • Loading branch information
davidlehn committed Apr 19, 2023
1 parent 828f093 commit 9f4a9f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- Test with karma.

### Changed
- **BREAKING**: Use latest [rdf-canon][] N-Quads canonical form. This can
change the canonical output! There is an expanded set of control characters
that are escaped as an `ECHAR` or `UCHAR` instead of using a native
representation.
- **BREAKING**: Use `globalThis` to access `crypto` in browsers. Use a polyfill
if your environment doesn't support `globalThis`.
- Update tooling.
Expand Down
21 changes: 15 additions & 6 deletions lib/NQuads.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function _compareTriples(t1, t2) {
);
}

const _stringLiteralEscapeRegex = /["\\\n\r]/g;
const _stringLiteralEscapeRegex = /[\u0000-\u001F\u007F"\\]/g;
/**
* Escape string to N-Quads literal
*/
Expand All @@ -391,16 +391,25 @@ function _stringLiteralEscape(s) {
}
return s.replace(_stringLiteralEscapeRegex, function(match) {
switch(match) {
case '"': return '\\"';
case '\\': return '\\\\';
case '\b': return '\\b';
case '\t': return '\\t';
case '\n': return '\\n';
case '\f': return '\\f';
case '\r': return '\\r';
case '"': return '\\"';
case '\\': return '\\\\';
case '\u007F': return '\\u007F';
}
return '\\u' + match
.codePointAt(0)
.toString(16)
.toUpperCase()
.padStart(4, '0');
});
}

const _stringLiteralUnescapeRegex =
/(?:\\([tbnrf"'\\]))|(?:\\u([0-9A-Fa-f]{4}))|(?:\\U([0-9A-Fa-f]{8}))/g;
/(?:\\([btnfr"'\\]))|(?:\\u([0-9A-Fa-f]{4}))|(?:\\U([0-9A-Fa-f]{8}))/g;
/**
* Unescape N-Quads literal to string
*/
Expand All @@ -411,11 +420,11 @@ function _stringLiteralUnescape(s) {
return s.replace(_stringLiteralUnescapeRegex, function(match, code, u, U) {
if(code) {
switch(code) {
case 't': return '\t';
case 'b': return '\b';
case 't': return '\t';
case 'n': return '\n';
case 'r': return '\r';
case 'f': return '\f';
case 'r': return '\r';
case '"': return '"';
case '\'': return '\'';
case '\\': return '\\';
Expand Down

0 comments on commit 9f4a9f6

Please sign in to comment.