public
Fork of evanphx/rubinius
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/ezmobius/rubinius.git
Search Repo:
Added CType#isctrl, #toprint. Rework String#inspect, #dump.
brixen (author)
Fri Mar 28 18:04:40 -0700 2008
commit  677412353409ba4e5d67f19a3d095c62d009c88f
tree    26951db5e2b8cbfdbfc674b0054ab5ff155deea6
parent  87ba991b9b488b808ebf729b9e41765df76cc602
...
280
281
282
 
 
 
 
283
284
285
...
280
281
282
283
284
285
286
287
288
289
0
@@ -280,6 +280,10 @@ Benchmark.bmbm do |x|
0
     (MAX*4.8).to_i.times { STRING.dup.insert(a, "world") }
0
   }
0
 
0
+ run("String#inspect") {
0
+ (MAX*1.2).to_i.times { STRING.inspect }
0
+ }
0
+
0
   run("String#intern") {
0
     string = STRING * (((MAX*11.6).to_i / STRING.size) + 2)
0
     (MAX*0.1).to_i.times { string.chop!.intern }
...
6
7
8
 
 
 
 
9
10
11
...
42
43
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
...
6
7
8
9
10
11
12
13
14
15
...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
0
@@ -6,6 +6,10 @@
0
 # See specs in spec/shotgun/ctype_spec.rb
0
 
0
 module CType
0
+ def isctrl
0
+ self == ?\n or self == ?\r or self == ?\t or self == ?\f or
0
+ self == ?\v or self == ?\a or self == ?\e or self == ?\b
0
+ end
0
 
0
   def isspace
0
     self == ?\s or self == ?\n or self == ?\t or self == ?\r or self == ?\f or self == ?\v
0
@@ -42,4 +46,33 @@ module CType
0
   def tolower
0
     self.isupper ? self.tolower! : self
0
   end
0
+
0
+ def toprint
0
+ if self == ?"
0
+ "\\\""
0
+ elsif self == ?\\
0
+ "\\\\"
0
+ elsif self == ?#
0
+ "\\#"
0
+ elsif isctrl
0
+ case self
0
+ when ?\n: "\\n"
0
+ when ?\t: "\\t"
0
+ when ?\a: "\\a"
0
+ when ?\v: "\\v"
0
+ when ?\f: "\\f"
0
+ when ?\r: "\\r"
0
+ when ?\e: "\\e"
0
+ when ?\b: "\\b"
0
+ end
0
+ elsif self < 32 || self > 126
0
+ str = "\\000"
0
+ str.modify!
0
+
0
+ c = self.to_s 8
0
+ str.copy_from c, 0, c.size, 4-c.size
0
+ else
0
+ self.chr
0
+ end
0
+ end
0
 end
...
57
58
59
60
61
62
 
63
64
65
...
57
58
59
 
 
 
60
61
62
63
0
@@ -57,9 +57,7 @@ class Integer < Numeric
0
 
0
   def chr
0
     raise RangeError.new("#{self} is out of the valid character range") if self > 255 || self < 0
0
- a = "x"
0
- a[0] = self
0
- a
0
+ String.template 1, self
0
   end
0
   
0
   def [](index)
...
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
 
 
 
 
 
 
 
1024
1025
 
 
1026
1027
1028
...
2161
2162
2163
2164
 
2165
2166
2167
...
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
 
 
 
 
2269
2270
2271
...
993
994
995
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
996
997
998
999
1000
1001
1002
1003
 
1004
1005
1006
1007
1008
...
2141
2142
2143
 
2144
2145
2146
2147
...
2234
2235
2236
 
 
 
 
 
 
 
 
 
 
 
 
2237
2238
2239
2240
2241
2242
2243
0
@@ -993,36 +993,16 @@ class String
0
   # str[3] = 8
0
   # str.inspect #=> "hel\010o"
0
   def inspect
0
- return "\"#{self}\"".copy_properties(self) if $KCODE == "UTF-8"
0
- res = escaped("\"")
0
- res << "\""
0
- return res.copy_properties(self)
0
- end
0
-
0
- def escaped(res="")
0
- self.each_byte do |char|
0
- if ci = ControlCharacters.index(char)
0
- res << ControlPrintValue[ci]
0
- elsif char == ?"
0
- res << "\\\""
0
- elsif char == ?\\
0
- res << "\\\\"
0
- elsif char == ?#
0
- res << "\\#"
0
- elsif char < 32 or char > 126
0
- v = char.to_s(8)
0
- if v.size == 1
0
- res << "\\00#{v}"
0
- elsif v.size == 2
0
- res << "\\0#{v}"
0
- else
0
- res << "\\#{v}"
0
- end
0
- else
0
- res << char.chr
0
- end
0
+ if $KCODE == "UTF-8"
0
+ str = "\"#{self}\""
0
+ else
0
+ str = "\""
0
+ i = -1
0
+ str << @data[i].toprint while (i += 1) < @bytes
0
+ str << "\""
0
     end
0
- return res
0
+ str.taint if tainted?
0
+ str
0
   end
0
 
0
   # Returns the length of <i>self</i>.
0
@@ -2161,7 +2141,7 @@ class String
0
     end
0
   end
0
 
0
- # Returns true if either the ByteArray object_id
0
+ # Raises RuntimeError if either the ByteArray object_id
0
   # or the size has changed.
0
   def modified?(id, size)
0
     if id != @data.object_id or size != @bytes
0
@@ -2254,18 +2234,10 @@ class String
0
   def dump
0
     kcode = $KCODE
0
     $KCODE = "NONE"
0
- ret = self.inspect.copy_properties(self)
0
- $KCODE = $KCODE
0
- ret
0
- end
0
-
0
- def copy_properties(original)
0
- ret = self.dup
0
- ret.taint if original.tainted?
0
- unless original.instance_of?(String)
0
- ret = original.class.new(ret)
0
- end
0
- ret
0
+ str = self.class.new self.inspect
0
+ $KCODE = kcode
0
+ str.taint if tainted?
0
+ str
0
   end
0
 
0
   def to_sexp(name="(eval)",line=1,newlines=true)
...
37
38
39
 
 
 
 
 
 
 
40
...
37
38
39
40
41
42
43
44
45
46
47
0
@@ -37,4 +37,11 @@ describe "String#inspect" do
0
     "foo".taint.inspect.tainted?.should == true
0
     "foo\n".taint.inspect.tainted?.should == true
0
   end
0
+
0
+ it "does not return subclass instances" do
0
+ str = StringSpecs::MyString.new
0
+ str << "test"
0
+ str.should == "test"
0
+ str.inspect.class.should_not == StringSpecs::MyString
0
+ end
0
 end

Comments

    No one has commented yet.