From 765a2b3ecffb68a18849de6db54a680a1fd6eee4 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Mon, 12 Jul 2010 05:51:02 +0800 Subject: Add a _vec.slice function that'll hold us over until .(a,b) syntax is implemented. This could actually replace .(a,b) syntax if the language grows optional function parameters. --- src/lib/_vec.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/lib') diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 06e738f5..1b9f5aaa 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -48,6 +48,21 @@ fn buf[T](vec[T] v) -> vbuf { ret rustrt.vec_buf[T](v); } +// Returns elements from [start..end) from v. +fn slice[T](vec[T] v, int start, int end) -> vec[T] { + check(0 <= start); + check(start <= end); + // FIXME #108: This doesn't work yet. + //check(end <= int(len[T](v))); + auto result = alloc[T](uint(end - start)); + let mutable int i = start; + while (i < end) { + result += vec(v.(i)); + i += 1; + } + ret result; +} + // Ought to take mutable &vec[T] v and just mutate it instead of copy // and return. Blocking on issue #89 for this. fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] { -- cgit v1.2.3