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
|
|
|
|
2016-02-25 21:03:11 +01:00
|
|
|
# Words are objects, that means they carry Type as index 0
|
2015-05-17 13:41:18 +02:00
|
|
|
# So all indexes are offset by one in the implementation
|
2016-02-25 20:50:10 +01:00
|
|
|
# Object length is measured in non-type cells though
|
2015-05-17 13:41:18 +02:00
|
|
|
|
2018-03-25 17:22:02 +02:00
|
|
|
class Word < Data8
|
2020-03-28 14:27:29 +01:00
|
|
|
attr_reader :char_length ,:next_word
|
2020-03-27 22:50:44 +01:00
|
|
|
|
2015-10-26 11:23:52 +01:00
|
|
|
|
2018-05-28 14:09:59 +02:00
|
|
|
def self.type_length
|
2019-09-22 23:07:30 +02:00
|
|
|
3 # 0 type , 1 char_length , next_word
|
2018-05-28 14:09:59 +02:00
|
|
|
end
|
2018-05-28 14:45:29 +02:00
|
|
|
def self.get_length_index
|
2019-09-22 23:07:30 +02:00
|
|
|
type_length - 2
|
2015-11-09 22:28:40 +01:00
|
|
|
end
|
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
|
2017-01-19 08:02:29 +01:00
|
|
|
# Risc provides methods to create Parfait objects from ruby
|
2018-05-14 10:55:01 +02:00
|
|
|
def initialize( len )
|
2015-05-15 15:45:36 +02:00
|
|
|
super()
|
2019-09-09 19:26:54 +02:00
|
|
|
@char_length = 0
|
2019-02-07 17:24:35 +01:00
|
|
|
raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer
|
2015-05-15 15:45:36 +02:00
|
|
|
raise "Must init with positive, not #{len}" if len < 0
|
2019-09-08 20:14:54 +02:00
|
|
|
fill_to( len , 32 ) unless len == 0 #32 being ascii space
|
2016-02-25 20:50:10 +01:00
|
|
|
#puts "type #{self.get_type} #{self.object_id.to_s(16)}"
|
2015-05-15 15:45:36 +02:00
|
|
|
end
|
|
|
|
|
2016-12-29 17:49:03 +01:00
|
|
|
|
2015-05-20 12:50:25 +02:00
|
|
|
# return a copy of self
|
|
|
|
def copy
|
2019-09-09 19:26:54 +02:00
|
|
|
cop = Word.new( @char_length )
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
2019-09-09 19:26:54 +02:00
|
|
|
while( index < @char_length )
|
|
|
|
cop.set_char(index , get_char(index))
|
2015-05-20 12:50:25 +02:00
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
cop
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the number of characters
|
2015-05-15 15:45:36 +02:00
|
|
|
def length()
|
2019-09-09 19:26:54 +02:00
|
|
|
@char_length
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2015-05-28 20:10:27 +02:00
|
|
|
# make every char equal the given one
|
2018-05-14 10:55:01 +02:00
|
|
|
def fill_with( char )
|
2015-05-28 20:10:27 +02:00
|
|
|
fill_from_with(0 , char)
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def fill_from_with( from , char )
|
2019-09-09 19:26:54 +02:00
|
|
|
len = @char_length
|
2018-05-14 10:55:01 +02:00
|
|
|
return if from < 0
|
|
|
|
while( from < len)
|
2015-05-28 20:10:27 +02:00
|
|
|
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?
|
2019-09-09 19:26:54 +02:00
|
|
|
return @char_length == 0
|
2015-05-15 15:45:36 +02:00
|
|
|
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
|
|
|
|
#
|
2019-09-08 20:14:54 +02:00
|
|
|
def fill_to(len , fill_char)
|
2015-05-17 14:34:45 +02:00
|
|
|
return if len <= 0
|
2019-09-09 19:26:54 +02:00
|
|
|
old = @char_length
|
2015-10-26 11:23:52 +01:00
|
|
|
return if old >= len
|
2019-09-09 19:26:54 +02:00
|
|
|
@char_length = len
|
2015-10-26 11:23:52 +01:00
|
|
|
check_length
|
2019-09-08 20:14:54 +02:00
|
|
|
fill_from_with( old , 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
|
2018-03-25 17:22:02 +02:00
|
|
|
def set_char( at , char )
|
2019-02-07 17:24:35 +01:00
|
|
|
raise "char not fixnum #{char.class}" unless char.kind_of? ::Integer
|
2015-05-14 19:39:12 +02:00
|
|
|
index = range_correct_index(at)
|
2015-11-19 09:09:24 +01:00
|
|
|
set_internal_byte( index , char)
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:22:02 +02:00
|
|
|
def set_internal_byte( index , char )
|
2015-11-20 12:25:49 +01:00
|
|
|
word_index = (index) / 4
|
|
|
|
rest = ((index) % 4)
|
2015-11-11 18:11:08 +01:00
|
|
|
shifted = char << (rest * 8)
|
2015-11-18 14:36:43 +01:00
|
|
|
was = get_internal_word( word_index )
|
2015-11-11 18:11:08 +01:00
|
|
|
was = 0 unless was.is_a?(Numeric)
|
2015-11-11 19:36:07 +01:00
|
|
|
mask = 0xFF << (rest * 8)
|
|
|
|
mask = 0xFFFFFFFF - mask
|
|
|
|
masked = was & mask
|
2015-11-11 18:11:08 +01:00
|
|
|
put = masked + shifted
|
2015-11-18 14:36:43 +01:00
|
|
|
set_internal_word( word_index , put )
|
2015-11-11 18:11:08 +01:00
|
|
|
msg = "set index=#{index} word_index=#{word_index} rest=#{rest}= "
|
|
|
|
msg += "char=#{char.to_s(16)} shifted=#{shifted.to_s(16)} "
|
|
|
|
msg += "was=#{was.to_s(16)} masked=#{masked.to_s(16)} put=#{put.to_s(16)}"
|
|
|
|
#puts msg
|
|
|
|
char
|
2015-05-13 15:17:10 +02:00
|
|
|
end
|
|
|
|
|
2015-11-11 18:11:08 +01:00
|
|
|
# get the character at the given index (lowest 1)
|
2015-05-20 12:50:25 +02:00
|
|
|
# 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
|
2018-03-25 17:22:02 +02:00
|
|
|
def get_char( at )
|
2015-05-14 19:39:12 +02:00
|
|
|
index = range_correct_index(at)
|
2015-11-19 09:09:24 +01:00
|
|
|
get_internal_byte(index)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_internal_byte( index )
|
2015-11-20 12:25:49 +01:00
|
|
|
word_index = (index ) / 4
|
|
|
|
rest = ((index) % 4)
|
2015-11-18 14:36:43 +01:00
|
|
|
char = get_internal_word(word_index)
|
2015-11-11 18:11:08 +01:00
|
|
|
char = 0 unless char.is_a?(Numeric)
|
|
|
|
shifted = char >> (8 * rest)
|
|
|
|
ret = shifted & 0xFF
|
|
|
|
msg = "get index=#{index} word_index=#{word_index} rest=#{rest}= "
|
|
|
|
msg += " char=#{char.to_s(16)} shifted=#{shifted.to_s(16)} ret=#{ret.to_s(16)}"
|
|
|
|
#puts msg
|
|
|
|
return ret
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
# private method to account for
|
|
|
|
def range_correct_index( at )
|
2015-05-14 19:39:12 +02:00
|
|
|
index = at
|
2015-11-20 12:25:49 +01:00
|
|
|
# index = self.length + at if at < 0
|
2019-02-07 17:24:35 +01:00
|
|
|
raise "index not integer #{at.class}" unless at.is_a?(::Integer)
|
2018-05-14 10:55:01 +02:00
|
|
|
raise "index must be positive , not #{at}" if (index < 0)
|
2020-03-28 20:26:58 +01:00
|
|
|
raise "index too large #{at} >= #{self.length}" if (index >= self.length )
|
2019-09-24 11:58:31 +02:00
|
|
|
return index + Word.type_length * 4
|
2015-05-14 19:39:12 +02:00
|
|
|
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
|
2016-12-31 14:17:45 +01:00
|
|
|
def compare( 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
|
2018-05-14 10:55:01 +02:00
|
|
|
len = self.length - 1
|
|
|
|
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-11-18 10:55:29 +01:00
|
|
|
def == other
|
|
|
|
return false unless other.is_a?(String) or other.is_a?(Word)
|
|
|
|
as_string = self.to_string
|
|
|
|
unless other.is_a? String
|
|
|
|
other = other.to_string
|
|
|
|
end
|
|
|
|
as_string == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_string
|
|
|
|
string = ""
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
2019-09-08 20:14:54 +02:00
|
|
|
#puts "Length = #{char_length}"
|
2019-09-09 19:26:54 +02:00
|
|
|
while( index < @char_length)
|
2015-11-18 10:55:29 +01:00
|
|
|
char = get_char(index)
|
|
|
|
string += char ? char.chr : "*"
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
string
|
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
# as we answered is_value? with true, rfx will create a basic node with this string
|
|
|
|
def to_rfx
|
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
|
|
|
|
2020-03-14 11:23:34 +01:00
|
|
|
def to_s
|
|
|
|
"Word:#{to_string}"
|
|
|
|
end
|
|
|
|
|
2015-11-14 14:04:04 +01:00
|
|
|
def padded_length
|
2019-09-09 19:26:54 +02:00
|
|
|
Object.padded( 4 * get_type().instance_length + @char_length )
|
2015-11-14 14:04:04 +01:00
|
|
|
end
|
2020-03-28 20:26:58 +01:00
|
|
|
|
|
|
|
# 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))
|
2020-03-28 18:19:34 +01:00
|
|
|
end
|
2020-03-28 20:26:58 +01:00
|
|
|
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)
|
|
|
|
index += (length + 1) if index < 0
|
|
|
|
copy = Word.new( length + other.length )
|
|
|
|
copy.copy_to_from(0 , self , 0 , index )
|
|
|
|
copy.copy_to_from(index , other , 0 , other.length)
|
|
|
|
copy.copy_to_from(index + other.length , self , index , copy.length - index - other.length)
|
2020-03-28 18:19:34 +01:00
|
|
|
end
|
2015-11-14 14:04:04 +01:00
|
|
|
|
2020-03-28 12:24:29 +01:00
|
|
|
def start_with(other)
|
|
|
|
return false if other.length > self.length
|
2020-03-28 16:15:00 +01:00
|
|
|
s = other.length
|
2020-03-28 12:24:29 +01:00
|
|
|
i=0
|
|
|
|
while i<=s-1
|
2020-03-28 17:12:27 +01:00
|
|
|
if other.get_char(i) != self.get_char(i)
|
|
|
|
return false
|
2020-03-27 22:50:44 +01:00
|
|
|
end
|
2020-03-28 12:24:29 +01:00
|
|
|
i=i+1
|
2020-03-27 22:50:44 +01:00
|
|
|
end
|
2020-03-28 17:12:27 +01:00
|
|
|
return true
|
2020-03-27 22:50:44 +01:00
|
|
|
end
|
2020-03-28 20:26:58 +01:00
|
|
|
|
2015-10-26 11:23:52 +01:00
|
|
|
private
|
|
|
|
def check_length
|
2019-09-09 19:26:54 +02:00
|
|
|
raise "Length out of bounds #{char_length}" if @char_length > 1000
|
2015-10-26 11:23:52 +01:00
|
|
|
end
|
2015-05-13 15:06:38 +02:00
|
|
|
end
|
2015-11-18 10:55:29 +01:00
|
|
|
|
2015-05-13 15:06:38 +02:00
|
|
|
end
|