Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

42
LINES

< > BotCompany Repo | #1035232 // byteArrayFromBase128Utf8 - see byteArrayToBase128Utf8 [dev.]

JavaX fragment (include)

1  
scope byteArrayFromBase128Utf8
2  
3  
static int #byteFromArray(byte[] data, int i) {
4  
  ret i >= data.length ? 0 : ubyteToInt(data[i]);
5  
}
6  
7  
static byte[] byteArrayFromBase128Utf8(byte[] data) {
8  
  int n = l(data);
9  
  bool anomaly = (n & 7) == 1 && (data[n-1] & 1) != 0;
10  
  int n2 = anomaly ? n-1 : n;
11  
  int nOut = toInt(n2*7L/8);
12  
  byte[] out = new byte[nOut];
13  
  int iIn = 0, iOut = 0;
14  
  while (iIn < n2) {
15  
    // take all 7 bits of byte 0 + top bit of byte 1
16  
    out[iOut++] = (byte) (data[iIn] << (8-1) | ubyteToInt(data[iIn+1]) >> (8-1);
17  
    if (++iIn >= n2) break;
18  
    // take bottom bit of byte 0 + top 6 bits of byte 1
19  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn) << (7-1) | byteFromArray(data, iIn+1) >> (8-6));
20  
    if (++iOut >= nOut) break;
21  
    // take bottom 2 bits of byte 1 + top 5 bits of byte 2
22  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+1) << (7-2) | byteFromArray(data, iIn+2) >> (8-5));
23  
    if (++iOut >= nOut) break;
24  
    // take bottom 3 bits of byte 2 + top 4 bits of byte 3
25  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+2) << (7-3) | byteFromArray(data, iIn+3) >> (8-4));
26  
    if (++iOut >= nOut) break;
27  
    // take bottom 4 bits of byte 3 + top 3 bits of byte 4
28  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+3) << (7-4) | byteFromArray(data, iIn+4) >> (8-3));
29  
    if (++iOut >= nOut) break;
30  
    // take bottom 5 bits of byte 4 + top 2 bits of byte 5
31  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+4) << (7-5) | byteFromArray(data, iIn+5) >> (8-2));
32  
    if (++iOut >= nOut) break;
33  
    // take bottom 6 bits of byte 5 + top bit of byte 6
34  
    out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+5) << (7-6) | byteFromArray(data, iIn+6) >> (8-1));
35  
    if (++iOut >= nOut) break;
36  
    // take bottom 7 bits of byte 6
37  
    out[iOut] = (byte) (0x7F & byteFromArray(data, iIn+6));
38  
    if (++iOut >= nOut) break;
39  
    iIn += 7;
40  
  }
41  
  ret out;
42  
}

Author comment

Began life as a copy of #1035231

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035232
Snippet name: byteArrayFromBase128Utf8 - see byteArrayToBase128Utf8 [dev.]
Eternal ID of this version: #1035232/2
Text MD5: 7f4a84a2423b3d4ac5d72885adabb015
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-18 04:11:58
Source code size: 1937 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 56 / 72
Version history: 1 change(s)
Referenced in: [show references]