Kernel-Collections.st 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. Smalltalk current createPackage: 'Kernel-Collections' properties: #{}!
  2. Object subclass: #Association
  3. instanceVariableNames: 'key value'
  4. package: 'Kernel-Collections'!
  5. !Association methodsFor: 'accessing'!
  6. key
  7. ^key
  8. !
  9. key: aKey
  10. key := aKey
  11. !
  12. value
  13. ^value
  14. !
  15. value: aValue
  16. value := aValue
  17. ! !
  18. !Association methodsFor: 'comparing'!
  19. = anAssociation
  20. ^self class = anAssociation class and: [
  21. self key = anAssociation key and: [
  22. self value = anAssociation value]]
  23. ! !
  24. !Association methodsFor: 'printing'!
  25. printString
  26. "print the contents of the Association into a string and return the string"
  27. ^String streamContents: [:aStream |
  28. self storeOn: aStream]
  29. !
  30. storeOn: aStream
  31. "Store in the format: key->value"
  32. key storeOn: aStream.
  33. aStream nextPutAll: '->'.
  34. value storeOn: aStream.
  35. ! !
  36. !Association class methodsFor: 'instance creation'!
  37. key: aKey value: aValue
  38. ^self new
  39. key: aKey;
  40. value: aValue;
  41. yourself
  42. ! !
  43. Object subclass: #Collection
  44. instanceVariableNames: ''
  45. package: 'Kernel-Collections'!
  46. !Collection methodsFor: 'accessing'!
  47. occurrencesOf: anObject
  48. "Answer how many of the receiver's elements are equal to anObject."
  49. | tally |
  50. tally := 0.
  51. self do: [:each | anObject = each ifTrue: [tally := tally + 1]].
  52. ^tally
  53. !
  54. readStream
  55. ^self stream
  56. !
  57. size
  58. self subclassResponsibility
  59. !
  60. stream
  61. ^self streamClass on: self
  62. !
  63. streamClass
  64. ^self class streamClass
  65. !
  66. writeStream
  67. ^self stream
  68. ! !
  69. !Collection methodsFor: 'adding/removing'!
  70. add: anObject
  71. self subclassResponsibility
  72. !
  73. addAll: aCollection
  74. aCollection do: [:each |
  75. self add: each].
  76. ^aCollection
  77. !
  78. remove: anObject
  79. ^self remove: anObject ifAbsent: [self errorNotFound]
  80. !
  81. remove: anObject ifAbsent: aBlock
  82. self subclassResponsibility
  83. ! !
  84. !Collection methodsFor: 'converting'!
  85. asArray
  86. ^Array withAll: self
  87. !
  88. asJSON
  89. ^self asArray collect: [:each | each asJSON]
  90. !
  91. asOrderedCollection
  92. ^self asArray
  93. !
  94. asSet
  95. ^Set withAll: self
  96. ! !
  97. !Collection methodsFor: 'copying'!
  98. , aCollection
  99. ^self copy
  100. addAll: aCollection;
  101. yourself
  102. !
  103. copyWith: anObject
  104. ^self copy add: anObject; yourself
  105. !
  106. copyWithAll: aCollection
  107. ^self copy addAll: aCollection; yourself
  108. !
  109. copyWithoutAll: aCollection
  110. "Answer a copy of the receiver that does not contain any elements
  111. equal to those in aCollection."
  112. ^ self reject: [:each | aCollection includes: each]
  113. ! !
  114. !Collection methodsFor: 'enumerating'!
  115. collect: aBlock
  116. | stream |
  117. stream := self class new writeStream.
  118. self do: [ :each |
  119. stream nextPut: (aBlock value: each) ].
  120. ^stream contents
  121. !
  122. detect: aBlock
  123. ^self detect: aBlock ifNone: [self errorNotFound]
  124. !
  125. detect: aBlock ifNone: anotherBlock
  126. self subclassResponsibility
  127. !
  128. do: aBlock
  129. self subclassResponsibility
  130. !
  131. do: aBlock separatedBy: anotherBlock
  132. | actionBeforeElement |
  133. actionBeforeElement := [actionBeforeElement := anotherBlock].
  134. self do: [:each |
  135. actionBeforeElement value.
  136. aBlock value: each]
  137. !
  138. inject: anObject into: aBlock
  139. | result |
  140. result := anObject.
  141. self do: [:each |
  142. result := aBlock value: result value: each].
  143. ^result
  144. !
  145. intersection: aCollection
  146. "Answer the set theoretic intersection of two collections."
  147. | set outputSet |
  148. set := self asSet.
  149. outputSet := Set new.
  150. aCollection do: [ :each |
  151. ((set includes: each) and: [(outputSet includes: each) not])
  152. ifTrue: [
  153. outputSet add: each]].
  154. ^ self class withAll: outputSet asArray
  155. !
  156. reject: aBlock
  157. ^self select: [:each | (aBlock value: each) = false]
  158. !
  159. select: aBlock
  160. | stream |
  161. stream := self class new writeStream.
  162. self do: [:each |
  163. (aBlock value: each) ifTrue: [
  164. stream nextPut: each]].
  165. ^stream contents
  166. ! !
  167. !Collection methodsFor: 'error handling'!
  168. errorNotFound
  169. self error: 'Object is not in the collection'
  170. ! !
  171. !Collection methodsFor: 'printing'!
  172. printString
  173. "print the contents of the Collection into a string and return it"
  174. ^String streamContents: [:aStream |
  175. aStream
  176. nextPutAll: super printString, ' ('.
  177. self do: [:each | aStream nextPutAll: each printString]
  178. separatedBy: [aStream nextPutAll: ' '].
  179. aStream nextPutAll: ')']
  180. ! !
  181. !Collection methodsFor: 'testing'!
  182. ifEmpty: aBlock
  183. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver. Note that the fact that this method returns its argument in case the receiver is not empty allows one to write expressions like the following ones: self classifyMethodAs:
  184. (myProtocol ifEmpty: ['As yet unclassified'])"
  185. ^ self isEmpty
  186. ifTrue: [ aBlock value ]
  187. ifFalse: [ self ]
  188. !
  189. ifNotEmpty: aBlock
  190. self notEmpty ifTrue: aBlock.
  191. !
  192. includes: anObject
  193. | sentinel |
  194. sentinel := Object new.
  195. ^(self detect: [ :each | each = anObject] ifNone: [ sentinel ]) ~= sentinel
  196. !
  197. isEmpty
  198. ^self size = 0
  199. !
  200. notEmpty
  201. ^self isEmpty not
  202. ! !
  203. !Collection class methodsFor: 'accessing'!
  204. streamClass
  205. ^Stream
  206. ! !
  207. !Collection class methodsFor: 'instance creation'!
  208. new: anInteger
  209. ^self new
  210. !
  211. with: anObject
  212. ^self new
  213. add: anObject;
  214. yourself
  215. !
  216. with: anObject with: anotherObject
  217. ^self new
  218. add: anObject;
  219. add: anotherObject;
  220. yourself
  221. !
  222. with: firstObject with: secondObject with: thirdObject
  223. ^self new
  224. add: firstObject;
  225. add: secondObject;
  226. add: thirdObject;
  227. yourself
  228. !
  229. withAll: aCollection
  230. ^self new
  231. addAll: aCollection;
  232. yourself
  233. ! !
  234. Collection subclass: #IndexableCollection
  235. instanceVariableNames: ''
  236. package: 'Kernel-Collections'!
  237. !IndexableCollection commentStamp!
  238. An IndexableCollection is a key-value store, that is,
  239. it stores values under indexes.
  240. As a rule of thumb, if a collection has at: and at:put:,
  241. it is an IndexableCollection.!
  242. !IndexableCollection methodsFor: 'enumeration'!
  243. with: anotherCollection do: aBlock
  244. "Calls aBlock with every value from self
  245. and with indetically-indexed value from anotherCollection"
  246. self withIndexDo: [ :each :index |
  247. aBlock value: each value: (anotherCollection at: index) ]
  248. !
  249. withIndexDo: aBlock
  250. "Calls aBlock with every value from self
  251. and with its index as the second argument"
  252. self subclassReponsibility
  253. ! !
  254. IndexableCollection subclass: #HashedCollection
  255. instanceVariableNames: ''
  256. package: 'Kernel-Collections'!
  257. !HashedCollection commentStamp!
  258. A HashedCollection is a traditional JavaScript object, or a Smalltalk Dictionary.
  259. Unlike a Dictionary, it can only have strings as keys.!
  260. !HashedCollection methodsFor: 'accessing'!
  261. associations
  262. | associations |
  263. associations := #().
  264. self associationsDo: [:each | associations add: each].
  265. ^associations
  266. !
  267. at: aKey
  268. ^self at: aKey ifAbsent: [self errorNotFound]
  269. !
  270. at: aKey ifAbsent: aBlock
  271. ^(self includesKey: aKey)
  272. ifTrue: [self basicAt: aKey]
  273. ifFalse: aBlock
  274. !
  275. at: aKey ifAbsentPut: aBlock
  276. ^self at: aKey ifAbsent: [
  277. self at: aKey put: aBlock value]
  278. !
  279. at: aKey ifPresent: aBlock
  280. "Lookup the given key in the receiver.
  281. If it is present, answer the value of evaluating the given block with the value associated with the key.
  282. Otherwise, answer nil."
  283. ^(self includesKey: aKey)
  284. ifTrue: [ aBlock value: (self at: aKey) ]
  285. ifFalse: [ nil ]
  286. !
  287. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  288. "Lookup the given key in the receiver.
  289. If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
  290. otherwise answer the value of absentBlock."
  291. ^(self includesKey: aKey)
  292. ifTrue: [ aBlock value: (self at: aKey) ]
  293. ifFalse: anotherBlock
  294. !
  295. at: aKey put: aValue
  296. ^self basicAt: aKey put: aValue
  297. !
  298. keys
  299. <
  300. if ('function'===typeof Object.keys) return Object.keys(self);
  301. var keys = [];
  302. for(var i in self) {
  303. if(self.hasOwnProperty(i)) {
  304. keys.push(i);
  305. }
  306. };
  307. return keys;
  308. >
  309. !
  310. size
  311. ^self keys size
  312. !
  313. values
  314. ^self keys collect: [:each | self at: each]
  315. ! !
  316. !HashedCollection methodsFor: 'adding/removing'!
  317. add: anAssociation
  318. self at: anAssociation key put: anAssociation value
  319. !
  320. addAll: aHashedCollection
  321. super addAll: aHashedCollection associations.
  322. ^aHashedCollection
  323. !
  324. remove: aKey ifAbsent: aBlock
  325. ^self removeKey: aKey ifAbsent: aBlock
  326. !
  327. removeKey: aKey
  328. ^self remove: aKey
  329. !
  330. removeKey: aKey ifAbsent: aBlock
  331. ^(self includesKey: aKey)
  332. ifFalse: [aBlock value]
  333. ifTrue: [self basicDelete: aKey]
  334. ! !
  335. !HashedCollection methodsFor: 'comparing'!
  336. = aHashedCollection
  337. self class = aHashedCollection class ifFalse: [^false].
  338. self size = aHashedCollection size ifFalse: [^false].
  339. ^self associations = aHashedCollection associations
  340. ! !
  341. !HashedCollection methodsFor: 'converting'!
  342. asDictionary
  343. ^Dictionary fromPairs: self associations
  344. !
  345. asJSON
  346. | c |
  347. c := self class new.
  348. self keysAndValuesDo: [:key :value |
  349. c at: key put: value asJSON].
  350. ^c
  351. ! !
  352. !HashedCollection methodsFor: 'copying'!
  353. , aCollection
  354. self shouldNotImplement
  355. !
  356. deepCopy
  357. | copy |
  358. copy := self class new.
  359. self keysAndValuesDo: [:key :value |
  360. copy at: key put: value deepCopy].
  361. ^copy
  362. !
  363. shallowCopy
  364. | copy |
  365. copy := self class new.
  366. self keysAndValuesDo: [:key :value |
  367. copy at: key put: value].
  368. ^copy
  369. ! !
  370. !HashedCollection methodsFor: 'enumerating'!
  371. associationsDo: aBlock
  372. self keysAndValuesDo: [:key :value |
  373. aBlock value: (Association key: key value: value)]
  374. !
  375. collect: aBlock
  376. | newDict |
  377. newDict := self class new.
  378. self keysAndValuesDo: [:key :value |
  379. newDict at: key put: (aBlock value: value)].
  380. ^newDict
  381. !
  382. detect: aBlock ifNone: anotherBlock
  383. ^self values detect: aBlock ifNone: anotherBlock
  384. !
  385. do: aBlock
  386. self valuesDo: aBlock
  387. !
  388. includes: anObject
  389. ^self values includes: anObject
  390. !
  391. keysAndValuesDo: aBlock
  392. self keysDo: [:each |
  393. aBlock value: each value: (self at: each)]
  394. !
  395. keysDo: aBlock
  396. self keys do: aBlock
  397. !
  398. select: aBlock
  399. | newDict |
  400. newDict := self class new.
  401. self keysAndValuesDo: [:key :value |
  402. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  403. ^newDict
  404. !
  405. valuesDo: aBlock
  406. self keysAndValuesDo: [ :key :value | aBlock value: value ]
  407. !
  408. withIndexDo: aBlock
  409. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  410. ! !
  411. !HashedCollection methodsFor: 'printing'!
  412. printString
  413. "print the contents of the HashedCollection into a string and return the string"
  414. ^String streamContents: [:aStream |
  415. aStream nextPutAll: 'a ', self class name, '('.
  416. self associations
  417. do: [:each | each storeOn: aStream]
  418. separatedBy: [ aStream nextPutAll: ' , '].
  419. aStream nextPutAll: ')']
  420. !
  421. storeOn: aStream
  422. aStream nextPutAll: '#{'.
  423. self associations
  424. do: [:each | each storeOn: aStream]
  425. separatedBy: [ aStream nextPutAll: '. '].
  426. aStream nextPutAll: '}'
  427. ! !
  428. !HashedCollection methodsFor: 'testing'!
  429. includesKey: aKey
  430. <return self.hasOwnProperty(aKey)>
  431. ! !
  432. !HashedCollection class methodsFor: 'instance creation'!
  433. fromPairs: aCollection
  434. | dict |
  435. dict := self new.
  436. aCollection do: [:each | dict add: each].
  437. ^dict
  438. ! !
  439. HashedCollection subclass: #Dictionary
  440. instanceVariableNames: 'keys values'
  441. package: 'Kernel-Collections'!
  442. !Dictionary methodsFor: 'accessing'!
  443. at: aKey ifAbsent: aBlock
  444. <
  445. var index = self._positionOfKey_(aKey);
  446. return index >>=0 ? self['@values'][index] : aBlock();
  447. >
  448. !
  449. at: aKey put: aValue
  450. <
  451. var index = self._positionOfKey_(aKey);
  452. if(index === -1) {
  453. var keys = self['@keys'];
  454. index = keys.length;
  455. keys.push(aKey);
  456. }
  457. return self['@values'][index] = aValue;
  458. >
  459. !
  460. keyAtValue: anObject
  461. ^ (self associations
  462. detect:[:k :v| v == anObject]
  463. ifNone:[self error: 'Not found']) key
  464. !
  465. keys
  466. ^keys copy
  467. !
  468. values
  469. ^values copy
  470. ! !
  471. !Dictionary methodsFor: 'adding/removing'!
  472. removeKey: aKey ifAbsent: aBlock
  473. <
  474. var index = self._positionOfKey_(aKey);
  475. if(index === -1) {
  476. return aBlock()
  477. } else {
  478. var keys = self['@keys'], values = self['@values'];
  479. var value = values[index], l = keys.length;
  480. keys[index] = keys[l-1];
  481. keys.pop();
  482. values[index] = values[l-1];
  483. values.pop();
  484. return value;
  485. }
  486. >
  487. ! !
  488. !Dictionary methodsFor: 'converting'!
  489. asHashedCollection
  490. ^HashedCollection fromPairs: self associations
  491. !
  492. asJSON
  493. ^self asHashedCollection asJSON
  494. ! !
  495. !Dictionary methodsFor: 'enumerating'!
  496. keysAndValuesDo: aBlock
  497. ^keys with: values do: aBlock
  498. !
  499. keysDo: aBlock
  500. ^keys do: aBlock
  501. !
  502. valuesDo: aBlock
  503. ^values do: aBlock
  504. ! !
  505. !Dictionary methodsFor: 'initialization'!
  506. initialize
  507. super initialize.
  508. keys := #().
  509. values := #()
  510. ! !
  511. !Dictionary methodsFor: 'private'!
  512. positionOfKey: anObject
  513. <
  514. var keys = self['@keys'];
  515. for(var i=0;i<keys.length;i++){
  516. if(keys[i].__eq(anObject)) { return i;}
  517. }
  518. return -1;
  519. >
  520. ! !
  521. !Dictionary methodsFor: 'testing'!
  522. includesKey: aKey
  523. < return self._positionOfKey_(aKey) >>= 0; >
  524. ! !
  525. IndexableCollection subclass: #SequenceableCollection
  526. instanceVariableNames: ''
  527. package: 'Kernel-Collections'!
  528. !SequenceableCollection commentStamp!
  529. A SequencableCollection is an IndexableCollection
  530. with numeric indexes starting with 1.!
  531. !SequenceableCollection methodsFor: 'accessing'!
  532. allButFirst
  533. ^self copyFrom: 2 to: self size
  534. !
  535. allButLast
  536. ^self copyFrom: 1 to: self size - 1
  537. !
  538. at: anIndex
  539. ^self at: anIndex ifAbsent: [
  540. self errorNotFound]
  541. !
  542. at: anIndex ifAbsent: aBlock
  543. self subclassResponsibility
  544. !
  545. at: anIndex put: anObject
  546. self subclassResponsibility
  547. !
  548. atRandom
  549. ^ self at: self size atRandom
  550. !
  551. first
  552. ^self at: 1
  553. !
  554. first: n
  555. "Answer the first n elements of the receiver.
  556. Raise an error if there are not enough elements."
  557. ^ self copyFrom: 1 to: n
  558. !
  559. fourth
  560. ^self at: 4
  561. !
  562. indexOf: anObject
  563. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  564. !
  565. indexOf: anObject ifAbsent: aBlock
  566. <
  567. for(var i=0;i<self.length;i++) {
  568. if(self[i].__eq(anObject)) {return i+1}
  569. };
  570. return aBlock();
  571. >
  572. !
  573. indexOf: anObject startingAt: start
  574. "Answer the index of the first occurence of anElement after start
  575. within the receiver. If the receiver does not contain anElement,
  576. answer 0."
  577. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  578. !
  579. indexOf: anObject startingAt: start ifAbsent: aBlock
  580. <
  581. for(var i=start-1;i<self.length;i++){
  582. if(self[i].__eq(anObject)) {return i+1}
  583. }
  584. return aBlock();
  585. >
  586. !
  587. last
  588. ^self at: self size
  589. !
  590. second
  591. ^self at: 2
  592. !
  593. third
  594. ^self at: 3
  595. ! !
  596. !SequenceableCollection methodsFor: 'adding'!
  597. addLast: anObject
  598. self add: anObject
  599. !
  600. removeLast
  601. self remove: self last
  602. ! !
  603. !SequenceableCollection methodsFor: 'comparing'!
  604. = aCollection
  605. (self class = aCollection class and: [
  606. self size = aCollection size]) ifFalse: [^false].
  607. self withIndexDo: [:each :i |
  608. (aCollection at: i) = each ifFalse: [^false]].
  609. ^true
  610. ! !
  611. !SequenceableCollection methodsFor: 'converting'!
  612. reversed
  613. self subclassResponsibility
  614. ! !
  615. !SequenceableCollection methodsFor: 'copying'!
  616. copyFrom: anIndex to: anotherIndex
  617. | range newCollection |
  618. range := anIndex to: anotherIndex.
  619. newCollection := self class new: range size.
  620. range withIndexDo: [:each :i |
  621. newCollection at: i put: (self at: each)].
  622. ^newCollection
  623. !
  624. deepCopy
  625. | newCollection |
  626. newCollection := self class new: self size.
  627. self withIndexDo: [:each :index |
  628. newCollection at: index put: each deepCopy].
  629. ^newCollection
  630. !
  631. shallowCopy
  632. | newCollection |
  633. newCollection := self class new: self size.
  634. self withIndexDo: [ :each :index |
  635. newCollection at: index put: each].
  636. ^newCollection
  637. ! !
  638. !SequenceableCollection methodsFor: 'enumerating'!
  639. detect: aBlock ifNone: anotherBlock
  640. <
  641. for(var i = 0; i < self.length; i++)
  642. if(aBlock(self[i]))
  643. return self[i];
  644. return anotherBlock();
  645. >
  646. !
  647. do: aBlock
  648. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  649. !
  650. with: anotherCollection do: aBlock
  651. <for(var i=0;i<self.length;i++){aBlock(self[i], anotherCollection[i]);}>
  652. !
  653. withIndexDo: aBlock
  654. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  655. ! !
  656. !SequenceableCollection methodsFor: 'testing'!
  657. includes: anObject
  658. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  659. ! !
  660. SequenceableCollection subclass: #Array
  661. instanceVariableNames: ''
  662. package: 'Kernel-Collections'!
  663. !Array methodsFor: 'accessing'!
  664. at: anIndex ifAbsent: aBlock
  665. <
  666. if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
  667. return self[anIndex - 1];
  668. >
  669. !
  670. at: anIndex put: anObject
  671. <return self[anIndex - 1] = anObject>
  672. !
  673. size
  674. <return self.length>
  675. ! !
  676. !Array methodsFor: 'adding/removing'!
  677. add: anObject
  678. <self.push(anObject); return anObject;>
  679. !
  680. remove: anObject ifAbsent: aBlock
  681. <
  682. for(var i=0;i<self.length;i++) {
  683. if(self[i] == anObject) {
  684. self.splice(i,1);
  685. return self;
  686. }
  687. };
  688. aBlock._value();
  689. >
  690. !
  691. removeFrom: aNumber to: anotherNumber
  692. <self.splice(aNumber - 1,anotherNumber - 1)>
  693. ! !
  694. !Array methodsFor: 'converting'!
  695. asJavascript
  696. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  697. !
  698. reversed
  699. <return self._copy().reverse()>
  700. ! !
  701. !Array methodsFor: 'enumerating'!
  702. join: aString
  703. <return self.join(aString)>
  704. !
  705. sort
  706. ^self basicPerform: 'sort'
  707. !
  708. sort: aBlock
  709. <
  710. return self.sort(function(a, b) {
  711. if(aBlock(a,b)) {return -1} else {return 1}
  712. })
  713. >
  714. !
  715. sorted
  716. ^self copy sort
  717. !
  718. sorted: aBlock
  719. ^self copy sort: aBlock
  720. ! !
  721. !Array class methodsFor: 'instance creation'!
  722. new: anInteger
  723. <return new Array(anInteger)>
  724. !
  725. with: anObject
  726. ^(self new: 1)
  727. at: 1 put: anObject;
  728. yourself
  729. !
  730. with: anObject with: anObject2
  731. ^(self new: 2)
  732. at: 1 put: anObject;
  733. at: 2 put: anObject2;
  734. yourself
  735. !
  736. with: anObject with: anObject2 with: anObject3
  737. ^(self new: 3)
  738. at: 1 put: anObject;
  739. at: 2 put: anObject2;
  740. at: 3 put: anObject3;
  741. yourself
  742. !
  743. withAll: aCollection
  744. | instance index |
  745. index := 1.
  746. instance := self new: aCollection size.
  747. aCollection do: [:each |
  748. instance at: index put: each.
  749. index := index + 1].
  750. ^instance
  751. ! !
  752. SequenceableCollection subclass: #CharacterArray
  753. instanceVariableNames: ''
  754. package: 'Kernel-Collections'!
  755. !CharacterArray methodsFor: 'accessing'!
  756. at: anIndex put: anObject
  757. self errorReadOnly
  758. ! !
  759. !CharacterArray methodsFor: 'adding'!
  760. add: anObject
  761. self errorReadOnly
  762. !
  763. remove: anObject
  764. self errorReadOnly
  765. ! !
  766. !CharacterArray methodsFor: 'converting'!
  767. asLowercase
  768. ^self class fromString: self asString asLowercase
  769. !
  770. asNumber
  771. ^self asString asNumber
  772. !
  773. asString
  774. ^self subclassResponsibility
  775. !
  776. asSymbol
  777. ^self subclassResponsibility
  778. !
  779. asUppercase
  780. ^self class fromString: self asString asUppercase
  781. ! !
  782. !CharacterArray methodsFor: 'copying'!
  783. , aString
  784. ^self asString, aString asString
  785. ! !
  786. !CharacterArray methodsFor: 'error handling'!
  787. errorReadOnly
  788. self error: 'Object is read-only'
  789. ! !
  790. !CharacterArray methodsFor: 'printing'!
  791. printString
  792. ^self asString printString
  793. ! !
  794. !CharacterArray class methodsFor: 'instance creation'!
  795. fromString: aString
  796. self subclassResponsibility
  797. ! !
  798. CharacterArray subclass: #String
  799. instanceVariableNames: ''
  800. package: 'Kernel-Collections'!
  801. !String methodsFor: 'accessing'!
  802. asciiValue
  803. <return self.charCodeAt(0);>
  804. !
  805. at: anIndex ifAbsent: aBlock
  806. <return String(self).charAt(anIndex - 1) || aBlock()>
  807. !
  808. escaped
  809. <return escape(self)>
  810. !
  811. size
  812. <return self.length>
  813. !
  814. unescaped
  815. <return unescape(self)>
  816. ! !
  817. !String methodsFor: 'comparing'!
  818. < aString
  819. <return String(self) < aString._asString()>
  820. !
  821. <= aString
  822. <return String(self) <= aString._asString()>
  823. !
  824. = aString
  825. <
  826. if(!! aString._isString || !! aString._isString()) {
  827. return false;
  828. }
  829. return String(self) === String(aString)
  830. >
  831. !
  832. == aString
  833. ^self = aString
  834. !
  835. > aString
  836. <return String(self) >> aString._asString()>
  837. !
  838. >= aString
  839. <return String(self) >>= aString._asString()>
  840. ! !
  841. !String methodsFor: 'converting'!
  842. asJSON
  843. ^self
  844. !
  845. asJavaScriptSelector
  846. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  847. !
  848. asJavascript
  849. <
  850. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  851. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  852. else
  853. return "\"" + self + "\"";
  854. >
  855. !
  856. asLowercase
  857. <return self.toLowerCase()>
  858. !
  859. asNumber
  860. <return Number(self)>
  861. !
  862. asRegexp
  863. ^ RegularExpression fromString: self
  864. !
  865. asSelector
  866. <return smalltalk.selector(self)>
  867. !
  868. asString
  869. ^self
  870. !
  871. asSymbol
  872. ^Symbol lookup: self
  873. !
  874. asUppercase
  875. <return self.toUpperCase()>
  876. !
  877. reversed
  878. <return self.split("").reverse().join("")>
  879. !
  880. tokenize: aString
  881. <return self.split(aString)>
  882. ! !
  883. !String methodsFor: 'copying'!
  884. , aString
  885. <return self + aString>
  886. !
  887. copyFrom: anIndex to: anotherIndex
  888. <return self.substring(anIndex - 1, anotherIndex)>
  889. !
  890. deepCopy
  891. ^self shallowCopy
  892. !
  893. shallowCopy
  894. ^self class fromString: self
  895. ! !
  896. !String methodsFor: 'enumerating'!
  897. do: aBlock
  898. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i));}>
  899. !
  900. withIndexDo: aBlock
  901. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i), i+1);}>
  902. ! !
  903. !String methodsFor: 'printing'!
  904. printNl
  905. <console.log(self)>
  906. !
  907. printString
  908. ^'''', self, ''''
  909. ! !
  910. !String methodsFor: 'regular expressions'!
  911. match: aRegexp
  912. <return self.search(aRegexp) !!= -1>
  913. !
  914. matchesOf: aRegularExpression
  915. <return self.match(aRegularExpression)>
  916. !
  917. replace: aString with: anotherString
  918. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  919. !
  920. replaceRegexp: aRegexp with: aString
  921. <return self.replace(aRegexp, aString)>
  922. !
  923. trimBoth
  924. ^self trimBoth: '\s'
  925. !
  926. trimBoth: separators
  927. ^(self trimLeft: separators) trimRight: separators
  928. !
  929. trimLeft
  930. ^self trimLeft: '\s'
  931. !
  932. trimLeft: separators
  933. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  934. !
  935. trimRight
  936. ^self trimRight: '\s'
  937. !
  938. trimRight: separators
  939. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  940. ! !
  941. !String methodsFor: 'split join'!
  942. join: aCollection
  943. ^ String
  944. streamContents: [:stream | aCollection
  945. do: [:each | stream nextPutAll: each asString]
  946. separatedBy: [stream nextPutAll: self]]
  947. !
  948. lineIndicesDo: aBlock
  949. "execute aBlock with 3 arguments for each line:
  950. - start index of line
  951. - end index of line without line delimiter
  952. - end index of line including line delimiter(s) CR, LF or CRLF"
  953. | cr lf start sz nextLF nextCR |
  954. start := 1.
  955. sz := self size.
  956. cr := String cr.
  957. nextCR := self indexOf: cr startingAt: 1.
  958. lf := String lf.
  959. nextLF := self indexOf: lf startingAt: 1.
  960. [ start <= sz ] whileTrue: [
  961. (nextLF = 0 and: [ nextCR = 0 ])
  962. ifTrue: [ "No more CR, nor LF, the string is over"
  963. aBlock value: start value: sz value: sz.
  964. ^self ].
  965. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  966. ifTrue: [ "Found a LF"
  967. aBlock value: start value: nextLF - 1 value: nextLF.
  968. start := 1 + nextLF.
  969. nextLF := self indexOf: lf startingAt: start ]
  970. ifFalse: [ 1 + nextCR = nextLF
  971. ifTrue: [ "Found a CR-LF pair"
  972. aBlock value: start value: nextCR - 1 value: nextLF.
  973. start := 1 + nextLF.
  974. nextCR := self indexOf: cr startingAt: start.
  975. nextLF := self indexOf: lf startingAt: start ]
  976. ifFalse: [ "Found a CR"
  977. aBlock value: start value: nextCR - 1 value: nextCR.
  978. start := 1 + nextCR.
  979. nextCR := self indexOf: cr startingAt: start ]]]
  980. !
  981. lineNumber: anIndex
  982. "Answer a string containing the characters in the given line number."
  983. | lineCount |
  984. lineCount := 0.
  985. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  986. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  987. ^nil
  988. !
  989. lines
  990. "Answer an array of lines composing this receiver without the line ending delimiters."
  991. | lines |
  992. lines := Array new.
  993. self linesDo: [:aLine | lines add: aLine].
  994. ^lines
  995. !
  996. linesDo: aBlock
  997. "Execute aBlock with each line in this string. The terminating line
  998. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  999. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1000. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1001. ! !
  1002. !String methodsFor: 'testing'!
  1003. includesSubString: subString
  1004. < return self.indexOf(subString) !!= -1 >
  1005. !
  1006. isString
  1007. ^true
  1008. ! !
  1009. !String class methodsFor: 'accessing'!
  1010. cr
  1011. <return '\r'>
  1012. !
  1013. crlf
  1014. <return '\r\n'>
  1015. !
  1016. lf
  1017. <return '\n'>
  1018. !
  1019. space
  1020. <return ' '>
  1021. !
  1022. streamClass
  1023. ^StringStream
  1024. !
  1025. tab
  1026. <return '\t'>
  1027. ! !
  1028. !String class methodsFor: 'instance creation'!
  1029. fromCharCode: anInteger
  1030. <return String.fromCharCode(anInteger)>
  1031. !
  1032. fromString: aString
  1033. <return new self.fn(aString)>
  1034. !
  1035. streamContents: blockWithArg
  1036. |stream|
  1037. stream := (self streamClass on: String new).
  1038. blockWithArg value: stream.
  1039. ^ stream contents
  1040. !
  1041. value: aUTFCharCode
  1042. <return String.fromCharCode(aUTFCharCode);>
  1043. ! !
  1044. CharacterArray subclass: #Symbol
  1045. instanceVariableNames: ''
  1046. package: 'Kernel-Collections'!
  1047. !Symbol methodsFor: 'accessing'!
  1048. at: anIndex ifAbsent: aBlock
  1049. ^self asString at: anIndex ifAbsent: aBlock
  1050. !
  1051. size
  1052. ^self asString size
  1053. ! !
  1054. !Symbol methodsFor: 'comparing'!
  1055. < aSymbol
  1056. ^self asString < aSymbol asString
  1057. !
  1058. <= aSymbol
  1059. ^self asString <= aSymbol asString
  1060. !
  1061. = aSymbol
  1062. aSymbol class = self class ifFalse: [^false].
  1063. ^self asString = aSymbol asString
  1064. !
  1065. > aSymbol
  1066. ^self asString > aSymbol asString
  1067. !
  1068. >= aSymbol
  1069. ^self asString >= aSymbol asString
  1070. ! !
  1071. !Symbol methodsFor: 'converting'!
  1072. asJSON
  1073. ^self asString asJSON
  1074. !
  1075. asJavascript
  1076. ^'smalltalk.symbolFor(', self asString asJavascript, ')'
  1077. !
  1078. asSelector
  1079. ^self asString asSelector
  1080. !
  1081. asString
  1082. <return self.value>
  1083. !
  1084. asSuperSelector
  1085. ^self asString asSuperSelector
  1086. !
  1087. asSymbol
  1088. ^self
  1089. ! !
  1090. !Symbol methodsFor: 'copying'!
  1091. copyFrom: anIndex to: anotherIndex
  1092. ^self class fromString: (self asString copyFrom: anIndex to: anotherIndex)
  1093. !
  1094. deepCopy
  1095. ^self
  1096. !
  1097. shallowCopy
  1098. ^self
  1099. ! !
  1100. !Symbol methodsFor: 'enumerating'!
  1101. collect: aBlock
  1102. ^ (self asString collect: aBlock) asSymbol
  1103. !
  1104. detect: aBlock
  1105. ^ self asString detect: aBlock
  1106. !
  1107. do: aBlock
  1108. self asString do: aBlock
  1109. !
  1110. select: aBlock
  1111. ^ (self asString select: aBlock) asSymbol
  1112. !
  1113. withIndexDo: aBlock
  1114. self asString withIndexDo: aBlock
  1115. ! !
  1116. !Symbol methodsFor: 'evaluating'!
  1117. value: anObject
  1118. ^anObject perform: self
  1119. ! !
  1120. !Symbol methodsFor: 'printing'!
  1121. isSymbol
  1122. ^true
  1123. !
  1124. printString
  1125. ^'#', self asString
  1126. ! !
  1127. !Symbol class methodsFor: 'instance creation'!
  1128. basicNew
  1129. self shouldNotImplement
  1130. !
  1131. fromString: aString
  1132. ^self lookup: aString
  1133. !
  1134. lookup: aString
  1135. <return smalltalk.symbolFor(aString);>
  1136. ! !
  1137. Collection subclass: #Set
  1138. instanceVariableNames: 'elements'
  1139. package: 'Kernel-Collections'!
  1140. !Set methodsFor: 'accessing'!
  1141. size
  1142. ^elements size
  1143. ! !
  1144. !Set methodsFor: 'adding/removing'!
  1145. add: anObject
  1146. <
  1147. var found;
  1148. for(var i=0; i < self['@elements'].length; i++) {
  1149. if(anObject == self['@elements'][i]) {
  1150. found = true;
  1151. break;
  1152. }
  1153. }
  1154. if(!!found) {self['@elements'].push(anObject)}
  1155. >
  1156. !
  1157. remove: anObject
  1158. elements remove: anObject
  1159. ! !
  1160. !Set methodsFor: 'comparing'!
  1161. = aCollection
  1162. self class = aCollection class ifFalse: [ ^ false ].
  1163. self size = aCollection size ifFalse: [ ^ false ].
  1164. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1165. ^ true
  1166. ! !
  1167. !Set methodsFor: 'converting'!
  1168. asArray
  1169. ^elements copy
  1170. ! !
  1171. !Set methodsFor: 'enumerating'!
  1172. collect: aBlock
  1173. ^self class withAll: (elements collect: aBlock)
  1174. !
  1175. detect: aBlock ifNone: anotherBlock
  1176. ^elements detect: aBlock ifNone: anotherBlock
  1177. !
  1178. do: aBlock
  1179. elements do: aBlock
  1180. !
  1181. select: aBlock
  1182. | collection |
  1183. collection := self class new.
  1184. self do: [:each |
  1185. (aBlock value: each) ifTrue: [
  1186. collection add: each]].
  1187. ^collection
  1188. ! !
  1189. !Set methodsFor: 'initialization'!
  1190. initialize
  1191. super initialize.
  1192. elements := #()
  1193. ! !
  1194. !Set methodsFor: 'testing'!
  1195. includes: anObject
  1196. ^elements includes: anObject
  1197. ! !
  1198. Object subclass: #Queue
  1199. instanceVariableNames: 'read readIndex write'
  1200. package: 'Kernel-Collections'!
  1201. !Queue commentStamp!
  1202. A Queue am a one-sided queue.
  1203. A Queue uses two OrderedCollections inside,
  1204. `read` is at the front, is not modified and only read using `readIndex`.
  1205. `write` is at the back and is appended new items.
  1206. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1207. As a consequence, no data moving is done by the Queue; write appending may do data moving
  1208. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1209. !Queue methodsFor: 'accessing'!
  1210. back: anObject
  1211. write add: anObject
  1212. !
  1213. front
  1214. ^self frontIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1215. !
  1216. frontIfAbsent: aBlock
  1217. | result |
  1218. result := read at: readIndex ifAbsent: [
  1219. write isEmpty ifTrue: [
  1220. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1221. ^aBlock value ].
  1222. read := write.
  1223. readIndex := 1.
  1224. write := OrderedCollection new.
  1225. read first ].
  1226. read at: readIndex put: nil.
  1227. readIndex := readIndex + 1.
  1228. ^result
  1229. ! !
  1230. !Queue methodsFor: 'initialization'!
  1231. initialize
  1232. super initialize.
  1233. read := OrderedCollection new.
  1234. write := OrderedCollection new.
  1235. readIndex := 1
  1236. ! !
  1237. Object subclass: #RegularExpression
  1238. instanceVariableNames: ''
  1239. package: 'Kernel-Collections'!
  1240. !RegularExpression methodsFor: 'evaluating'!
  1241. compile: aString
  1242. <return self.compile(aString)>
  1243. !
  1244. exec: aString
  1245. <return self.exec(aString) || nil>
  1246. !
  1247. test: aString
  1248. <return self.test(aString)>
  1249. ! !
  1250. !RegularExpression class methodsFor: 'instance creation'!
  1251. fromString: aString
  1252. ^self fromString: aString flag: ''
  1253. !
  1254. fromString: aString flag: anotherString
  1255. <return new RegExp(aString, anotherString)>
  1256. ! !
  1257. Object subclass: #Stream
  1258. instanceVariableNames: 'collection position streamSize'
  1259. package: 'Kernel-Collections'!
  1260. !Stream methodsFor: 'accessing'!
  1261. collection
  1262. ^collection
  1263. !
  1264. contents
  1265. ^self collection
  1266. copyFrom: 1
  1267. to: self streamSize
  1268. !
  1269. position
  1270. ^position ifNil: [position := 0]
  1271. !
  1272. position: anInteger
  1273. position := anInteger
  1274. !
  1275. setCollection: aCollection
  1276. collection := aCollection
  1277. !
  1278. setStreamSize: anInteger
  1279. streamSize := anInteger
  1280. !
  1281. size
  1282. ^self streamSize
  1283. !
  1284. streamSize
  1285. ^streamSize
  1286. ! !
  1287. !Stream methodsFor: 'actions'!
  1288. close
  1289. !
  1290. flush
  1291. !
  1292. reset
  1293. self position: 0
  1294. !
  1295. resetContents
  1296. self reset.
  1297. self setStreamSize: 0
  1298. ! !
  1299. !Stream methodsFor: 'enumerating'!
  1300. do: aBlock
  1301. [self atEnd] whileFalse: [aBlock value: self next]
  1302. ! !
  1303. !Stream methodsFor: 'positioning'!
  1304. setToEnd
  1305. self position: self size
  1306. !
  1307. skip: anInteger
  1308. self position: ((self position + anInteger) min: self size max: 0)
  1309. ! !
  1310. !Stream methodsFor: 'reading'!
  1311. next
  1312. ^self atEnd
  1313. ifTrue: [nil]
  1314. ifFalse: [
  1315. self position: self position + 1.
  1316. collection at: self position]
  1317. !
  1318. next: anInteger
  1319. | tempCollection |
  1320. tempCollection := self collection class new.
  1321. anInteger timesRepeat: [
  1322. self atEnd ifFalse: [
  1323. tempCollection add: self next]].
  1324. ^tempCollection
  1325. !
  1326. peek
  1327. ^self atEnd ifFalse: [
  1328. self collection at: self position + 1]
  1329. ! !
  1330. !Stream methodsFor: 'testing'!
  1331. atEnd
  1332. ^self position = self size
  1333. !
  1334. atStart
  1335. ^self position = 0
  1336. !
  1337. isEmpty
  1338. ^self size = 0
  1339. ! !
  1340. !Stream methodsFor: 'writing'!
  1341. nextPut: anObject
  1342. self position: self position + 1.
  1343. self collection at: self position put: anObject.
  1344. self setStreamSize: (self streamSize max: self position)
  1345. !
  1346. nextPutAll: aCollection
  1347. aCollection do: [:each |
  1348. self nextPut: each]
  1349. ! !
  1350. !Stream class methodsFor: 'instance creation'!
  1351. on: aCollection
  1352. ^self new
  1353. setCollection: aCollection;
  1354. setStreamSize: aCollection size;
  1355. yourself
  1356. ! !
  1357. Stream subclass: #StringStream
  1358. instanceVariableNames: ''
  1359. package: 'Kernel-Collections'!
  1360. !StringStream methodsFor: 'reading'!
  1361. next: anInteger
  1362. | tempCollection |
  1363. tempCollection := self collection class new.
  1364. anInteger timesRepeat: [
  1365. self atEnd ifFalse: [
  1366. tempCollection := tempCollection, self next]].
  1367. ^tempCollection
  1368. ! !
  1369. !StringStream methodsFor: 'writing'!
  1370. cr
  1371. ^self nextPutAll: String cr
  1372. !
  1373. crlf
  1374. ^self nextPutAll: String crlf
  1375. !
  1376. lf
  1377. ^self nextPutAll: String lf
  1378. !
  1379. nextPut: aString
  1380. self nextPutAll: aString
  1381. !
  1382. nextPutAll: aString
  1383. self setCollection:
  1384. (self collection copyFrom: 1 to: self position),
  1385. aString,
  1386. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1387. self position: self position + aString size.
  1388. self setStreamSize: (self streamSize max: self position)
  1389. !
  1390. space
  1391. self nextPut: ' '
  1392. ! !