1
0

Kernel-Collections.st 35 KB

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