Kernel-Collections.st 35 KB

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