Skip to content

Commit e31b3b9

Browse files
simplified (thx to ctn-malone)
1 parent 0c483c6 commit e31b3b9

File tree

1 file changed

+4
-60
lines changed

1 file changed

+4
-60
lines changed

examples/qjs/request-reply.js

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,8 @@
11
#!/usr/bin/env -S qjs --module
22
import * as std from "std";
33

4-
var utf8ArrayToStr = (function() {
5-
var charCache = new Array(128); // Preallocate the cache for the common single byte chars
6-
var charFromCodePt = String.fromCodePoint || String.fromCharCode;
7-
var result = [];
8-
9-
return function(array) {
10-
var codePt, byte1;
11-
var buffLen = array.length;
12-
13-
result.length = 0;
14-
15-
for (var i = 0; i < buffLen;) {
16-
byte1 = array[i++];
17-
18-
if (byte1 <= 0x7f) {
19-
codePt = byte1;
20-
} else if (byte1 <= 0xdf) {
21-
codePt = ((byte1 & 0x1f) << 6) | (array[i++] & 0x3f);
22-
} else if (byte1 <= 0xef) {
23-
codePt =
24-
((byte1 & 0x0f) << 12) |
25-
((array[i++] & 0x3f) << 6) |
26-
(array[i++] & 0x3f);
27-
} else if (String.fromCodePoint) {
28-
codePt =
29-
((byte1 & 0x07) << 18) |
30-
((array[i++] & 0x3f) << 12) |
31-
((array[i++] & 0x3f) << 6) |
32-
(array[i++] & 0x3f);
33-
} else {
34-
codePt = 63; // Cannot convert four byte code points, so use "?" instead
35-
i += 3;
36-
}
37-
38-
result.push(
39-
charCache[codePt] ||
40-
(charCache[codePt] = charFromCodePt(codePt))
41-
);
42-
}
43-
44-
return result.join("");
45-
};
46-
})();
47-
48-
function main() {
49-
let result = [];
50-
let chunk;
51-
while (std.in.eof() != true ) {
52-
chunk = std.in.getByte();
53-
if (chunk > 0) {
54-
result.push(chunk);
55-
if( chunk == 10 ){
56-
console.log( "RCVD: "+utf8ArrayToStr(result).replace(/\n/g,'') )
57-
std.out.flush()
58-
result = []
59-
}
60-
}
61-
}
4+
let line;
5+
while ((line = std.in.getline()) != null) {
6+
console.log("RCVD: " + line)
7+
std.out.flush();
628
}
63-
64-
main();

0 commit comments

Comments
 (0)