about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-03-31 11:54:52 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-03-31 11:54:52 -0700
commitdec92d392e59075ceee750b4b816a84c873a5212 (patch)
tree61bc06e910d5d0c85d3a0c74e6fa720618349666 /src/lib
parent7f3f66df7b791ea16a64f5939a5607e8f50f23dc (diff)
downloadrust-dec92d392e59075ceee750b4b816a84c873a5212.tar.gz
rust-dec92d392e59075ceee750b4b816a84c873a5212.zip
stdlib: Add a write_be_uint() function to writers
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/io.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/lib/io.rs b/src/lib/io.rs
index 84de18157c1..11e6408c626 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.