Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: abbrev.rb
# frozen_string_literal: true
[0] Fix | Delete
#--
[1] Fix | Delete
# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
[2] Fix | Delete
#
[3] Fix | Delete
# All rights reserved. You can redistribute and/or modify it under
[4] Fix | Delete
# the same terms as Ruby.
[5] Fix | Delete
#
[6] Fix | Delete
# $Idaemons: /home/cvs/rb/abbrev.rb,v 1.2 2001/05/30 09:37:45 knu Exp $
[7] Fix | Delete
# $RoughId: abbrev.rb,v 1.4 2003/10/14 19:45:42 knu Exp $
[8] Fix | Delete
# $Id$
[9] Fix | Delete
#++
[10] Fix | Delete
[11] Fix | Delete
##
[12] Fix | Delete
# Calculates the set of unambiguous abbreviations for a given set of strings.
[13] Fix | Delete
#
[14] Fix | Delete
# require 'abbrev'
[15] Fix | Delete
# require 'pp'
[16] Fix | Delete
#
[17] Fix | Delete
# pp Abbrev.abbrev(['ruby'])
[18] Fix | Delete
# #=> {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}
[19] Fix | Delete
#
[20] Fix | Delete
# pp Abbrev.abbrev(%w{ ruby rules })
[21] Fix | Delete
#
[22] Fix | Delete
# _Generates:_
[23] Fix | Delete
# { "ruby" => "ruby",
[24] Fix | Delete
# "rub" => "ruby",
[25] Fix | Delete
# "rules" => "rules",
[26] Fix | Delete
# "rule" => "rules",
[27] Fix | Delete
# "rul" => "rules" }
[28] Fix | Delete
#
[29] Fix | Delete
# It also provides an array core extension, Array#abbrev.
[30] Fix | Delete
#
[31] Fix | Delete
# pp %w{ summer winter }.abbrev
[32] Fix | Delete
#
[33] Fix | Delete
# _Generates:_
[34] Fix | Delete
# { "summer" => "summer",
[35] Fix | Delete
# "summe" => "summer",
[36] Fix | Delete
# "summ" => "summer",
[37] Fix | Delete
# "sum" => "summer",
[38] Fix | Delete
# "su" => "summer",
[39] Fix | Delete
# "s" => "summer",
[40] Fix | Delete
# "winter" => "winter",
[41] Fix | Delete
# "winte" => "winter",
[42] Fix | Delete
# "wint" => "winter",
[43] Fix | Delete
# "win" => "winter",
[44] Fix | Delete
# "wi" => "winter",
[45] Fix | Delete
# "w" => "winter" }
[46] Fix | Delete
[47] Fix | Delete
module Abbrev
[48] Fix | Delete
[49] Fix | Delete
# Given a set of strings, calculate the set of unambiguous abbreviations for
[50] Fix | Delete
# those strings, and return a hash where the keys are all the possible
[51] Fix | Delete
# abbreviations and the values are the full strings.
[52] Fix | Delete
#
[53] Fix | Delete
# Thus, given +words+ is "car" and "cone", the keys pointing to "car" would
[54] Fix | Delete
# be "ca" and "car", while those pointing to "cone" would be "co", "con", and
[55] Fix | Delete
# "cone".
[56] Fix | Delete
#
[57] Fix | Delete
# require 'abbrev'
[58] Fix | Delete
#
[59] Fix | Delete
# Abbrev.abbrev(%w{ car cone })
[60] Fix | Delete
# #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
[61] Fix | Delete
#
[62] Fix | Delete
# The optional +pattern+ parameter is a pattern or a string. Only input
[63] Fix | Delete
# strings that match the pattern or start with the string are included in the
[64] Fix | Delete
# output hash.
[65] Fix | Delete
#
[66] Fix | Delete
# Abbrev.abbrev(%w{car box cone crab}, /b/)
[67] Fix | Delete
# #=> {"box"=>"box", "bo"=>"box", "b"=>"box", "crab" => "crab"}
[68] Fix | Delete
#
[69] Fix | Delete
# Abbrev.abbrev(%w{car box cone}, 'ca')
[70] Fix | Delete
# #=> {"car"=>"car", "ca"=>"car"}
[71] Fix | Delete
def abbrev(words, pattern = nil)
[72] Fix | Delete
table = {}
[73] Fix | Delete
seen = Hash.new(0)
[74] Fix | Delete
[75] Fix | Delete
if pattern.is_a?(String)
[76] Fix | Delete
pattern = /\A#{Regexp.quote(pattern)}/ # regard as a prefix
[77] Fix | Delete
end
[78] Fix | Delete
[79] Fix | Delete
words.each do |word|
[80] Fix | Delete
next if word.empty?
[81] Fix | Delete
word.size.downto(1) { |len|
[82] Fix | Delete
abbrev = word[0...len]
[83] Fix | Delete
[84] Fix | Delete
next if pattern && pattern !~ abbrev
[85] Fix | Delete
[86] Fix | Delete
case seen[abbrev] += 1
[87] Fix | Delete
when 1
[88] Fix | Delete
table[abbrev] = word
[89] Fix | Delete
when 2
[90] Fix | Delete
table.delete(abbrev)
[91] Fix | Delete
else
[92] Fix | Delete
break
[93] Fix | Delete
end
[94] Fix | Delete
}
[95] Fix | Delete
end
[96] Fix | Delete
[97] Fix | Delete
words.each do |word|
[98] Fix | Delete
next if pattern && pattern !~ word
[99] Fix | Delete
[100] Fix | Delete
table[word] = word
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
table
[104] Fix | Delete
end
[105] Fix | Delete
[106] Fix | Delete
module_function :abbrev
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
class Array
[110] Fix | Delete
# Calculates the set of unambiguous abbreviations for the strings in +self+.
[111] Fix | Delete
#
[112] Fix | Delete
# require 'abbrev'
[113] Fix | Delete
# %w{ car cone }.abbrev
[114] Fix | Delete
# #=> {"car"=>"car", "ca"=>"car", "cone"=>"cone", "con"=>"cone", "co"=>"cone"}
[115] Fix | Delete
#
[116] Fix | Delete
# The optional +pattern+ parameter is a pattern or a string. Only input
[117] Fix | Delete
# strings that match the pattern or start with the string are included in the
[118] Fix | Delete
# output hash.
[119] Fix | Delete
#
[120] Fix | Delete
# %w{ fast boat day }.abbrev(/^.a/)
[121] Fix | Delete
# #=> {"fast"=>"fast", "fas"=>"fast", "fa"=>"fast", "day"=>"day", "da"=>"day"}
[122] Fix | Delete
#
[123] Fix | Delete
# Abbrev.abbrev(%w{car box cone}, "ca")
[124] Fix | Delete
# #=> {"car"=>"car", "ca"=>"car"}
[125] Fix | Delete
#
[126] Fix | Delete
# See also Abbrev.abbrev
[127] Fix | Delete
def abbrev(pattern = nil)
[128] Fix | Delete
Abbrev::abbrev(self, pattern)
[129] Fix | Delete
end
[130] Fix | Delete
end
[131] Fix | Delete
[132] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function