From 5374163c9598b4dd1258f8102c7ab14224657be2 Mon Sep 17 00:00:00 2001 From: Mike Jumper Date: Fri, 5 May 2023 11:12:37 -0700 Subject: [PATCH] GUACAMOLE-615: Clarify/document internals of JS implementation of codePointCount(). --- guacamole-common-js/src/main/webapp/modules/Parser.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/guacamole-common-js/src/main/webapp/modules/Parser.js b/guacamole-common-js/src/main/webapp/modules/Parser.js index faec802d8..554e163e1 100644 --- a/guacamole-common-js/src/main/webapp/modules/Parser.js +++ b/guacamole-common-js/src/main/webapp/modules/Parser.js @@ -268,9 +268,20 @@ Guacamole.Parser = function Parser() { * given string. */ Guacamole.Parser.codePointCount = function codePointCount(str, start, end) { + + // Count only characters within the specified region str = str.substring(start || 0, end); + + // Locate each proper Unicode surrogate pair (one high surrogate followed + // by one low surrogate) var surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g); + + // Each surrogate pair represents a single codepoint but is represented by + // two characters in a JavaScript string, and thus is counted twice toward + // string length. Subtracting the number of surrogate pairs adjusts that + // length value such that it gives us the number of codepoints. return str.length - (surrogatePairs ? surrogatePairs.length : 0); + }; /**