1
0

Kernel-Collections.st 35 KB

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