File: simple_upcase.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-- 781 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_UPCASE_MAP = Impl.read_code_point_map("simple_uc_map") # :nodoc:

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

end