lib/ri_cal/component/calendar.rb in rubyredrick-ri_cal-0.6.3 vs lib/ri_cal/component/calendar.rb in rubyredrick-ri_cal-0.7.0

- old
+ new

@@ -170,26 +170,42 @@ end def string #:nodoc: stream.string end - - def valid_utf8?(string) - string.unpack("U") rescue nil - end - def utf8_safe_split(string, n) - if string.length <= n - [string, nil] - else - before = string[0, n] - after = string[n..-1] - until valid_utf8?(after) - n = n - 1 + if RUBY_VERSION =~ /^1\.9/ + def utf8_safe_split(string, n) + if string.bytesize <= n + [string, nil] + else + bytes = string.bytes.to_a + while (128..191).include?(bytes[n]) + n = n - 1 + end + before = bytes[0,n] + after = bytes[n..-1] + [before.pack("C*").force_encoding("utf-8"), after.empty? ? nil : after.pack("C*").force_encoding("utf-8")] + end + end + else + def valid_utf8?(string) + string.unpack("U") rescue nil + end + + def utf8_safe_split(string, n) + if string.length <= n + [string, nil] + else before = string[0, n] after = string[n..-1] - end - [before, after.empty? ? nil : after] + until valid_utf8?(after) + n = n - 1 + before = string[0, n] + after = string[n..-1] + end + [before, after.empty? ? nil : after] + end end end def fold(string) #:nodoc: line, remainder = *utf8_safe_split(string, 73)