basic word functionality
and conversion from string
This commit is contained in:
parent
5e0e8c8364
commit
164816c441
@ -48,3 +48,14 @@ class Parfait::List
|
|||||||
internal_object_length
|
internal_object_length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Functions to generate parfait objects
|
||||||
|
String.class_eval do
|
||||||
|
def to_word
|
||||||
|
word = Parfait::Word.new( self.length )
|
||||||
|
codepoints.each_with_index do |code , index |
|
||||||
|
word.set_char(index , char)
|
||||||
|
end
|
||||||
|
word
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -8,10 +8,64 @@ module Parfait
|
|||||||
# Words are constant, maybe like js strings, ruby symbols
|
# Words are constant, maybe like js strings, ruby symbols
|
||||||
# Words are short, but may have spaces
|
# Words are short, but may have spaces
|
||||||
class Word < Object
|
class Word < Object
|
||||||
def initialize str
|
# initialize with length. For now we try to keep all non-parfait (including String) out
|
||||||
@string = str
|
# Virtual provides methods to create Parfait objects from ruby
|
||||||
|
def initialize length
|
||||||
|
super
|
||||||
|
set_length( length , 32 )
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def set_length(len , fill_char)
|
||||||
|
return if len < 0
|
||||||
|
counter = self.length()
|
||||||
|
return if len >= counter
|
||||||
|
internal_object_grow( ( len + 1 ) / 2)
|
||||||
|
while( counter < len)
|
||||||
|
set_char( counter , fill_char)
|
||||||
|
counter = counter + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_char at , char
|
||||||
|
raise "char out of range #{char}" if (char < 0) or (char > 65000)
|
||||||
|
index = range_correct_index(at)
|
||||||
|
was = internal_object_get(index / 2 )
|
||||||
|
if index % 2
|
||||||
|
char = (char << 16) + (was & 0xFFFF)
|
||||||
|
else
|
||||||
|
char = char + (was & 0xFFFF0000)
|
||||||
|
end
|
||||||
|
internal_object_set( index / 2 , char )
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_char at
|
||||||
|
index = range_correct_index(at)
|
||||||
|
whole = internal_object_get(index / 2 )
|
||||||
|
if index % 2
|
||||||
|
char = whole >> 16
|
||||||
|
else
|
||||||
|
char = whole & 0xFFFF
|
||||||
|
end
|
||||||
|
return char
|
||||||
|
end
|
||||||
|
|
||||||
|
def range_correct_index at
|
||||||
|
index = at
|
||||||
|
index = self.length + at if at < 0
|
||||||
|
raise "index out of range #{at}" if index < 0
|
||||||
|
return index
|
||||||
|
end
|
||||||
|
|
||||||
|
def == other
|
||||||
|
return false if other.length != self.length
|
||||||
|
len = self.length
|
||||||
|
while len
|
||||||
|
return false if self.get_char(len) != other.get_char(len)
|
||||||
|
len = len - 1
|
||||||
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
attr_reader :string
|
|
||||||
|
|
||||||
def result= value
|
def result= value
|
||||||
raise "called"
|
raise "called"
|
||||||
|
Loading…
Reference in New Issue
Block a user