|
| 1 | +/// @docImport 'uint16.dart'; |
| 2 | +/// @docImport 'uint32.dart'; |
| 3 | +library; |
| 4 | + |
1 | 5 | import 'dart:math' as math;
|
| 6 | +import 'dart:typed_data'; |
2 | 7 |
|
3 | 8 | /// Additional functionality for any integer, without size restrictions.
|
4 | 9 | ///
|
@@ -120,3 +125,60 @@ extension IntExtension on int {
|
120 | 125 | return (this + other) ~/ 2;
|
121 | 126 | }
|
122 | 127 | }
|
| 128 | + |
| 129 | +/// Additional functionality for [BytesBuilder]. |
| 130 | +extension BytesBuilderExtension on BytesBuilder { |
| 131 | + /// Appends [word] to the current contents of this builder. |
| 132 | + /// |
| 133 | + /// The [word] will be truncated to a [Uint16]. |
| 134 | + void addWord(int word, [Endian endian = Endian.big]) { |
| 135 | + final byte1 = (word >> 8) & 0xFF; |
| 136 | + final byte2 = word & 0xFF; |
| 137 | + if (endian == Endian.big) { |
| 138 | + addByte(byte1); |
| 139 | + addByte(byte2); |
| 140 | + } else { |
| 141 | + addByte(byte2); |
| 142 | + addByte(byte1); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /// Appends [words] to the current contents of this builder. |
| 147 | + /// |
| 148 | + /// Each value of [words] will be truncated to a [Uint16]. |
| 149 | + void addWords(List<int> words, [Endian endian = Endian.big]) { |
| 150 | + for (final word in words) { |
| 151 | + addWord(word, endian); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// Appends [dword] to the current contents of this builder. |
| 156 | + /// |
| 157 | + /// The [dword] will be truncated to a [Uint32]. |
| 158 | + void addDWord(int dword, [Endian endian = Endian.big]) { |
| 159 | + final byte1 = (dword >> 24) & 0xFF; |
| 160 | + final byte2 = (dword >> 16) & 0xFF; |
| 161 | + final byte3 = (dword >> 8) & 0xFF; |
| 162 | + final byte4 = dword & 0xFF; |
| 163 | + if (endian == Endian.big) { |
| 164 | + addByte(byte1); |
| 165 | + addByte(byte2); |
| 166 | + addByte(byte3); |
| 167 | + addByte(byte4); |
| 168 | + } else { |
| 169 | + addByte(byte4); |
| 170 | + addByte(byte3); |
| 171 | + addByte(byte2); |
| 172 | + addByte(byte1); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /// Appends [dwords] to the current contents of this builder. |
| 177 | + /// |
| 178 | + /// Each value of [dwords] will be truncated to a [Uint32]. |
| 179 | + void addDWords(List<int> dwords, [Endian endian = Endian.big]) { |
| 180 | + for (final dword in dwords) { |
| 181 | + addDWord(dword, endian); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments