class String

Constants

EscapeTable

Copied from gonzui-0.1 Use this method instead of WEBrick::HTMLUtils.escape for performance reason.

KCONV_TO_CHARSET

Public Instance Methods

base64() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 43
def base64
  return Base64.encode64(self).chomp
end
charset() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 55
def charset
  return @charset if defined?(@charset)
  return nil
end
chompp() click to toggle source

Used from parser.rb

# File vendor/qwik/lib/qwik/util-string.rb, line 19
def chompp
  return self.sub(/[\n\r]+\z/, '')
end
del!(regexp) click to toggle source
# File vendor/qwik/lib/qwik/dev-extract-catalog.rb, line 25
def del!(regexp)
  self.gsub!(regexp) { '' }
end
escape() click to toggle source
escape

Copied from cgi.rb

# File vendor/qwik/lib/qwik/util-string.rb, line 49
def escape
  return self.gsub(/([^ a-zA-Z0-9_.-]+)/) {
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  }.tr(' ', "+")
end
escapeHTML() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 101
def escapeHTML
  string = self
  return string.gsub(/[&"<>]/) {|x| EscapeTable[x] }
end
euctojis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 28
def euctojis
  self.kconv(Kconv::JIS, Kconv::EUC).set_jis
end
euctosjis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 16
def euctosjis
  self.kconv(Kconv::SJIS, Kconv::EUC).set_sjis
end
euctou8() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 39
def euctou8;  Iconv.iconv_to_from('UTF-8', 'EUC-JP', self)     end
guess_charset() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 79
def guess_charset
  kconv_charset = Kconv::guess(self)
  charset = KCONV_TO_CHARSET[kconv_charset]
  if /\A\?/ =~ charset
    #p kconv_charset, charset
  end
  return charset
end
guess_charset!() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 88
def guess_charset!
  set_charset(self.guess_charset)
  return self
end
jistoeuc() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 32
def jistoeuc
  self.kconv(Kconv::EUC, Kconv::JIS).set_euc
end
jistosjis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 24
def jistosjis
  self.kconv(Kconv::SJIS, Kconv::JIS).set_sjis
end
jistou8() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 41
def jistou8;  Iconv.iconv_to_from('UTF-8', 'ISO-2022-JP', self)        end
mb_length() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 75
def mb_length
  case self.charset || self.guess_charset
  when 'UTF-8';       return self.split(//u).length
  when 'Shift_JIS';   return self.split(//s).length
  when 'EUC-JP';      return self.split(//e).length
  end
  return self.length
end
mb_substring(s,e) click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 84
def mb_substring(s,e)
  case self.charset || self.guess_charset
  when 'UTF-8';       return self.split(//u)[s...e].to_s
  when 'Shift_JIS';   return self.split(//s)[s...e].to_s
  when 'EUC-JP';      return self.split(//e)[s...e].to_s
  end
  return self[s...e]
end
md5() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 35
def md5
  return Digest::MD5.digest(self)
end
md5hex() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 39
def md5hex
  return Digest::MD5.hexdigest(self)
end
normalize_eol() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 23
def normalize_eol
  return self.xchomp+"\n"
end
normalize_newline() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 27
def normalize_newline
  return self.gsub("\r\n", "\n").gsub("\r", "\n")
end
page_to_xml() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 159
def page_to_xml
  self.set_page_charset.to_xml_charset
end
path() click to toggle source
# File vendor/qwik/lib/qwik/util-pathname.rb, line 10
def path
  return Pathname.new(self)
end
set_charset(charset) click to toggle source
charset
# File vendor/qwik/lib/qwik/util-charset.rb, line 50
def set_charset(charset)
  @charset = charset
  return self
end
set_euc() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 62
def set_euc;  set_charset('EUC-JP')            end
set_filename_charset()
Alias for: set_utf8
set_jis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 63
def set_jis;  set_charset('ISO-2022-JP')       end
Also aliased as: set_mail_charset
set_mail_charset()

Some use case.

Alias for: set_jis
set_page_charset()
Alias for: set_sjis
set_sjis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 61
def set_sjis; set_charset('Shift_JIS')        end
set_sourcecode_charset()
Alias for: set_sjis
set_url_charset()
Alias for: set_utf8
set_utf8() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 60
def set_utf8; set_charset('UTF-8')            end
set_xml_charset()
Alias for: set_utf8
sjistoeuc() click to toggle source
kconv
# File vendor/qwik/lib/qwik/util-charset.rb, line 12
def sjistoeuc
  self.kconv(Kconv::EUC, Kconv::SJIS).set_euc
end
sjistojis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 20
def sjistojis
  self.kconv(Kconv::JIS, Kconv::SJIS).set_jis
end
sjistou8() click to toggle source
iconv
# File vendor/qwik/lib/qwik/util-charset.rb, line 37
def sjistou8; Iconv.iconv_to_from('UTF-8', 'Shift_JIS', self) end
strip_metachar!() click to toggle source
# File vendor/qwik/lib/qwik/dev-extract-catalog.rb, line 18
def strip_metachar!
  self.gsub!(/\\n/, '')
  self.gsub!(/\\"/, '"')
  self.sub!(/\A['"]/, '')
  self.sub!(/['"]\Z/, '')
end
sub_str(pattern, replace) click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 31
def sub_str(pattern, replace)
  return sub(Regexp.new(Regexp.escape(pattern)), replace)
end
to_euc() click to toggle source

toeuc is used in kconv.rb

# File vendor/qwik/lib/qwik/util-charset.rb, line 107
def to_euc
  raise 'Undefined charset.' if self.charset.nil?
  case self.charset
  when 'ASCII';       return self
  when 'UTF-8';       return self.u8toeuc
  when 'Shift_JIS';   return self.sjistoeuc
  when 'EUC-JP';      return self
  when 'ISO-2022-JP'; return self.jistoeuc
  end
  raise 'Unknown charset.'
end
to_filename_charset()
Alias for: to_utf8
to_jis() click to toggle source

tojis is used in kconv.rb

# File vendor/qwik/lib/qwik/util-charset.rb, line 120
def to_jis
  raise 'Undefined charset.' if self.charset.nil?
  case self.charset
  when 'ASCII';       return self
  when 'UTF-8';       return self.u8tojis
  when 'Shift_JIS';   return self.sjistojis
  when 'EUC-JP';      return self.euctojis
  when 'ISO-2022-JP'; return self
  end
  raise 'Unknown charset.'
end
Also aliased as: to_mail_charset
to_mail_charset()
Alias for: to_jis
to_page_charset()
Alias for: to_sjis
to_sjis() click to toggle source

tosjis is used in kconv.rb

# File vendor/qwik/lib/qwik/util-charset.rb, line 94
def to_sjis
  raise 'Undefined charset.' if self.charset.nil?
  case self.charset
  when 'ASCII';       return self
  when 'UTF-8';       return self.u8tosjis
  when 'Shift_JIS';   return self
  when 'EUC-JP';      return self.euctosjis
  when 'ISO-2022-JP'; return self.jistosjis
  end
  raise 'Unknown charset.'
end
Also aliased as: to_page_charset
to_url_charset()
Alias for: to_utf8
to_utf8() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 132
def to_utf8
 #raise 'Undefined charset.' if self.charset.nil?
  self.guess_charset! if self.charset.nil?
  case self.charset
  when 'ASCII';       return self
  when 'UTF-8';       return self
  when 'Shift_JIS';   return self.sjistou8
  when 'EUC-JP';      return self.euctou8
  when 'ISO-2022-JP'; return self.jistou8
  end
  raise 'Unknown charset.'
end
to_xml_charset()
Alias for: to_utf8
trim_line!() click to toggle source
# File vendor/qwik/lib/qwik/dev-extract-catalog.rb, line 12
def trim_line!
  self.chomp!
  self.gsub!(/\A\s*/, '')
  self.gsub!(/\s*\z/, '')
end
u8toeuc() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 40
def u8toeuc;  Iconv.iconv_to_from('EUC-JP', 'UTF-8', self)     end
u8tojis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 42
def u8tojis
  iconv = Iconv.new('ISO-2022-JP', 'UTF-8')
  out = iconv.iconv(self)+iconv.close
  out.set_charset('ISO-2022-JP')
  out
end
u8tosjis() click to toggle source
# File vendor/qwik/lib/qwik/util-charset.rb, line 38
def u8tosjis; Iconv.iconv_to_from('Shift_JIS', 'UTF-8', self) end
unescape() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 55
def unescape
  return self.tr("+", ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) {
    [$1.delete('%')].pack('H*')
  }
end
unescapeHTML() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 61
def unescapeHTML
  return self.gsub(/&(.*?);/) {
    match = $1.dup
    case match
    when /\Aamp\z/i   then "&"
    when /\Aquot\z/i  then "'"
    when /\Agt\z/i    then ">"
    when /\Alt\z/i    then "<"
    else
      "&#{match};"
    end
  }
end
xchomp() click to toggle source
# File vendor/qwik/lib/qwik/util-string.rb, line 14
def xchomp
  return self.chomp("\n").chomp("\r")
end