Class: String::SBEncoded

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

Overview

SingleByte encoded Strings, more speed than MBEncoded for operations like length, index, [] and []= (predefined length of characters)

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) ⇒ SBEncoded

Returns a new instance of SBEncoded.



17
18
19
20
21
# File 'lib/string/sbencoded.rb', line 17

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.



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

def collation
  @collation
end

#encodingObject (readonly)

Returns the value of attribute encoding.



13
14
15
# File 'lib/string/sbencoded.rb', line 13

def encoding
  @encoding
end

Class Method Details

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



6
7
8
9
10
11
# File 'lib/string/sbencoded.rb', line 6

def self.new(string, encoding, collation=nil)
	raise "No encoding given" unless Encodings[encoding]
	obj = allocate
	obj.send(:initialize, string, encoding, collation)
	obj
end

Instance Method Details

#<=>(other) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/string/sbencoded.rb', line 23

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 <=> other.utf8
end

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



38
39
40
# File 'lib/string/sbencoded.rb', line 38

def [](*args)
	String::SBEncoded.new(super, @encoding, @collation)
end

#capitalizeObject



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

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

#capitalize!Object



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

def capitalize!; replace(capitalize); end

#chopObject



43
44
45
# File 'lib/string/sbencoded.rb', line 43

def chop
	String::SBEncoded.new(super, @encoding, @collation)
end

#downcaseObject



35
# File 'lib/string/sbencoded.rb', line 35

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

#downcase!Object



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

def downcase!;   replace(downcase);   end

#dupObject



62
63
64
# File 'lib/string/sbencoded.rb', line 62

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

#inspectObject



74
75
76
# File 'lib/string/sbencoded.rb', line 74

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

#lstripObject



49
50
51
# File 'lib/string/sbencoded.rb', line 49

def lstrip
	String::SBEncoded.new(super, @encoding, @collation)
end

#rstripObject



52
53
54
# File 'lib/string/sbencoded.rb', line 52

def rstrip
	String::SBEncoded.new(super, @encoding, @collation)
end

#stripObject



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

def strip
	String::SBEncoded.new(super, @encoding, @collation)
end

#upcaseObject

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



34
# File 'lib/string/sbencoded.rb', line 34

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

#upcase!Object

! variants, using self.replace



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

def upcase!;     replace(upcase);     end

#utf8(collation = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/string/sbencoded.rb', line 66

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