2015-05-13 15:06:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
module Parfait
|
|
|
|
# A word is a a short sequence of characters
|
|
|
|
# Characters are not modeled as objects but as (small) integers
|
2015-10-26 11:23:52 +01:00
|
|
|
# The small means two of them have to fit into a machine word, utf16 or similar
|
2015-05-13 15:06:38 +02:00
|
|
|
#
|
|
|
|
# Words are constant, maybe like js strings, ruby symbols
|
|
|
|
# Words are short, but may have spaces
|
2015-05-15 15:45:36 +02:00
|
|
|
|
2015-05-17 13:41:18 +02:00
|
|
|
# Words are objects, that means they carry Layout as index 0
|
|
|
|
# So all indexes are offset by one in the implementation
|
|
|
|
# Object length is measured in non-layout cells though
|
|
|
|
|
2015-05-15 15:45:36 +02:00
|
|
|
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
2015-05-13 15:06:38 +02:00
|
|
|
class Word < Object
|
2015-10-26 11:23:52 +01:00
|
|
|
attribute :char_length
|
|
|
|
|
2015-05-14 19:39:12 +02:00
|
|
|
# initialize with length. For now we try to keep all non-parfait (including String) out
|
2015-05-20 12:50:25 +02:00
|
|
|
# String will contain spaces for non-zero length
|
2015-10-22 17:16:29 +02:00
|
|
|
# Register provides methods to create Parfait objects from ruby
|
2015-05-15 15:45:36 +02:00
|
|
|
def initialize len
|
|
|
|
super()
|
2015-10-26 11:23:52 +01:00
|
|
|
self.char_length = 0
|
2015-05-15 15:45:36 +02:00
|
|
|
raise "Must init with int, not #{len.class}" unless len.kind_of? Fixnum
|
|
|
|
raise "Must init with positive, not #{len}" if len < 0
|
2015-10-26 11:23:52 +01:00
|
|
|
set_length( len , 32 ) unless len == 0 #32 beeing ascii space
|
2015-05-15 15:45:36 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# return a copy of self
|
|
|
|
def copy
|
|
|
|
cop = Word.new( self.length )
|
|
|
|
index = 1
|
|
|
|
while( index <= self.length )
|
|
|
|
cop.set_char(index , self.get_char(index))
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
cop
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the number of characters
|
2015-05-15 15:45:36 +02:00
|
|
|
def length()
|
2015-10-26 11:23:52 +01:00
|
|
|
obj_len = self.char_length
|
2015-05-15 15:45:36 +02:00
|
|
|
return obj_len
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2015-05-28 20:10:27 +02:00
|
|
|
# make every char equal the given one
|
|
|
|
def fill_with char
|
|
|
|
fill_from_with(0 , char)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_from_with from , char
|
|
|
|
len = self.length()
|
|
|
|
return if from <= 0
|
|
|
|
while( from <= len)
|
|
|
|
set_char( from , char)
|
|
|
|
from = from + 1
|
|
|
|
end
|
|
|
|
from
|
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# true if no characters
|
2015-05-15 15:45:36 +02:00
|
|
|
def empty?
|
|
|
|
return self.length == 0
|
|
|
|
end
|
2015-05-14 19:39:12 +02:00
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# pad the string with the given character to the given length
|
|
|
|
#
|
2015-05-14 19:39:12 +02:00
|
|
|
def set_length(len , fill_char)
|
2015-05-17 14:34:45 +02:00
|
|
|
return if len <= 0
|
2015-10-26 11:23:52 +01:00
|
|
|
old = self.char_length
|
|
|
|
return if old >= len
|
|
|
|
self.char_length = len
|
|
|
|
check_length
|
|
|
|
fill_from_with( old + 1 , fill_char )
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# set the character at the given index to the given character
|
|
|
|
# character must be an integer, as is the index
|
|
|
|
# the index starts at one, but may be negative to count from the end
|
|
|
|
# indexes out of range will raise an error
|
2015-05-14 19:39:12 +02:00
|
|
|
def set_char at , char
|
2015-10-26 11:23:52 +01:00
|
|
|
raise "char not fixnum #{char.class}" unless char.kind_of? Fixnum
|
2015-05-14 19:39:12 +02:00
|
|
|
index = range_correct_index(at)
|
2015-10-26 11:23:52 +01:00
|
|
|
internal_object_set( index + 2, char )
|
2015-05-13 15:17:10 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# get the character at the given index
|
|
|
|
# the index starts at one, but may be negative to count from the end
|
|
|
|
# indexes out of range will raise an error
|
|
|
|
#the return "character" is an integer
|
2015-05-14 19:39:12 +02:00
|
|
|
def get_char at
|
|
|
|
index = range_correct_index(at)
|
2015-10-26 11:23:52 +01:00
|
|
|
return internal_object_get(index + 2 )
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# private method to calculate negative indexes into positives
|
2015-05-14 19:39:12 +02:00
|
|
|
def range_correct_index at
|
|
|
|
index = at
|
|
|
|
index = self.length + at if at < 0
|
2015-05-18 09:10:31 +02:00
|
|
|
raise "index must be positive , not #{at}" if (index <= 0)
|
|
|
|
raise "index too large #{at} > #{self.length}" if (index > self.length)
|
2015-05-14 19:39:12 +02:00
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# compare the word to another
|
|
|
|
# currently checks for same class, though really identity of the characters
|
|
|
|
# in right order would suffice
|
2015-05-14 19:39:12 +02:00
|
|
|
def == other
|
2015-05-15 15:45:36 +02:00
|
|
|
return false if other.class != self.class
|
2015-05-14 19:39:12 +02:00
|
|
|
return false if other.length != self.length
|
|
|
|
len = self.length
|
2015-05-20 12:50:25 +02:00
|
|
|
while(len > 0)
|
2015-05-14 19:39:12 +02:00
|
|
|
return false if self.get_char(len) != other.get_char(len)
|
|
|
|
len = len - 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2015-05-15 15:45:36 +02:00
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# as we answered is_value? with true, sof will create a basic node with this string
|
2015-05-15 20:11:29 +02:00
|
|
|
def to_sof
|
2015-05-24 15:55:03 +02:00
|
|
|
"'" + to_s + "'"
|
2015-05-15 20:11:29 +02:00
|
|
|
end
|
2015-05-18 09:47:29 +02:00
|
|
|
|
2015-10-26 11:23:52 +01:00
|
|
|
private
|
|
|
|
def check_length
|
|
|
|
raise "Length out of bounds #{self.char_length}" if self.char_length > 32
|
|
|
|
end
|
2015-05-13 15:06:38 +02:00
|
|
|
end
|
|
|
|
end
|