ImmutableString.st
branchjv
changeset 20079 8d884971c2ed
parent 18705 843770030c5f
parent 20034 2ba574fda7ab
child 20579 9add81aadb7a
--- a/ImmutableString.st	Thu Jun 30 21:11:02 2016 +0100
+++ b/ImmutableString.st	Thu Jun 30 21:12:35 2016 +0100
@@ -129,6 +129,46 @@
 
 !ImmutableString methodsFor:'copying'!
 
+copy
+    "return a copy of the receiver"
+
+    ^ self copyFrom:1
+
+    "
+        'abcd' asImmutableString copy
+    "
+!
+
+deepCopy
+    "when copying, return a real (mutable) String"
+
+    (self class == ImmutableString) ifTrue:[
+        ^ self copyFrom:1
+    ].
+    ^ super deepCopy
+
+    "
+     'hello world' asImmutableString deepCopy
+    "
+!
+
+deepCopyUsing:aDictionary postCopySelector:postCopySelector
+    "return a deep copy of the receiver - reimplemented to be a bit faster"
+
+    "
+     could be an instance of a subclass which needs deepCopy
+     of its named instvars ...
+    "
+    (self class == ImmutableString) ifTrue:[
+        ^ self copyFrom:1
+    ].
+    ^ super deepCopyUsing:aDictionary postCopySelector:postCopySelector
+
+    "
+     'hello world' asImmutableString deepCopyUsing:nil postCopySelector:nil
+    "
+!
+
 postCopy
     "when copied, make me a real (mutable) String"
 
@@ -152,16 +192,29 @@
 shallowCopy
     "when copying, return a real (mutable) String"
 
-    |sz|
-
-    sz := self size.
-    ^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1
+    (self class == ImmutableString) ifTrue:[
+        ^ self copyFrom:1
+    ].
+    ^ super shallowCopy
 
     "
      'hello world' asImmutableString shallowCopy
     "
 
     "Created: / 3.8.1998 / 14:47:00 / cg"
+!
+
+simpleDeepCopy
+    "when copying, return a real (mutable) String"
+
+    (self class == ImmutableString) ifTrue:[
+        ^ self copyFrom:1
+    ].
+    ^ super simpleDeepCopy
+
+    "
+     'hello world' asImmutableString simpleDeepCopy
+    "
 ! !
 
 !ImmutableString methodsFor:'private'!