about summary refs log tree commit homepage
path: root/lib/rainbows/byte_slice.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/byte_slice.rb')
-rw-r--r--lib/rainbows/byte_slice.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/rainbows/byte_slice.rb b/lib/rainbows/byte_slice.rb
new file mode 100644
index 0000000..09d1188
--- /dev/null
+++ b/lib/rainbows/byte_slice.rb
@@ -0,0 +1,16 @@
+# -*- encoding: binary -*-
+module Rainbows::ByteSlice
+  if String.method_defined?(:encoding)
+    def byte_slice(buf, range)
+      if buf.encoding != Encoding::BINARY
+        buf.dup.force_encoding(Encoding::BINARY).slice!(range)
+      else
+        buf[range]
+      end
+    end
+  else
+    def byte_slice(buf, range)
+      buf[range]
+    end
+  end
+end