Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[implement] recursor for byteLength function
  • Loading branch information
jtenner committed Aug 22, 2019
commit b88e7b8c1c0e4115125cc6de4cdc696f2f10fbeb
21 changes: 19 additions & 2 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,31 @@ export namespace Buffer {
}

export function byteLength(input: string): i32 {
let length = input.length;
if (length & 0b11) return -1;
length -= 2;
let char: u16;
for (let i = 0; i < length; i++) {
char = load<u16>(changetype<usize>(input) + (<usize>i << alignof<u16>()));
if (i32(char >= 0x41) & i32(char <= 0x5A)) continue;
else if (i32(char >= 0x61) & i32(char <= 0x7A)) continue;
else if (i32(char >= 0x31) & i32(char <= 0x39)) continue;
else if(i32(char == 0x2B) | i32(char == 0x2F)) continue;
return -1;
}
// check each char
char = load<u16>(changetype<usize>(input) + (<usize>length << alignof<u16>()), 2);

char = load<u16>(changetype<usize>(input) + (<usize>length << alignof<u16>()), 4);
return -1;
}

export function stringLength(buffer: ArrayBuffer): i32 {
return <i32>Math.ceil(<f32>buffer.byteLength / 3) << 2;
export function stringLength(byteLength: i32): i32 {
return (<i32>Math.floor(byteLength / 3) << 2) + select(4, 0, byteLength % 3 != 0);
}

export function encode(input: string): ArrayBuffer {

return new ArrayBuffer(0);
}

Expand Down