Sha256: 0b8cdcab7707dc81a4b0326b82c9c1295ac2b469b4d76415ffd33dd39300c82b

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# Copyright 2018 Twitter, Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

module Twitter
  module TwitterText
    # This module lazily defines constants of the form Uxxxx for all Unicode
    # codepoints from U0000 to U10FFFF. The value of each constant is the
    # UTF-8 string for the codepoint.
    # Examples:
    #   copyright = Unicode::U00A9
    #   euro = Unicode::U20AC
    #   infinity = Unicode::U221E
    #
    module Unicode
      CODEPOINT_REGEX = /^U_?([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})$/

      def self.const_missing(name)
        # Check that the constant name is of the right form: U0000 to U10FFFF
        if name.to_s =~ CODEPOINT_REGEX
          # Convert the codepoint to an immutable UTF-8 string,
          # define a real constant for that value and return the value
          #p name, name.class
          const_set(name, [$1.to_i(16)].pack("U").freeze)
        else  # Raise an error for constants that are not Unicode.
          raise NameError, "Uninitialized constant: Unicode::#{name}"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 4 rubygems

Version Path
twitter-text-kow-1.3.1.0 lib/twitter-text/unicode.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/twitter-text-3.1.0/lib/twitter-text/unicode.rb
twitter-text-3.1.0 lib/twitter-text/unicode.rb
twitter-text-simpleidn-3.0.0.0 lib/twitter-text/unicode.rb
twitter-text-3.0.0 lib/twitter-text/unicode.rb