Skip to content

Commit b4e24d7

Browse files
committed
fix
1 parent 63caf7b commit b4e24d7

2 files changed

Lines changed: 85 additions & 35 deletions

File tree

js/alpha-5/alpha-5.js

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
export {
2-
encode, decode, BIT_LENGTH, ALPHABET_LENGTH,// public
1+
export {
2+
// public
3+
encode, decode,
4+
BIT_LENGTH, ALPHABET_LENGTH,
5+
// private
36
_0To31NumberFromCharacter,
47
_0To31NumbersFromString,
58
_compactUInt8ArrayFrom0To31Numbers,
@@ -54,47 +57,47 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
5457
const compactUInt8Array = new Uint8Array(byteLength);
5558

5659
let bitPosition = 0;
57-
let currentByte = 0;
60+
let currentNumberToBeSaved = 0;
5861
let byteIndex = 0;
59-
let lastPrintNeeded = false;
62+
6063
const saveByte = (number) => {
61-
console.log(`save ${number} (${number.toString(2)})`)
64+
console.log(`save ${number} (${number.toString(2).padStart(8, "0")})`)
6265
compactUInt8Array[byteIndex] = number;
6366
byteIndex += 1;
64-
currentByte = 0;
67+
currentNumberToBeSaved = 0;
6568
bitPosition = 0;
6669
};
6770
_0To31Numbers.forEach(number => {
68-
if (bitPosition + BIT_LENGTH < BYTE) { // can fit at once
71+
if (bitPosition + BIT_LENGTH <= BYTE) { // can fit at once
6972
const toShift = BYTE - BIT_LENGTH - bitPosition;
70-
currentByte += number << toShift;
73+
currentNumberToBeSaved += number << toShift;
7174
bitPosition += BIT_LENGTH;
7275

7376
if (bitPosition === BYTE) {
74-
saveByte(currentByte);
77+
saveByte(currentNumberToBeSaved);
7578
}
7679
} else { // cannot fit at once
7780
const shiftRight = -BYTE + bitPosition + BIT_LENGTH; // 2
7881
const missedBits = BIT_LENGTH - shiftRight; // 3
7982
const clamped = number >> shiftRight;
80-
currentByte += clamped;
83+
currentNumberToBeSaved += clamped;
8184

82-
saveByte(currentByte);
85+
saveByte(currentNumberToBeSaved);
8386

8487
const substractHead = clamped << shiftRight;
8588
const rest = number - substractHead;
86-
currentByte += rest << (BYTE+1) - missedBits;
87-
bitPosition += missedBits;
89+
currentNumberToBeSaved += rest << (BYTE+1) - missedBits;
90+
bitPosition += missedBits-1; //3
8891
}
8992
});
9093
// todo handle all cases
91-
console.log(90,currentByte)
92-
if (currentByte) {
93-
saveByte(currentByte);
94+
// console.log(90,currentNumberToBeSaved)
95+
if (currentNumberToBeSaved) {
96+
saveByte(currentNumberToBeSaved);
9497
} //else {
9598
// compactUInt8Array[byteIndex] = remainder;
9699
// }
97-
console.log(96,compactUInt8Array)
100+
console.log("3_compactUInt8ArrayFrom0To31Numbers RESULT",compactUInt8Array)
98101
return compactUInt8Array;
99102
};
100103

@@ -148,13 +151,63 @@ const _0To31NumbersFromCompactUInt8Array = compactUInt8Array => {
148151
return _0To31Numbers;
149152
};
150153

154+
const _bytesRequiredToSaveNumber = number => {
155+
// maybe there is a simpler way
156+
let bits = 0;
157+
let divided = number;
158+
while (divided > 0) {
159+
bits += 1;
160+
divided = divided >> 1;
161+
}
162+
const bytesRequired = Math.ceil(bits / BYTE);
163+
console.log("bits required", bits);
164+
console.log("bytes required", bytesRequired);
165+
return bytesRequired;
166+
};
167+
168+
const _initializeUint8WithLength = characters => {
169+
const textBitLength = characters * BIT_LENGTH;
170+
const textByteLength = Math.ceil(textBitLength / BYTE);
171+
const bytesRequiredForTextByteLength = _bytesRequiredToSaveNumber(textByteLength);
172+
let totalByteLength = 1 + bytesRequiredForTextByteLength + textByteLength;
173+
let ArrayConstructor;
174+
if (bytesRequiredForTextByteLength === 1) {
175+
ArrayConstructor = Uint8Array;
176+
} else if (bytesRequiredForTextByteLength === 2) {
177+
ArrayConstructor = Uint16Array;
178+
totalByteLength += 1; // see todo
179+
} else if (bytesRequiredForTextByteLength === 3) {
180+
console.warn(`unhandled case byte ${Byte} === 3`);
181+
} else if (bytesRequiredForTextByteLength === 4) {
182+
ArrayConstructor = Uint32Array;
183+
totalByteLength += 3; // see todo
184+
} else if (bytesRequiredForTextByteLength > 4) {
185+
console.warn(`unhandled case byte ${Byte} > 4`);
186+
}
187+
const uInt8WithLength = new Uint8Array(totalByteLength);
188+
uInt8WithLength[0] = bytesRequiredForTextByteLength;
189+
const tempArray = new ArrayConstructor(uInt8WithLength.buffer);
190+
// todo : solve problem this can leave empty spaces
191+
tempArray[1] = textByteLength;
192+
const offset = totalByteLength - textByteLength;
193+
return [uInt8WithLength, offset];
194+
195+
};
196+
197+
198+
const _joinUint8Array = function (target, copyInto, byteOffset) {
199+
target.set(copyInto, byteOffset);
200+
return target;
201+
};
202+
151203
const encode = string => {
204+
const { length } = string;
152205
const _0To31Numbers = _0To31NumbersFromString(string);
206+
const [uInt8WithLength, offset] = _initializeUint8WithLength(length);
153207
const uInt8Array = _compactUInt8ArrayFrom0To31Numbers(_0To31Numbers);
154-
return uInt8Array;
208+
return _joinUint8Array(uInt8WithLength, uInt8Array, offset);
155209
};
156210

157-
158211
const decode = uInt8Array => {
159212
const _0To31Numbers = _0To31NumbersFromCompactUInt8Array(uInt8Array);
160213
const string = stringFrom0To31Numbers(_0To31Numbers);

js/alpha-5/tests/specification/alpha-5.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,15 @@ test(`_compactUInt8ArrayFrom0To31Numbers
4040
1, 00001
4141
2, 00010
4242
3, 00011`, t => {
43-
console.log("log before")
44-
console.log(_compactUInt8ArrayFrom0To31Numbers(
45-
[1,2,3]
46-
))
4743
t.deepEqual(_compactUInt8ArrayFrom0To31Numbers(
4844
[1,2,3]
4945
), Uint8Array.from([
5046
0b00001000, // 1-2
5147
0b10000110, // 2-3
48+
5249
]));
53-
54-
5550
});
56-
/*
51+
5752
test(`_compactUInt8ArrayFrom0To31Numbers
5853
1, 00001
5954
2, 00010
@@ -63,6 +58,17 @@ test(`_compactUInt8ArrayFrom0To31Numbers
6358
6, 00110
6459
7, 00111
6560
8, 01000`, t => {
61+
62+
console.log(37,_compactUInt8ArrayFrom0To31Numbers([1,2,3,4,5,6,7,8
63+
]))
64+
console.log(38,Uint8Array.from([
65+
0b00001000, // 1-2
66+
0b10000110, // 2-3-4
67+
0b01000010, // 4-5
68+
0b10011000, // 5-6-7
69+
0b11101000, // 7-8
70+
]))
71+
6672
t.deepEqual(_compactUInt8ArrayFrom0To31Numbers([1,2,3,4,5,6,7,8
6773
]), Uint8Array.from([
6874
0b00001000, // 1-2
@@ -72,15 +78,6 @@ test(`_compactUInt8ArrayFrom0To31Numbers
7278
0b11101000, // 7-8
7379
]));
7480

75-
console.log(37,_compactUInt8ArrayFrom0To31Numbers([1,2,3,4,5,6,7,8
76-
]))
77-
console.log(38,Uint8Array.from([
78-
0b00001000, // 1-2
79-
0b10000110, // 2-3-4
80-
0b01000010, // 4-5
81-
0b10011000, // 5-6-7
82-
0b11101000, // 7-8
83-
]))
8481
});
8582
/*
8683
*/

0 commit comments

Comments
 (0)