Collection.st
changeset 3191 390764c0d7b3
parent 3124 d3b7387842d2
child 3367 9510bc82d4d6
equal deleted inserted replaced
3190:81ffb25d1d86 3191:390764c0d7b3
  1336 !Collection methodsFor:'printing & storing'!
  1336 !Collection methodsFor:'printing & storing'!
  1337 
  1337 
  1338 displayString
  1338 displayString
  1339     "return a printed representation of the receiver for display in inspectors etc."
  1339     "return a printed representation of the receiver for display in inspectors etc."
  1340 
  1340 
  1341     ^ self printOrDisplayStringUsing:#displayString 
  1341     |thisString buffer count string noneYet total limit|
  1342 !
       
  1343 
       
  1344 maxPrint
       
  1345     "the print-limit; printOn: will try to not produce more output
       
  1346      than the limit defined here."
       
  1347 
       
  1348     ^ 5000
       
  1349 !
       
  1350 
       
  1351 printOn:aStream
       
  1352     "append a user readable representation of the receiver to aStream.
       
  1353      The text appended is not meant to be read back for reconstruction of
       
  1354      the receiver. Also, this method limits the size of generated string.
       
  1355     "
       
  1356 
       
  1357     |limit firstOne string|
       
  1358 
  1342 
  1359     thisContext isRecursive ifTrue:[
  1343     thisContext isRecursive ifTrue:[
  1360         'Collection [error]: printOn: of self referencing collection.' errorPrintCR.
  1344         'Collection [error]: displayString of self referencing collection.' errorPrintCR.
  1361         aStream nextPutAll:'#("recursive")'.
       
  1362         ^ self
       
  1363     ].
       
  1364 
       
  1365     limit := self maxPrint.
       
  1366     aStream nextPutAll:self class name.
       
  1367     aStream nextPut:$(.
       
  1368     firstOne := true.
       
  1369 
       
  1370     self do:[:element |
       
  1371         firstOne ifFalse:[
       
  1372             aStream space
       
  1373         ] ifTrue:[
       
  1374             firstOne := false
       
  1375         ].
       
  1376         (limit <= 0) ifTrue:[
       
  1377             aStream nextPutAll:'...etc...)'.
       
  1378             ^ self
       
  1379         ] ifFalse:[
       
  1380 "/ old code, worked only on positionable streams
       
  1381 "/
       
  1382 "/              pos1 := aStream position.
       
  1383 "/              element printOn:aStream.
       
  1384 "/              limit := limit - (aStream position - pos1)
       
  1385 
       
  1386             string := element printString.
       
  1387             aStream nextPutAll:string.
       
  1388             limit := limit - string size.
       
  1389         ].
       
  1390     ].
       
  1391     aStream nextPut:$)
       
  1392 
       
  1393     "
       
  1394      #(1 2 3 'hello' $a) printOn:Transcript
       
  1395      (Array new:100000) printOn:Transcript
       
  1396      (Array new:100000) printString size 
       
  1397      (Dictionary new at:#hello put:'world'; yourself) printOn:Transcript
       
  1398     "
       
  1399     "
       
  1400      |a| 
       
  1401      a := Array new:3. 
       
  1402      a at:2 put:a.
       
  1403      a printOn:Transcript
       
  1404     "
       
  1405 
       
  1406     "Modified: 28.1.1997 / 00:39:17 / cg"
       
  1407 !
       
  1408 
       
  1409 printOrDisplayStringUsing:aSelector
       
  1410     "common code for printString and displayString; they only differ in
       
  1411      the print-message sent to the elements"
       
  1412 
       
  1413     |thisString buffer count string noneYet total limit|
       
  1414 
       
  1415     thisContext isRecursive ifTrue:[
       
  1416         'Collection [error]: print/storeString of self referencing collection.' errorPrintCR.
       
  1417         ^ '#("recursive")'
  1345         ^ '#("recursive")'
  1418     ].
  1346     ].
  1419 
  1347 
  1420     string := (self class name) , '('.
  1348     string := (self class name) , '('.
  1421     noneYet := true.
  1349     noneYet := true.
  1422     buffer := ''.
  1350     buffer := ''.
  1423     count := 0.
  1351     count := 0.
  1424     total := 0.
  1352     total := 0.
  1425     limit := self maxPrint.
  1353     limit := self maxPrint.
  1426 
  1354 
  1427     self do: [:element |
  1355     self printElementsDo: [:element |
  1428         thisString := element perform:aSelector.
  1356         thisString := element displayString.
  1429         noneYet ifTrue:[
  1357         noneYet ifTrue:[
  1430             noneYet := false.
  1358             noneYet := false.
  1431             buffer := buffer , thisString
  1359             buffer := buffer , thisString
  1432         ] ifFalse:[
  1360         ] ifFalse:[
  1433             buffer := buffer , (' ' , thisString)
  1361             buffer := buffer , (' ' , thisString)
  1445         ]
  1373         ]
  1446     ].
  1374     ].
  1447     string := string , buffer , ')'.
  1375     string := string , buffer , ')'.
  1448     ^ string
  1376     ^ string
  1449 
  1377 
  1450     "Modified: 28.1.1997 / 00:39:07 / cg"
  1378     "
       
  1379      #(1 2 3 'hello' $a) asOrderedCollection displayString
       
  1380 
       
  1381      (Dictionary new at:#hello put:'world'; 
       
  1382                      at:#foo put:'bar'; yourself) displayString
       
  1383     "
       
  1384 
       
  1385     "Modified: / 28.1.1997 / 00:39:07 / cg"
       
  1386     "Modified: / 20.1.1998 / 14:11:03 / stefan"
       
  1387 !
       
  1388 
       
  1389 maxPrint
       
  1390     "the print-limit; printOn: will try to not produce more output
       
  1391      than the limit defined here."
       
  1392 
       
  1393     ^ 5000
       
  1394 !
       
  1395 
       
  1396 printElementsDo:aBlock
       
  1397     "perform aBlock (1 arg) for all elements.
       
  1398      Used in #printOn:.
       
  1399      Subclasses (e.g. Dictionary) may redefine this."
       
  1400 
       
  1401     ^ self do:aBlock
       
  1402 
       
  1403     "Created: / 20.1.1998 / 14:11:02 / stefan"
       
  1404 !
       
  1405 
       
  1406 printOn:aStream
       
  1407     "append a user readable representation of the receiver to aStream.
       
  1408      The text appended is not meant to be read back for reconstruction of
       
  1409      the receiver. Also, this method limits the size of generated string.
       
  1410     "
       
  1411 
       
  1412     |limit firstOne s string|
       
  1413 
       
  1414     thisContext isRecursive ifTrue:[
       
  1415         'Collection [error]: printOn: of self referencing collection.' errorPrintCR.
       
  1416         aStream nextPutAll:'#("recursive")'.
       
  1417         ^ self
       
  1418     ].
       
  1419 
       
  1420     aStream nextPutAll:self class name.
       
  1421     aStream nextPut:$(.
       
  1422     firstOne := true.
       
  1423 
       
  1424     "
       
  1425      if aStream is not positionable, create an temporary positionable stream
       
  1426      (needed for limit calculation)
       
  1427     "
       
  1428     aStream isPositionable ifTrue:[
       
  1429         s := aStream.
       
  1430     ] ifFalse:[
       
  1431         s := WriteStream on:(String new:50).
       
  1432     ].
       
  1433     limit := s position + self maxPrint.
       
  1434 
       
  1435     self printElementsDo:[:element |
       
  1436         firstOne ifFalse:[
       
  1437             s space
       
  1438         ] ifTrue:[
       
  1439             firstOne := false
       
  1440         ].
       
  1441         (s position >= limit) ifTrue:[
       
  1442             s ~~ aStream ifTrue:[
       
  1443                 aStream nextPutAll:(s contents).
       
  1444             ].
       
  1445             aStream nextPutAll:'...etc...)'.
       
  1446             ^ self
       
  1447         ] ifFalse:[
       
  1448             element printOn:s.
       
  1449         ].
       
  1450     ].
       
  1451     s ~~ aStream ifTrue:[
       
  1452         aStream nextPutAll:(s contents).
       
  1453     ].
       
  1454     aStream nextPut:$)
       
  1455 
       
  1456     "
       
  1457      #(1 2 3 'hello' $a) printOn:Transcript
       
  1458      (Array new:100000) printOn:Transcript
       
  1459      (Array new:100000) printOn:Stdout          
       
  1460      (Array new:100000) printString size 
       
  1461      (Dictionary new at:#hello put:'world'; 
       
  1462                      at:#foo put:'bar'; yourself) printOn:Transcript
       
  1463     "
       
  1464     "
       
  1465      |a| 
       
  1466      a := Array new:3. 
       
  1467      a at:2 put:a.
       
  1468      a printOn:Transcript
       
  1469     "
       
  1470 
       
  1471     "Modified: / 28.1.1997 / 00:39:17 / cg"
       
  1472     "Modified: / 20.1.1998 / 14:11:03 / stefan"
  1451 !
  1473 !
  1452 
  1474 
  1453 storeOn:aStream
  1475 storeOn:aStream
  1454     "output a printed representation onto the argument, aStream.
  1476     "output a printed representation onto the argument, aStream.
  1455      The text can be re-read to reconstruct (a copy of) the receiver.
  1477      The text can be re-read to reconstruct (a copy of) the receiver.
  1713 ! !
  1735 ! !
  1714 
  1736 
  1715 !Collection class methodsFor:'documentation'!
  1737 !Collection class methodsFor:'documentation'!
  1716 
  1738 
  1717 version
  1739 version
  1718     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.75 1997-12-08 20:35:31 tz Exp $'
  1740     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.76 1998-01-20 18:11:31 stefan Exp $'
  1719 ! !
  1741 ! !
  1720 Collection initialize!
  1742 Collection initialize!