Skip to content

Commit

Permalink
Partial implementation of Array#insert().
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Reed <ted.reed@gmail.com>
  • Loading branch information
dtm authored and treed committed Jul 31, 2009
1 parent 3508b5e commit eca2172
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -252,6 +252,7 @@ namespace :test do |ns|
test "array/grep.t"
test "array/include.t"
test "array/index.t"
test "array/insert.t"
test "array/intersection.t"
test "array/join.t"
test "array/mathop.t"
Expand All @@ -267,7 +268,7 @@ namespace :test do |ns|
test "array/values_at.t"
test "array/warray.t"

task :all => [:array, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :intersection, :join, :mathop, :pop, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
task :all => [:array, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :pop, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
end

namespace :file do
Expand Down
27 changes: 27 additions & 0 deletions src/classes/Array.pir
Expand Up @@ -396,6 +396,33 @@ Return index (or nil) of ELEMENT, starting from the end
.return($I0)
.end

=item insert(INDEX, ELEMENT, ...)

Insert ELEMENT(s) at INDEX

=cut

.sub 'insert' :method
.param int index
.param pmc args :slurpy
.local int len

len = elements self

if index >= 0 goto skip_negative
index = index + 1
index = index + len
if index < 0 goto throw_index_exception

skip_negative:
splice self, args, index, 0

.return(self)

throw_index_exception:
.return(self)
.end

=item replace(ARRAY)

Replace current contents of array with ARRAY.
Expand Down
15 changes: 15 additions & 0 deletions t/array/insert.t
@@ -0,0 +1,15 @@
require 'Test'
include Test
plan 4

a = [ 1, 2, 3, 4 ]

is a.insert(0, 'cat'), ['cat', 1, 2, 3, 4,], "insert front"
is a.insert(0, 'cat', 8), ['cat', 8, 'cat', 1, 2, 3, 4,], "insert front"
is a.insert(-1, 'dog'), ['cat', 8, 'cat', 1, 2, 3, 4, 'dog'], "insert end"

todo "Need to sort out nil vs undef", "30"
is a.insert(9, 'fish'), ['cat', 8, 'cat', 1, 2, 3, 4, 'dog', nil, 'fish'], "insert and extend"

todo "Throw exceptions correctly", "29"
#is a.insert(-88, 'shark') should throw an exception

0 comments on commit eca2172

Please sign in to comment.