Sha256: f93ad73e8d39414ccd4ba5708663dcf2bcde6337e469f0fe43393b1771ebcef0

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 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

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
protobuf-3.8.2 lib/protobuf/field/string_field.rb
protobuf-3.8.1 lib/protobuf/field/string_field.rb
protobuf-3.8.0 lib/protobuf/field/string_field.rb
protobuf-3.8.0.pre1 lib/protobuf/field/string_field.rb
protobuf-3.7.5 lib/protobuf/field/string_field.rb
protobuf-3.7.4 lib/protobuf/field/string_field.rb
protobuf-3.7.3 lib/protobuf/field/string_field.rb
protobuf-3.7.2 lib/protobuf/field/string_field.rb
protobuf-3.7.2.pre1 lib/protobuf/field/string_field.rb
protobuf-3.7.1 lib/protobuf/field/string_field.rb
protobuf-3.7.0 lib/protobuf/field/string_field.rb
protobuf-3.7.0.pre3 lib/protobuf/field/string_field.rb