Kernel-Collections.st 34 KB

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