implement byte access for some word

wouldn’t have been possible without the ruby version
This commit is contained in:
Torsten Ruger 2015-11-11 20:36:07 +02:00
parent 9bfc9cf6c2
commit 1e7935bf85
3 changed files with 58 additions and 12 deletions

View File

@ -94,7 +94,9 @@ module Parfait
shifted = char << (rest * 8)
was = get_internal( word_index )
was = 0 unless was.is_a?(Numeric)
masked = was & [ 0xFFFFFF00 , 0xFFFF00FF , 0xFF00FFFF , 0x00FFFFFF ][rest]
mask = 0xFF << (rest * 8)
mask = 0xFFFFFFFF - mask
masked = was & mask
put = masked + shifted
set_internal( word_index , put )
msg = "set index=#{index} word_index=#{word_index} rest=#{rest}= "

View File

@ -1,8 +1,16 @@
class Word < Object
int char_at(int index)
index = index + 2
return get_internal(index)
int word_index = index - 1
word_index = word_index / 4
word_index = word_index + 3
int rest = index - 1
rest = rest.mod(4)
int char = get_internal(word_index)
int shifted = 8 * rest
shifted = char >> shifted
int ret = shifted & 255
return ret
end
int set_length(int i)
@ -10,11 +18,30 @@ class Word < Object
return i
end
Word push_char(int i)
int l = self.char_length + 1
self.set_length(l)
l = l + 2
set_internal( l , i)
Word push_char(int char)
int index = self.char_length + 1
self.set_length(index)
int word_index = index - 1
word_index = word_index / 4
word_index = word_index + 3
int rest = index - 1
rest = rest.mod(4)
int shifted = rest * 8
shifted = char << shifted
int was = get_internal( word_index )
int mask = rest * 8
mask = 255 << mask
mask = 4294967295 - mask
int masked = was & mask
int put = masked + shifted
set_internal( word_index , put )
return self
end
end

View File

@ -23,19 +23,20 @@ HERE
def test_add_doesnt_change1
@string_input = <<HERE
Word w = " "
w.push_char(32)
w.push_char(48)
return w.char_at(1)
HERE
check_return 32
check_return 32
end
def test_after_add_get_works
@string_input = <<HERE
Word w = " "
w.push_char(32)
w.push_char(48)
return w.char_at(2)
HERE
check_return 32
check_return 48
end
def test_after_add_length_works
@string_input = <<HERE
Word w = " "
@ -44,4 +45,20 @@ return w.char_length
HERE
check_return 2
end
def test_get1
@string_input = <<HERE
Word w = "12345"
return w.char_at(1)
HERE
check_return 49
end
def test_get2
@string_input = <<HERE
Word w = "12345"
return w.char_at(2)
HERE
check_return 50
end
end