Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegexpError: invalid multibyte character #2528

Closed
nbroyles opened this issue Jan 27, 2015 · 1 comment
Closed

RegexpError: invalid multibyte character #2528

nbroyles opened this issue Jan 27, 2015 · 1 comment

Comments

@nbroyles
Copy link

Seeing the following error when attempting to run .sub on a US-ASCII string with a UTF-8 encoded argument string that has a non-ASCII character:

MRI:

1.9.3-p547 :001 > str = 9999.to_s
 => "9999"
1.9.3-p547 :002 > str.encoding
 => #<Encoding:US-ASCII>
1.9.3-p547 :003 > str.sub("¥", "")
 => "9999"

JRuby:

jruby-1.7.18 :002 > str = 9999.to_s
 => "9999"
jruby-1.7.18 :003 > str.encoding
 => #<Encoding:US-ASCII>
jruby-1.7.18 :005 > str.sub("¥", "")
RegexpError: invalid multibyte character: /Â¥/
    from org/jruby/RubyString.java:2817:in `sub!'
    from org/jruby/RubyString.java:2777:in `sub'
    from (irb):5:in `evaluate'
    from org/jruby/RubyKernel.java:1107:in `eval'
    from org/jruby/RubyKernel.java:1507:in `loop'
    from org/jruby/RubyKernel.java:1270:in `catch'
    from org/jruby/RubyKernel.java:1270:in `catch'
    from /Users/nb/.rvm/gems/jruby-1.7.18/bundler/gems/rails-fc40cbbc4aaa/railties/lib/rails/commands/console.rb:110:in `start'
    from /Users/nb/.rvm/gems/jruby-1.7.18/bundler/gems/rails-fc40cbbc4aaa/railties/lib/rails/commands/console.rb:9:in `start'
    from /Users/nb/.rvm/gems/jruby-1.7.18/bundler/gems/rails-fc40cbbc4aaa/railties/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/nb/.rvm/gems/jruby-1.7.18/bundler/gems/rails-fc40cbbc4aaa/railties/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/nb/.rvm/gems/jruby-1.7.18/bundler/gems/rails-fc40cbbc4aaa/railties/lib/rails/commands.rb:17:in `(root)'
    from org/jruby/RubyKernel.java:1071:in `require'
    from script/rails:1:in `(root)'

JRuby version info:

jruby-1.7.18 :006 > `jruby -v`
 => "jruby 1.7.18 (1.9.3p551) 2014-12-22 625381c on Java HotSpot(TM) 64-Bit Server VM 1.8.0-b132 [darwin-x86_64]\n"

This is relevant as it manifests itself in the Ruby Money gem at the following location:
https://github.com/RubyMoney/money/blob/master/lib/money/money/formatting.rb#L338

@enebo
Copy link
Member

enebo commented Jan 28, 2015

cherry-picked to master. Closing. For posterity MRI does not use regexps for string subs and has this dual-use loop where search and replace code is calls either regexp or string stuff. It was pretty invasive to do it that way and it seemed kind of difficult to follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants