Skip to content

Instantly share code, notes, and snippets.

@rochefort
Created October 26, 2011 18:23
Show Gist options
  • Select an option

  • Save rochefort/1317257 to your computer and use it in GitHub Desktop.

Select an option

Save rochefort/1317257 to your computer and use it in GitHub Desktop.
マルチバイト文字列を2バイトとみなし、指定したバイト数でカット。中途半端は切る。
class StringUtil
def self.split_by_size(str, max_byte_size)
length, bytes = 0, 0
str.each_char do |char|
char_size = multi_bytes?(char)? 2 : 1
break if (bytes + char_size) > max_byte_size
bytes += char_size
length += 1
end
str[0, length]
end
def self.multi_bytes?(char)
char.bytesize > 1
end
end
# -*- coding: UTF-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/string_util.rb')
describe 'StringUtil' do
describe '#muti_bytes?' do
context 'with single byte char "a"' do
it { StringUtil.multi_bytes?("a").should be_false }
end
context 'with multi bytes char "あ"' do
it { StringUtil.multi_bytes?("あ").should be_true }
end
end
describe '#split_by_size' do
context 'with "abcdefg", 5' do
it { StringUtil.split_by_size("abcdefg", 5).should == "abcde" }
end
context 'with "abcあdefg", 5' do
it { StringUtil.split_by_size("abcあdefg", 5).should == "abcあ" }
end
context 'with "abcdあefg", 5' do
it { StringUtil.split_by_size("abcdあefg", 5).should == "abcd" }
end
context 'with "abcdeあfg", 5' do
it { StringUtil.split_by_size("abcdeあfg", 5).should == "abcde" }
end
context 'with "aあいbcdefg", 5' do
it { StringUtil.split_by_size("aあいbcdefg", 5).should == "aあい" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment