PROTON-736: Default Ruby 1.8 encoding to be binary unless explicit

Also updated the patch to not re-encode the original string while
checking its value.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/cccd0506
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/cccd0506
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/cccd0506

Branch: refs/heads/examples
Commit: cccd05067d45623e3690945ebe4cda764c96e955
Parents: ebf80e2
Author: Darryl L. Pierce <mcpie...@gmail.com>
Authored: Tue Nov 11 14:04:58 2014 -0500
Committer: Darryl L. Pierce <mcpie...@gmail.com>
Committed: Wed Nov 12 11:51:31 2014 -0500

----------------------------------------------------------------------
 .../bindings/ruby/lib/qpid_proton/mapping.rb    | 25 ++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/cccd0506/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb 
b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
index e7e3322..778f936 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
@@ -106,15 +106,32 @@ module Qpid # :nodoc:
     DECIMAL128 = Mapping.new(Cproton::PN_DECIMAL128, "decimal128")
     UUID       = Mapping.new(Cproton::PN_UUID, "uuid")
     BINARY     = Mapping.new(Cproton::PN_BINARY, "binary")
-    STRING     = Mapping.new(Cproton::PN_STRING, "string", [String, Symbol])
+    STRING     = Mapping.new(Cproton::PN_STRING, "string", [String, Symbol,
+                                                           UTFString,
+                                                           BinaryString])
 
     class << STRING
       def put(data, value)
-        if value.is_a?(Qpid::Proton::UTFString) || 
Qpid::Proton.is_valid_utf?(value)
-          data.string = value.to_s
+        isutf = false
+
+        if value.is_a?(Qpid::Proton::UTFString)
+          isutf = true
         else
-          data.binary = value.to_s
+          # For Ruby 1.8 we will just treat all strings as binary.
+          # For Ruby 1.9+ we can check the encoding first to see what it is
+          if RUBY_VERSION >= "1.9"
+            # If the string is ASCII-8BIT then treat is as binary. Otherwise,
+            # try to convert it to UTF-8 and, if successful, send as that.
+            if value.encoding != Encoding::ASCII_8BIT &&
+                value.encode(Encoding::UTF_8).valid_encoding?
+              isutf = true
+            end
+          end
         end
+
+        data.string = value if isutf
+        data.binary = value if !isutf
+
       end
     end
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to