Merge pull request #40 from deepigarg/master
Issue #32 Implemented Insert method
This commit is contained in:
commit
4e99938f3a
@ -190,6 +190,33 @@ module Parfait
|
||||
def padded_length
|
||||
Object.padded( 4 * get_type().instance_length + @char_length )
|
||||
end
|
||||
|
||||
def insert(index, other)
|
||||
if index<0
|
||||
index = length+1+index
|
||||
end
|
||||
cpy = Word.new( length+other.length )
|
||||
cpy_ind=0
|
||||
str_ind=0
|
||||
while ( str_ind<index )
|
||||
cpy.set_char(cpy_ind,get_char(str_ind))
|
||||
cpy_ind=cpy_ind+1
|
||||
str_ind=str_ind+1
|
||||
end
|
||||
oth_ind=0
|
||||
while ( oth_ind<other.length )
|
||||
cpy.set_char(cpy_ind,other.get_char(oth_ind))
|
||||
cpy_ind=cpy_ind+1
|
||||
oth_ind=oth_ind+1
|
||||
end
|
||||
while ( str_ind<length )
|
||||
cpy.set_char(cpy_ind,get_char(str_ind))
|
||||
str_ind=str_ind+1
|
||||
cpy_ind=cpy_ind+1
|
||||
end
|
||||
cpy
|
||||
end
|
||||
|
||||
def start_with(other)
|
||||
return false if other.length > self.length
|
||||
s = other.length
|
||||
@ -202,6 +229,7 @@ module Parfait
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
private
|
||||
def check_length
|
||||
raise "Length out of bounds #{char_length}" if @char_length > 1000
|
||||
|
@ -26,5 +26,23 @@ module Parfait
|
||||
@word.set_char(1 , 32)
|
||||
end
|
||||
end
|
||||
def test_insert_1
|
||||
a = Parfait.new_word("abc")
|
||||
b = Parfait.new_word("d")
|
||||
ans = Parfait.new_word("abcd")
|
||||
assert_equal ans, a.insert(-1,b)
|
||||
end
|
||||
def test_insert_2
|
||||
a1 = Parfait.new_word("what name")
|
||||
b1 = Parfait.new_word("is your ")
|
||||
ans = Parfait.new_word("what is your name")
|
||||
assert_equal ans, a1.insert(5, b1)
|
||||
end
|
||||
def test_insert_3
|
||||
a2 = Parfait.new_word("life")
|
||||
b2 = Parfait.new_word("sad ")
|
||||
ans = Parfait.new_word("sad life")
|
||||
assert_equal ans, a2.insert(0, b2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user