@@ -3,11 +3,9 @@ import namespaces from './IRIs';
33import queueMicrotask from 'queue-microtask' ;
44
55const { xsd } = namespaces ;
6- const { fromCharCode } = String ;
76
8- // Regular expression and replacement string to escape N3 strings.
9- // Note how we catch invalid unicode sequences separately (they will trigger an error).
10- var escapeSequence = / \\ u ( [ a - f A - F 0 - 9 ] { 4 } ) | \\ U ( [ a - f A - F 0 - 9 ] { 8 } ) | \\ [ u U ] | \\ ( .) / g;
7+ // Regular expression and replacement string to escape N3 strings
8+ var escapeSequence = / \\ u ( [ a - f A - F 0 - 9 ] { 4 } ) | \\ U ( [ a - f A - F 0 - 9 ] { 8 } ) | \\ ( [ ^ ] ) / g;
119var escapeReplacements = {
1210 '\\' : '\\' , "'" : "'" , '"' : '"' ,
1311 'n' : '\n' , 'r' : '\r' , 't' : '\t' , 'f' : '\f' , 'b' : '\b' ,
@@ -351,29 +349,25 @@ export default class N3Lexer {
351349
352350 // ### `_unescape` replaces N3 escape codes by their corresponding characters
353351 _unescape ( item ) {
354- try {
355- return item . replace ( escapeSequence , function ( sequence , unicode4 , unicode8 , escapedChar ) {
356- var charCode ;
357- if ( unicode4 ) {
358- charCode = parseInt ( unicode4 , 16 ) ;
359- if ( isNaN ( charCode ) ) throw new Error ( ) ; // can never happen (regex), but helps performance
360- return fromCharCode ( charCode ) ;
361- }
362- else if ( unicode8 ) {
363- charCode = parseInt ( unicode8 , 16 ) ;
364- if ( isNaN ( charCode ) ) throw new Error ( ) ; // can never happen (regex), but helps performance
365- if ( charCode <= 0xFFFF ) return fromCharCode ( charCode ) ;
366- return fromCharCode ( 0xD800 + ( ( charCode -= 0x10000 ) / 0x400 ) , 0xDC00 + ( charCode & 0x3FF ) ) ;
367- }
368- else {
369- var replacement = escapeReplacements [ escapedChar ] ;
370- if ( ! replacement )
371- throw new Error ( ) ;
372- return replacement ;
373- }
374- } ) ;
375- }
376- catch ( error ) { return null ; }
352+ let invalid = false ;
353+ const replaced = item . replace ( escapeSequence , ( sequence , unicode4 , unicode8 , escapedChar ) => {
354+ // 4-digit unicode character
355+ if ( typeof unicode4 === 'string' )
356+ return String . fromCharCode ( Number . parseInt ( unicode4 , 16 ) ) ;
357+ // 8-digit unicode character
358+ if ( typeof unicode8 === 'string' ) {
359+ let charCode = Number . parseInt ( unicode8 , 16 ) ;
360+ return charCode <= 0xFFFF ? String . fromCharCode ( Number . parseInt ( unicode8 , 16 ) ) :
361+ String . fromCharCode ( 0xD800 + ( ( charCode -= 0x10000 ) >> 10 ) , 0xDC00 + ( charCode & 0x3FF ) ) ;
362+ }
363+ // fixed escape sequence
364+ if ( escapedChar in escapeReplacements )
365+ return escapeReplacements [ escapedChar ] ;
366+ // invalid escape sequence
367+ invalid = true ;
368+ return '' ;
369+ } ) ;
370+ return invalid ? null : replaced ;
377371 }
378372
379373 // ### `_parseLiteral` parses a literal into an unescaped value
0 commit comments