From 94b9b951414cf4db7f6ab63f35ffa9ef1a7fb01c Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 28 Mar 2020 21:26:58 +0200 Subject: [PATCH] rewrote insert, a bit more than anticipated --- CodeStyle.md | 19 +++++++++++++++-- lib/parfait/word.rb | 50 ++++++++++++++++++++++----------------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/CodeStyle.md b/CodeStyle.md index 1c32f5b4..1503fbd5 100644 --- a/CodeStyle.md +++ b/CodeStyle.md @@ -10,10 +10,25 @@ While the days of 80 are over, too big steps seems difficult. I've settled on 90 ### Brackets -While ruby allows the omission of brackets even with arguments, i try to avoid that because -of readability. There may be an exception for an assignment, a single call with a single arg. +While ruby allows the omission of brackets even with arguments, i try to avoid that +because of readability. There may be an exception for an assignment, a single call +with a single arg. 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 Methods should not be more than 10 lines long. It points to bad design if they are, diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index 15e0e3d6..52320baa 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -139,7 +139,7 @@ module Parfait # index = self.length + at if at < 0 raise "index not integer #{at.class}" unless at.is_a?(::Integer) 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 end @@ -190,31 +190,29 @@ module Parfait def padded_length Object.padded( 4 * get_type().instance_length + @char_length ) 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) - if index<0 - index = length+1+index - end - cpy = Word.new( length+other.length ) - cpy_ind=0 - str_ind=0 - while ( str_ind 1000