Issues regarding the charcodeat function in JavaScript, specifically focusing on the hex values 0xd7ff, 0xe000, and 0

Can you explain the significance of the values 0xd7ff,0xe000,and

(code << 10) + next - 0x35fdc00
?

const nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/

        const fullCharCodeAtPos = (input: string, pos: number) => {
            if (String.fromCharCode) return input.codePointAt(pos)
            const code = input.charCodeAt(pos)
            if (code <= 0xd7ff || code >= 0xe000) return code

            const next = input.charCodeAt(pos + 1)
            return (code << 10) + next - 0x35fdc00
        }
    

Answer №1

When the condition

(code <= 0xd7ff || code >= 0xe000)
is met, it signifies that the code is not a surrogate, as detailed in UnicodeData.txt:

…
D800;<Non Private Use High Surrogate, First>;Cs;0;L;;;;;N;;;;;
DB7F;<Non Private Use High Surrogate, Last>;Cs;0;L;;;;;N;;;;;
DB80;<Private Use High Surrogate, First>;Cs;0;L;;;;;N;;;;;
DBFF;<Private Use High Surrogate, Last>;Cs;0;L;;;;;N;;;;;
DC00;<Low Surrogate, First>;Cs;0;L;;;;;N;;;;;
DFFF;<Low Surrogate, Last>;Cs;0;L;;;;;N;;;;;
…

The range for surrogates lies between 0xD800 (== 0xd7ff + 1) and 0xDFFF (== 0xe000 - 1).


To decode UTF-16 surrogate pair code and next into a code point (beyond Basic Multilingual Plane), the formula used is

(code << 10) + next - 0x35fdc00
.

This formula represents another version of a more easily understandable expression:

0x10000 + ((code - 0xD800) << 10) + (next - 0xDC00)

(as discussed in the informative article on The WTF-8 encoding site).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps should I take to execute a npm demonstration?

Here, you can find a great example of how to convert a full JSON file into a CSV format. To make it work, I downloaded the json-2-csv program by running the command npm install json-2-csv After setting up my JavaScript file with the example code, I encou ...

Can you explain the meaning behind the exclamation mark in Angular syntax?

I've noticed this popping up in a few spots lately, but I haven't been able to find any information about it. I'm intrigued by the use of the '!' symbol in Angular syntax. Can anyone explain what it does? https://i.sstatic.net/sj ...

Infinite Loop in JavaScript

Is there a way to create an infinite loop in the JavaScript code below? Currently, there is a timer set to run every 50 seconds, but I need it to continuously repeat every 50 seconds. My knowledge of JS is very limited, and while some parts of the code w ...

Choose all elements with a specific class name without the need to specify their position in the list

I'm working on adding a function to my WordPress site that will allow me to show/hide page elements with a specific class. For example, I want any elements (such as rows, containers, and text blocks) that have the 'show-hide' class to be hid ...

What is the best way to showcase an Angular component that has been initialized programmatically?

I currently have the following set up: Users Component HTML: <div class="users"> <ul class="users__list"> <li *ngFor="let user of users"></li> </ul> </div> Users Component TS: impo ...

What is the best way to utilize ng-if in the index.html page depending on the URL?

Is there a way to hide certain elements in the index page based on the URL of nested views? In my index.html file, I am looking to implement something like this: <top-bar ng-if="!home"></top-bar> <ui-view class="reveal-animation"> ...

Add all elements except for the last one

<div id="container"> <div class="sub">a</div> <span id="add">add</span> </div> $('#add').click(function(){ $('#container').append('<div class="sub">a</div>&ap ...

Automatically closing conditional statement/loop

I'm experiencing an issue with the following if condition: function turnLedOn1(evt) { led.on(); var executed = false; if (!executed) { executed = true; checkin1 = timestamp; alert('checkin1'); } } De ...

The drop-down menu selection is non-functional on mobile and iPad devices when using Bootstrap

<div class="panel panel-default"> <select> <option value="B3">B3</option> <option value="B4">B4</option> <option value="B5">B5</option> & ...

Issue with RequireJS: The data-main attribute fails to load the specified file

As I convert my small project into nodejs, I am facing an issue with the requireJS file that defines the JS to be used in the project not loading properly. Below is the structure of my project: https://i.sstatic.net/oYnqn.png The ng-app specifies the fr ...

AngularJS - Retrieve the Key of the Selected Object from ng-options when it is an Object

I am trying to capture both the key and value from a <select> element using ng-options with an object. Currently, my ng-model captures only the selected object, but I also need to capture the key that was selected. Here is the HTML snippet I'm w ...

My confidential environment variables are being inadvertently revealed to the client by Next.js

I am encountering a problem with my project in which an environment variable is being revealed to the browser. Despite the documentation stating that only environment variables prefixed with NEXT_PUBLIC_ should be accessible in the browser environment, all ...

Streaming audio from a microphone to speakers on Ubuntu using HTML5

Currently diving into html5 and eager to experiment with playing back what I say on a microphone through speakers. Here is the JavaScript code I've put together: navigator.getUserMedia = navigator.getUserMedia ||navigator.webkitGetUserMedia || naviga ...

Error handling with JSON in Parse.com causes compatibility issues in Express js 4

I need help figuring out why the data I send in a POST call to an Express JS server hosted on Parse.com is not being received properly. This is how I send the data: var data = new Array(); data["firstName"] = firstName; data["lastName"] = las ...

What's the Hold-Up with IntersectionObserver in Brackets?

As a novice in the world of web development, I have been utilizing Brackets as my main tool. Recently, I've encountered a few hurdles specifically related to javascript. One issue I'm facing is trying to implement lazy loading on a div containing ...

Tips for limiting a website to load exclusively within an iframe

I am managing two different websites: https://exampleiframe.com (a third-party website), https://example.com (my own website) My goal is to limit the loading of https://example.com so that it only opens within an iframe on https://exampleiframe.com To a ...

Ensure the forkjoin operation completes before proceeding with the following code

Seeking assistance with a behavior that I am uncertain about. The issue I am facing is that the clients.forEach() function is throwing an error in my code snippet below. I suspect this is happening because it runs simultaneously with the forkJoin(). As a ...

Check if the element is not present in the array, then add it to the array

I am attempting to generate an array in JavaScript with unique elements extracted from a JSON feed. My thought process is possibly too influenced by PHP, where a simple script like the one below would suffice: $uniqueArray = array(); foreach($array as $ke ...

Tips for avoiding an npm package from exporting shortened component and module names when released as an Angular library

Following the steps outlined in this helpful guide, I successfully created an Angular library and published it to our GitLab package registry. Although I managed to install the package without any issues, I faced a challenge when trying to import the libr ...

Remove an item from Firebase using React with ES6 syntax

[HELP NEEDED]Seeking solution below Encountering an issue with deleting an object from the Firebase database. I have experience with this, but for some reason it's not functioning as expected: Action: export const firebase_db = firebase.database(). ...