Menu

#60 Converting Ruby string to NSString results in NSNull

open
5
2009-12-09
2009-10-30
Axel Roest
No

I have a ruby string which is made by filling an RTF template. The template is read in from a file with seemingly a Mac OS Roman encoding (or so says BBEdit). The template is expanded using ruby string concatenation and replacement. At the end it is about 150kB in length, but length is not the issue here. When I convert it back to NSString, I oftentimes get an NSNull instead of an NSString, and subsequent code crashes.

I've traced it down to an incorrect NSString.guess_nsencoding in cocoa_macros.rb. It guesses the data has encoding '8', which is NSShiftJISStringEncoding, instead of NSMacOSRomanStringEncoding.

The subsequent result is that NSString.alloc.initWithData_encoding returns an NSNull instead of the string.
When I manually convert using
data = NSData.dataWithRubyString( ftext )
enc = NSMacOSRomanStringEncoding
nsstr = OSX::NSString.alloc.initWithData_encoding( data, enc )

it works fine.

I hope you are able to reproduce the error, it has baffled me for days.

Enclosed is a simple appcontroller which is able to reproduce the bug if put in a rubycocoa application.
Template.rtf : the original template, which can correctly be converted
data.rtf: the generated data, which crashes the conversion.
Both rtf files open fine in TextEdit

OS platforms: the bug existed in 10.5.x and still exists in 10.6.1

Discussion

  • Axel Roest

    Axel Roest - 2009-10-30

    sample code and data

     
  • kimura wataru

    kimura wataru - 2009-12-09
    • assigned_to: nobody --> kimuraw
     
  • kimura wataru

    kimura wataru - 2009-12-09

    thanks for your report,

    there are two issues.

    (1) NSData.dataWithRubyString do not throw an exception at conversion failure
    (2) the method NSData.dataWithRubyString should be deprecated,
    because the detected encoding from NKF.guess might be wrong, its only *guess*.
    we should use initialize methods of NSString, as you say.

    If you know the encoding of the text is compatible with UTF-8 (UTF-8 or ASCII),
    your app will work with implicit conversion between NSString and Ruby String.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.