1
0

Kernel-Collections.st 34 KB

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