Skip to content

Commit

Permalink
Merge pull request #722 from nanobowers/string_unpack_s
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m authored Nov 20, 2022
2 parents bca436b + bef1a0d commit a0f302e
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/natalie/string_unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Natalie {
class StringUnpacker : public Cell {
using Tokenizer = ArrayPacker::Tokenizer;
using Token = ArrayPacker::Token;
using Endianness = ArrayPacker::Endianness;

public:
StringUnpacker(const StringObject *source, String directives, nat_int_t offset)
Expand Down Expand Up @@ -66,6 +67,12 @@ class StringUnpacker : public Cell {
case 'p':
unpack_p();
break;
case 'S':
unpack_s<uint16_t>(token);
break;
case 's':
unpack_s<int16_t>(token);
break;
case 'U':
unpack_U(env, token);
break;
Expand Down Expand Up @@ -175,6 +182,30 @@ class StringUnpacker : public Cell {
}
}

template <typename T>
void unpack_s(Token &token) {
bool little_endian = (token.endianness == ArrayPacker::Endianness::Little) || (token.endianness == Endianness::Native && system_is_little_endian());
nat_int_t consumed = 0;
if (token.count == -1) token.count = 1;
while (token.star ? !at_end() : consumed < token.count) {
if ((m_index + 2) > m_source->length()) {
if (!token.star)
m_unpacked->push(NilObject::the());
m_index++;
} else {
unsigned char c0 = next_char();
unsigned char c1 = next_char();
T result;
if (little_endian)
result = (c1 << 8) + c0;
else
result = (c0 << 8) + c1;
m_unpacked->push(Value::integer(result));
}
consumed++;
}
}

void unpack_b(Token &token) {
auto out = String();

Expand Down Expand Up @@ -580,6 +611,11 @@ class StringUnpacker : public Cell {

bool at_end() { return m_index >= m_source->length(); }

bool system_is_little_endian() {
int i = 1;
return *((char *)&i) == 1;
}

const StringObject *m_source;
TM::Vector<Token> *m_directives;
ArrayObject *m_unpacked { new ArrayObject };
Expand Down
152 changes: 152 additions & 0 deletions spec/core/string/unpack/s_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'
require_relative 'shared/basic'
require_relative 'shared/integer'

describe "String#unpack with format 'S'" do
describe "with modifier '<'" do
it_behaves_like :string_unpack_16bit_le, 'S<'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S<'
end

describe "with modifier '<' and '_'" do
it_behaves_like :string_unpack_16bit_le, 'S<_'
it_behaves_like :string_unpack_16bit_le, 'S_<'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S_<'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S<_'
end

describe "with modifier '<' and '!'" do
it_behaves_like :string_unpack_16bit_le, 'S<!'
it_behaves_like :string_unpack_16bit_le, 'S!<'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S!<'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S<!'
end

describe "with modifier '>'" do
it_behaves_like :string_unpack_16bit_be, 'S>'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S>'
end

describe "with modifier '>' and '_'" do
it_behaves_like :string_unpack_16bit_be, 'S>_'
it_behaves_like :string_unpack_16bit_be, 'S_>'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S>_'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S_>'
end

describe "with modifier '>' and '!'" do
it_behaves_like :string_unpack_16bit_be, 'S>!'
it_behaves_like :string_unpack_16bit_be, 'S!>'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S>!'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S!>'
end
end

describe "String#unpack with format 's'" do
describe "with modifier '<'" do
it_behaves_like :string_unpack_16bit_le, 's<'
it_behaves_like :string_unpack_16bit_le_signed, 's<'
end

describe "with modifier '<' and '_'" do
it_behaves_like :string_unpack_16bit_le, 's<_'
it_behaves_like :string_unpack_16bit_le, 's_<'
it_behaves_like :string_unpack_16bit_le_signed, 's<_'
it_behaves_like :string_unpack_16bit_le_signed, 's_<'
end

describe "with modifier '<' and '!'" do
it_behaves_like :string_unpack_16bit_le, 's<!'
it_behaves_like :string_unpack_16bit_le, 's!<'
it_behaves_like :string_unpack_16bit_le_signed, 's<!'
it_behaves_like :string_unpack_16bit_le_signed, 's!<'
end

describe "with modifier '>'" do
it_behaves_like :string_unpack_16bit_be, 's>'
it_behaves_like :string_unpack_16bit_be_signed, 's>'
end

describe "with modifier '>' and '_'" do
it_behaves_like :string_unpack_16bit_be, 's>_'
it_behaves_like :string_unpack_16bit_be, 's_>'
it_behaves_like :string_unpack_16bit_be_signed, 's>_'
it_behaves_like :string_unpack_16bit_be_signed, 's_>'
end

describe "with modifier '>' and '!'" do
it_behaves_like :string_unpack_16bit_be, 's>!'
it_behaves_like :string_unpack_16bit_be, 's!>'
it_behaves_like :string_unpack_16bit_be_signed, 's>!'
it_behaves_like :string_unpack_16bit_be_signed, 's!>'
end
end

little_endian do
describe "String#unpack with format 'S'" do
it_behaves_like :string_unpack_basic, 'S'
it_behaves_like :string_unpack_16bit_le, 'S'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S'
end

describe "String#unpack with format 'S' with modifier '_'" do
it_behaves_like :string_unpack_16bit_le, 'S_'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S_'
end

describe "String#unpack with format 'S' with modifier '!'" do
it_behaves_like :string_unpack_16bit_le, 'S!'
it_behaves_like :string_unpack_16bit_le_unsigned, 'S!'
end

describe "String#unpack with format 's'" do
it_behaves_like :string_unpack_basic, 's'
it_behaves_like :string_unpack_16bit_le, 's'
it_behaves_like :string_unpack_16bit_le_signed, 's'
end

describe "String#unpack with format 's' with modifier '_'" do
it_behaves_like :string_unpack_16bit_le, 's_'
it_behaves_like :string_unpack_16bit_le_signed, 's_'
end

describe "String#unpack with format 's' with modifier '!'" do
it_behaves_like :string_unpack_16bit_le, 's!'
it_behaves_like :string_unpack_16bit_le_signed, 's!'
end
end

big_endian do
describe "String#unpack with format 'S'" do
it_behaves_like :string_unpack_basic, 'S'
it_behaves_like :string_unpack_16bit_be, 'S'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S'
end

describe "String#unpack with format 'S' with modifier '_'" do
it_behaves_like :string_unpack_16bit_be, 'S_'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S_'
end

describe "String#unpack with format 'S' with modifier '!'" do
it_behaves_like :string_unpack_16bit_be, 'S!'
it_behaves_like :string_unpack_16bit_be_unsigned, 'S!'
end

describe "String#unpack with format 's'" do
it_behaves_like :string_unpack_basic, 's'
it_behaves_like :string_unpack_16bit_be, 's'
it_behaves_like :string_unpack_16bit_be_signed, 's'
end

describe "String#unpack with format 's' with modifier '_'" do
it_behaves_like :string_unpack_16bit_be, 's_'
it_behaves_like :string_unpack_16bit_be_signed, 's_'
end

describe "String#unpack with format 's' with modifier '!'" do
it_behaves_like :string_unpack_16bit_be, 's!'
it_behaves_like :string_unpack_16bit_be_signed, 's!'
end
end

0 comments on commit a0f302e

Please sign in to comment.