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
|
2016-12-29 17:49:03 +01:00
|
|
|
attr_reader :char_length
|
2015-10-26 11:23:52 +01:00
|
|
|
|
2015-11-09 22:28:40 +01:00
|
|
|
#semi "indexed" methods for interpreter
|
|
|
|
def self.get_length_index
|
2016-02-25 20:50:10 +01:00
|
|
|
2 # 2 is the amount of attributes, type and char_length. the offset after which chars start
|
2015-11-09 22:28:40 +01:00
|
|
|
end
|
2018-03-25 17:22:02 +02:00
|
|
|
def self.get_indexed( i )
|
2015-11-19 09:09:24 +01:00
|
|
|
i + get_length_index * 4
|
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()
|
2016-12-29 17:49:03 +01:00
|
|
|
@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
|
2018-05-14 10:55:01 +02:00
|
|
|
set_length( 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
|
|
|
|
cop = Word.new( self.length )
|
2018-05-14 10:55:01 +02:00
|
|
|
index = 0
|
|
|
|
while( index < self.length )
|
2015-05-20 12:50:25 +02:00
|
|
|
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()
|
2016-12-29 17:49:03 +01:00
|
|
|
obj_len = @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
|
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 )
|
2015-05-28 20:10:27 +02:00
|
|
|
len = self.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?
|
|
|
|
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
|
2016-12-29 17:49:03 +01:00
|
|
|
old = @char_length
|
2015-10-26 11:23:52 +01:00
|
|
|
return if old >= len
|
2016-12-29 17:49:03 +01:00
|
|
|
@char_length = len
|
2015-10-26 11:23:52 +01:00
|
|
|
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
|
2018-03-25 17:22:02 +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-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
|
2018-05-14 10:55:01 +02:00
|
|
|
raise "index must be positive , not #{at}" if (index < 0)
|
|
|
|
raise "index too large #{at} > #{self.length}" if (index >= self.length )
|
2015-11-20 12:25:49 +01:00
|
|
|
return index + 11
|
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
|
|
|
|
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
|
|
|
|
2015-11-14 14:04:04 +01:00
|
|
|
def padded_length
|
2016-12-31 19:08:33 +01:00
|
|
|
Padding.padded( 4 * get_type().instance_length + @char_length )
|
2015-11-14 14:04:04 +01:00
|
|
|
end
|
|
|
|
|
2015-10-26 11:23:52 +01:00
|
|
|
private
|
|
|
|
def check_length
|
2016-12-29 17:49:03 +01: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-11-19 09:09:24 +01:00
|
|
|
# Word from string
|
2015-11-18 10:55:29 +01:00
|
|
|
def self.new_word( string )
|
|
|
|
string = string.to_s if string.is_a? Symbol
|
2016-12-30 18:17:15 +01:00
|
|
|
word = Word.new( string.length )
|
2015-11-18 10:55:29 +01:00
|
|
|
string.codepoints.each_with_index do |code , index |
|
2018-05-14 10:55:01 +02:00
|
|
|
word.set_char(index , code)
|
2015-11-18 10:55:29 +01:00
|
|
|
end
|
|
|
|
word
|
|
|
|
end
|
|
|
|
|
2015-05-13 15:06:38 +02:00
|
|
|
end
|