Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/runicode/utf8.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#chrObject

Returns a string containing the UTF-8 encoded character represented by the receiver’s value.

[View source]

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/runicode/utf8.rb', line 104

def chr
  case to_i
  when 0xD800..0xDFFF
    raise RangeError, 'these values are surrogate pairs'
  when 0x0000..0x007F
    return "%c" % (to_i & 0b01111111) # 7
  when 0x0080..0x07FF
    byte = []
    byte[0] = 0b11000000 + ((to_i >> 6) & 0b11111) # 5
    byte[1] = 0b10000000 + (to_i & 0b111111) # 6
    return "%c%c" % byte
  when 0x0800..0xFFFF
    byte = []
    byte[0] = 0b11100000 + ((to_i >> 12) & 0b1111) # 4
    byte[1] = 0b10000000 + ((to_i >> 6) & 0b111111) # 6
    byte[2] = 0b10000000 + (to_i & 0b111111) # 6
    return "%c%c%c" % byte
  when 0x00010000..0x0010FFFF
    byte = []
    byte[0] = 0b11110000 + ((to_i >> 18) & 0b11111) # 5
    byte[1] = 0b10000000 + ((to_i >> 12) & 0b111111) # 6
    byte[2] = 0b10000000 + ((to_i >> 6) & 0b111111) # 6
    byte[3] = 0b10000000 + (to_i & 0b111111) # 6
    return "%c%c%c%c" % byte
  else
    raise RangeError, 'out of Unicode range'
  end
end