\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5\D6?\BD\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2 \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4\E9\F2\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/unicode_utils-1.4.0/lib/unicode_utils/

Viewing File: debug.rb

# -*- encoding: utf-8 -*-

require "unicode_utils/display_width"
require "unicode_utils/graphic_char_q"
require "unicode_utils/char_display_width"
require "unicode_utils/sid"
require "unicode_utils/general_category"

module UnicodeUtils

  # Print a table with detailed information about each code point in
  # +str+. +opts+ can have the following keys:
  #
  # +:io+:: An IO compatible object. Receives the output.
  #         Defaults to <tt>$stdout</tt>.
  #
  # +str+ may also be an Integer, in which case it is interpreted as a
  # single code point that must be in UnicodeUtils::Codepoint::RANGE.
  # 
  # Examples:
  #
  #   $ ruby -r unicode_utils/u -e 'U.debug "良い一日"'
  #    Char | Ordinal | Sid                        | General Category | UTF-8
  #   ------+---------+----------------------------+------------------+----------
  #    "良" |    826F | CJK UNIFIED IDEOGRAPH-826F | Other_Letter     | E8 89 AF
  #    "い" |    3044 | HIRAGANA LETTER I          | Other_Letter     | E3 81 84
  #    "一" |    4E00 | CJK UNIFIED IDEOGRAPH-4E00 | Other_Letter     | E4 B8 80
  #    "日" |    65E5 | CJK UNIFIED IDEOGRAPH-65E5 | Other_Letter     | E6 97 A5
  #
  #   $ ruby -r unicode_utils/u -e 'U.debug 0xd800'
  #    Char | Ordinal | Sid              | General Category | UTF-8
  #   ------+---------+------------------+------------------+-------
  #    N/A  |    D800 | <surrogate-D800> | Surrogate        | N/A
  #
  # The output is purely informal and may change even in minor
  # releases.
  def debug(str, opts = {})
    io = opts[:io] || $stdout
    table = [Impl::DEBUG_COLUMNS.keys]
    if str.kind_of?(Integer)
      table << Impl::DEBUG_COLUMNS.values.map { |f| f.call(str) }
    else
      str.each_codepoint { |cp|
        table << Impl::DEBUG_COLUMNS.values.map { |f| f.call(cp) }
      }
    end
    Impl.print_table(table, io)
    nil
  end
  module_function :debug

  module Impl # :nodoc:all

    DEBUG_COLUMNS = {
      "Char" => -> cp {
        case cp
        when 0x07 then '"\a"'
        when 0x08 then '"\b"'
        when 0x09 then '"\t"'
        when 0x0A then '"\n"'
        when 0x0D then '"\r"'
        else
          if UnicodeUtils.graphic_char?(cp) &&
                UnicodeUtils.char_display_width(cp) > 0
            '"' + cp.chr(Encoding::UTF_8) + '"'
          else
            "N/A"
          end
        end
      },
      "Ordinal" => -> cp {
        cp.to_s(16).upcase.rjust(7)
      },
      "Sid" => -> cp {
        UnicodeUtils.sid(cp)
      },
      "General Category" => -> cp {
        UnicodeUtils.general_category(cp).to_s
      },
      "UTF-8" => -> cp {
        begin
          cp.chr(Encoding::UTF_8).bytes.map { |b| sprintf("%02X", b) }.join(" ")
        rescue RangeError # surrogate code points are not valid in utf-8
          "N/A"
        end
      }
    }

    def self.column_widths(table)
      Array.new.tap { |column_widths|
        table.each_with_index { |row|
          row.each_with_index { |txt, col_i|
            dw = UnicodeUtils.display_width(txt)
            cw = column_widths[col_i]
            column_widths[col_i] = dw if cw.nil? || cw < dw
          }
        }
      }
    end

    def self.print_row(row, column_widths, io)
      row.each_with_index { |txt, col_i|
        io.print(" ")
        io.print(txt)
        if col_i != row.length - 1
          dw = UnicodeUtils.display_width(txt)
          d = column_widths[col_i] - dw
          io.print(" " * (d + 1))
          io.print("|")
        end
      }
      io.puts
    end

    def self.print_separator_row(column_widths, io)
      column_widths.each_with_index { |cw, col_i|
        io.print("-" * (cw + 2))
        if col_i != column_widths.length - 1
          io.print("+")
        end
      }
      io.puts
    end

    def self.print_table(table, io)
      cws = column_widths(table)
      print_row(table[0], cws, io)
      print_separator_row(cws, io)
      table[1..-1].each { |row|
        print_row(row, cws, io)
      }
      io.flush
    end

  end

end