Sha256: acb6ad1a6a3bf4cd92f12c6440187c3e20c85e2784eb077459ed7bbf1b610a86

Contents?: true

Size: 831 Bytes

Versions: 8

Compression:

Stored size: 831 Bytes

Contents

require 'protobuf/field/bytes_field'

module Protobuf
  module Field
    class StringField < BytesField

      ##
      # Constants
      #

      ENCODING = Encoding::UTF_8

      ##
      # Public Instance Methods
      #

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

      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-3.3.6 lib/protobuf/field/string_field.rb
protobuf-3.3.5 lib/protobuf/field/string_field.rb
protobuf-3.3.4 lib/protobuf/field/string_field.rb
protobuf-3.3.3 lib/protobuf/field/string_field.rb
protobuf-3.3.2 lib/protobuf/field/string_field.rb
protobuf-3.3.1 lib/protobuf/field/string_field.rb
protobuf-3.3.0 lib/protobuf/field/string_field.rb
protobuf-3.2.1 lib/protobuf/field/string_field.rb