Sha256: f1d7116851bf52ff73ee0425eb64454e1ee50992118fdccb7d0cc494d2cbd1b2

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

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)

        "#{::Protobuf::Field::VarintField.encode(value_to_encode.size)}#{value_to_encode}"
      end

      def encode_to_stream(value, stream)
        if value.encoding != ::Protobuf::Field::StringField::ENCODING
          value = value.dup
          value.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
        end

        byte_size = ::Protobuf::Field::VarintField.encode(value.bytesize)
        stream << tag_encoded << byte_size << value
      end

      def json_encode(value)
        value
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protobuf-3.8.5 lib/protobuf/field/string_field.rb
protobuf-3.8.5.pre0 lib/protobuf/field/string_field.rb
protobuf-3.8.4 lib/protobuf/field/string_field.rb
protobuf-3.8.3 lib/protobuf/field/string_field.rb