Method: SimpleIDN::Punycode.encode

Defined in:
lib/simpleidn.rb

.encode(input) ⇒ Object

Main encode function



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/simpleidn.rb', line 153

def encode(input)
  input = input.unpack("U*")
  output = []

  # Initialize the state:
  n = INITIAL_N
  delta = 0
  bias = INITIAL_BIAS

  # Handle the basic code points:
  output = input.select do |char|
    char if char < 0x80
  end

  h = b = output.length

  # h is the number of code points that have been handled, b is the
  # number of basic code points

  output << DELIMITER if b > 0

  # Main encoding loop:
  while h < input.length do
    # All non-basic code points < n have been
    # handled already. Find the next larger one:

    m = MAXINT

    input.each do |char|
      m = char if char >= n && char < m
    end

    # Increase delta enough to advance the decoder's
    # <n,i> state to <m,0>, but guard against overflow:

    raise(ConversionError, "punycode_overflow (1)") if m - n > ((MAXINT - delta) / (h + 1)).floor

    delta += (m - n) * (h + 1)
    n = m

    input.each_with_index do |char, j|
      if char < n
        delta += 1
        raise(ConversionError, "punycode_overflow(2)") if delta > MAXINT
      end

      if (char == n)
          # Represent delta as a generalized variable-length integer:
          q = delta
          k = BASE
          while true do
              t = k <= bias ? TMIN : k >= bias + TMAX ? TMAX : k - bias
              break if q < t
              output << encode_digit(t + (q - t) % (BASE - t))
              q = ( (q - t) / (BASE - t) ).floor
              k += BASE
          end
          output << encode_digit(q)
          bias = adapt(delta, h + 1, h == b)
          delta = 0
          h += 1
      end
    end

    delta += 1
    n += 1
  end
  return output.collect {|c| c.to_utf8_character}.join
end