Kernel-Collections.st 35 KB

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