Kernel-Collections.st 35 KB

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