Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2013-10-26 11:57:53 +0400
committerFeross Aboukhadijeh <feross@feross.org>2013-10-26 11:57:53 +0400
commita64446d811fd9b5d676e7cb886a4e4f4072d99bb (patch)
treed724f7c63870e29aa868897db10f49ab60525044 /lib
parent40aa2dfd7660885ba1587e9fa591c608745a9832 (diff)
chrome app that can send UDP packets to an echo server
Diffstat (limited to 'lib')
-rw-r--r--lib/string.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/string.js b/lib/string.js
new file mode 100644
index 0000000..80b1362
--- /dev/null
+++ b/lib/string.js
@@ -0,0 +1,90 @@
+/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding */
+
+exports.fromUTF8Arr = UTF8ArrToStr
+exports.toUTF8Arr = strToUTF8Arr
+
+/* UTF-8 array to DOMString and vice versa */
+
+function UTF8ArrToStr (aBytes) {
+
+ var sView = "";
+
+ for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) {
+ nPart = aBytes[nIdx];
+ sView += String.fromCharCode(
+ nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
+ /* (nPart - 252 << 32) is not possible in ECMAScript! So...: */
+ (nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
+ : nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
+ (nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
+ : nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
+ (nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
+ : nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
+ (nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128
+ : nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
+ (nPart - 192 << 6) + aBytes[++nIdx] - 128
+ : /* nPart < 127 ? */ /* one byte */
+ nPart
+ );
+ }
+
+ return sView;
+
+}
+
+function strToUTF8Arr (sDOMStr) {
+
+ var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0;
+
+ /* mapping... */
+
+ for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
+ nChr = sDOMStr.charCodeAt(nMapIdx);
+ nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
+ }
+
+ aBytes = new Uint8Array(nArrLen);
+
+ /* transcription... */
+
+ for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
+ nChr = sDOMStr.charCodeAt(nChrIdx);
+ if (nChr < 128) {
+ /* one byte */
+ aBytes[nIdx++] = nChr;
+ } else if (nChr < 0x800) {
+ /* two bytes */
+ aBytes[nIdx++] = 192 + (nChr >>> 6);
+ aBytes[nIdx++] = 128 + (nChr & 63);
+ } else if (nChr < 0x10000) {
+ /* three bytes */
+ aBytes[nIdx++] = 224 + (nChr >>> 12);
+ aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
+ aBytes[nIdx++] = 128 + (nChr & 63);
+ } else if (nChr < 0x200000) {
+ /* four bytes */
+ aBytes[nIdx++] = 240 + (nChr >>> 18);
+ aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
+ aBytes[nIdx++] = 128 + (nChr & 63);
+ } else if (nChr < 0x4000000) {
+ /* five bytes */
+ aBytes[nIdx++] = 248 + (nChr >>> 24);
+ aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
+ aBytes[nIdx++] = 128 + (nChr & 63);
+ } else /* if (nChr <= 0x7fffffff) */ {
+ /* six bytes */
+ aBytes[nIdx++] = 252 + /* (nChr >>> 32) is not possible in ECMAScript! So...: */ (nChr / 1073741824);
+ aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
+ aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
+ aBytes[nIdx++] = 128 + (nChr & 63);
+ }
+ }
+
+ return aBytes;
+
+} \ No newline at end of file