rewrote insert, a bit more than anticipated
This commit is contained in:
parent
a758d610d6
commit
94b9b95141
19
CodeStyle.md
19
CodeStyle.md
@ -10,10 +10,25 @@ While the days of 80 are over, too big steps seems difficult. I've settled on 90
|
|||||||
|
|
||||||
### Brackets
|
### Brackets
|
||||||
|
|
||||||
While ruby allows the omission of brackets even with arguments, i try to avoid that because
|
While ruby allows the omission of brackets even with arguments, i try to avoid that
|
||||||
of readability. There may be an exception for an assignment, a single call with a single arg.
|
because of readability. There may be an exception for an assignment, a single call
|
||||||
|
with a single arg.
|
||||||
Brackets without arguments look funny though.
|
Brackets without arguments look funny though.
|
||||||
|
|
||||||
|
### Spacing and indent
|
||||||
|
|
||||||
|
Indent at 2 Spaces (no tabs, not 4 spaces)
|
||||||
|
|
||||||
|
Space around things, like assignment, operators
|
||||||
|
|
||||||
|
### Single line versions
|
||||||
|
|
||||||
|
Ruby has single line versions for mny things, like if and while. Use them if
|
||||||
|
the code fits comfortably on the line. Eg "return true if(somthing)".
|
||||||
|
|
||||||
|
Each is used a lot in ruby, and for each/collect etc and other block passing
|
||||||
|
the convention is to use curly braces for one-liners, and do-end for multi-line.
|
||||||
|
|
||||||
### Method length
|
### Method length
|
||||||
|
|
||||||
Methods should not be more than 10 lines long. It points to bad design if they are,
|
Methods should not be more than 10 lines long. It points to bad design if they are,
|
||||||
|
@ -139,7 +139,7 @@ module Parfait
|
|||||||
# index = self.length + at if at < 0
|
# index = self.length + at if at < 0
|
||||||
raise "index not integer #{at.class}" unless at.is_a?(::Integer)
|
raise "index not integer #{at.class}" unless at.is_a?(::Integer)
|
||||||
raise "index must be positive , not #{at}" if (index < 0)
|
raise "index must be positive , not #{at}" if (index < 0)
|
||||||
raise "index too large #{at} > #{self.length}" if (index >= self.length )
|
raise "index too large #{at} >= #{self.length}" if (index >= self.length )
|
||||||
return index + Word.type_length * 4
|
return index + Word.type_length * 4
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -190,31 +190,29 @@ module Parfait
|
|||||||
def padded_length
|
def padded_length
|
||||||
Object.padded( 4 * get_type().instance_length + @char_length )
|
Object.padded( 4 * get_type().instance_length + @char_length )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# copy a chunk from the given word, to the current one
|
||||||
|
# start at to index at self
|
||||||
|
# start at from index at the given word
|
||||||
|
# copy length amount of characters
|
||||||
|
def copy_to_from(to , word , from , len)
|
||||||
|
raise "from not in range #{from}:#{len}" if (from + len) > word.length
|
||||||
|
raise "to not in range #{to}:#{len}" if (to + len) > self.length
|
||||||
|
while( len > 0)
|
||||||
|
len -= 1
|
||||||
|
set_char( to + len , word.get_char(from + len))
|
||||||
|
end
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
# insert the string other, at index index
|
||||||
|
# index may be negative in which case it counts from the end
|
||||||
def insert(index, other)
|
def insert(index, other)
|
||||||
if index<0
|
index += (length + 1) if index < 0
|
||||||
index = length+1+index
|
copy = Word.new( length + other.length )
|
||||||
end
|
copy.copy_to_from(0 , self , 0 , index )
|
||||||
cpy = Word.new( length+other.length )
|
copy.copy_to_from(index , other , 0 , other.length)
|
||||||
cpy_ind=0
|
copy.copy_to_from(index + other.length , self , index , copy.length - index - other.length)
|
||||||
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
|
end
|
||||||
|
|
||||||
def start_with(other)
|
def start_with(other)
|
||||||
@ -229,7 +227,7 @@ module Parfait
|
|||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def check_length
|
def check_length
|
||||||
raise "Length out of bounds #{char_length}" if @char_length > 1000
|
raise "Length out of bounds #{char_length}" if @char_length > 1000
|
||||||
|
Loading…
Reference in New Issue
Block a user