File: simple_downcase.rb

package info (click to toggle)
ruby-unicode-utils 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,988 kB
  • sloc: ruby: 1,877; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- encoding: utf-8 -*-

require "unicode_utils/read_cdata"

module UnicodeUtils

  SIMPLE_DOWNCASE_MAP = Impl.read_code_point_map("simple_lc_map") # :nodoc:

  # Map each code point in +str+ that has a single code point
  # lowercase-mapping to that lowercase mapping. The returned string
  # has the same length as the original string.
  #
  # This function is locale independent.
  #
  # Examples:
  #
  #   require "unicode_utils/simple_downcase"
  #   UnicodeUtils.simple_downcase("ÜMIT: 123") => "ümit: 123"
  #   UnicodeUtils.simple_downcase("STRASSE") => "strasse"
  def simple_downcase(str)
    String.new.force_encoding(str.encoding).tap { |res|
      str.each_codepoint { |cp|
        res << (SIMPLE_DOWNCASE_MAP[cp] || cp)
      }
    }
  end
  module_function :simple_downcase

end