Skip to content

Commit c592615

Browse files
authored
Merge pull request #5 from lihugang/support-utf
2 parents 05079d8 + 6a3615b commit c592615

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

ghStream.js

+79-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function fopen(Filename, Mode, gh_token = DEFAULT) {
180180
},
181181
f_status: xhr.status,
182182
f_mode: Mode,
183-
f_content: _string_to_array(atob(JSON.parse(xhr.response).content)),
183+
f_content: _string_to_array(utf8to16(atob(JSON.parse(xhr.response).content))),
184184
f_sha: JSON.parse(xhr.response).sha,
185185
gh_token: gh_token,
186186
pos: 0
@@ -245,8 +245,8 @@ function fclose(fp) {
245245
xhr.open("PUT", "https://api.github.com/repos/" + fp.f_info.username + "/" + fp.f_info.repo_name + "/contents/" + fp.f_info.path + "?rand=" + _rand(16), false);
246246
xhr.setRequestHeader("Authorization", "token " + fp.gh_token);
247247
if (fp.f_mode.contains("w") && fp.f_status == 404)
248-
xhr.send(JSON.stringify({ message: "ghStream.js submitted the file", content: btoa(_array_to_string(fp.f_content)) }));
249-
else xhr.send(JSON.stringify({ message: "ghStream.js submitted the file", sha: fp.f_sha, content: btoa(_array_to_string(fp.f_content)) }));
248+
xhr.send(JSON.stringify({ message: "ghStream.js submitted the file", content: btoa(utf16to8(_array_to_string(fp.f_content))) }));
249+
else xhr.send(JSON.stringify({ message: "ghStream.js submitted the file", sha: fp.f_sha, content: btoa(utf16to8(_array_to_string(fp.f_content))) }));
250250
/*
251251
Bug report:@lihugang v1.0.2 Jan24,2022
252252
ghStream.js cannot created files
@@ -486,3 +486,79 @@ function setdata(stream, data) {
486486
//};
487487
//};
488488
//_init_ghStream();
489+
490+
/* utf.js - UTF-8 <=> UTF-16 convertion
491+
*
492+
* Copyright (C) 1999 Masanao Izumo <[email protected]>
493+
* Version: 1.0
494+
* LastModified: Dec 25 1999
495+
* This library is free. You can redistribute it and/or modify it.
496+
*/
497+
498+
/*
499+
* Interfaces:
500+
* utf8 = utf16to8(utf16);
501+
* utf16 = utf8to16(utf8);
502+
*/
503+
504+
function utf16to8(str) {
505+
var out, i, len, c;
506+
507+
out = "";
508+
len = str.length;
509+
for (i = 0; i < len; i++) {
510+
c = str.charCodeAt(i);
511+
if ((c >= 0x0001) && (c <= 0x007F)) {
512+
out += str.charAt(i);
513+
} else if (c > 0x07FF) {
514+
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
515+
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
516+
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
517+
} else {
518+
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
519+
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
520+
}
521+
}
522+
return out;
523+
}
524+
525+
function utf8to16(str) {
526+
var out, i, len, c;
527+
var char2, char3;
528+
529+
out = "";
530+
len = str.length;
531+
i = 0;
532+
while (i < len) {
533+
c = str.charCodeAt(i++);
534+
switch (c >> 4) {
535+
case 0:
536+
case 1:
537+
case 2:
538+
case 3:
539+
case 4:
540+
case 5:
541+
case 6:
542+
case 7:
543+
// 0xxxxxxx
544+
out += str.charAt(i - 1);
545+
break;
546+
case 12:
547+
case 13:
548+
// 110x xxxx 10xx xxxx
549+
char2 = str.charCodeAt(i++);
550+
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
551+
break;
552+
case 14:
553+
// 1110 xxxx 10xx xxxx 10xx xxxx
554+
char2 = str.charCodeAt(i++);
555+
char3 = str.charCodeAt(i++);
556+
out += String.fromCharCode(((c & 0x0F) << 12) |
557+
((char2 & 0x3F) << 6) |
558+
((char3 & 0x3F) << 0));
559+
break;
560+
}
561+
}
562+
563+
return out;
564+
}

0 commit comments

Comments
 (0)