Skip to content

Commit dec92d3

Browse files
committed
stdlib: Add a write_be_uint() function to writers
1 parent 7f3f66d commit dec92d3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lib/io.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ type writer =
345345
impure fn write_bytes(vec[u8] bytes);
346346
impure fn write_le_uint(uint n, uint size);
347347
impure fn write_le_int(int n, uint size);
348+
impure fn write_be_uint(uint n, uint size);
348349
};
349350

350351
fn uint_to_le_bytes(uint n, uint size) -> vec[u8] {
@@ -357,6 +358,16 @@ fn uint_to_le_bytes(uint n, uint size) -> vec[u8] {
357358
ret bytes;
358359
}
359360

361+
fn uint_to_be_bytes(uint n, uint size) -> vec[u8] {
362+
let vec[u8] bytes = vec();
363+
auto i = (size - 1u) as int;
364+
while (i >= 0) {
365+
bytes += vec(((n >> ((i * 8) as uint)) & 255u) as u8);
366+
i -= 1;
367+
}
368+
ret bytes;
369+
}
370+
360371
state obj new_writer(buf_writer out) {
361372
fn get_buf_writer() -> buf_writer {
362373
ret out;
@@ -383,6 +394,9 @@ state obj new_writer(buf_writer out) {
383394
impure fn write_le_int(int n, uint size) {
384395
out.write(uint_to_le_bytes(n as uint, size));
385396
}
397+
impure fn write_be_uint(uint n, uint size) {
398+
out.write(uint_to_be_bytes(n, size));
399+
}
386400
}
387401

388402
// FIXME: Remove me once objects are exported.

0 commit comments

Comments
 (0)