-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndstring.js
68 lines (64 loc) · 1.93 KB
/
ndstring.js
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
"use strict"
var ndarray = require("ndarray")
//Adapted from MDN example for unicode processing:
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
//
function ndarrayFromString(str, shape, stride, offset) {
var n = str.length
var arr = new Uint32Array(n)
var ncode_points = 0
for(var i=0; i<n; ++i) {
var code = str.charCodeAt(i)
var hi, low
if (0xD800 <= code && code <= 0xDBFF) {
hi = code
low = str.charCodeAt(++i)
if (isNaN(low)) {
throw 'High surrogate not followed by low surrogate'
}
arr[ncode_points++] = ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000
}
if (0xDC00 <= code && code <= 0xDFFF) {
continue
}
arr[ncode_points++] = code
}
if(!shape) {
shape = [ncode_points]
}
return ndarray(arr, shape, stride, offset)
}
//Unicode conversion routines adapted from here:
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FString%2FfromCharCode#Getting_it_to_work_with_higher_values
//
function ndarrayToString(arr) {
var d = arr.shape.length
if(d === 1) {
var n = arr.shape[0]
var codePoints = new Array(n)
for(var i=0; i<n; ++i) {
var code = arr.get(i)|0
if(code >= 0x10000) {
var offset = point - 0x10000
var units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point]
codePoints[i] = String.fromCharCode.apply(null, units)
} else {
codePoints[i] = String.fromCharCode(code)
}
}
return codePoints.join("")
} else if(d > 1) {
var n = arr.shape[0]
var r = new Array(n)
for(var i=0; i<n; ++i) {
r[i] = ndarrayToString(arr.pick(i))
}
return r
} else {
return []
}
}
module.exports = ndarrayFromString
module.exports.toString = ndarrayToString