Module: ZhongwenTools::String

Extended by:
String
Includes:
Conversion
Included in:
String
Defined in:
lib/zhongwen_tools/string.rb,
lib/zhongwen_tools/string/ruby18.rb,
lib/zhongwen_tools/conversion/string.rb

Constant Summary collapse

UNICODE_REGEX =

Deprecated: a Hash of unicode Regexes. Use ZhongwenTools::Regex.zh instead

{
  :zh => Regex.zh,
  :punc => Regex.zh_punc
}

Constants included from Conversion

Conversion::ZH_CONVERSION_TABLE, Conversion::ZH_TYPES

Instance Method Summary collapse

Methods included from Conversion

#to_zhcn, #to_zhhk, #to_zhs, #to_zht, #to_zhtw

Instance Method Details

#ascii?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/zhongwen_tools/string.rb', line 118

def ascii?(str = nil)
  str ||= self
  str.chars.size == str.bytes.to_a.size
end

#capitalize(str = nil) ⇒ Object



75
76
77
78
79
# File 'lib/zhongwen_tools/string.rb', line 75

def capitalize(str = nil)
  str ||= self

  str.capitalize
end

#chars(str = nil) ⇒ Object



98
99
100
# File 'lib/zhongwen_tools/string.rb', line 98

def chars(str = nil)
  (str || self).scan(/./mu).to_a
end

#downcase(str = nil) ⇒ Object



63
64
65
66
67
# File 'lib/zhongwen_tools/string.rb', line 63

def downcase(str = nil)
  str ||= self

  str.downcase
end

#from_codepoint(str = nil) ⇒ Object



151
152
153
154
155
# File 'lib/zhongwen_tools/string.rb', line 151

def from_codepoint(str = nil)
  str ||= self

  [str.sub(/\\?u/,'').hex].pack("U")
end

#fullwidth?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
# File 'lib/zhongwen_tools/string.rb', line 132

def fullwidth?(str = nil)
  str ||= self
  !self.halfwidth?(str) && self.to_halfwidth(str) != str
end

#halfwidth?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/zhongwen_tools/string.rb', line 127

def halfwidth?(str = nil)
  str ||= self
  str[Regex.fullwidth].nil?
end

#has_zh?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/zhongwen_tools/string.rb', line 51

def has_zh?(str = nil)
  str ||= self

  !str[/(#{Regex.zh}|#{Regex.zh_punc})/].nil?
end

#has_zh_punctuation?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/zhongwen_tools/string.rb', line 81

def has_zh_punctuation?(str = nil)
  str ||= self

  !str[Regex.zh_punc].nil?
end

#multibyte?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/zhongwen_tools/string.rb', line 123

def multibyte?(str = nil)
  !(str || self).ascii?
end

#reverse(str = nil) ⇒ Object



102
103
104
105
# File 'lib/zhongwen_tools/string.rb', line 102

def reverse(str = nil)
  str ||= self
  str.chars.reverse.join
end

#size(str = nil) ⇒ Object



93
94
95
96
# File 'lib/zhongwen_tools/string.rb', line 93

def size(str = nil)
  str ||= self
  str.chars.size
end

#strip_zh_punctuation(str = nil) ⇒ Object



87
88
89
90
91
# File 'lib/zhongwen_tools/string.rb', line 87

def strip_zh_punctuation(str = nil)
  str ||= self

  str.gsub(Regex.zh_punc, '')
end

#to_codepoint(str = nil) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/zhongwen_tools/string.rb', line 143

def to_codepoint(str = nil)
  str ||= self
  #chars = (self.class.to_s == 'String')? self.chars : self.chars(str)
  codepoints = str.chars.map{|c| "\\u%04x" % c.unpack("U")[0]}

  codepoints.join
end

#to_halfwidth(str = nil) ⇒ Object



137
138
139
140
141
# File 'lib/zhongwen_tools/string.rb', line 137

def to_halfwidth(str = nil)
  str ||= self

  str.gsub(/(#{Regex.fullwidth})/){  ZhongwenTools::FW_HW[$1] }
end

#to_utf8(str = nil) ⇒ Object



46
47
48
49
# File 'lib/zhongwen_tools/string.rb', line 46

def to_utf8(str = nil)
  (str || self).force_encoding('utf-8')
  #TODO: better conversion methods can be extracted from categories service
end

#upcase(str = nil) ⇒ Object



69
70
71
72
73
# File 'lib/zhongwen_tools/string.rb', line 69

def upcase(str = nil)
  str ||= self

  str.upcase
end

#uri_encode(str = nil) ⇒ Object



107
108
109
110
# File 'lib/zhongwen_tools/string.rb', line 107

def uri_encode(str = nil)
  str ||= self
  URI.encode str
end

#uri_escape(str = nil) ⇒ Object



112
113
114
115
116
# File 'lib/zhongwen_tools/string.rb', line 112

def uri_escape(str = nil)
  str ||= self

  URI.escape(str, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

#zh?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/zhongwen_tools/string.rb', line 57

def zh?(str = nil)
  str ||= self

  str.scan(/(#{Regex.zh}+|#{Regex.zh_punc}+|\s+)/).join == str
end

#zhs?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/zhongwen_tools/conversion/string.rb', line 13

def zhs?(str = nil)
  str ||= self

  str == convert(:zhs, str)
end

#zht?(str = nil) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/zhongwen_tools/conversion/string.rb', line 7

def zht?(str = nil)
  str ||= self

  str == convert(:zht, str) ||  str == convert(:zhhk, str)
end