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) shifted = char << (rest * 8)
was = get_internal( word_index ) was = get_internal( word_index )
was = 0 unless was.is_a?(Numeric) 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 put = masked + shifted
set_internal( word_index , put ) set_internal( word_index , put )
msg = "set index=#{index} word_index=#{word_index} rest=#{rest}= " msg = "set index=#{index} word_index=#{word_index} rest=#{rest}= "

View File

@ -1,8 +1,16 @@
class Word < Object class Word < Object
int char_at(int index) int char_at(int index)
index = index + 2 int word_index = index - 1
return get_internal(index) 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 end
int set_length(int i) int set_length(int i)
@ -10,11 +18,30 @@ class Word < Object
return i return i
end end
Word push_char(int i) Word push_char(int char)
int l = self.char_length + 1 int index = self.char_length + 1
self.set_length(l) self.set_length(index)
l = l + 2
set_internal( l , i) 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 return self
end end
end end

View File

@ -23,19 +23,20 @@ HERE
def test_add_doesnt_change1 def test_add_doesnt_change1
@string_input = <<HERE @string_input = <<HERE
Word w = " " Word w = " "
w.push_char(32) w.push_char(48)
return w.char_at(1) return w.char_at(1)
HERE HERE
check_return 32 check_return 32
end end
def test_after_add_get_works def test_after_add_get_works
@string_input = <<HERE @string_input = <<HERE
Word w = " " Word w = " "
w.push_char(32) w.push_char(48)
return w.char_at(2) return w.char_at(2)
HERE HERE
check_return 32 check_return 48
end end
def test_after_add_length_works def test_after_add_length_works
@string_input = <<HERE @string_input = <<HERE
Word w = " " Word w = " "
@ -44,4 +45,20 @@ return w.char_length
HERE HERE
check_return 2 check_return 2
end 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 end