Kernel-Collections.st 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  1. Smalltalk 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: #BucketStore
  41. instanceVariableNames: 'buckets hashBlock'
  42. package: 'Kernel-Collections'!
  43. !BucketStore commentStamp!
  44. I am an helper class for hash-based stores.
  45. I hold buckets which are selected by a hash, specified using `#hashBlock:`.
  46. The hash can be any object, and
  47. it is used as a JS property (that is, in ES5
  48. its toString() value counts).
  49. ## API
  50. I maintain a list of buckets. Client code can use this API:
  51. - `#bucketOfElement:` (to ask a bucket for element, I can return JS null if n/a)
  52. - `#do:` (to enumerate all elements of all buckets)
  53. - `#removeAll` (to remove all buckets)
  54. Client code itself should add/remove elements
  55. in a bucket. The `nil` object should not be put into any bucket.
  56. Types of buckets are the responsibility of subclasses via `#newBucket`.!
  57. !BucketStore methodsFor: 'accessing'!
  58. bucketOfElement: anObject
  59. <inlineJS: '
  60. var hash = self[''@hashBlock''](anObject);
  61. if (!!hash) return null;
  62. var buckets = self[''@buckets''],
  63. bucket = buckets[hash];
  64. if (!!bucket) { bucket = buckets[hash] = self._newBucket(); }
  65. return bucket;
  66. '>
  67. !
  68. hashBlock: aBlock
  69. hashBlock := aBlock
  70. ! !
  71. !BucketStore methodsFor: 'adding/removing'!
  72. removeAll
  73. <inlineJS: 'self[''@buckets''] = Object.create(null);'>
  74. ! !
  75. !BucketStore methodsFor: 'enumerating'!
  76. do: aBlock
  77. <inlineJS: '
  78. var buckets = self[''@buckets''];
  79. var keys = Object.keys(buckets);
  80. for (var i = 0; i < keys.length; ++i) { buckets[keys[i]]._do_(aBlock); }
  81. '>
  82. ! !
  83. !BucketStore methodsFor: 'initialization'!
  84. initialize
  85. super initialize.
  86. self removeAll
  87. ! !
  88. !BucketStore methodsFor: 'private'!
  89. newBucket
  90. self subclassResponsibility
  91. ! !
  92. !BucketStore class methodsFor: 'instance creation'!
  93. hashBlock: aBlock
  94. ^ self new
  95. hashBlock: aBlock;
  96. yourself
  97. ! !
  98. BucketStore subclass: #ArrayBucketStore
  99. instanceVariableNames: ''
  100. package: 'Kernel-Collections'!
  101. !ArrayBucketStore commentStamp!
  102. I am a concrete `BucketStore` with buckets being instance of `Array`.!
  103. !ArrayBucketStore methodsFor: 'private'!
  104. newBucket
  105. ^ #()
  106. ! !
  107. Object subclass: #Collection
  108. instanceVariableNames: ''
  109. package: 'Kernel-Collections'!
  110. !Collection commentStamp!
  111. I am the abstract superclass of all classes that represent a group of elements.
  112. I provide a set of useful methods to the Collection hierarchy such as enumerating and converting methods.!
  113. !Collection methodsFor: 'accessing'!
  114. anyOne
  115. "Answer a representative sample of the receiver. This method can
  116. be helpful when needing to preinfer the nature of the contents of
  117. semi-homogeneous collections."
  118. self ifEmpty: [ self error: 'Collection is empty' ].
  119. self do: [ :each | ^ each ]
  120. !
  121. occurrencesOf: anObject
  122. "Answer how many of the receiver's elements are equal to anObject."
  123. | tally |
  124. tally := 0.
  125. self do: [ :each | anObject = each ifTrue: [ tally := tally + 1 ]].
  126. ^ tally
  127. !
  128. single
  129. "Answer a single element.
  130. Raise an error if collection holds less or more than one element."
  131. self ifEmpty: [ self error: 'Collection is empty' ].
  132. self size > 1 ifTrue: [ self error: 'Collection holds more than one element' ].
  133. ^ self anyOne
  134. !
  135. size
  136. self subclassResponsibility
  137. ! !
  138. !Collection methodsFor: 'adding/removing'!
  139. add: anObject
  140. self subclassResponsibility
  141. !
  142. addAll: aCollection
  143. aCollection do: [ :each |
  144. self add: each ].
  145. ^ aCollection
  146. !
  147. remove: anObject
  148. ^ self remove: anObject ifAbsent: [ self errorNotFound ]
  149. !
  150. remove: anObject ifAbsent: aBlock
  151. self subclassResponsibility
  152. !
  153. removeAll
  154. self subclassResponsibility
  155. ! !
  156. !Collection methodsFor: 'converting'!
  157. asArray
  158. ^ Array withAll: self
  159. !
  160. asJSON
  161. ^ self asArray collect: [ :each | each asJSON ]
  162. !
  163. asOrderedCollection
  164. ^ self asArray
  165. !
  166. asSet
  167. ^ Set withAll: self
  168. ! !
  169. !Collection methodsFor: 'copying'!
  170. , aCollection
  171. ^ self copy
  172. addAll: aCollection;
  173. yourself
  174. !
  175. copyWith: anObject
  176. ^ self copy add: anObject; yourself
  177. !
  178. copyWithAll: aCollection
  179. ^ self copy addAll: aCollection; yourself
  180. !
  181. copyWithout: anObject
  182. "Answer a copy of the receiver that does not contain
  183. any occurrences of anObject."
  184. ^ self reject: [ :each | each = anObject ]
  185. !
  186. copyWithoutAll: aCollection
  187. "Answer a copy of the receiver that does not contain any elements
  188. equal to those in aCollection."
  189. ^ self reject: [ :each | aCollection includes: each ]
  190. !
  191. deepCopy
  192. ^ self collect: [ :each | each deepCopy ]
  193. !
  194. shallowCopy
  195. ^ self collect: [ :each | each ]
  196. ! !
  197. !Collection methodsFor: 'enumerating'!
  198. allSatisfy: aBlock
  199. "Evaluate aBlock with the elements of the receiver.
  200. If aBlock returns false for any element return false.
  201. Otherwise return true."
  202. self do: [ :each | (aBlock value: each) ifFalse: [ ^ false ] ].
  203. ^ true
  204. !
  205. anySatisfy: aBlock
  206. "Evaluate aBlock with the elements of the receiver.
  207. If aBlock returns true for any element return true.
  208. Otherwise return false."
  209. self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].
  210. ^ false
  211. !
  212. collect: aBlock
  213. | stream |
  214. stream := self class new writeStream.
  215. self do: [ :each |
  216. stream nextPut: (aBlock value: each) ].
  217. ^ stream contents
  218. !
  219. detect: aBlock
  220. ^ self detect: aBlock ifNone: [ self errorNotFound ]
  221. !
  222. detect: aBlock ifNone: anotherBlock
  223. self subclassResponsibility
  224. !
  225. do: aBlock
  226. self subclassResponsibility
  227. !
  228. do: aBlock separatedBy: anotherBlock
  229. | actionBeforeElement |
  230. actionBeforeElement := [ actionBeforeElement := anotherBlock ].
  231. self do: [ :each |
  232. actionBeforeElement value.
  233. aBlock value: each ]
  234. !
  235. inject: anObject into: aBlock
  236. | result |
  237. result := anObject.
  238. self do: [ :each |
  239. result := aBlock value: result value: each ].
  240. ^ result
  241. !
  242. intersection: aCollection
  243. "Answer the set theoretic intersection of two collections."
  244. | set outputSet |
  245. set := self asSet.
  246. outputSet := Set new.
  247. aCollection do: [ :each |
  248. ((set includes: each) and: [ (outputSet includes: each) not ])
  249. ifTrue: [
  250. outputSet add: each ]].
  251. ^ self class withAll: outputSet asArray
  252. !
  253. noneSatisfy: aBlock
  254. "Evaluate aBlock with the elements of the receiver.
  255. If aBlock returns false for all elements return true.
  256. Otherwise return false"
  257. self do: [ :item | (aBlock value: item) ifTrue: [ ^ false ] ].
  258. ^ true
  259. !
  260. reject: aBlock
  261. ^ self select: [ :each | (aBlock value: each) = false ]
  262. !
  263. select: aBlock
  264. | stream |
  265. stream := self class new writeStream.
  266. self do: [ :each |
  267. (aBlock value: each) ifTrue: [
  268. stream nextPut: each ] ].
  269. ^ stream contents
  270. !
  271. select: selectBlock thenCollect: collectBlock
  272. | stream |
  273. stream := self class new writeStream.
  274. self do: [ :each |
  275. (selectBlock value: each) ifTrue: [
  276. stream nextPut: (collectBlock value: each) ] ].
  277. ^ stream contents
  278. ! !
  279. !Collection methodsFor: 'error handling'!
  280. errorNotFound
  281. self error: 'Object is not in the collection'
  282. ! !
  283. !Collection methodsFor: 'streaming'!
  284. putOn: aStream
  285. self do: [ :each | each putOn: aStream ]
  286. ! !
  287. !Collection methodsFor: 'testing'!
  288. ifEmpty: aBlock
  289. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver.
  290. 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:
  291. self classifyMethodAs:
  292. (myProtocol ifEmpty: ['As yet unclassified'])"
  293. ^ self isEmpty
  294. ifTrue: "aBlock" [ aBlock value ]
  295. ifFalse: [ self ]
  296. !
  297. ifEmpty: aBlock ifNotEmpty: anotherBlock
  298. ^ self isEmpty
  299. ifTrue: "aBlock" [ aBlock value ]
  300. ifFalse: [ anotherBlock value: self ]
  301. !
  302. ifNotEmpty: aBlock
  303. ^ self notEmpty
  304. ifTrue: [ aBlock value: self ]
  305. ifFalse: [ self ]
  306. !
  307. ifNotEmpty: aBlock ifEmpty: anotherBlock
  308. ^ self notEmpty
  309. ifTrue: [ aBlock value: self ]
  310. ifFalse: "anotherBlock" [ anotherBlock value ]
  311. !
  312. includes: anObject
  313. ^ self anySatisfy: [ :each | each = anObject ]
  314. !
  315. isEmpty
  316. ^ self size = 0
  317. !
  318. notEmpty
  319. ^ self isEmpty not
  320. ! !
  321. !Collection class methodsFor: 'accessing'!
  322. classTag
  323. "Returns a tag or general category for this class.
  324. Typically used to help tools do some reflection.
  325. Helios, for example, uses this to decide what icon the class should display."
  326. ^ 'collection'
  327. ! !
  328. !Collection class methodsFor: 'instance creation'!
  329. new: anInteger
  330. ^ self new
  331. !
  332. with: anObject
  333. ^ self new
  334. add: anObject;
  335. yourself
  336. !
  337. with: anObject with: anotherObject
  338. ^ self new
  339. add: anObject;
  340. add: anotherObject;
  341. yourself
  342. !
  343. with: firstObject with: secondObject with: thirdObject
  344. ^ self new
  345. add: firstObject;
  346. add: secondObject;
  347. add: thirdObject;
  348. yourself
  349. !
  350. withAll: aCollection
  351. ^ self new
  352. addAll: aCollection;
  353. yourself
  354. ! !
  355. Collection subclass: #IndexableCollection
  356. instanceVariableNames: ''
  357. package: 'Kernel-Collections'!
  358. !IndexableCollection commentStamp!
  359. I am a key-value store collection, that is,
  360. I store values under indexes.
  361. As a rule of thumb, if a collection has `#at:` and `#at:put:`,
  362. it is an IndexableCollection.!
  363. !IndexableCollection methodsFor: 'accessing'!
  364. at: anIndex
  365. "Lookup the given index in the receiver.
  366. If it is present, answer the value stored at anIndex.
  367. Otherwise, raise an error."
  368. ^ self at: anIndex ifAbsent: [ self errorNotFound ]
  369. !
  370. at: anIndex ifAbsent: aBlock
  371. "Lookup the given index in the receiver.
  372. If it is present, answer the value stored at anIndex.
  373. Otherwise, answer the value of aBlock."
  374. self subclassResponsibility
  375. !
  376. at: aKey ifAbsentPut: aBlock
  377. ^ self at: aKey ifAbsent: [
  378. self at: aKey put: aBlock value ]
  379. !
  380. at: anIndex ifPresent: aBlock
  381. "Lookup the given index in the receiver.
  382. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  383. Otherwise, answer nil."
  384. ^ self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  385. !
  386. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  387. "Lookup the given index in the receiver.
  388. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  389. Otherwise, answer the value of anotherBlock."
  390. self subclassResponsibility
  391. !
  392. at: anIndex put: anObject
  393. "Store anObject under the given index in the receiver."
  394. self subclassResponsibility
  395. !
  396. indexOf: anObject
  397. "Lookup index at which anObject is stored in the receiver.
  398. If not present, raise an error."
  399. ^ self indexOf: anObject ifAbsent: [ self errorNotFound ]
  400. !
  401. indexOf: anObject ifAbsent: aBlock
  402. "Lookup index at which anObject is stored in the receiver.
  403. If not present, return value of executing aBlock."
  404. self subclassResponsibility
  405. ! !
  406. !IndexableCollection methodsFor: 'enumerating'!
  407. with: anotherCollection do: aBlock
  408. "Calls aBlock with every value from self
  409. and with indetically-indexed value from anotherCollection"
  410. self withIndexDo: [ :each :index |
  411. aBlock value: each value: (anotherCollection at: index) ]
  412. !
  413. withIndexDo: aBlock
  414. "Calls aBlock with every value from self
  415. and with its index as the second argument"
  416. self subclassResponsibility
  417. ! !
  418. IndexableCollection subclass: #AssociativeCollection
  419. instanceVariableNames: ''
  420. package: 'Kernel-Collections'!
  421. !AssociativeCollection commentStamp!
  422. I am a base class for object-indexed collections (Dictionary et.al.).!
  423. !AssociativeCollection methodsFor: 'accessing'!
  424. associations
  425. | associations |
  426. associations := #().
  427. self associationsDo: [ :each | associations add: each ].
  428. ^ associations
  429. !
  430. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  431. "Lookup the given key in the receiver.
  432. If it is present, answer the value of evaluating the oneArgBlock
  433. with the value associated with the key, otherwise answer the value
  434. of absentBlock."
  435. ^ (self includesKey: aKey)
  436. ifTrue: [ aBlock value: (self at: aKey) ]
  437. ifFalse: [ anotherBlock value ]
  438. !
  439. indexOf: anObject ifAbsent: aBlock
  440. ^ self keys
  441. detect: [ :each | (self at: each) = anObject ]
  442. ifNone: aBlock
  443. !
  444. keyAtValue: anObject
  445. ^ self keyAtValue: anObject ifAbsent: [ self errorNotFound ]
  446. !
  447. keyAtValue: anObject ifAbsent: aBlock
  448. ^ self indexOf: anObject ifAbsent: aBlock
  449. !
  450. keys
  451. self subclassResponsibility
  452. !
  453. size
  454. ^ self keys size
  455. !
  456. values
  457. self subclassResponsibility
  458. ! !
  459. !AssociativeCollection methodsFor: 'adding/removing'!
  460. add: anAssociation
  461. self at: anAssociation key put: anAssociation value
  462. !
  463. addAll: anAssociativeCollection
  464. super addAll: anAssociativeCollection associations.
  465. ^ anAssociativeCollection
  466. !
  467. remove: aKey ifAbsent: aBlock
  468. ^ self removeKey: aKey ifAbsent: aBlock
  469. !
  470. removeAll
  471. ^ self keys do: [ :each | self removeKey: each ]
  472. !
  473. removeKey: aKey
  474. ^ self remove: aKey
  475. !
  476. removeKey: aKey ifAbsent: aBlock
  477. self subclassResponsibility
  478. ! !
  479. !AssociativeCollection methodsFor: 'comparing'!
  480. = anAssocitativeCollection
  481. self class = anAssocitativeCollection class ifFalse: [ ^ false ].
  482. self size = anAssocitativeCollection size ifFalse: [ ^ false ].
  483. ^ self associations = anAssocitativeCollection associations
  484. ! !
  485. !AssociativeCollection methodsFor: 'converting'!
  486. asDictionary
  487. ^ Dictionary from: self associations
  488. !
  489. asHashedCollection
  490. ^ HashedCollection from: self associations
  491. !
  492. asJSON
  493. | hash |
  494. hash := HashedCollection new.
  495. self keysAndValuesDo: [ :key :value |
  496. hash at: key put: value asJSON ].
  497. ^ hash
  498. ! !
  499. !AssociativeCollection methodsFor: 'copying'!
  500. deepCopy
  501. | copy |
  502. copy := self class new.
  503. self keysAndValuesDo: [ :key :value |
  504. copy at: key put: value deepCopy ].
  505. ^ copy
  506. !
  507. shallowCopy
  508. | copy |
  509. copy := self class new.
  510. self keysAndValuesDo: [ :key :value |
  511. copy at: key put: value ].
  512. ^ copy
  513. ! !
  514. !AssociativeCollection methodsFor: 'enumerating'!
  515. associationsDo: aBlock
  516. self keysAndValuesDo: [ :key :value |
  517. aBlock value: (Association key: key value: value) ]
  518. !
  519. collect: aBlock
  520. | newDict |
  521. newDict := self class new.
  522. self keysAndValuesDo: [ :key :value |
  523. newDict at: key put: (aBlock value: value) ].
  524. ^ newDict
  525. !
  526. detect: aBlock ifNone: anotherBlock
  527. ^ self values detect: aBlock ifNone: anotherBlock
  528. !
  529. do: aBlock
  530. self valuesDo: aBlock
  531. !
  532. includes: anObject
  533. ^ self values includes: anObject
  534. !
  535. keysAndValuesDo: aBlock
  536. self keysDo: [ :each |
  537. aBlock value: each value: (self at: each) ]
  538. !
  539. keysDo: aBlock
  540. self subclassResponsibility
  541. !
  542. select: aBlock
  543. | newDict |
  544. newDict := self class new.
  545. self keysAndValuesDo: [ :key :value |
  546. (aBlock value: value) ifTrue: [ newDict at: key put: value ]].
  547. ^ newDict
  548. !
  549. select: selectBlock thenCollect: collectBlock
  550. | newDict |
  551. newDict := self class new.
  552. self keysAndValuesDo: [ :key :value |
  553. (selectBlock value: value) ifTrue: [ newDict at: key put: (collectBlock value: value) ]].
  554. ^ newDict
  555. !
  556. valuesDo: aBlock
  557. self subclassResponsibility
  558. !
  559. withIndexDo: aBlock
  560. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  561. ! !
  562. !AssociativeCollection methodsFor: 'printing'!
  563. printOn: aStream
  564. super printOn: aStream.
  565. aStream nextPutAll: ' ('.
  566. self associations
  567. do: [ :each | each printOn: aStream ]
  568. separatedBy: [ aStream nextPutAll: ' , ' ].
  569. aStream nextPutAll: ')'
  570. ! !
  571. !AssociativeCollection methodsFor: 'testing'!
  572. includesKey: aKey
  573. self subclassResponsibility
  574. ! !
  575. !AssociativeCollection class methodsFor: 'instance creation'!
  576. from: aCollection
  577. | newCollection |
  578. newCollection := self new.
  579. aCollection do: [ :each | newCollection add: each ].
  580. ^ newCollection
  581. !
  582. fromPairs: aCollection
  583. "This message is poorly named and has been replaced by #from:"
  584. ^ self from: aCollection
  585. !
  586. newFromPairs: aCollection
  587. "Accept an array of elements where every two elements form an
  588. association - the odd element being the key, and the even element the value."
  589. | newCollection |
  590. aCollection size even ifFalse: [
  591. self error: '#newFromPairs only accepts arrays of an even length' ].
  592. newCollection := self new.
  593. ( 1 to: aCollection size by: 2 ) do: [ :each |
  594. newCollection at: (aCollection at: each) put: (aCollection at: each + 1) ].
  595. ^ newCollection
  596. ! !
  597. AssociativeCollection subclass: #Dictionary
  598. instanceVariableNames: 'keys values'
  599. package: 'Kernel-Collections'!
  600. !Dictionary commentStamp!
  601. I represent a set of elements that can be viewed from one of two perspectives: a set of associations,
  602. or a container of values that are externally named where the name can be any object that responds to `=`.
  603. The external name is referred to as the key.!
  604. !Dictionary methodsFor: 'accessing'!
  605. at: aKey ifAbsent: aBlock
  606. <inlineJS: '
  607. var index = self._positionOfKey_(aKey);
  608. return index >=0 ? self[''@values''][index] : aBlock._value();
  609. '>
  610. !
  611. at: aKey put: aValue
  612. <inlineJS: '
  613. var index = self._positionOfKey_(aKey);
  614. if(index === -1) {
  615. var keys = self[''@keys''];
  616. index = keys.length;
  617. keys.push(aKey);
  618. }
  619. return self[''@values''][index] = aValue;
  620. '>
  621. !
  622. indexOf: anObject ifAbsent: aBlock
  623. | index |
  624. index := values
  625. indexOf: anObject
  626. ifAbsent: [ 0 ].
  627. ^ index = 0
  628. ifTrue: [ aBlock value ]
  629. ifFalse: [ keys at: index ]
  630. !
  631. keys
  632. ^ keys copy
  633. !
  634. values
  635. ^ values
  636. ! !
  637. !Dictionary methodsFor: 'adding/removing'!
  638. removeAll
  639. keys removeAll.
  640. values removeAll
  641. !
  642. removeKey: aKey ifAbsent: aBlock
  643. <inlineJS: '
  644. var index = self._positionOfKey_(aKey);
  645. if(index === -1) {
  646. return aBlock._value()
  647. } else {
  648. var keys = self[''@keys''], values = self[''@values''];
  649. var value = values[index], l = keys.length;
  650. keys[index] = keys[l-1];
  651. keys.pop();
  652. values[index] = values[l-1];
  653. values.pop();
  654. return value;
  655. }
  656. '>
  657. ! !
  658. !Dictionary methodsFor: 'enumerating'!
  659. keysAndValuesDo: aBlock
  660. ^ keys with: values do: aBlock
  661. !
  662. keysDo: aBlock
  663. ^ keys do: aBlock
  664. !
  665. valuesDo: aBlock
  666. ^ values do: aBlock
  667. ! !
  668. !Dictionary methodsFor: 'initialization'!
  669. initialize
  670. super initialize.
  671. keys := #().
  672. values := #()
  673. ! !
  674. !Dictionary methodsFor: 'private'!
  675. positionOfKey: anObject
  676. <inlineJS: '
  677. var keys = self[''@keys''];
  678. for(var i=0;i<keys.length;i++){
  679. if(keys[i].__eq(anObject)) { return i;}
  680. }
  681. return -1;
  682. '>
  683. ! !
  684. !Dictionary methodsFor: 'testing'!
  685. includesKey: aKey
  686. <inlineJS: 'return self._positionOfKey_(aKey) >= 0;'>
  687. ! !
  688. AssociativeCollection subclass: #HashedCollection
  689. instanceVariableNames: ''
  690. package: 'Kernel-Collections'!
  691. !HashedCollection commentStamp!
  692. I am a traditional JavaScript object, or a Smalltalk `Dictionary`.
  693. Unlike a `Dictionary`, I can only have strings as keys.!
  694. !HashedCollection methodsFor: 'accessing'!
  695. at: aKey ifAbsent: aBlock
  696. ^ (self includesKey: aKey)
  697. ifTrue: [ self basicAt: aKey ]
  698. ifFalse: [ aBlock value ]
  699. !
  700. at: aKey put: aValue
  701. ^ self basicAt: aKey put: aValue
  702. !
  703. keys
  704. <inlineJS: 'return Object.keys(self)'>
  705. !
  706. values
  707. <inlineJS: '
  708. return self._keys().map(function(key){
  709. return self._at_(key);
  710. });
  711. '>
  712. ! !
  713. !HashedCollection methodsFor: 'adding/removing'!
  714. removeKey: aKey ifAbsent: aBlock
  715. ^ self
  716. at: aKey
  717. ifPresent: [ :removed | self basicDelete: aKey. removed ]
  718. ifAbsent: [ aBlock value ]
  719. ! !
  720. !HashedCollection methodsFor: 'enumerating'!
  721. keysDo: aBlock
  722. self keys do: aBlock
  723. !
  724. valuesDo: aBlock
  725. self values do: aBlock
  726. ! !
  727. !HashedCollection methodsFor: 'testing'!
  728. includesKey: aKey
  729. <inlineJS: 'return self.hasOwnProperty(aKey)'>
  730. ! !
  731. IndexableCollection subclass: #SequenceableCollection
  732. instanceVariableNames: ''
  733. package: 'Kernel-Collections'!
  734. !SequenceableCollection commentStamp!
  735. I am an IndexableCollection
  736. with numeric indexes starting with 1.!
  737. !SequenceableCollection methodsFor: 'accessing'!
  738. allButFirst
  739. ^ self copyFrom: 2 to: self size
  740. !
  741. allButLast
  742. ^ self copyFrom: 1 to: self size - 1
  743. !
  744. anyOne
  745. ^ self at: 1
  746. !
  747. atRandom
  748. ^ self at: self size atRandom
  749. !
  750. first
  751. ^ self at: 1
  752. !
  753. first: aNumber
  754. "Answer the first `aNumber` elements of the receiver.
  755. Raise an error if there are not enough elements in the receiver."
  756. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  757. ^ self copyFrom: 1 to: aNumber
  758. !
  759. fourth
  760. ^ self at: 4
  761. !
  762. indexOf: anObject ifAbsent: aBlock
  763. <inlineJS: '
  764. self = self._numericallyIndexable();
  765. for(var i=0; i < self.length; i++) {
  766. if($recv(self[i]).__eq(anObject)) {return i+1}
  767. };
  768. return aBlock._value();
  769. '>
  770. !
  771. indexOf: anObject startingAt: start
  772. "Answer the index of the first occurence of anElement after start
  773. within the receiver. If the receiver does not contain anElement,
  774. answer 0."
  775. ^ self indexOf: anObject startingAt: start ifAbsent: [ 0 ]
  776. !
  777. indexOf: anObject startingAt: start ifAbsent: aBlock
  778. <inlineJS: '
  779. self = self._numericallyIndexable();
  780. for(var i=start - 1; i < self.length; i++){
  781. if($recv(self[i]).__eq(anObject)) {return i+1}
  782. }
  783. return aBlock._value();
  784. '>
  785. !
  786. last
  787. ^ self at: self size
  788. !
  789. last: aNumber
  790. "Answer the last aNumber elements of the receiver.
  791. Raise an error if there are not enough elements in the receiver."
  792. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  793. ^ self copyFrom: self size - aNumber + 1 to: self size
  794. !
  795. second
  796. ^ self at: 2
  797. !
  798. single
  799. <inlineJS: '
  800. if (self.length == 0) throw new Error("Collection is empty");
  801. if (self.length > 1) throw new Error("Collection holds more than one element.");
  802. return self[0];
  803. '>
  804. !
  805. third
  806. ^ self at: 3
  807. ! !
  808. !SequenceableCollection methodsFor: 'adding/removing'!
  809. addLast: anObject
  810. self add: anObject
  811. !
  812. removeLast
  813. ^ self remove: self last
  814. ! !
  815. !SequenceableCollection methodsFor: 'comparing'!
  816. = aCollection
  817. (self class = aCollection class and: [
  818. self size = aCollection size ]) ifFalse: [ ^ false ].
  819. self withIndexDo: [ :each :i |
  820. (aCollection at: i) = each ifFalse: [ ^ false ]].
  821. ^ true
  822. ! !
  823. !SequenceableCollection methodsFor: 'converting'!
  824. reversed
  825. self subclassResponsibility
  826. ! !
  827. !SequenceableCollection methodsFor: 'copying'!
  828. copyFrom: anIndex to: anotherIndex
  829. self subclassResponsibility
  830. ! !
  831. !SequenceableCollection methodsFor: 'enumerating'!
  832. detect: aBlock ifNone: anotherBlock
  833. <inlineJS: '
  834. self = self._numericallyIndexable();
  835. for(var i = 0; i < self.length; i++)
  836. if(aBlock._value_(self[i]))
  837. return self[i];
  838. return anotherBlock._value();
  839. '>
  840. !
  841. do: aBlock
  842. <inlineJS: '
  843. self = self._numericallyIndexable();
  844. for(var i=0; i < self.length; i++) {
  845. aBlock._value_(self[i]);
  846. }
  847. '>
  848. !
  849. with: anotherCollection do: aBlock
  850. <inlineJS: '
  851. self = self._numericallyIndexable();
  852. anotherCollection = anotherCollection._numericallyIndexable();
  853. for(var i=0; i<self.length; i++) {
  854. aBlock._value_value_(self[i], anotherCollection[i]);
  855. }
  856. '>
  857. !
  858. withIndexDo: aBlock
  859. <inlineJS: '
  860. self = self._numericallyIndexable();
  861. for(var i=0; i < self.length; i++) {
  862. aBlock._value_value_(self[i], i+1);
  863. }
  864. '>
  865. ! !
  866. !SequenceableCollection methodsFor: 'private'!
  867. numericallyIndexable
  868. "This is an internal converting message.
  869. It answeres a representation of the receiver
  870. that can use foo[i] in JavaScript code.
  871. It fixes IE8, where boxed String is unable
  872. to numerically index its characters,
  873. but primitive string can."
  874. self subclassResponsibility
  875. ! !
  876. !SequenceableCollection methodsFor: 'streaming'!
  877. newStream
  878. ^ self streamClass on: self
  879. !
  880. readStream
  881. "For Pharo compatibility"
  882. ^ self stream
  883. !
  884. stream
  885. ^ self newStream
  886. !
  887. streamClass
  888. ^ self class streamClass
  889. !
  890. writeStream
  891. "For Pharo compatibility"
  892. ^ self stream
  893. ! !
  894. !SequenceableCollection methodsFor: 'testing'!
  895. beginsWith: prefix
  896. self size < prefix size ifTrue: [ ^ false ].
  897. ^ (self first: prefix size) = prefix
  898. !
  899. endsWith: suffix
  900. self size < suffix size ifTrue: [ ^ false ].
  901. ^ (self last: suffix size) = suffix
  902. !
  903. includes: anObject
  904. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  905. ! !
  906. !SequenceableCollection class methodsFor: 'accessing'!
  907. streamClass
  908. ^ Stream
  909. ! !
  910. !SequenceableCollection class methodsFor: 'streaming'!
  911. streamContents: aBlock
  912. | stream |
  913. stream := (self streamClass on: self new).
  914. aBlock value: stream.
  915. ^ stream contents
  916. ! !
  917. SequenceableCollection subclass: #Array
  918. instanceVariableNames: ''
  919. package: 'Kernel-Collections'!
  920. !Array commentStamp!
  921. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  922. I am directly mapped to JavaScript Number.
  923. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  924. !Array methodsFor: 'accessing'!
  925. at: anIndex ifAbsent: aBlock
  926. <inlineJS: '
  927. return anIndex >= 1 && anIndex <= self.length
  928. ? self[anIndex - 1]
  929. : aBlock._value()
  930. '>
  931. !
  932. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  933. <inlineJS: '
  934. return anIndex >= 1 && anIndex <= self.length
  935. ? aBlock._value_(self[anIndex - 1])
  936. : anotherBlock._value()
  937. '>
  938. !
  939. at: anIndex put: anObject
  940. <inlineJS: 'return self[anIndex - 1] = anObject'>
  941. !
  942. size
  943. <inlineJS: 'return self.length'>
  944. ! !
  945. !Array methodsFor: 'adding/removing'!
  946. add: anObject
  947. <inlineJS: 'self.push(anObject); return anObject;'>
  948. !
  949. addAll: aCollection
  950. <inlineJS: '
  951. if (Array.isArray(aCollection) && aCollection.length < 65000) self.push.apply(self, aCollection);
  952. else $globals.Array.superclass.fn.prototype._addAll_.call(self, aCollection);
  953. return aCollection;
  954. '>
  955. !
  956. addFirst: anObject
  957. <inlineJS: 'self.unshift(anObject); return anObject;'>
  958. !
  959. remove: anObject ifAbsent: aBlock
  960. | index |
  961. index := self indexOf: anObject ifAbsent: [ 0 ].
  962. ^ index = 0
  963. ifFalse: [ self removeIndex: index. anObject ]
  964. ifTrue: [ aBlock value ]
  965. !
  966. removeAll
  967. <inlineJS: 'self.length = 0'>
  968. !
  969. removeFrom: aNumber to: anotherNumber
  970. <inlineJS: 'self.splice(aNumber -1, anotherNumber - aNumber + 1)'>
  971. !
  972. removeIndex: anInteger
  973. <inlineJS: 'self.splice(anInteger - 1, 1)'>
  974. !
  975. removeLast
  976. <inlineJS: 'return self.pop();'>
  977. ! !
  978. !Array methodsFor: 'converting'!
  979. asJavascript
  980. ^ '[', ((self collect: [:each | each asJavascript ]) join: ', '), ']'
  981. !
  982. reversed
  983. <inlineJS: 'return self.slice().reverse()'>
  984. ! !
  985. !Array methodsFor: 'copying'!
  986. copyFrom: anIndex to: anotherIndex
  987. <inlineJS: '
  988. if (anIndex >= 1 && anotherIndex <= self.length) {
  989. return self.slice(anIndex - 1, anotherIndex);
  990. } else {
  991. self._at_(anIndex);
  992. self._at_(self.length + 1);
  993. throw new Error("Incorrect indexes in #copyFrom:to: not caught by #at:");
  994. }
  995. '>
  996. !
  997. shallowCopy
  998. <inlineJS: 'return self.slice()'>
  999. ! !
  1000. !Array methodsFor: 'enumerating'!
  1001. collect: aBlock
  1002. "Optimized version"
  1003. <inlineJS: 'return self.map(function(each) {return aBlock._value_(each)})'>
  1004. !
  1005. join: aString
  1006. <inlineJS: 'return self.join(aString)'>
  1007. !
  1008. select: aBlock
  1009. "Optimized version"
  1010. <inlineJS: 'return self.filter(function(each) {return aBlock._value_(each)})'>
  1011. !
  1012. sort
  1013. ^ self sort: [ :a :b | a < b ]
  1014. !
  1015. sort: aBlock
  1016. <inlineJS: '
  1017. return self.sort(function(a, b) {
  1018. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  1019. })
  1020. '>
  1021. !
  1022. sorted
  1023. ^ self copy sort
  1024. !
  1025. sorted: aBlock
  1026. ^ self copy sort: aBlock
  1027. ! !
  1028. !Array methodsFor: 'printing'!
  1029. printOn: aStream
  1030. super printOn: aStream.
  1031. aStream nextPutAll: ' ('.
  1032. self
  1033. do: [ :each | each printOn: aStream ]
  1034. separatedBy: [ aStream nextPutAll: ' ' ].
  1035. aStream nextPutAll: ')'
  1036. ! !
  1037. !Array methodsFor: 'private'!
  1038. numericallyIndexable
  1039. ^ self
  1040. ! !
  1041. !Array class methodsFor: 'instance creation'!
  1042. new: anInteger
  1043. <inlineJS: 'return new Array(anInteger)'>
  1044. !
  1045. with: anObject
  1046. ^ (self new: 1)
  1047. at: 1 put: anObject;
  1048. yourself
  1049. !
  1050. with: anObject with: anObject2
  1051. ^ (self new: 2)
  1052. at: 1 put: anObject;
  1053. at: 2 put: anObject2;
  1054. yourself
  1055. !
  1056. with: anObject with: anObject2 with: anObject3
  1057. ^ (self new: 3)
  1058. at: 1 put: anObject;
  1059. at: 2 put: anObject2;
  1060. at: 3 put: anObject3;
  1061. yourself
  1062. !
  1063. withAll: aCollection
  1064. | instance index |
  1065. index := 1.
  1066. instance := self new: aCollection size.
  1067. aCollection do: [ :each |
  1068. instance at: index put: each.
  1069. index := index + 1 ].
  1070. ^ instance
  1071. ! !
  1072. SequenceableCollection subclass: #CharacterArray
  1073. instanceVariableNames: ''
  1074. package: 'Kernel-Collections'!
  1075. !CharacterArray commentStamp!
  1076. I am the abstract superclass of string-like collections.!
  1077. !CharacterArray methodsFor: 'accessing'!
  1078. at: anIndex put: anObject
  1079. self errorReadOnly
  1080. ! !
  1081. !CharacterArray methodsFor: 'adding/removing'!
  1082. add: anObject
  1083. self errorReadOnly
  1084. !
  1085. remove: anObject
  1086. self errorReadOnly
  1087. ! !
  1088. !CharacterArray methodsFor: 'converting'!
  1089. asLowercase
  1090. ^ self class fromString: self asString asLowercase
  1091. !
  1092. asNumber
  1093. ^ self asString asNumber
  1094. !
  1095. asString
  1096. ^ self subclassResponsibility
  1097. !
  1098. asSymbol
  1099. ^ self asString
  1100. !
  1101. asUppercase
  1102. ^ self class fromString: self asString asUppercase
  1103. ! !
  1104. !CharacterArray methodsFor: 'copying'!
  1105. , aString
  1106. ^ self asString, aString asString
  1107. ! !
  1108. !CharacterArray methodsFor: 'error handling'!
  1109. errorReadOnly
  1110. self error: 'Object is read-only'
  1111. ! !
  1112. !CharacterArray methodsFor: 'printing'!
  1113. asSymbolPrintOn: aStream
  1114. aStream nextPutAll: '#'.
  1115. self asString isSelector
  1116. ifTrue: [ aStream nextPut: self ]
  1117. ifFalse: [ self printOn: aStream ]
  1118. !
  1119. printOn: aStream
  1120. self asString printOn: aStream
  1121. !
  1122. symbolPrintString
  1123. ^ String streamContents: [ :str | self asSymbolPrintOn: str ]
  1124. ! !
  1125. !CharacterArray methodsFor: 'streaming'!
  1126. putOn: aStream
  1127. aStream nextPutString: self
  1128. ! !
  1129. !CharacterArray class methodsFor: 'instance creation'!
  1130. fromString: aString
  1131. self subclassResponsibility
  1132. ! !
  1133. CharacterArray subclass: #String
  1134. instanceVariableNames: ''
  1135. package: 'Kernel-Collections'!
  1136. !String commentStamp!
  1137. 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.
  1138. String inherits many useful methods from its hierarchy, such as
  1139. `Collection >> #,`!
  1140. !String methodsFor: 'accessing'!
  1141. asciiValue
  1142. <inlineJS: 'return self.charCodeAt(0);'>
  1143. !
  1144. at: anIndex ifAbsent: aBlock
  1145. <inlineJS: 'return String(self)[anIndex - 1] || aBlock._value()'>
  1146. !
  1147. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1148. <inlineJS: '
  1149. var result = String(self)[anIndex - 1];
  1150. return result ? aBlock._value_(result) : anotherBlock._value();
  1151. '>
  1152. !
  1153. charCodeAt: anInteger
  1154. <inlineJS: 'return self.charCodeAt(anInteger - 1)'>
  1155. !
  1156. size
  1157. <inlineJS: 'return self.length'>
  1158. ! !
  1159. !String methodsFor: 'comparing'!
  1160. < aString
  1161. <inlineJS: 'return String(self) < aString._asString()'>
  1162. !
  1163. <= aString
  1164. <inlineJS: 'return String(self) <= aString._asString()'>
  1165. !
  1166. = aString
  1167. <inlineJS:
  1168. 'return aString !!= null && String(self) === (typeof aString === "string" ? aString : aString.valueOf())'>
  1169. !
  1170. == aString
  1171. <inlineJS: '
  1172. if (typeof aString === "string") return String(self) === aString;
  1173. else if (aString !!= null && typeof aString === "object") return String(self) === aString.valueOf();
  1174. else return false;
  1175. '>
  1176. !
  1177. > aString
  1178. <inlineJS: 'return String(self) > aString._asString()'>
  1179. !
  1180. >= aString
  1181. <inlineJS: 'return String(self) >= aString._asString()'>
  1182. ! !
  1183. !String methodsFor: 'converting'!
  1184. asJSON
  1185. ^ self
  1186. !
  1187. asJavaScriptMethodName
  1188. <inlineJS: 'return $core.st2js(self)'>
  1189. !
  1190. asJavascript
  1191. <inlineJS: '
  1192. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1193. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1194. else
  1195. return "\"" + self + "\"";
  1196. '>
  1197. !
  1198. asLowercase
  1199. <inlineJS: 'return self.toLowerCase()'>
  1200. !
  1201. asMutator
  1202. "Answer a setter selector. For example,
  1203. #name asMutator returns #name:"
  1204. self last = ':' ifFalse: [ ^ self, ':' ].
  1205. ^ self
  1206. !
  1207. asNumber
  1208. <inlineJS: 'return Number(self)'>
  1209. !
  1210. asRegexp
  1211. ^ RegularExpression fromString: self
  1212. !
  1213. asString
  1214. ^ self
  1215. !
  1216. asSymbol
  1217. ^ self
  1218. !
  1219. asUppercase
  1220. <inlineJS: 'return self.toUpperCase()'>
  1221. !
  1222. capitalized
  1223. ^ self ifNotEmpty: [ self first asUppercase, self allButFirst ]
  1224. !
  1225. crlfSanitized
  1226. ^ self lines join: String lf
  1227. !
  1228. escaped
  1229. <inlineJS: 'return escape(self)'>
  1230. !
  1231. reversed
  1232. <inlineJS: 'return self.split("").reverse().join("")'>
  1233. !
  1234. unescaped
  1235. <inlineJS: 'return unescape(self)'>
  1236. !
  1237. uriComponentDecoded
  1238. <inlineJS: 'return decodeURIComponent(self)'>
  1239. !
  1240. uriComponentEncoded
  1241. <inlineJS: 'return encodeURIComponent(self)'>
  1242. !
  1243. uriDecoded
  1244. <inlineJS: 'return decodeURI(self)'>
  1245. !
  1246. uriEncoded
  1247. <inlineJS: 'return encodeURI(self)'>
  1248. ! !
  1249. !String methodsFor: 'copying'!
  1250. , aString
  1251. <inlineJS: 'return String(self) + aString'>
  1252. !
  1253. copyFrom: anIndex to: anotherIndex
  1254. <inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
  1255. !
  1256. deepCopy
  1257. ^ self shallowCopy
  1258. !
  1259. shallowCopy
  1260. ^ self
  1261. ! !
  1262. !String methodsFor: 'evaluating'!
  1263. value: anObject
  1264. ^ anObject perform: self
  1265. ! !
  1266. !String methodsFor: 'printing'!
  1267. printNl
  1268. <inlineJS: 'console.log(self)'>
  1269. !
  1270. printOn: aStream
  1271. aStream
  1272. nextPutAll: '''';
  1273. nextPutAll: (self replace: '''' with: '''''');
  1274. nextPutAll: ''''
  1275. ! !
  1276. !String methodsFor: 'private'!
  1277. numericallyIndexable
  1278. <inlineJS: 'return String(self)'>
  1279. ! !
  1280. !String methodsFor: 'regular expressions'!
  1281. match: aRegexp
  1282. <inlineJS: 'return self.search(aRegexp) !!= -1'>
  1283. !
  1284. matchesOf: aRegularExpression
  1285. <inlineJS: 'return self.match(aRegularExpression)'>
  1286. !
  1287. replace: aString with: anotherString
  1288. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1289. !
  1290. replaceRegexp: aRegexp with: aString
  1291. <inlineJS: 'return self.replace(aRegexp, aString)'>
  1292. !
  1293. trimBoth
  1294. ^ self trimBoth: '\s'
  1295. !
  1296. trimBoth: separators
  1297. ^ (self trimLeft: separators) trimRight: separators
  1298. !
  1299. trimLeft
  1300. ^ self trimLeft: '\s'
  1301. !
  1302. trimLeft: separators
  1303. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1304. !
  1305. trimRight
  1306. ^ self trimRight: '\s'
  1307. !
  1308. trimRight: separators
  1309. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1310. ! !
  1311. !String methodsFor: 'split join'!
  1312. join: aCollection
  1313. ^ String
  1314. streamContents: [ :stream | aCollection
  1315. do: [ :each | stream nextPutAll: each asString ]
  1316. separatedBy: [ stream nextPutAll: self ]]
  1317. !
  1318. lineIndicesDo: aBlock
  1319. "execute aBlock with 3 arguments for each line:
  1320. - start index of line
  1321. - end index of line without line delimiter
  1322. - end index of line including line delimiter(s) CR, LF or CRLF"
  1323. | cr lf start sz nextLF nextCR |
  1324. start := 1.
  1325. sz := self size.
  1326. cr := String cr.
  1327. nextCR := self indexOf: cr startingAt: 1.
  1328. lf := String lf.
  1329. nextLF := self indexOf: lf startingAt: 1.
  1330. [ start <= sz ] whileTrue: [
  1331. (nextLF = 0 and: [ nextCR = 0 ])
  1332. ifTrue: [ "No more CR, nor LF, the string is over"
  1333. aBlock value: start value: sz value: sz.
  1334. ^ self ].
  1335. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1336. ifTrue: [ "Found a LF"
  1337. aBlock value: start value: nextLF - 1 value: nextLF.
  1338. start := 1 + nextLF.
  1339. nextLF := self indexOf: lf startingAt: start ]
  1340. ifFalse: [ 1 + nextCR = nextLF
  1341. ifTrue: [ "Found a CR-LF pair"
  1342. aBlock value: start value: nextCR - 1 value: nextLF.
  1343. start := 1 + nextLF.
  1344. nextCR := self indexOf: cr startingAt: start.
  1345. nextLF := self indexOf: lf startingAt: start ]
  1346. ifFalse: [ "Found a CR"
  1347. aBlock value: start value: nextCR - 1 value: nextCR.
  1348. start := 1 + nextCR.
  1349. nextCR := self indexOf: cr startingAt: start ] ]]
  1350. !
  1351. lineNumber: anIndex
  1352. "Answer a string containing the characters in the given line number."
  1353. | lineCount |
  1354. lineCount := 0.
  1355. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1356. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1357. ^ nil
  1358. !
  1359. lines
  1360. "Answer an array of lines composing this receiver without the line ending delimiters."
  1361. <inlineJS: '
  1362. var result = self.split(/\r\n|\r|\n/);
  1363. if (!!result[result.length-1]) result.pop();
  1364. return result;
  1365. '>
  1366. !
  1367. linesDo: aBlock
  1368. "Execute aBlock with each line in this string. The terminating line
  1369. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1370. self lines do: aBlock
  1371. !
  1372. subStrings: aString
  1373. ^ self tokenize: aString
  1374. !
  1375. tokenize: aString
  1376. <inlineJS: 'return self.split(aString)'>
  1377. ! !
  1378. !String methodsFor: 'testing'!
  1379. includesSubString: subString
  1380. <inlineJS: 'return self.indexOf(subString) !!= -1'>
  1381. !
  1382. isCapitalized
  1383. ^ self first asUppercase == self first
  1384. !
  1385. isImmutable
  1386. ^ true
  1387. !
  1388. isSelector
  1389. <inlineJS:
  1390. 'return !!!!self.match(/^([a-zA-Z][a-zA-Z0-9]*|[\\+*/=><,@%~|&-]+|([a-zA-Z][a-zA-Z0-9]*\:)+)$/)'
  1391. >
  1392. !
  1393. isString
  1394. ^ true
  1395. !
  1396. isVowel
  1397. "Answer true if the receiver is a one character string containing a voyel"
  1398. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1399. ! !
  1400. !String class methodsFor: 'accessing'!
  1401. cr
  1402. <inlineJS: 'return "\r"'>
  1403. !
  1404. crlf
  1405. <inlineJS: 'return "\r\n"'>
  1406. !
  1407. esc
  1408. ^ self fromCharCode: 27
  1409. !
  1410. lf
  1411. <inlineJS: 'return "\n"'>
  1412. !
  1413. space
  1414. <inlineJS: 'return " "'>
  1415. !
  1416. streamClass
  1417. ^ StringStream
  1418. !
  1419. tab
  1420. <inlineJS: 'return "\t"'>
  1421. ! !
  1422. !String class methodsFor: 'instance creation'!
  1423. fromCharCode: anInteger
  1424. <inlineJS: 'return String.fromCharCode(anInteger)'>
  1425. !
  1426. fromString: aString
  1427. <inlineJS: 'return String(aString)'>
  1428. !
  1429. value: aUTFCharCode
  1430. <inlineJS: 'return String.fromCharCode(aUTFCharCode);'>
  1431. ! !
  1432. !String class methodsFor: 'random'!
  1433. random
  1434. "Returns random alphanumeric string beginning with letter"
  1435. <inlineJS: 'return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);'>
  1436. !
  1437. randomNotIn: aString
  1438. | result |
  1439. [ result := self random. aString includesSubString: result ] whileTrue.
  1440. ^ result
  1441. ! !
  1442. Collection subclass: #Set
  1443. instanceVariableNames: 'defaultBucket slowBucketStores fastBuckets size'
  1444. package: 'Kernel-Collections'!
  1445. !Set commentStamp!
  1446. I represent an unordered set of objects without duplicates.
  1447. ## Implementation notes
  1448. I put elements into different stores based on their type.
  1449. The goal is to store some elements into native JS object property names to be fast.
  1450. If an unboxed element has typeof 'string', 'boolean' or 'number', or an element is nil, null or undefined,
  1451. I store it as a property name in an empty (== Object.create(null)) JS object, different for each type
  1452. (for simplicity, nil/null/undefined is treated as one and included with the two booleans).
  1453. If element happen to be an object, I try to store them in `ArrayBucketStore`. I have two of them by default,
  1454. one hashed using the Smalltalk class name, the other one using the JS constructor name. It is possible to have more or less
  1455. instances of `ArrayBucketStores`, see `#initializeSlowBucketStores`.
  1456. As a last resort, if none of the `ArrayBucketStore` instances can find a suitable bucket, the `defaultBucket` is used,
  1457. which is an `Array`.!
  1458. !Set methodsFor: 'accessing'!
  1459. size
  1460. ^ size
  1461. ! !
  1462. !Set methodsFor: 'adding/removing'!
  1463. add: anObject
  1464. | bucket |
  1465. bucket := self bucketsOfElement: anObject.
  1466. ^ bucket second
  1467. ifNil: [
  1468. | object slowBucket |
  1469. object := bucket first.
  1470. slowBucket := bucket third.
  1471. slowBucket
  1472. indexOf: object
  1473. ifAbsent: [
  1474. slowBucket add: object.
  1475. size := size + 1 ].
  1476. object ]
  1477. ifNotNil: [ :primitiveBucket |
  1478. self
  1479. add: bucket first
  1480. in: primitiveBucket ]
  1481. !
  1482. remove: anObject ifAbsent: aBlock
  1483. | bucket |
  1484. bucket := self bucketsOfElement: anObject.
  1485. ^ bucket second
  1486. ifNil: [ bucket third remove: bucket first ifAbsent: [ ^aBlock value ]. size := size - 1 ]
  1487. ifNotNil: [ :primitiveBucket | self remove: bucket first in: primitiveBucket ]
  1488. !
  1489. removeAll
  1490. <inlineJS: '
  1491. self[''@fastBuckets''] = {
  1492. "boolean": { store: Object.create(null), fn: function (x) { return {"true": true, "false": false, "null": null}[x]; } },
  1493. "number": { store: Object.create(null), fn: Number },
  1494. "string": { store: Object.create(null) }
  1495. };
  1496. self[''@slowBucketStores''].forEach(function (x) { x._removeAll(); });
  1497. self[''@defaultBucket'']._removeAll();
  1498. self[''@size''] = 0;
  1499. '>
  1500. ! !
  1501. !Set methodsFor: 'comparing'!
  1502. = aCollection
  1503. self class = aCollection class ifFalse: [ ^ false ].
  1504. self size = aCollection size ifFalse: [ ^ false ].
  1505. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1506. ^ true
  1507. ! !
  1508. !Set methodsFor: 'enumerating'!
  1509. collect: aBlock
  1510. | collection |
  1511. collection := self class new.
  1512. self do: [ :each | collection add: (aBlock value: each) ].
  1513. ^ collection
  1514. !
  1515. detect: aBlock ifNone: anotherBlock
  1516. self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
  1517. ^ anotherBlock value
  1518. !
  1519. do: aBlock
  1520. <inlineJS: '
  1521. var el, keys, i;
  1522. el = self[''@fastBuckets''];
  1523. keys = Object.keys(el);
  1524. for (i = 0; i < keys.length; ++i) {
  1525. var fastBucket = el[keys[i]], fn = fastBucket.fn, store = Object.keys(fastBucket.store);
  1526. if (fn) { for (var j = 0; j < store.length; ++j) { aBlock._value_(fn(store[j])); } }
  1527. else { store._do_(aBlock); }
  1528. }
  1529. el = self[''@slowBucketStores''];
  1530. for (i = 0; i < el.length; ++i) { el[i]._do_(aBlock); }
  1531. self[''@defaultBucket'']._do_(aBlock);
  1532. '>
  1533. !
  1534. select: aBlock
  1535. | collection |
  1536. collection := self class new.
  1537. self do: [ :each |
  1538. (aBlock value: each) ifTrue: [
  1539. collection add: each ] ].
  1540. ^ collection
  1541. !
  1542. select: selectBlock thenCollect: collectBlock
  1543. | collection |
  1544. collection := self class new.
  1545. self do: [ :each |
  1546. (selectBlock value: each) ifTrue: [
  1547. collection add: (collectBlock value: each) ] ].
  1548. ^ collection
  1549. ! !
  1550. !Set methodsFor: 'initialization'!
  1551. initialize
  1552. super initialize.
  1553. defaultBucket := #().
  1554. self
  1555. initializeSlowBucketStores;
  1556. removeAll
  1557. !
  1558. initializeSlowBucketStores
  1559. slowBucketStores := {
  1560. ArrayBucketStore hashBlock: [ :x | self classNameOf: x ].
  1561. ArrayBucketStore hashBlock: [ :x | self jsConstructorNameOf: x ]
  1562. }
  1563. ! !
  1564. !Set methodsFor: 'printing'!
  1565. printOn: aStream
  1566. super printOn: aStream.
  1567. aStream nextPutAll: ' ('.
  1568. self
  1569. do: [ :each | each printOn: aStream ]
  1570. separatedBy: [ aStream nextPutAll: ' ' ].
  1571. aStream nextPutAll: ')'
  1572. ! !
  1573. !Set methodsFor: 'private'!
  1574. add: anObject in: anotherObject
  1575. <inlineJS: '
  1576. if (anObject in anotherObject.store) { return false; }
  1577. self[''@size'']++;
  1578. anotherObject.store[anObject] = true;
  1579. return anObject;
  1580. '>
  1581. !
  1582. bucketsOfElement: anObject
  1583. "Find the appropriate bucket for `anObject`.
  1584. For optimization purposes, directly answer an array with:
  1585. - the object to be store
  1586. - the primitive bucket
  1587. - the slow bucket"
  1588. <inlineJS: '
  1589. // include nil to well-known objects under "boolean" fastBucket
  1590. if (anObject == null || anObject.isNil) return [ null, self[''@fastBuckets''].boolean ];
  1591. var prim = anObject.valueOf();
  1592. if (typeof prim === "object" || typeof prim === "function" || !!self[''@fastBuckets''][typeof prim]) {
  1593. var bucket = null;
  1594. self[''@slowBucketStores''].some(function (store) {
  1595. return bucket = store._bucketOfElement_(anObject);
  1596. });
  1597. return [ anObject, null, bucket || self[''@defaultBucket''] ];
  1598. }
  1599. return [ prim, self[''@fastBuckets''][typeof prim] ];
  1600. '>
  1601. !
  1602. classNameOf: anObject
  1603. "Answer the class name of `anObject`, or `undefined`
  1604. if `anObject` is not an Smalltalk object"
  1605. <inlineJS: 'return anObject.klass !!= null && anObject.klass.className'>
  1606. !
  1607. includes: anObject in: anotherObject
  1608. <inlineJS: 'return anObject in anotherObject.store'>
  1609. !
  1610. jsConstructorNameOf: anObject
  1611. <inlineJS: 'return anObject.constructor && anObject.constructor.name'>
  1612. !
  1613. remove: anObject in: anotherObject
  1614. <inlineJS: 'if (delete anotherObject.store[anObject]) self[''@size'']--'>
  1615. ! !
  1616. !Set methodsFor: 'testing'!
  1617. includes: anObject
  1618. | bucket |
  1619. bucket := self bucketsOfElement: anObject.
  1620. ^ bucket second
  1621. ifNil: [ bucket third includes: bucket first ]
  1622. ifNotNil: [ :primitiveBucket | self includes: bucket first in: primitiveBucket ]
  1623. ! !
  1624. Object subclass: #ProtoStream
  1625. instanceVariableNames: ''
  1626. package: 'Kernel-Collections'!
  1627. !ProtoStream commentStamp!
  1628. I am the abstract base for different accessor for a sequence of objects. This sequence is referred to as my "contents".
  1629. My instances are read/write streams modifying the contents.!
  1630. !ProtoStream methodsFor: 'accessing'!
  1631. contents
  1632. self subclassResponsibility
  1633. ! !
  1634. !ProtoStream methodsFor: 'actions'!
  1635. reset
  1636. self subclassResponsibility
  1637. !
  1638. resetContents
  1639. self subclassResponsibility
  1640. ! !
  1641. !ProtoStream methodsFor: 'enumerating'!
  1642. do: aBlock
  1643. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1644. ! !
  1645. !ProtoStream methodsFor: 'positioning'!
  1646. setToEnd
  1647. self subclassResponsibility
  1648. !
  1649. setToStart
  1650. self reset
  1651. ! !
  1652. !ProtoStream methodsFor: 'reading'!
  1653. next
  1654. ^ self atEnd
  1655. ifTrue: [ nil ]
  1656. ifFalse: [ self subclassResponsibility ]
  1657. !
  1658. peek
  1659. ^ self atEnd
  1660. ifTrue: [ nil ]
  1661. ifFalse: [ self subclassResponsibility ]
  1662. ! !
  1663. !ProtoStream methodsFor: 'testing'!
  1664. atEnd
  1665. self subclassResponsibility
  1666. !
  1667. atStart
  1668. self subclassResponsibility
  1669. !
  1670. isEmpty
  1671. ^ self atStart and: [ self atEnd ]
  1672. ! !
  1673. !ProtoStream methodsFor: 'writing'!
  1674. << anObject
  1675. self write: anObject
  1676. !
  1677. nextPut: anObject
  1678. self subclassResponsibility
  1679. !
  1680. nextPutAll: aCollection
  1681. aCollection do: [ :each |
  1682. self nextPut: each ]
  1683. !
  1684. nextPutJSObject: aJSObject
  1685. self nextPut: aJSObject
  1686. !
  1687. nextPutString: aString
  1688. self nextPut: aString
  1689. !
  1690. write: anObject
  1691. anObject putOn: self
  1692. ! !
  1693. !ProtoStream class methodsFor: 'instance creation'!
  1694. on: aCollection
  1695. ^ self new
  1696. setCollection: aCollection;
  1697. setStreamSize: aCollection size;
  1698. yourself
  1699. ! !
  1700. ProtoStream subclass: #Stream
  1701. instanceVariableNames: 'collection position streamSize'
  1702. package: 'Kernel-Collections'!
  1703. !Stream commentStamp!
  1704. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1705. My instances are read/write streams to the contents sequence collection.!
  1706. !Stream methodsFor: 'accessing'!
  1707. collection
  1708. ^ collection
  1709. !
  1710. contents
  1711. ^ self collection
  1712. copyFrom: 1
  1713. to: self streamSize
  1714. !
  1715. position
  1716. ^ position ifNil: [ position := 0 ]
  1717. !
  1718. position: anInteger
  1719. position := anInteger
  1720. !
  1721. setCollection: aCollection
  1722. collection := aCollection
  1723. !
  1724. setStreamSize: anInteger
  1725. streamSize := anInteger
  1726. !
  1727. size
  1728. ^ self streamSize
  1729. !
  1730. streamSize
  1731. ^ streamSize
  1732. ! !
  1733. !Stream methodsFor: 'actions'!
  1734. close
  1735. !
  1736. flush
  1737. !
  1738. reset
  1739. self position: 0
  1740. !
  1741. resetContents
  1742. self reset.
  1743. self setStreamSize: 0
  1744. ! !
  1745. !Stream methodsFor: 'positioning'!
  1746. setToEnd
  1747. self position: self size
  1748. !
  1749. skip: anInteger
  1750. self position: ((self position + anInteger) min: self size max: 0)
  1751. ! !
  1752. !Stream methodsFor: 'reading'!
  1753. next
  1754. ^ self atEnd
  1755. ifTrue: [ nil ]
  1756. ifFalse: [
  1757. self position: self position + 1.
  1758. collection at: self position ]
  1759. !
  1760. next: anInteger
  1761. | tempCollection |
  1762. tempCollection := self collection class new.
  1763. anInteger timesRepeat: [
  1764. self atEnd ifFalse: [
  1765. tempCollection add: self next ]].
  1766. ^ tempCollection
  1767. !
  1768. peek
  1769. ^ self atEnd ifFalse: [
  1770. self collection at: self position + 1 ]
  1771. ! !
  1772. !Stream methodsFor: 'testing'!
  1773. atEnd
  1774. ^ self position = self size
  1775. !
  1776. atStart
  1777. ^ self position = 0
  1778. !
  1779. isEmpty
  1780. ^ self size = 0
  1781. ! !
  1782. !Stream methodsFor: 'writing'!
  1783. nextPut: anObject
  1784. self position: self position + 1.
  1785. self collection at: self position put: anObject.
  1786. self setStreamSize: (self streamSize max: self position)
  1787. ! !
  1788. !Stream class methodsFor: 'instance creation'!
  1789. on: aCollection
  1790. ^ self new
  1791. setCollection: aCollection;
  1792. setStreamSize: aCollection size;
  1793. yourself
  1794. ! !
  1795. Stream subclass: #StringStream
  1796. instanceVariableNames: ''
  1797. package: 'Kernel-Collections'!
  1798. !StringStream commentStamp!
  1799. I am a Stream specific to `String` objects.!
  1800. !StringStream methodsFor: 'reading'!
  1801. next: anInteger
  1802. | tempCollection |
  1803. tempCollection := self collection class new.
  1804. anInteger timesRepeat: [
  1805. self atEnd ifFalse: [
  1806. tempCollection := tempCollection, self next ]].
  1807. ^ tempCollection
  1808. ! !
  1809. !StringStream methodsFor: 'writing'!
  1810. cr
  1811. ^ self nextPutAll: String cr
  1812. !
  1813. crlf
  1814. ^ self nextPutAll: String crlf
  1815. !
  1816. lf
  1817. ^ self nextPutAll: String lf
  1818. !
  1819. nextPut: aString
  1820. self nextPutAll: aString
  1821. !
  1822. nextPutAll: aString
  1823. | pre post |
  1824. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1825. pre := self collection copyFrom: 1 to: self position.
  1826. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1827. self setCollection: pre, aString, post
  1828. ].
  1829. self position: self position + aString size.
  1830. self setStreamSize: (self streamSize max: self position)
  1831. !
  1832. nextPutString: aString
  1833. self nextPutAll: aString
  1834. !
  1835. print: anObject
  1836. anObject printOn: self
  1837. !
  1838. printSymbol: anObject
  1839. anObject asSymbolPrintOn: self
  1840. !
  1841. space
  1842. self nextPut: ' '
  1843. !
  1844. tab
  1845. ^ self nextPutAll: String tab
  1846. ! !
  1847. Object subclass: #Queue
  1848. instanceVariableNames: 'read readIndex write'
  1849. package: 'Kernel-Collections'!
  1850. !Queue commentStamp!
  1851. I am a one-sided queue.
  1852. ## Usage
  1853. Use `#nextPut:` to add items to the queue.
  1854. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1855. ## Implementation notes
  1856. A Queue uses two OrderedCollections inside,
  1857. `read` is at the front, is not modified and only read using `readIndex`.
  1858. `write` is at the back and is appended new items.
  1859. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1860. As a consequence, no data moving is done by me, write appending may do data moving
  1861. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1862. !Queue methodsFor: 'accessing'!
  1863. next
  1864. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1865. !
  1866. nextIfAbsent: aBlock
  1867. | result |
  1868. result := read at: readIndex ifAbsent: [
  1869. write ifEmpty: [
  1870. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1871. ^ aBlock value ].
  1872. read := write.
  1873. readIndex := 1.
  1874. write := OrderedCollection new.
  1875. read first ].
  1876. read at: readIndex put: nil.
  1877. readIndex := readIndex + 1.
  1878. ^ result
  1879. !
  1880. nextPut: anObject
  1881. write add: anObject
  1882. ! !
  1883. !Queue methodsFor: 'initialization'!
  1884. initialize
  1885. super initialize.
  1886. read := OrderedCollection new.
  1887. write := OrderedCollection new.
  1888. readIndex := 1
  1889. ! !
  1890. Object subclass: #RegularExpression
  1891. instanceVariableNames: ''
  1892. package: 'Kernel-Collections'!
  1893. !RegularExpression commentStamp!
  1894. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1895. !RegularExpression methodsFor: 'evaluating'!
  1896. compile: aString
  1897. <inlineJS: 'return self.compile(aString)'>
  1898. !
  1899. exec: aString
  1900. <inlineJS: 'return self.exec(aString) || nil'>
  1901. !
  1902. test: aString
  1903. <inlineJS: 'return self.test(aString)'>
  1904. ! !
  1905. !RegularExpression class methodsFor: 'instance creation'!
  1906. fromString: aString
  1907. ^ self fromString: aString flag: ''
  1908. !
  1909. fromString: aString flag: anotherString
  1910. <inlineJS: 'return new RegExp(aString, anotherString)'>
  1911. ! !