From fd679563ceaaacaadf5a8ebdd75b52cabf4019f5 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Sat, 28 Nov 2015 21:09:27 -0500 Subject: [PATCH] lib/core: Added `join_bytes` top-level function to join an array of `Bytes` Signed-off-by: Lucas Bajolet --- lib/core/bytes.nit | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/core/bytes.nit b/lib/core/bytes.nit index 3f5bee8..439a621 100644 --- a/lib/core/bytes.nit +++ b/lib/core/bytes.nit @@ -612,3 +612,20 @@ redef class NativeString return new Bytes(self, len, len) end end + +# Joins an array of bytes `arr` separated by `sep` +# +# assert join_bytes(["String".to_bytes, "is".to_bytes, "string".to_bytes], ' '.ascii).hexdigest == "537472696E6720697320737472696E67" +fun join_bytes(arr: Array[Bytes], sep: nullable BytePattern): Bytes do + if arr.is_empty then return new Bytes.empty + sep = sep or else new Bytes.empty + var endln = sep.pattern_length * (arr.length - 1) + for i in arr do endln += i.length + var ret = new Bytes.with_capacity(endln) + ret.append(arr.first) + for i in [1 .. arr.length[ do + sep.append_to(ret) + ret.append arr[i] + end + return ret +end -- 1.7.9.5