Kernel-Collections.st 34 KB

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