Sha256: b0b9a976df037726624df07ae7f544581e3ef3522e98e8bb25d5af2bbe890f07

Contents?: true

Size: 792 Bytes

Versions: 8

Compression:

Stored size: 792 Bytes

Contents

require 'protobuf/field/bytes_field'

module Protobuf
  module Field
    class StringField < BytesField
      ENCODING = 'UTF-8'.freeze

      def decode(bytes)
        bytes_to_decode = bytes.dup
        bytes_to_decode.force_encoding(::Protobuf::Field::StringField::ENCODING)
        bytes_to_decode
      end

      # TODO: make replace character configurable?
      def encode(value)
        value_to_encode = value.dup
        value_to_encode.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
        value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)

        string_size = ::Protobuf::Field::VarintField.encode(value_to_encode.size)
        string_size << value_to_encode
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
protobuf-2.8.13 lib/protobuf/field/string_field.rb
protobuf-2.8.12 lib/protobuf/field/string_field.rb
protobuf-2.8.11 lib/protobuf/field/string_field.rb
protobuf-2.8.10 lib/protobuf/field/string_field.rb
protobuf-2.8.9 lib/protobuf/field/string_field.rb
protobuf-2.8.8 lib/protobuf/field/string_field.rb
protobuf-2.8.7 lib/protobuf/field/string_field.rb
protobuf-2.8.6 lib/protobuf/field/string_field.rb