@@ -3,6 +3,22 @@ import { AstFactory } from './AstFactory.js';
33import type { SparqlContext , SparqlGeneratorContext } from './sparql12HelperTypes.js' ;
44import type { Sparql12Nodes } from './sparql12Types.js' ;
55
6+ /**
7+ * Decode UCHAR codepoint escapes (\\uXXXX / \\UXXXXXXXX) within a string according to
8+ * [SPARQL 1.2 §19.2](https://www.w3.org/TR/sparql12-query/#sec-escapes).
9+ *
10+ * Unlike the SPARQL 1.1 variant, this function rejects surrogate code points (U+D800–U+DFFF)
11+ * even when they would form a valid surrogate pair.
12+ * @deprecated will be removed in next MAJOR in favor of the less usecase dependent {@link decodeUchar}.
13+ */
14+ export function sparql12CodepointEscape ( input : string ) : string {
15+ return input . replaceAll (
16+ / \\ u ( [ 0 - 9 a - f A - F ] { 4 } ) | \\ U ( [ 0 - 9 a - f A - F ] { 8 } ) / gu,
17+ ( _ , unicode4 : string | undefined , unicode8 : string | undefined ) =>
18+ decodeUchar ( ( unicode4 ?? unicode8 ) ! ) ,
19+ ) ;
20+ }
21+
622export function decodeUchar ( hex : string ) : string {
723 const codePoint = Number . parseInt ( hex , 16 ) ;
824 if ( codePoint >= 0xD800 && codePoint <= 0xDFFF ) {
@@ -20,6 +36,10 @@ export function completeParseContext(
2036 prefixes : Object . assign ( Object . create ( null ) , context . prefixes ) ,
2137 parseMode : context . parseMode ? new Set ( context . parseMode ) : new Set ( [ 'canParseVars' , 'canCreateBlankNodes' ] ) ,
2238 skipValidation : context . skipValidation ?? false ,
39+ /**
40+ * @deprecated since it cannot be used for string decoding.
41+ */
42+ codepointEscape : context . codepointEscape ?? sparql12CodepointEscape ,
2343 } ;
2444}
2545
0 commit comments