added #contentsIsPartOf (prefix contents check)
authorClaus Gittinger <cg@exept.de>
Tue, 12 Jun 2001 12:26:24 +0200
changeset 5876 e38638bad111
parent 5875 5ccb39187705
child 5877 ff4ebbdbacd6
added #contentsIsPartOf (prefix contents check)
Filename.st
--- a/Filename.st	Wed May 23 01:07:54 2001 +0200
+++ b/Filename.st	Tue Jun 12 12:26:24 2001 +0200
@@ -1095,21 +1095,15 @@
     ^ false
 !
 
-hash
-    "return an integer useful as a hash-key"
-
-    ^ nameString hash
-!
-
-sameContentsAs:aFilename
-    "return true if the file represented by the receiver has the
-     same contents as the file represented by the argument, aFilename.
+contentsIsPartOf:aFilename
+    "return true if the contents of the file represented by the receiver
+     is the same as or a prefix of the contents of the file represented by the argument, aFilename.
      This compares the files actual contents; not the filenames."
 
     |f2 s1 s2 buffer1 buffer2 rslt n|
 
     f2 := aFilename asFilename.
-    f2 fileSize = self fileSize ifFalse:[^ false].
+    self fileSize > f2 fileSize ifTrue:[^ false].
 
     buffer1 := ByteArray new:8192.
     buffer2 := ByteArray new:8192.
@@ -1128,9 +1122,9 @@
     [s1 atEnd] whileFalse:[
         n := s1 nextBytes:8192 into:buffer1 startingAt:1.
         n == 0 ifTrue:[
-            rslt := s2 atEnd.
+            "/ receiver shorter.
             s1 close. s2 close.
-            ^ rslt
+            ^ true
         ].
         (s2 nextBytes:n into:buffer2 startingAt:1) ~~ n ifTrue:[
             "/ aFilename shorter
@@ -1142,13 +1136,117 @@
             ^ false
         ]
     ].
-    rslt := s2 atEnd.
+    "/ receiver shorter or same size.
     s1 close. s2 close.
-    ^ rslt
+    ^ true
+
+    "
+     |s|
+
+     s := 'testFile1' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s close.
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s close.
+
+     ('testFile1' asFilename contentsIsPartOf:'testFile2'  ) ifFalse:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s nextPutAll:'44444'.
+     s close.
+
+     ('testFile1' asFilename contentsIsPartOf:'testFile2'  ) ifFalse:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s close.
+
+     ('testFile1' asFilename contentsIsPartOf:'testFile2'  ) ifTrue:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33334'.
+     s close.
+
+     ('testFile1' asFilename contentsIsPartOf:'testFile2'  ) ifTrue:[self halt].
+
+    "
+!
+
+hash
+    "return an integer useful as a hash-key"
+
+    ^ nameString hash
+!
+
+sameContentsAs:aFilename
+    "return true if the file represented by the receiver has the
+     same contents as the file represented by the argument, aFilename.
+     This compares the files actual contents; not the filenames."
+
+    |f2|
+
+    f2 := aFilename asFilename.
+    f2 fileSize = self fileSize ifFalse:[^ false].
+    ^ self contentsIsPartOf:f2.
 
     "
      'Make.proto' asFilename sameContentsAs:'Makefile'  
     "
+
+    "
+     |s|
+
+     s := 'testFile1' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s close.
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s close.
+
+     ('testFile1' asFilename sameContentsAs:'testFile2'  ) ifFalse:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33333'.
+     s nextPutAll:'44444'.
+     s close.
+
+     ('testFile1' asFilename sameContentsAs:'testFile2'  ) ifTrue:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s close.
+
+     ('testFile1' asFilename sameContentsAs:'testFile2'  ) ifTrue:[self halt].
+
+     s := 'testFile2' asFilename writeStream.
+     s nextPutAll:'11111'.
+     s nextPutAll:'22222'.
+     s nextPutAll:'33334'.
+     s close.
+
+     ('testFile1' asFilename sameContentsAs:'testFile2'  ) ifTrue:[self halt].
+
+    "
 ! !
 
 !Filename methodsFor:'converting'!
@@ -3793,6 +3891,6 @@
 !Filename class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.194 2001-05-17 14:48:43 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.195 2001-06-12 10:26:24 cg Exp $'
 ! !
 Filename initialize!