Skip to content

Commit 70ff585

Browse files
authored
Merge pull request #1322 from mathjax/fix/replaceUnicode
Update `replaceUnicode()` to handle two `\U{}` calls in a row.
2 parents dc661ff + ad0ab7e commit 70ff585

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

testsuite/tests/util/string.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ describe('string functions', () => {
4848
expect(string.replaceUnicode(String.raw`\U006d`)).toBe('m');
4949
expect(string.replaceUnicode(String.raw`\U{6D}`)).toBe('m');
5050
expect(string.replaceUnicode(String.raw`\U{6d}`)).toBe('m');
51+
expect(string.replaceUnicode(String.raw`\U{6d}\U{6d}`)).toBe('mm');
5152
expect(string.replaceUnicode(String.raw`a \U{62} c`)).toBe('a b c');
52-
expect(string.replaceUnicode(String.raw`\\U{61}`)).toBe(String.raw`\\U{61}`);
53-
expect(string.replaceUnicode(String.raw`\\\U{61}`)).toBe(String.raw`\\a`);
54-
expect(string.replaceUnicode(String.raw`\\\\U{61}`)).toBe(String.raw`\\\\U{61}`);
55-
expect(string.replaceUnicode(String.raw`\\\\\U{61}`)).toBe(String.raw`\\\\a`);
56-
expect(string.replaceUnicode(String.raw`x\\U{61}`)).toBe(String.raw`x\\U{61}`);
57-
expect(string.replaceUnicode(String.raw`x\\\U{61}`)).toBe(String.raw`x\\a`);
58-
expect(string.replaceUnicode(String.raw`x\\\\U{61}`)).toBe(String.raw`x\\\\U{61}`);
59-
expect(string.replaceUnicode(String.raw`x\\\\\U{61}`)).toBe(String.raw`x\\\\a`);
53+
expect(string.replaceUnicode(String.raw`\\U{61}`)).toBe(String.raw`\U{61}`);
54+
expect(string.replaceUnicode(String.raw`\\\U{61}`)).toBe(String.raw`\a`);
55+
expect(string.replaceUnicode(String.raw`\\\\U{61}`)).toBe(String.raw`\\U{61}`);
56+
expect(string.replaceUnicode(String.raw`\\\\\U{61}`)).toBe(String.raw`\\a`);
57+
expect(string.replaceUnicode(String.raw`x\\U{61}`)).toBe(String.raw`x\U{61}`);
58+
expect(string.replaceUnicode(String.raw`x\\\U{61}`)).toBe(String.raw`x\a`);
59+
expect(string.replaceUnicode(String.raw`x\\\\U{61}`)).toBe(String.raw`x\\U{61}`);
60+
expect(string.replaceUnicode(String.raw`x\\\\\U{61}`)).toBe(String.raw`x\\a`);
6061
});
6162

6263
test('toEntity()', () => {

ts/util/string.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function sortLength(a: string, b: string): number {
4242
* Quote a string for use in regular expressions
4343
*
4444
* @param {string} text The text whose regex characters are to be quoted
45-
* @returns {string} The quoted string
45+
* @returns {string} The quoted string
4646
*/
4747
export function quotePattern(text: string): string {
4848
return text.replace(/([\^$(){}.+*?\-|[\]:\\])/g, '\\$1');
@@ -52,7 +52,7 @@ export function quotePattern(text: string): string {
5252
* Convert a UTF-8 string to an array of unicode code points
5353
*
5454
* @param {string} text The string to be turned into unicode positions
55-
* @returns {number[]} Array of numbers representing the string's unicode character positions
55+
* @returns {number[]} Array of numbers representing the string's unicode character positions
5656
*/
5757
export function unicodeChars(text: string): number[] {
5858
return Array.from(text).map((c) => c.codePointAt(0));
@@ -62,7 +62,7 @@ export function unicodeChars(text: string): number[] {
6262
* Convert an array of unicode code points to a string
6363
*
6464
* @param {number[]} data The array of unicode code points
65-
* @returns {string} The string consisting of the characters at those points
65+
* @returns {string} The string consisting of the characters at those points
6666
*/
6767
export function unicodeString(data: number[]): string {
6868
return String.fromCodePoint(...data);
@@ -71,7 +71,7 @@ export function unicodeString(data: number[]): string {
7171
/**
7272
* Test if a value is a percentage
7373
*
74-
* @param {string} x The string to test
74+
* @param {string} x The string to test
7575
* @returns {boolean} True if the string ends with a percent sign
7676
*/
7777
export function isPercent(x: string): boolean {
@@ -81,23 +81,24 @@ export function isPercent(x: string): boolean {
8181
/**
8282
* Split a space-separated string of values
8383
*
84-
* @param {string} x The string to be split
84+
* @param {string} x The string to be split
8585
* @returns {string[]} The list of white-space-separated "words" in the string
8686
*/
8787
export function split(x: string): string[] {
8888
return x.trim().split(/\s+/);
8989
}
9090

9191
/**
92-
* Replace \U{...} with the specified unicode character
92+
* Replace \U{...} with the specified unicode character and \\ with \
9393
*
94-
* @param {string} text The string to be scanned for \U{...}
95-
* @returns {string} The string with the unicode characters in place of \U{...}
94+
* @param {string} text The string to be scanned for \U{...} and \\
95+
* @returns {string} The string with the unicode characters in place of \U{...}
9696
*/
9797
export function replaceUnicode(text: string): string {
9898
return text.replace(
99-
/((?:^|[^\\])(?:\\\\)*)\\U(?:([0-9A-Fa-f]{4})|\{\s*([0-9A-Fa-f]{1,6})\s*\})/g,
100-
(_m, pre, h1, h2) => pre + String.fromCodePoint(parseInt(h1 || h2, 16))
99+
/\\U(?:([0-9A-Fa-f]{4})|\{\s*([0-9A-Fa-f]{1,6})\s*\})|\\./g,
100+
(m, h1, h2) =>
101+
m === '\\\\' ? '\\' : String.fromCodePoint(parseInt(h1 || h2, 16))
101102
);
102103
}
103104

0 commit comments

Comments
 (0)