File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env -S qjs --module
2
+ import * as std from "std" ;
3
+
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
+ }
62
+ }
63
+
64
+ main ( ) ;
You can’t perform that action at this time.
0 commit comments