Class: String::MBEncoded

Inherits:
String
  • Object
show all
Defined in:
lib/string/mbencoded.rb

Constant Summary

Constants inherited from String

Binary, COLORS, Encodings, Escapes, UNICODE_LEADERS_AND_TRAILERS, UNICODE_LT_PAT, UNICODE_L_PAT, UNICODE_T_PAT, UNICODE_WHITESPACE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#arguments, #ascii, #binary, #camelcase, #chunks, #config_key, encodings, #mirc_formatted, #mirc_stripped, #mirc_translated_color, #post_arguments, #same_nick?, #strip_user_prefixes, #to_flags, #to_s, #unescaped, #user_prefixes, #valid_channelname?, #valid_nickname?, #valid_user?

Constructor Details

#initialize(string, encoding, collation = nil) ⇒ MBEncoded

Returns a new instance of MBEncoded.



19
20
21
22
23
# File 'lib/string/mbencoded.rb', line 19

def initialize(string, encoding, collation=nil)
	super(string)
	@encoding  = encoding.freeze
	@collation = collation.freeze
end

Instance Attribute Details

#collationObject (readonly)

Returns the value of attribute collation.



17
18
19
# File 'lib/string/mbencoded.rb', line 17

def collation
  @collation
end

#encodingObject (readonly)

Returns the value of attribute encoding.



15
16
17
# File 'lib/string/mbencoded.rb', line 15

def encoding
  @encoding
end

Class Method Details

.new(string, encoding, collation = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/string/mbencoded.rb', line 4

def self.new(string, encoding, collation=nil)
	raise "No encoding given" unless encoding
	if encoding == 'utf-8' then
		String::UTF8.new(string, collation)
	else
		obj = allocate
		obj.send(:initialize, string, encoding, collation)
		obj
	end
end

Instance Method Details

#<=>(other) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/string/mbencoded.rb', line 25

def <=>(other)
	unless other.collation == @collation
		raise ArgumentError, "Collations don't match: #{@collation} <=> #{other.collation}"
	end
	unless other.encoding == @encoding
		raise ArgumentError.new("Encodings don't match: #{@encoding} <=> #{other.encoding}")
	end
	self.utf8(@collation) <=> other.utf8(@collation)
end

#[](*a) ⇒ Object Also known as: slice



46
# File 'lib/string/mbencoded.rb', line 46

def [](*a);      utf8[*a].to_s(@encoding, @collation); end

#[]=(*args) ⇒ Object



48
49
50
51
52
53
# File 'lib/string/mbencoded.rb', line 48

def []=(*args)
	value    = args.pop
	a        = self.utf8
	a[*args] = value
	self.replace(a.to_s(@encoding, @collation))
end

#capitalizeObject



39
# File 'lib/string/mbencoded.rb', line 39

def capitalize;  utf8.capitalize.to_s(@encoding, @collation); end

#capitalize!Object



58
# File 'lib/string/mbencoded.rb', line 58

def capitalize!; replace(capitalize); end

#chopObject



40
# File 'lib/string/mbencoded.rb', line 40

def chop;        utf8.chop.to_s(@encoding, @collation); end

#chop!Object



62
# File 'lib/string/mbencoded.rb', line 62

def chop!;       replace(chop); end

#downcaseObject



38
# File 'lib/string/mbencoded.rb', line 38

def downcase;    utf8.downcase.to_s(@encoding, @collation); end

#downcase!Object



57
# File 'lib/string/mbencoded.rb', line 57

def downcase!;   replace(downcase); end

#dupObject



64
65
66
# File 'lib/string/mbencoded.rb', line 64

def dup
	String::MBEncoded.new(self, @encoding, @collation)
end

#index(*a) ⇒ Object



41
# File 'lib/string/mbencoded.rb', line 41

def index(*a);   utf8.index(*a); end

#inspectObject



76
77
78
# File 'lib/string/mbencoded.rb', line 76

def inspect
	"#{encoding}(#{collation||'none'}):#{super}"
end

#lengthObject

all methods delegated to utf8-ified strings (and if necessary it’s reconversion)



36
# File 'lib/string/mbencoded.rb', line 36

def length;      utf8.length; end

#lstripObject



44
# File 'lib/string/mbencoded.rb', line 44

def lstrip;      utf8.lstrip.to_s(@encoding, @collation);   end

#lstrip!Object



59
# File 'lib/string/mbencoded.rb', line 59

def lstrip!;     replace(lstrip); end

#rindex(*a) ⇒ Object



42
# File 'lib/string/mbencoded.rb', line 42

def rindex(*a);  utf8.rindex(*a); end

#rstripObject



45
# File 'lib/string/mbencoded.rb', line 45

def rstrip;      utf8.rstrip.to_s(@encoding, @collation);   end

#rstrip!Object



60
# File 'lib/string/mbencoded.rb', line 60

def rstrip!;     replace(rstrip); end

#stripObject



43
# File 'lib/string/mbencoded.rb', line 43

def strip;       utf8.strip.to_s(@encoding, @collation);   end

#strip!Object



61
# File 'lib/string/mbencoded.rb', line 61

def strip!;      replace(strip); end

#upcaseObject



37
# File 'lib/string/mbencoded.rb', line 37

def upcase;      utf8.upcase.to_s(@encoding, @collation);   end

#upcase!Object

! variants, using self.replace



56
# File 'lib/string/mbencoded.rb', line 56

def upcase!;     replace(upcase); end

#utf8(collation = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/string/mbencoded.rb', line 68

def utf8(collation=nil)
	String::UTF8.new(
		Iconv.iconv(String::UTF8::UTF8, @encoding, self).first,
		String::UTF8::UTF8,
		collation || @collation
	)
end