2
0

Kernel-Collections.st 32 KB

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