Return length in encode method for gsm7bit and ucs2

develop
Guillaume DOTT 2013-09-09 15:48:36 +02:00
parent 885ac26cd1
commit dbd2f09ec2
4 changed files with 31 additions and 16 deletions

View File

@ -30,36 +30,48 @@ module Biju
def self.decode(string, length: 0) def self.decode(string, length: 0)
res = '' res = ''
next_char = 0 next_char = 0
current_length = 0
string.scan(/../).map(&:hex).each_with_index do |octet, i| string.scan(/../).map(&:hex).each_with_index do |octet, i|
index = i % 7 index = i % 7
current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char
res = add_char(res, current) res = add_char(res, current)
current_length += 1
break if length > 0 && current_length >= length
next_char = octet >> (7 - index) next_char = octet >> (7 - index)
if index == 6 if index == 6
res = add_char(res, next_char) res = add_char(res, next_char)
current_length += 1
next_char = 0 next_char = 0
end end
end end
res[0..(length - 1)] res
end end
def self.encode(string) def self.encode(string)
res = '' res = ''
length = 0
string.chars.each do |char| string.chars.each do |char|
if get_septet(char) if get_septet(char)
res << get_septet(char).reverse res << get_septet(char).reverse
length += 1
elsif get_septet(char, escape: true) elsif get_septet(char, escape: true)
res << get_septet("\e").reverse res << get_septet("\e").reverse
res << get_septet(char, escape: true).reverse res << get_septet(char, escape: true).reverse
length += 2
end end
end end
res << ("0" * (8 - (res.length % 8))) unless res.length % 8 == 0 res << ("0" * (8 - (res.length % 8))) unless res.length % 8 == 0
res.scan(/.{8}/).map { |octet| "%02x" % octet.reverse.to_i(2) }.join [
res.scan(/.{8}/).map { |octet| "%02x" % octet.reverse.to_i(2) }.join,
length: length,
]
end end
private private

View File

@ -7,7 +7,10 @@ module Biju
end end
def self.encode(string) def self.encode(string)
string.encode('UCS-2BE').chars.map { |char| "%04x" % char.ord }.join [
string.encode('UCS-2BE').chars.map { |char| "%04x" % char.ord }.join,
length: string.length * 2,
]
end end
end end
end end

View File

@ -8,7 +8,7 @@ describe Biju::PDU::GSM7Bit do
end end
it "decodes character from extension set" do it "decodes character from extension set" do
expect(Biju::PDU::GSM7Bit.decode('9B32', length: 1)).to eq('€') expect(Biju::PDU::GSM7Bit.decode('9B32', length: 2)).to eq('€')
end end
it "decodes character with a length of 7" do it "decodes character with a length of 7" do
@ -18,15 +18,15 @@ describe Biju::PDU::GSM7Bit do
describe '::encode' do describe '::encode' do
it "encodes string" do it "encodes string" do
expect(Biju::PDU::GSM7Bit.encode('Test').upcase).to eq('D4F29C0E') expect(Biju::PDU::GSM7Bit.encode('Test').first.upcase).to eq('D4F29C0E')
end end
it "encodes character from extension set" do it "encodes character from extension set" do
expect(Biju::PDU::GSM7Bit.encode('€').upcase).to eq('9B32') expect(Biju::PDU::GSM7Bit.encode('€').first.upcase).to eq('9B32')
end end
it "encodes character with a length of 7" do it "encodes character with a length of 7" do
expect(Biju::PDU::GSM7Bit.encode('a' * 7).upcase).to eq('E170381C0E8701') expect(Biju::PDU::GSM7Bit.encode('a' * 7).first.upcase).to eq('E170381C0E8701')
end end
end end
@ -40,7 +40,7 @@ describe Biju::PDU::GSM7Bit do
strings.each do |string| strings.each do |string|
expect(Biju::PDU::GSM7Bit.decode( expect(Biju::PDU::GSM7Bit.decode(
Biju::PDU::GSM7Bit.encode(string), length: string.length)).to eq(string) *Biju::PDU::GSM7Bit.encode(string))).to eq(string)
end end
end end
end end

View File

@ -10,7 +10,7 @@ describe Biju::PDU::UCS2 do
describe '::encode' do describe '::encode' do
it "encodes string" do it "encodes string" do
expect(Biju::PDU::UCS2.encode('Ççâãåäūøœ').upcase).to eq('00C700E700E200E300E500E4016B00F80153') expect(Biju::PDU::UCS2.encode('Ççâãåäūøœ').first.upcase).to eq('00C700E700E200E300E500E4016B00F80153')
end end
end end
@ -24,7 +24,7 @@ describe Biju::PDU::UCS2 do
strings.each do |string| strings.each do |string|
expect(Biju::PDU::UCS2.decode( expect(Biju::PDU::UCS2.decode(
Biju::PDU::UCS2.encode(string), length: string.length)).to eq(string) *Biju::PDU::UCS2.encode(string))).to eq(string)
end end
end end
end end