diff options
| author | Patrick Walton <[email protected]> | 2011-03-31 11:54:52 -0700 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2011-03-31 11:54:52 -0700 |
| commit | dec92d392e59075ceee750b4b816a84c873a5212 (patch) | |
| tree | 61bc06e910d5d0c85d3a0c74e6fa720618349666 /src | |
| parent | rustc: Use the scaled index, not the raw index, if a vector has generic size.... (diff) | |
| download | rust-dec92d392e59075ceee750b4b816a84c873a5212.tar.xz rust-dec92d392e59075ceee750b4b816a84c873a5212.zip | |
stdlib: Add a write_be_uint() function to writers
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/io.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/lib/io.rs b/src/lib/io.rs index 84de1815..11e6408c 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -345,6 +345,7 @@ type writer = impure fn write_bytes(vec[u8] bytes); impure fn write_le_uint(uint n, uint size); impure fn write_le_int(int n, uint size); + impure fn write_be_uint(uint n, uint size); }; 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] { ret bytes; } +fn uint_to_be_bytes(uint n, uint size) -> vec[u8] { + let vec[u8] bytes = vec(); + auto i = (size - 1u) as int; + while (i >= 0) { + bytes += vec(((n >> ((i * 8) as uint)) & 255u) as u8); + i -= 1; + } + ret bytes; +} + state obj new_writer(buf_writer out) { fn get_buf_writer() -> buf_writer { ret out; @@ -383,6 +394,9 @@ state obj new_writer(buf_writer out) { impure fn write_le_int(int n, uint size) { out.write(uint_to_le_bytes(n as uint, size)); } + impure fn write_be_uint(uint n, uint size) { + out.write(uint_to_be_bytes(n, size)); + } } // FIXME: Remove me once objects are exported. |