PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/jruby-1.5.5/lib/ruby/1.8/jcode.rb

https://github.com/ThoughtWorksStudios/mingle_hg_plugin
Ruby | 220 lines | 216 code | 3 blank | 1 comment | 7 complexity | 807e53d9f319bc2aa93b94b1bd4c0105 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, JSON
  1. # jcode.rb - ruby code to handle japanese (EUC/SJIS) string
  2. if $VERBOSE && $KCODE == "NONE"
  3. warn "Warning: $KCODE is NONE."
  4. end
  5. $vsave, $VERBOSE = $VERBOSE, false
  6. class String
  7. warn "feel free for some warnings:\n" if $VERBOSE
  8. def _regex_quote(str)
  9. str.gsub(/(\\[\[\]\-\\])|\\(.)|([\[\]\\])/) do
  10. $1 || $2 || '\\' + $3
  11. end
  12. end
  13. private :_regex_quote
  14. PATTERN_SJIS = '[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]'
  15. PATTERN_EUC = '[\xa1-\xfe][\xa1-\xfe]'
  16. PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]'
  17. RE_SJIS = Regexp.new(PATTERN_SJIS, 0, 'n')
  18. RE_EUC = Regexp.new(PATTERN_EUC, 0, 'n')
  19. RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n')
  20. SUCC = {}
  21. SUCC['s'] = Hash.new(1)
  22. for i in 0 .. 0x3f
  23. SUCC['s'][i.chr] = 0x40 - i
  24. end
  25. SUCC['s']["\x7e"] = 0x80 - 0x7e
  26. SUCC['s']["\xfd"] = 0x100 - 0xfd
  27. SUCC['s']["\xfe"] = 0x100 - 0xfe
  28. SUCC['s']["\xff"] = 0x100 - 0xff
  29. SUCC['e'] = Hash.new(1)
  30. for i in 0 .. 0xa0
  31. SUCC['e'][i.chr] = 0xa1 - i
  32. end
  33. SUCC['e']["\xfe"] = 2
  34. SUCC['u'] = Hash.new(1)
  35. for i in 0 .. 0x7f
  36. SUCC['u'][i.chr] = 0x80 - i
  37. end
  38. SUCC['u']["\xbf"] = 0x100 - 0xbf
  39. def mbchar?
  40. case $KCODE[0]
  41. when ?s, ?S
  42. self =~ RE_SJIS
  43. when ?e, ?E
  44. self =~ RE_EUC
  45. when ?u, ?U
  46. self =~ RE_UTF8
  47. else
  48. nil
  49. end
  50. end
  51. def end_regexp
  52. case $KCODE[0]
  53. when ?s, ?S
  54. /#{PATTERN_SJIS}$/on
  55. when ?e, ?E
  56. /#{PATTERN_EUC}$/on
  57. when ?u, ?U
  58. /#{PATTERN_UTF8}$/on
  59. else
  60. /.$/on
  61. end
  62. end
  63. alias original_succ! succ!
  64. private :original_succ!
  65. alias original_succ succ
  66. private :original_succ
  67. def succ!
  68. reg = end_regexp
  69. if $KCODE != 'NONE' && self =~ reg
  70. succ_table = SUCC[$KCODE[0,1].downcase]
  71. begin
  72. self[-1] += succ_table[self[-1]]
  73. self[-2] += 1 if self[-1] == 0
  74. end while self !~ reg
  75. self
  76. else
  77. original_succ!
  78. end
  79. end
  80. def succ
  81. str = self.dup
  82. str.succ! or str
  83. end
  84. private
  85. def _expand_ch str
  86. a = []
  87. str.scan(/(?:\\(.)|([^\\]))-(?:\\(.)|([^\\]))|(?:\\(.)|(.))/m) do
  88. from = $1 || $2
  89. to = $3 || $4
  90. one = $5 || $6
  91. if one
  92. a.push one
  93. elsif from.length != to.length
  94. next
  95. elsif from.length == 1
  96. from[0].upto(to[0]) { |c| a.push c.chr }
  97. else
  98. from.upto(to) { |c| a.push c }
  99. end
  100. end
  101. a
  102. end
  103. def expand_ch_hash from, to
  104. h = {}
  105. afrom = _expand_ch(from)
  106. ato = _expand_ch(to)
  107. afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end
  108. h
  109. end
  110. HashCache = {}
  111. TrPatternCache = {}
  112. DeletePatternCache = {}
  113. SqueezePatternCache = {}
  114. public
  115. def tr!(from, to)
  116. return nil if from == ""
  117. return self.delete!(from) if to == ""
  118. pattern = TrPatternCache[from] ||= /[#{_regex_quote(from)}]/
  119. if from[0] == ?^
  120. last = /.$/.match(to)[0]
  121. self.gsub!(pattern, last)
  122. else
  123. h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
  124. self.gsub!(pattern) do |c| h[c] end
  125. end
  126. end
  127. def tr(from, to)
  128. (str = self.dup).tr!(from, to) or str
  129. end
  130. def delete!(del)
  131. return nil if del == ""
  132. self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '')
  133. end
  134. def delete(del)
  135. (str = self.dup).delete!(del) or str
  136. end
  137. def squeeze!(del=nil)
  138. return nil if del == ""
  139. pattern =
  140. if del
  141. SqueezePatternCache[del] ||= /([#{_regex_quote(del)}])\1+/
  142. else
  143. /(.|\n)\1+/
  144. end
  145. self.gsub!(pattern, '\1')
  146. end
  147. def squeeze(del=nil)
  148. (str = self.dup).squeeze!(del) or str
  149. end
  150. def tr_s!(from, to)
  151. return self.delete!(from) if to.length == 0
  152. pattern = SqueezePatternCache[from] ||= /([#{_regex_quote(from)}])\1*/
  153. if from[0] == ?^
  154. last = /.$/.match(to)[0]
  155. self.gsub!(pattern, last)
  156. else
  157. h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
  158. self.gsub!(pattern) do h[$1] end
  159. end
  160. end
  161. def tr_s(from, to)
  162. (str = self.dup).tr_s!(from,to) or str
  163. end
  164. def chop!
  165. self.gsub!(/(?:.|\r?\n)\z/, '')
  166. end
  167. def chop
  168. (str = self.dup).chop! or str
  169. end
  170. def jlength
  171. self.gsub(/[^\Wa-zA-Z_\d]/, ' ').length
  172. end
  173. alias jsize jlength
  174. def jcount(str)
  175. self.delete("^#{str}").jlength
  176. end
  177. def each_char
  178. if block_given?
  179. scan(/./m) do |x|
  180. yield x
  181. end
  182. else
  183. scan(/./m)
  184. end
  185. end
  186. end
  187. $VERBOSE = $vsave