This project is archived and is in readonly mode.

#3192 ✓committed
sarah (at ultrasaurus)

should be able to truncate a multibyte string to a max # of bytes

Reported by sarah (at ultrasaurus) | September 12th, 2009 @ 12:06 AM

Common issue when you have a database column limited to a specific number of bytes, but you might have a multi-byte string. We think there should be a method to help with this. Here's our proposal (spec followed by implementation):

require File.dirname(FILE) + '/../spec_helper'

describe "Chars#limit_bytes" do
it 'should return "" on ""' do

"".mb_chars.limit_bytes(0).should == ""
"".mb_chars.limit_bytes(1).should == ""

end

it 'should truncate single byte character strings as expected' do

a = "abcd"
a.mb_chars.limit_bytes(0).should == ''
a.mb_chars.limit_bytes(1).should == 'a'
a.mb_chars.limit_bytes(50).should == 'abcd'

end

it 'should truncate multi-byte character strings at character boundaries' do

k = "こんいちわ"
k.mb_chars.limit_bytes(0).should == ''
k.mb_chars.limit_bytes(1).should == ''
k.mb_chars.limit_bytes(3).should == 'こ'
k.mb_chars.limit_bytes(4).should == 'こ'
k.mb_chars.limit_bytes(5).should == 'こ'
k.mb_chars.limit_bytes(6).should == 'こん'
k.mb_chars.limit_bytes(7).should == 'こん'
k.mb_chars.limit_bytes(50).should == 'こんいちわ'

end end

module ActiveSupport #:nodoc:
module Multibyte #:nodoc:

class Chars
  def limit_bytes(limit)
    limit -= 1 while !valid_boundary?(limit)
    s = @wrapped_string.slice(0,limit)
    s.mb_chars
  end

  def valid_boundary?(length)
    chunk = @wrapped_string.slice(0,length)
    begin
      chunk.unpack('U*')
      true
    rescue
      false
    end
  end
end

end end

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages