Module: Svn::CodesetUtil

Defined in:
lib/Svn/mime_type.rb

Class Method Summary collapse

Class Method Details

.from_utf8(str, encoding) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/Svn/mime_type.rb', line 459

def self.from_utf8(str, encoding)
  str ||= ''
  if str.respond_to?(:force_encoding)
    str.force_encoding('UTF-8')
    if encoding.upcase != 'UTF-8'
      str = str.encode(encoding, :invalid => :replace,
      :undef => :replace, :replace => '?')
    else
      str = self.replace_invalid_utf8(str)
    end
  elsif RUBY_PLATFORM == 'java'
    begin
      ic = Iconv.new(encoding, 'UTF-8')
      str = ic.iconv(str)
    rescue
      str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
    end
  else
    ic = Iconv.new(encoding, 'UTF-8')
    txtar = ""
    begin
      txtar += ic.iconv(str)
    rescue Iconv::IllegalSequence
      txtar += $!.success
      str = '?' + $!.failed[1, $!.failed.length]
      retry
    rescue
      txtar += $!.success
    end
    str = txtar
  end
end

.replace_invalid_utf8(str) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/Svn/mime_type.rb', line 357

def self.replace_invalid_utf8(str)
  return str if str.nil?
  if str.respond_to?(:force_encoding)
    str.force_encoding('UTF-8')
    if ! str.valid_encoding?
      str = str.encode("US-ASCII", :invalid => :replace,
      :undef => :replace, :replace => '?').encode("UTF-8")
    end
  elsif RUBY_PLATFORM == 'java'
    begin
      ic = Iconv.new('UTF-8', 'UTF-8')
      str = ic.iconv(str)
    rescue
      str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
    end
  else
    ic = Iconv.new('UTF-8', 'UTF-8')
    txtar = ""
    begin
      txtar += ic.iconv(str)
    rescue Iconv::IllegalSequence
      txtar += $!.success
      str = '?' + $!.failed[1,$!.failed.length]
      retry
    rescue
      txtar += $!.success
    end
    str = txtar
  end
  str
end

.to_utf8(str, encoding) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/Svn/mime_type.rb', line 389

def self.to_utf8(str, encoding)
  return str if str.nil?
  str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
  if str.empty?
    str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
    return str
  end
  enc = encoding.blank? ? "UTF-8" : encoding
  if str.respond_to?(:force_encoding)
    if enc.upcase != "UTF-8"
      str.force_encoding(enc)
      str = str.encode("UTF-8", :invalid => :replace,
      :undef => :replace, :replace => '?')
    else
      str.force_encoding("UTF-8")
      if ! str.valid_encoding?
        str = str.encode("US-ASCII", :invalid => :replace,
        :undef => :replace, :replace => '?').encode("UTF-8")
      end
    end
  elsif RUBY_PLATFORM == 'java'
    begin
      ic = Iconv.new('UTF-8', enc)
      str = ic.iconv(str)
    rescue
      str = str.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
    end
  else
    ic = Iconv.new('UTF-8', enc)
    txtar = ""
    begin
      txtar += ic.iconv(str)
    rescue Iconv::IllegalSequence
      txtar += $!.success
      str = '?' + $!.failed[1,$!.failed.length]
      retry
    rescue
      txtar += $!.success
    end
    str = txtar
  end
  str
end

.to_utf8_by_setting(str) ⇒ Object



433
434
435
436
437
438
439
440
# File 'lib/Svn/mime_type.rb', line 433

def self.to_utf8_by_setting(str)
  return str if str.nil?
  str = self.to_utf8_by_setting_internal(str)
  if str.respond_to?(:force_encoding)
    str.force_encoding('UTF-8')
  end
  str
end

.to_utf8_by_setting_internal(str) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/Svn/mime_type.rb', line 442

def self.to_utf8_by_setting_internal(str)
  return str if str.nil?
  if str.respond_to?(:force_encoding)
    str.force_encoding('ASCII-8BIT')
  end
  return str if str.empty?
  return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
  if str.respond_to?(:force_encoding)
    str.force_encoding('UTF-8')
  end
  str = self.replace_invalid_utf8(str)
  if str.respond_to?(:force_encoding)
    str.force_encoding('UTF-8')
  end
  str
end