Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 9x 9x 42x 9x 9x 24x 24x 24x 24x 24x 24x 24x 24x 42x 74x 74x 74x 74x 13x 13x 13x 13x 13x 13x 13x 74x 74x 74x 74x 74x 74x 11x 11x 11x 11x 11x 11x 11x 74x 74x 74x 74x 51x 51x 74x 24x 42x 6x 6x 18x 42x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var format = require( '@stdlib/string/format' );
// VARIABLES //
var RE_UTF16_LOW_SURROGATE = /[\uDC00-\uDFFF]/; // TODO: replace with stdlib pkg
var RE_UTF16_HIGH_SURROGATE = /[\uD800-\uDBFF]/; // TODO: replace with stdlib pkg
// MAIN //
/**
* Calculates the Hamming distance between two equal-length strings by comparing Unicode code points.
*
* ## Notes
*
* - The function returns a sentinel value of `-1` if the two input strings differ in the number of Unicode code points.
*
* @param {string} s1 - first input string
* @param {string} s2 - second input string
* @throws {TypeError} first argument must be a string
* @throws {TypeError} second argument must be a string
* @returns {integer} Hamming distance
*
* @example
* var dist = hammingDistanceCodePoints( 'fly', 'ant' );
* // returns 3
*
* @example
* var dist = hammingDistanceCodePoints( '👋', '🌍' );
* // returns 1
*
* @example
* var dist = hammingDistanceCodePoints( 'a👋', 'b🌍' );
* // returns 2
*/
function hammingDistanceCodePoints( s1, s2 ) {
var out;
var cp1;
var cp2;
var ch1;
var ch2;
var n1;
var n2;
var i1;
var i2;
if ( !isString( s1 ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', s1 ) );
}
if ( !isString( s2 ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', s2 ) );
}
n1 = s1.length;
n2 = s2.length;
out = 0;
i1 = 0;
i2 = 0;
// Simultaneously iterate over both strings one Unicode code point at a time
while ( i1 < n1 && i2 < n2 ) {
// Extract the next code point from s1, handling UTF-16 surrogate pairs
ch1 = s1[ i1 ];
cp1 = ch1;
if ( i1 < n1-1 && RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) {
ch2 = s1[ i1+1 ];
if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) {
// We found a surrogate pair; treat as a single code point:
cp1 += ch2;
i1 += 1;
}
}
i1 += 1;
// Extract the next code point from s2, handling UTF-16 surrogate pairs
ch1 = s2[ i2 ];
cp2 = ch1;
if ( i2 < n2-1 && RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) {
ch2 = s2[ i2+1 ];
if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) {
// We found a surrogate pair; treat as a single code point:
cp2 += ch2;
i2 += 1;
}
}
i2 += 1;
// Compare the extracted code points:
if ( cp1 !== cp2 ) {
out += 1;
}
}
// If either string has remaining code points, the strings have unequal lengths:
if ( i1 < n1 || i2 < n2 ) {
return -1;
}
return out;
}
// EXPORTS //
module.exports = hammingDistanceCodePoints;
|