Passed
Push — master ( 309f86...ed6a26 )
by Rafael S.
01:30
created

write-bytes.js ➔ write1Bit   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 4
rs 10
1
/*
2
 * Functions to turn data into bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const float = require("../src/float.js");
8
const intBits = require("int-bits");
9
10
function write64Bit(bytes, numbers, i, j) {
11
    let number = float.toFloat64(numbers[i]);
12
    bytes[j++] = number[1] & 0xFF;
13
    bytes[j++] = number[1] >>> 8 & 0xFF;
14
    bytes[j++] = number[1] >>> 16 & 0xFF;
15
    bytes[j++] = number[1] >>> 24 & 0xFF;
16
    bytes[j++] = number[0] & 0xFF;
17
    bytes[j++] = number[0] >>> 8 & 0xFF;
18
    bytes[j++] = number[0] >>> 16 & 0xFF;
19
    bytes[j++] = number[0] >>> 24 & 0xFF;
20
    return j;
21
}
22
23
// https://github.com/majimboo/c-struct
24
function write48Bit(bytes, numbers, i, j) {
25
    bytes[j++] = numbers[i] & 0xFF;
26
    bytes[j++] = numbers[i] >> 8 & 0xFF;
27
    bytes[j++] = numbers[i] >> 16 & 0xFF;
28
    bytes[j++] = numbers[i] >> 24 & 0xFF;
29
    bytes[j++] = numbers[i] / 0x100000000 & 0xFF;
30
    bytes[j++] = numbers[i] / 0x10000000000 & 0xFF;
31
    return j;
32
}
33
34
// https://github.com/majimboo/c-struct
35
function write40Bit(bytes, numbers, i, j) {
36
    bytes[j++] = numbers[i] & 0xFF;
37
    bytes[j++] = numbers[i] >> 8 & 0xFF;
38
    bytes[j++] = numbers[i] >> 16 & 0xFF;
39
    bytes[j++] = numbers[i] >> 24 & 0xFF;
40
    bytes[j++] = numbers[i] / 0x100000000 & 0xFF;
41
    return j;
42
}
43
44
function write32BitFloat(bytes, numbers, i, j) {
45
    numbers[i] = intBits.unpack(numbers[i]);
46
    bytes[j++] = numbers[i] & 0xFF;
47
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
48
    bytes[j++] = numbers[i] >>> 16 & 0xFF;
49
    bytes[j++] = numbers[i] >>> 24 & 0xFF;
50
    return j;
51
}
52
53
function write32Bit(bytes, numbers, i, j) {
54
    bytes[j++] = numbers[i] & 0xFF;
55
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
56
    bytes[j++] = numbers[i] >>> 16 & 0xFF;
57
    bytes[j++] = numbers[i] >>> 24 & 0xFF;
58
    return j;
59
}
60
61
function write24Bit(bytes, numbers, i, j) {
62
    bytes[j++] = numbers[i] & 0xFF;
63
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
64
    bytes[j++] = numbers[i] >>> 16 & 0xFF;
65
    return j;
66
}
67
68
function write16Bit(bytes, numbers, i, j) {
69
    bytes[j++] = numbers[i] & 0xFF;
70
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
71
    return j;
72
}
73
74
function write16BitFloat(bytes, numbers, i, j) {
75
    numbers[i] = float.toHalf(numbers[i]);
76
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
77
    bytes[j++] = numbers[i] & 0xFF;
78
    return j;
79
}
80
81
function write8Bit(bytes, numbers, i, j) {
82
    bytes[j++] = numbers[i] & 0xFF;
83
    return j;
84
}
85
86
function write4Bit(bytes, numbers, i, j) {
87
    bytes[j++] = numbers[i] & 0xF;
88
    return j;
89
}
90
91
function write2Bit(bytes, numbers, i, j) {
92
    bytes[j++] = numbers[i] < 0 ? numbers[i] + 4 : numbers[i];
93
    return j;
94
}
95
96
function write1Bit(bytes, numbers, i, j) {
97
    bytes[j++] = numbers[i] ? 1 : 0;
98
    return j;
99
}
100
101
function writeString(bytes, string, i, j) {
102
    bytes[j++] = string.charCodeAt(i);
103
    return j;
104
}
105
106
module.exports.write64Bit = write64Bit;
107
module.exports.write48Bit = write48Bit;
108
module.exports.write40Bit = write40Bit;
109
module.exports.write32BitFloat = write32BitFloat;
110
module.exports.write32Bit = write32Bit;
111
module.exports.write24Bit = write24Bit;
112
module.exports.write16Bit = write16Bit;
113
module.exports.write16BitFloat = write16BitFloat;
114
module.exports.write8Bit = write8Bit;
115
module.exports.write4Bit = write4Bit;
116
module.exports.write2Bit = write2Bit;
117
module.exports.write1Bit = write1Bit;
118
module.exports.writeString = writeString;
119