1
0

Kernel-Collections.st 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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. asJavaScriptObject
  161. ^ self asArray collect: [ :each | each asJavaScriptObject ]
  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. asJavaScriptObject
  493. | hash |
  494. hash := HashedCollection new.
  495. self keysAndValuesDo: [ :key :value |
  496. hash at: key put: value asJavaScriptObject ].
  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. var nself = $self._numericallyIndexable();
  765. for(var i=0; i < nself.length; i++) {
  766. if($recv(nself[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. var nself = $self._numericallyIndexable();
  780. for(var i=start - 1; i < nself.length; i++){
  781. if($recv(nself[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._numericallyIndexable()[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. var nself = $self._numericallyIndexable();
  835. for(var i = 0; i < nself.length; i++)
  836. if(aBlock._value_(nself[i]))
  837. return nself[i];
  838. return anotherBlock._value();
  839. '>
  840. !
  841. do: aBlock
  842. <inlineJS: '
  843. var nself = $self._numericallyIndexable();
  844. for(var i=0; i < nself.length; i++) {
  845. aBlock._value_(nself[i]);
  846. }
  847. '>
  848. !
  849. reverseDo: aBlock
  850. self reversed do: aBlock
  851. !
  852. with: anotherCollection do: aBlock
  853. <inlineJS: '
  854. var nself = $self._numericallyIndexable();
  855. anotherCollection = anotherCollection._numericallyIndexable();
  856. for(var i=0; i<nself.length; i++) {
  857. aBlock._value_value_(nself[i], anotherCollection[i]);
  858. }
  859. '>
  860. !
  861. withIndexDo: aBlock
  862. <inlineJS: '
  863. var nself = $self._numericallyIndexable();
  864. for(var i=0; i < nself.length; i++) {
  865. aBlock._value_value_(nself[i], i+1);
  866. }
  867. '>
  868. ! !
  869. !SequenceableCollection methodsFor: 'private'!
  870. numericallyIndexable
  871. "This is an internal converting message.
  872. It answeres a representation of the receiver
  873. that can use foo[i] in JavaScript code.
  874. It fixes IE8, where boxed String is unable
  875. to numerically index its characters,
  876. but primitive string can."
  877. self subclassResponsibility
  878. ! !
  879. !SequenceableCollection methodsFor: 'streaming'!
  880. newStream
  881. ^ self streamClass on: self
  882. !
  883. readStream
  884. "For Pharo compatibility"
  885. ^ self stream
  886. !
  887. stream
  888. ^ self newStream
  889. !
  890. streamClass
  891. ^ self class streamClass
  892. !
  893. writeStream
  894. "For Pharo compatibility"
  895. ^ self stream
  896. ! !
  897. !SequenceableCollection methodsFor: 'testing'!
  898. beginsWith: prefix
  899. self size < prefix size ifTrue: [ ^ false ].
  900. ^ (self first: prefix size) = prefix
  901. !
  902. endsWith: suffix
  903. self size < suffix size ifTrue: [ ^ false ].
  904. ^ (self last: suffix size) = suffix
  905. !
  906. includes: anObject
  907. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  908. ! !
  909. !SequenceableCollection class methodsFor: 'accessing'!
  910. streamClass
  911. ^ Stream
  912. ! !
  913. !SequenceableCollection class methodsFor: 'streaming'!
  914. streamContents: aBlock
  915. | stream |
  916. stream := (self streamClass on: self new).
  917. aBlock value: stream.
  918. ^ stream contents
  919. ! !
  920. SequenceableCollection subclass: #Array
  921. instanceVariableNames: ''
  922. package: 'Kernel-Collections'!
  923. !Array commentStamp!
  924. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  925. I am directly mapped to JavaScript Number.
  926. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  927. !Array methodsFor: 'accessing'!
  928. at: anIndex ifAbsent: aBlock
  929. <inlineJS: '
  930. return anIndex >= 1 && anIndex <= self.length
  931. ? self[anIndex - 1]
  932. : aBlock._value()
  933. '>
  934. !
  935. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  936. <inlineJS: '
  937. return anIndex >= 1 && anIndex <= self.length
  938. ? aBlock._value_(self[anIndex - 1])
  939. : anotherBlock._value()
  940. '>
  941. !
  942. at: anIndex put: anObject
  943. <inlineJS: 'return self[anIndex - 1] = anObject'>
  944. !
  945. size
  946. <inlineJS: 'return self.length'>
  947. ! !
  948. !Array methodsFor: 'adding/removing'!
  949. add: anObject
  950. <inlineJS: 'self.push(anObject); return anObject;'>
  951. !
  952. addAll: aCollection
  953. <inlineJS: '
  954. if (Array.isArray(aCollection) && aCollection.length < 65000) self.push.apply(self, aCollection);
  955. else $globals.Array.superclass.fn.prototype._addAll_.call($self, aCollection);
  956. return aCollection;
  957. '>
  958. !
  959. addFirst: anObject
  960. <inlineJS: 'self.unshift(anObject); return anObject;'>
  961. !
  962. remove: anObject ifAbsent: aBlock
  963. | index |
  964. index := self indexOf: anObject ifAbsent: [ 0 ].
  965. ^ index = 0
  966. ifFalse: [ self removeIndex: index. anObject ]
  967. ifTrue: [ aBlock value ]
  968. !
  969. removeAll
  970. <inlineJS: 'self.length = 0'>
  971. !
  972. removeFrom: aNumber to: anotherNumber
  973. <inlineJS: 'self.splice(aNumber -1, anotherNumber - aNumber + 1)'>
  974. !
  975. removeIndex: anInteger
  976. <inlineJS: 'self.splice(anInteger - 1, 1)'>
  977. !
  978. removeLast
  979. <inlineJS: 'return self.pop();'>
  980. ! !
  981. !Array methodsFor: 'converting'!
  982. asJavaScriptSource
  983. ^ '[', ((self collect: [:each | each asJavaScriptSource ]) join: ', '), ']'
  984. !
  985. reversed
  986. <inlineJS: 'return self.slice().reverse()'>
  987. ! !
  988. !Array methodsFor: 'copying'!
  989. copyFrom: anIndex to: anotherIndex
  990. <inlineJS: '
  991. if (anIndex >= 1 && anotherIndex <= self.length) {
  992. return self.slice(anIndex - 1, anotherIndex);
  993. } else {
  994. self._at_(anIndex);
  995. self._at_(self.length + 1);
  996. throw new Error("Incorrect indexes in #copyFrom:to: not caught by #at:");
  997. }
  998. '>
  999. !
  1000. shallowCopy
  1001. <inlineJS: 'return self.slice()'>
  1002. ! !
  1003. !Array methodsFor: 'enumerating'!
  1004. collect: aBlock
  1005. "Optimized version"
  1006. <inlineJS: 'return self.map(function(each) {return aBlock._value_(each)})'>
  1007. !
  1008. join: aString
  1009. <inlineJS: 'return self.join(aString)'>
  1010. !
  1011. select: aBlock
  1012. "Optimized version"
  1013. <inlineJS: 'return self.filter(function(each) {return aBlock._value_(each)})'>
  1014. !
  1015. sort
  1016. ^ self sort: [ :a :b | a < b ]
  1017. !
  1018. sort: aBlock
  1019. <inlineJS: '
  1020. return self.sort(function(a, b) {
  1021. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  1022. })
  1023. '>
  1024. !
  1025. sorted
  1026. ^ self copy sort
  1027. !
  1028. sorted: aBlock
  1029. ^ self copy sort: aBlock
  1030. ! !
  1031. !Array methodsFor: 'printing'!
  1032. printOn: aStream
  1033. super printOn: aStream.
  1034. aStream nextPutAll: ' ('.
  1035. self
  1036. do: [ :each | each printOn: aStream ]
  1037. separatedBy: [ aStream nextPutAll: ' ' ].
  1038. aStream nextPutAll: ')'
  1039. ! !
  1040. !Array methodsFor: 'private'!
  1041. numericallyIndexable
  1042. ^ self
  1043. ! !
  1044. !Array class methodsFor: 'instance creation'!
  1045. new: anInteger
  1046. <inlineJS: 'return new Array(anInteger)'>
  1047. !
  1048. with: anObject
  1049. ^ (self new: 1)
  1050. at: 1 put: anObject;
  1051. yourself
  1052. !
  1053. with: anObject with: anObject2
  1054. ^ (self new: 2)
  1055. at: 1 put: anObject;
  1056. at: 2 put: anObject2;
  1057. yourself
  1058. !
  1059. with: anObject with: anObject2 with: anObject3
  1060. ^ (self new: 3)
  1061. at: 1 put: anObject;
  1062. at: 2 put: anObject2;
  1063. at: 3 put: anObject3;
  1064. yourself
  1065. !
  1066. withAll: aCollection
  1067. | instance index |
  1068. index := 1.
  1069. instance := self new: aCollection size.
  1070. aCollection do: [ :each |
  1071. instance at: index put: each.
  1072. index := index + 1 ].
  1073. ^ instance
  1074. ! !
  1075. SequenceableCollection subclass: #CharacterArray
  1076. instanceVariableNames: ''
  1077. package: 'Kernel-Collections'!
  1078. !CharacterArray commentStamp!
  1079. I am the abstract superclass of string-like collections.!
  1080. !CharacterArray methodsFor: 'accessing'!
  1081. at: anIndex put: anObject
  1082. self errorReadOnly
  1083. ! !
  1084. !CharacterArray methodsFor: 'adding/removing'!
  1085. add: anObject
  1086. self errorReadOnly
  1087. !
  1088. remove: anObject
  1089. self errorReadOnly
  1090. !
  1091. remove: anObject ifAbsent: aBlock
  1092. self errorReadOnly
  1093. ! !
  1094. !CharacterArray methodsFor: 'converting'!
  1095. asLowercase
  1096. ^ self class fromString: self asString asLowercase
  1097. !
  1098. asNumber
  1099. ^ self asString asNumber
  1100. !
  1101. asString
  1102. ^ self subclassResponsibility
  1103. !
  1104. asSymbol
  1105. ^ self asString
  1106. !
  1107. asUppercase
  1108. ^ self class fromString: self asString asUppercase
  1109. ! !
  1110. !CharacterArray methodsFor: 'copying'!
  1111. , aString
  1112. ^ self asString, aString asString
  1113. ! !
  1114. !CharacterArray methodsFor: 'error handling'!
  1115. errorReadOnly
  1116. self error: 'Object is read-only'
  1117. ! !
  1118. !CharacterArray methodsFor: 'printing'!
  1119. asSymbolPrintOn: aStream
  1120. aStream nextPutAll: '#'.
  1121. self asString isSelector
  1122. ifTrue: [ aStream nextPut: self ]
  1123. ifFalse: [ self printOn: aStream ]
  1124. !
  1125. printOn: aStream
  1126. self asString printOn: aStream
  1127. !
  1128. symbolPrintString
  1129. ^ String streamContents: [ :str | self asSymbolPrintOn: str ]
  1130. ! !
  1131. !CharacterArray methodsFor: 'streaming'!
  1132. putOn: aStream
  1133. aStream nextPutString: self
  1134. ! !
  1135. !CharacterArray class methodsFor: 'instance creation'!
  1136. fromString: aString
  1137. self subclassResponsibility
  1138. ! !
  1139. CharacterArray subclass: #String
  1140. instanceVariableNames: ''
  1141. package: 'Kernel-Collections'!
  1142. !String commentStamp!
  1143. 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.
  1144. String inherits many useful methods from its hierarchy, such as
  1145. `Collection >> #,`!
  1146. !String methodsFor: 'accessing'!
  1147. asciiValue
  1148. <inlineJS: 'return self.charCodeAt(0);'>
  1149. !
  1150. at: anIndex ifAbsent: aBlock
  1151. <inlineJS: 'return String(self)[anIndex - 1] || aBlock._value()'>
  1152. !
  1153. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1154. <inlineJS: '
  1155. var result = String(self)[anIndex - 1];
  1156. return result ? aBlock._value_(result) : anotherBlock._value();
  1157. '>
  1158. !
  1159. charCodeAt: anInteger
  1160. <inlineJS: 'return self.charCodeAt(anInteger - 1)'>
  1161. !
  1162. size
  1163. <inlineJS: 'return self.length'>
  1164. ! !
  1165. !String methodsFor: 'comparing'!
  1166. < aString
  1167. <inlineJS: 'return String(self) < aString._asString()'>
  1168. !
  1169. <= aString
  1170. <inlineJS: 'return String(self) <= aString._asString()'>
  1171. !
  1172. = aString
  1173. <inlineJS:
  1174. 'return aString !!= null && String(self) === (typeof aString === "string" ? aString : aString.valueOf())'>
  1175. !
  1176. == aString
  1177. <inlineJS: '
  1178. if (typeof aString === "string") return String(self) === aString;
  1179. else if (aString !!= null && typeof aString === "object") return String(self) === aString.valueOf();
  1180. else return false;
  1181. '>
  1182. !
  1183. > aString
  1184. <inlineJS: 'return String(self) > aString._asString()'>
  1185. !
  1186. >= aString
  1187. <inlineJS: 'return String(self) >= aString._asString()'>
  1188. ! !
  1189. !String methodsFor: 'converting'!
  1190. asJavaScriptMethodName
  1191. <inlineJS: 'return $core.st2js(self)'>
  1192. !
  1193. asJavaScriptObject
  1194. ^ self
  1195. !
  1196. asJavaScriptSource
  1197. <inlineJS: '
  1198. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1199. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1200. else
  1201. return "\"" + self + "\"";
  1202. '>
  1203. !
  1204. asLowercase
  1205. <inlineJS: 'return self.toLowerCase()'>
  1206. !
  1207. asMutator
  1208. "Answer a setter selector. For example,
  1209. #name asMutator returns #name:"
  1210. self last = ':' ifFalse: [ ^ self, ':' ].
  1211. ^ self
  1212. !
  1213. asNumber
  1214. <inlineJS: 'return Number(self)'>
  1215. !
  1216. asRegexp
  1217. ^ RegularExpression fromString: self
  1218. !
  1219. asString
  1220. ^ self
  1221. !
  1222. asSymbol
  1223. ^ self
  1224. !
  1225. asUppercase
  1226. <inlineJS: 'return self.toUpperCase()'>
  1227. !
  1228. capitalized
  1229. ^ self ifNotEmpty: [ self first asUppercase, self allButFirst ]
  1230. !
  1231. crlfSanitized
  1232. ^ self lines join: String lf
  1233. !
  1234. escaped
  1235. <inlineJS: 'return escape(self)'>
  1236. !
  1237. reversed
  1238. <inlineJS: 'return self.split("").reverse().join("")'>
  1239. !
  1240. unescaped
  1241. <inlineJS: 'return unescape(self)'>
  1242. !
  1243. uriComponentDecoded
  1244. <inlineJS: 'return decodeURIComponent(self)'>
  1245. !
  1246. uriComponentEncoded
  1247. <inlineJS: 'return encodeURIComponent(self)'>
  1248. !
  1249. uriDecoded
  1250. <inlineJS: 'return decodeURI(self)'>
  1251. !
  1252. uriEncoded
  1253. <inlineJS: 'return encodeURI(self)'>
  1254. ! !
  1255. !String methodsFor: 'copying'!
  1256. , aString
  1257. <inlineJS: 'return String(self) + aString'>
  1258. !
  1259. copyFrom: anIndex to: anotherIndex
  1260. <inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
  1261. !
  1262. deepCopy
  1263. ^ self shallowCopy
  1264. !
  1265. shallowCopy
  1266. ^ self
  1267. ! !
  1268. !String methodsFor: 'evaluating'!
  1269. value: anObject
  1270. ^ anObject perform: self
  1271. ! !
  1272. !String methodsFor: 'printing'!
  1273. printNl
  1274. <inlineJS: 'console.log(self)'>
  1275. !
  1276. printOn: aStream
  1277. aStream
  1278. nextPutAll: '''';
  1279. nextPutAll: (self replace: '''' with: '''''');
  1280. nextPutAll: ''''
  1281. ! !
  1282. !String methodsFor: 'private'!
  1283. numericallyIndexable
  1284. <inlineJS: 'return String(self)'>
  1285. ! !
  1286. !String methodsFor: 'regular expressions'!
  1287. match: aRegexp
  1288. <inlineJS: 'return self.search(aRegexp) !!= -1'>
  1289. !
  1290. matchesOf: aRegularExpression
  1291. <inlineJS: 'return self.match(aRegularExpression)'>
  1292. !
  1293. replace: aString with: anotherString
  1294. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1295. !
  1296. replaceRegexp: aRegexp with: aString
  1297. <inlineJS: 'return self.replace(aRegexp, aString)'>
  1298. !
  1299. trimBoth
  1300. ^ self trimBoth: '\s'
  1301. !
  1302. trimBoth: separators
  1303. ^ (self trimLeft: separators) trimRight: separators
  1304. !
  1305. trimLeft
  1306. ^ self trimLeft: '\s'
  1307. !
  1308. trimLeft: separators
  1309. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1310. !
  1311. trimRight
  1312. ^ self trimRight: '\s'
  1313. !
  1314. trimRight: separators
  1315. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1316. ! !
  1317. !String methodsFor: 'split join'!
  1318. join: aCollection
  1319. ^ String
  1320. streamContents: [ :stream | aCollection
  1321. do: [ :each | stream nextPutAll: each asString ]
  1322. separatedBy: [ stream nextPutAll: self ]]
  1323. !
  1324. lineIndicesDo: aBlock
  1325. "execute aBlock with 3 arguments for each line:
  1326. - start index of line
  1327. - end index of line without line delimiter
  1328. - end index of line including line delimiter(s) CR, LF or CRLF"
  1329. | cr lf start sz nextLF nextCR |
  1330. start := 1.
  1331. sz := self size.
  1332. cr := String cr.
  1333. nextCR := self indexOf: cr startingAt: 1.
  1334. lf := String lf.
  1335. nextLF := self indexOf: lf startingAt: 1.
  1336. [ start <= sz ] whileTrue: [
  1337. (nextLF = 0 and: [ nextCR = 0 ])
  1338. ifTrue: [ "No more CR, nor LF, the string is over"
  1339. aBlock value: start value: sz value: sz.
  1340. ^ self ].
  1341. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1342. ifTrue: [ "Found a LF"
  1343. aBlock value: start value: nextLF - 1 value: nextLF.
  1344. start := 1 + nextLF.
  1345. nextLF := self indexOf: lf startingAt: start ]
  1346. ifFalse: [ 1 + nextCR = nextLF
  1347. ifTrue: [ "Found a CR-LF pair"
  1348. aBlock value: start value: nextCR - 1 value: nextLF.
  1349. start := 1 + nextLF.
  1350. nextCR := self indexOf: cr startingAt: start.
  1351. nextLF := self indexOf: lf startingAt: start ]
  1352. ifFalse: [ "Found a CR"
  1353. aBlock value: start value: nextCR - 1 value: nextCR.
  1354. start := 1 + nextCR.
  1355. nextCR := self indexOf: cr startingAt: start ] ]]
  1356. !
  1357. lineNumber: anIndex
  1358. "Answer a string containing the characters in the given line number."
  1359. | lineCount |
  1360. lineCount := 0.
  1361. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1362. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1363. ^ nil
  1364. !
  1365. lines
  1366. "Answer an array of lines composing this receiver without the line ending delimiters."
  1367. <inlineJS: '
  1368. var result = self.split(/\r\n|\r|\n/);
  1369. if (!!result[result.length-1]) result.pop();
  1370. return result;
  1371. '>
  1372. !
  1373. linesDo: aBlock
  1374. "Execute aBlock with each line in this string. The terminating line
  1375. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1376. self lines do: aBlock
  1377. !
  1378. subStrings: aString
  1379. ^ self tokenize: aString
  1380. !
  1381. tokenize: aString
  1382. <inlineJS: 'return self.split(aString)'>
  1383. ! !
  1384. !String methodsFor: 'testing'!
  1385. includesSubString: subString
  1386. <inlineJS: 'return self.indexOf(subString) !!= -1'>
  1387. !
  1388. isCapitalized
  1389. ^ self first asUppercase == self first
  1390. !
  1391. isImmutable
  1392. ^ true
  1393. !
  1394. isSelector
  1395. <inlineJS:
  1396. 'return !!!!self.match(/^([a-zA-Z][a-zA-Z0-9]*|[\\+*/=><,@%~|&-]+|([a-zA-Z][a-zA-Z0-9]*\:)+)$/)'
  1397. >
  1398. !
  1399. isString
  1400. ^ true
  1401. !
  1402. isVowel
  1403. "Answer true if the receiver is a one character string containing a voyel"
  1404. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1405. ! !
  1406. !String class methodsFor: 'accessing'!
  1407. cr
  1408. <inlineJS: 'return "\r"'>
  1409. !
  1410. crlf
  1411. <inlineJS: 'return "\r\n"'>
  1412. !
  1413. esc
  1414. ^ self fromCharCode: 27
  1415. !
  1416. lf
  1417. <inlineJS: 'return "\n"'>
  1418. !
  1419. space
  1420. <inlineJS: 'return " "'>
  1421. !
  1422. streamClass
  1423. ^ StringStream
  1424. !
  1425. tab
  1426. <inlineJS: 'return "\t"'>
  1427. ! !
  1428. !String class methodsFor: 'instance creation'!
  1429. fromCharCode: anInteger
  1430. <inlineJS: 'return String.fromCharCode(anInteger)'>
  1431. !
  1432. fromString: aString
  1433. <inlineJS: 'return String(aString)'>
  1434. !
  1435. value: aUTFCharCode
  1436. <inlineJS: 'return String.fromCharCode(aUTFCharCode);'>
  1437. ! !
  1438. !String class methodsFor: 'random'!
  1439. random
  1440. "Returns random alphanumeric string beginning with letter"
  1441. <inlineJS: 'return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);'>
  1442. !
  1443. randomNotIn: aString
  1444. | result |
  1445. [ result := self random. aString includesSubString: result ] whileTrue.
  1446. ^ result
  1447. ! !
  1448. Collection subclass: #Set
  1449. instanceVariableNames: 'defaultBucket slowBucketStores fastBuckets size'
  1450. package: 'Kernel-Collections'!
  1451. !Set commentStamp!
  1452. I represent an unordered set of objects without duplicates.
  1453. ## Implementation notes
  1454. I put elements into different stores based on their type.
  1455. The goal is to store some elements into native JS object property names to be fast.
  1456. If an unboxed element has typeof 'string', 'boolean' or 'number', or an element is nil, null or undefined,
  1457. I store it as a property name in an empty (== Object.create(null)) JS object, different for each type
  1458. (for simplicity, nil/null/undefined is treated as one and included with the two booleans).
  1459. If element happen to be an object, I try to store them in `ArrayBucketStore`. I have two of them by default,
  1460. one hashed using the Smalltalk class name, the other one using the JS constructor name. It is possible to have more or less
  1461. instances of `ArrayBucketStores`, see `#initializeSlowBucketStores`.
  1462. As a last resort, if none of the `ArrayBucketStore` instances can find a suitable bucket, the `defaultBucket` is used,
  1463. which is an `Array`.!
  1464. !Set methodsFor: 'accessing'!
  1465. size
  1466. ^ size
  1467. ! !
  1468. !Set methodsFor: 'adding/removing'!
  1469. add: anObject
  1470. | bucket |
  1471. bucket := self bucketsOfElement: anObject.
  1472. ^ bucket second
  1473. ifNil: [
  1474. | object slowBucket |
  1475. object := bucket first.
  1476. slowBucket := bucket third.
  1477. slowBucket
  1478. indexOf: object
  1479. ifAbsent: [
  1480. slowBucket add: object.
  1481. size := size + 1 ].
  1482. object ]
  1483. ifNotNil: [ :primitiveBucket |
  1484. self
  1485. add: bucket first
  1486. in: primitiveBucket ]
  1487. !
  1488. remove: anObject ifAbsent: aBlock
  1489. | bucket |
  1490. bucket := self bucketsOfElement: anObject.
  1491. ^ bucket second
  1492. ifNil: [ bucket third remove: bucket first ifAbsent: [ ^aBlock value ]. size := size - 1 ]
  1493. ifNotNil: [ :primitiveBucket | self remove: bucket first in: primitiveBucket ]
  1494. !
  1495. removeAll
  1496. <inlineJS: '
  1497. $self[''@fastBuckets''] = {
  1498. "boolean": { store: Object.create(null), fn: function (x) { return {"true": true, "false": false, "null": null}[x]; } },
  1499. "number": { store: Object.create(null), fn: Number },
  1500. "string": { store: Object.create(null) }
  1501. };
  1502. $self[''@slowBucketStores''].forEach(function (x) { x._removeAll(); });
  1503. $self[''@defaultBucket'']._removeAll();
  1504. $self[''@size''] = 0;
  1505. '>
  1506. ! !
  1507. !Set methodsFor: 'comparing'!
  1508. = aCollection
  1509. self class = aCollection class ifFalse: [ ^ false ].
  1510. self size = aCollection size ifFalse: [ ^ false ].
  1511. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1512. ^ true
  1513. ! !
  1514. !Set methodsFor: 'enumerating'!
  1515. collect: aBlock
  1516. | collection |
  1517. collection := self class new.
  1518. self do: [ :each | collection add: (aBlock value: each) ].
  1519. ^ collection
  1520. !
  1521. detect: aBlock ifNone: anotherBlock
  1522. self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
  1523. ^ anotherBlock value
  1524. !
  1525. do: aBlock
  1526. <inlineJS: '
  1527. var el, keys, i;
  1528. el = $self[''@fastBuckets''];
  1529. keys = Object.keys(el);
  1530. for (i = 0; i < keys.length; ++i) {
  1531. var fastBucket = el[keys[i]], fn = fastBucket.fn, store = Object.keys(fastBucket.store);
  1532. if (fn) { for (var j = 0; j < store.length; ++j) { aBlock._value_(fn(store[j])); } }
  1533. else { store._do_(aBlock); }
  1534. }
  1535. el = $self[''@slowBucketStores''];
  1536. for (i = 0; i < el.length; ++i) { el[i]._do_(aBlock); }
  1537. $self[''@defaultBucket'']._do_(aBlock);
  1538. '>
  1539. !
  1540. select: aBlock
  1541. | collection |
  1542. collection := self class new.
  1543. self do: [ :each |
  1544. (aBlock value: each) ifTrue: [
  1545. collection add: each ] ].
  1546. ^ collection
  1547. !
  1548. select: selectBlock thenCollect: collectBlock
  1549. | collection |
  1550. collection := self class new.
  1551. self do: [ :each |
  1552. (selectBlock value: each) ifTrue: [
  1553. collection add: (collectBlock value: each) ] ].
  1554. ^ collection
  1555. ! !
  1556. !Set methodsFor: 'initialization'!
  1557. initialize
  1558. super initialize.
  1559. defaultBucket := #().
  1560. self
  1561. initializeSlowBucketStores;
  1562. removeAll
  1563. !
  1564. initializeSlowBucketStores
  1565. slowBucketStores := {
  1566. ArrayBucketStore hashBlock: [ :x | self classNameOf: x ].
  1567. ArrayBucketStore hashBlock: [ :x | self jsConstructorNameOf: x ]
  1568. }
  1569. ! !
  1570. !Set methodsFor: 'printing'!
  1571. printOn: aStream
  1572. super printOn: aStream.
  1573. aStream nextPutAll: ' ('.
  1574. self
  1575. do: [ :each | each printOn: aStream ]
  1576. separatedBy: [ aStream nextPutAll: ' ' ].
  1577. aStream nextPutAll: ')'
  1578. ! !
  1579. !Set methodsFor: 'private'!
  1580. add: anObject in: anotherObject
  1581. <inlineJS: '
  1582. if (anObject in anotherObject.store) { return false; }
  1583. $self[''@size'']++;
  1584. anotherObject.store[anObject] = true;
  1585. return anObject;
  1586. '>
  1587. !
  1588. bucketsOfElement: anObject
  1589. "Find the appropriate bucket for `anObject`.
  1590. For optimization purposes, directly answer an array with:
  1591. - the object to be store
  1592. - the primitive bucket
  1593. - the slow bucket"
  1594. <inlineJS: '
  1595. // include nil to well-known objects under "boolean" fastBucket
  1596. if (anObject == null || anObject.a$nil) return [ null, $self[''@fastBuckets''].boolean ];
  1597. var prim = anObject.valueOf();
  1598. if (typeof prim === "object" || typeof prim === "function" || !!$self[''@fastBuckets''][typeof prim]) {
  1599. var bucket = null;
  1600. $self[''@slowBucketStores''].some(function (store) {
  1601. return bucket = store._bucketOfElement_(anObject);
  1602. });
  1603. return [ anObject, null, bucket || $self[''@defaultBucket''] ];
  1604. }
  1605. return [ prim, $self[''@fastBuckets''][typeof prim] ];
  1606. '>
  1607. !
  1608. classNameOf: anObject
  1609. "Answer the class name of `anObject`, or `undefined`
  1610. if `anObject` is not an Smalltalk object"
  1611. <inlineJS: 'return anObject.a$cls !!= null && anObject.a$cls.className'>
  1612. !
  1613. includes: anObject in: anotherObject
  1614. <inlineJS: 'return anObject in anotherObject.store'>
  1615. !
  1616. jsConstructorNameOf: anObject
  1617. <inlineJS: 'return anObject.constructor && anObject.constructor.name'>
  1618. !
  1619. remove: anObject in: anotherObject
  1620. <inlineJS: 'if (anObject in anotherObject.store) { delete anotherObject.store[anObject]; $self[''@size'']--; }'>
  1621. ! !
  1622. !Set methodsFor: 'testing'!
  1623. includes: anObject
  1624. | bucket |
  1625. bucket := self bucketsOfElement: anObject.
  1626. ^ bucket second
  1627. ifNil: [ bucket third includes: bucket first ]
  1628. ifNotNil: [ :primitiveBucket | self includes: bucket first in: primitiveBucket ]
  1629. ! !
  1630. Object subclass: #ProtoStream
  1631. instanceVariableNames: ''
  1632. package: 'Kernel-Collections'!
  1633. !ProtoStream commentStamp!
  1634. I am the abstract base for different accessor for a sequence of objects. This sequence is referred to as my "contents".
  1635. My instances are read/write streams modifying the contents.!
  1636. !ProtoStream methodsFor: 'accessing'!
  1637. contents
  1638. self subclassResponsibility
  1639. ! !
  1640. !ProtoStream methodsFor: 'actions'!
  1641. reset
  1642. self subclassResponsibility
  1643. !
  1644. resetContents
  1645. self subclassResponsibility
  1646. ! !
  1647. !ProtoStream methodsFor: 'enumerating'!
  1648. do: aBlock
  1649. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1650. ! !
  1651. !ProtoStream methodsFor: 'positioning'!
  1652. setToEnd
  1653. self subclassResponsibility
  1654. !
  1655. setToStart
  1656. self reset
  1657. ! !
  1658. !ProtoStream methodsFor: 'reading'!
  1659. next
  1660. ^ self atEnd
  1661. ifTrue: [ nil ]
  1662. ifFalse: [ self subclassResponsibility ]
  1663. !
  1664. peek
  1665. ^ self atEnd
  1666. ifTrue: [ nil ]
  1667. ifFalse: [ self subclassResponsibility ]
  1668. ! !
  1669. !ProtoStream methodsFor: 'testing'!
  1670. atEnd
  1671. self subclassResponsibility
  1672. !
  1673. atStart
  1674. self subclassResponsibility
  1675. !
  1676. isEmpty
  1677. ^ self atStart and: [ self atEnd ]
  1678. ! !
  1679. !ProtoStream methodsFor: 'writing'!
  1680. << anObject
  1681. self write: anObject
  1682. !
  1683. nextPut: anObject
  1684. self subclassResponsibility
  1685. !
  1686. nextPutAll: aCollection
  1687. aCollection do: [ :each |
  1688. self nextPut: each ]
  1689. !
  1690. nextPutString: aString
  1691. self nextPut: aString
  1692. !
  1693. write: anObject
  1694. anObject putOn: self
  1695. ! !
  1696. !ProtoStream class methodsFor: 'instance creation'!
  1697. on: aCollection
  1698. ^ self new
  1699. setCollection: aCollection;
  1700. setStreamSize: aCollection size;
  1701. yourself
  1702. ! !
  1703. ProtoStream subclass: #Stream
  1704. instanceVariableNames: 'collection position streamSize'
  1705. package: 'Kernel-Collections'!
  1706. !Stream commentStamp!
  1707. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1708. My instances are read/write streams to the contents sequence collection.!
  1709. !Stream methodsFor: 'accessing'!
  1710. collection
  1711. ^ collection
  1712. !
  1713. contents
  1714. ^ self collection
  1715. copyFrom: 1
  1716. to: self streamSize
  1717. !
  1718. position
  1719. ^ position ifNil: [ position := 0 ]
  1720. !
  1721. position: anInteger
  1722. position := anInteger
  1723. !
  1724. setCollection: aCollection
  1725. collection := aCollection
  1726. !
  1727. setStreamSize: anInteger
  1728. streamSize := anInteger
  1729. !
  1730. size
  1731. ^ self streamSize
  1732. !
  1733. streamSize
  1734. ^ streamSize
  1735. ! !
  1736. !Stream methodsFor: 'actions'!
  1737. close
  1738. !
  1739. flush
  1740. !
  1741. reset
  1742. self position: 0
  1743. !
  1744. resetContents
  1745. self reset.
  1746. self setStreamSize: 0
  1747. ! !
  1748. !Stream methodsFor: 'positioning'!
  1749. setToEnd
  1750. self position: self size
  1751. !
  1752. skip: anInteger
  1753. self position: ((self position + anInteger) min: self size max: 0)
  1754. ! !
  1755. !Stream methodsFor: 'reading'!
  1756. next
  1757. ^ self atEnd
  1758. ifTrue: [ nil ]
  1759. ifFalse: [
  1760. self position: self position + 1.
  1761. collection at: self position ]
  1762. !
  1763. next: anInteger
  1764. | tempCollection |
  1765. tempCollection := self collection class new.
  1766. anInteger timesRepeat: [
  1767. self atEnd ifFalse: [
  1768. tempCollection add: self next ]].
  1769. ^ tempCollection
  1770. !
  1771. peek
  1772. ^ self atEnd ifFalse: [
  1773. self collection at: self position + 1 ]
  1774. ! !
  1775. !Stream methodsFor: 'testing'!
  1776. atEnd
  1777. ^ self position = self size
  1778. !
  1779. atStart
  1780. ^ self position = 0
  1781. !
  1782. isEmpty
  1783. ^ self size = 0
  1784. ! !
  1785. !Stream methodsFor: 'writing'!
  1786. nextPut: anObject
  1787. self position: self position + 1.
  1788. self collection at: self position put: anObject.
  1789. self setStreamSize: (self streamSize max: self position)
  1790. ! !
  1791. !Stream class methodsFor: 'instance creation'!
  1792. on: aCollection
  1793. ^ self new
  1794. setCollection: aCollection;
  1795. setStreamSize: aCollection size;
  1796. yourself
  1797. ! !
  1798. Stream subclass: #StringStream
  1799. instanceVariableNames: ''
  1800. package: 'Kernel-Collections'!
  1801. !StringStream commentStamp!
  1802. I am a Stream specific to `String` objects.!
  1803. !StringStream methodsFor: 'reading'!
  1804. next: anInteger
  1805. | tempCollection |
  1806. tempCollection := self collection class new.
  1807. anInteger timesRepeat: [
  1808. self atEnd ifFalse: [
  1809. tempCollection := tempCollection, self next ]].
  1810. ^ tempCollection
  1811. ! !
  1812. !StringStream methodsFor: 'writing'!
  1813. cr
  1814. ^ self nextPutAll: String cr
  1815. !
  1816. crlf
  1817. ^ self nextPutAll: String crlf
  1818. !
  1819. lf
  1820. ^ self nextPutAll: String lf
  1821. !
  1822. nextPut: aString
  1823. self nextPutAll: aString
  1824. !
  1825. nextPutAll: aString
  1826. | pre post |
  1827. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1828. pre := self collection copyFrom: 1 to: self position.
  1829. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1830. self setCollection: pre, aString, post
  1831. ].
  1832. self position: self position + aString size.
  1833. self setStreamSize: (self streamSize max: self position)
  1834. !
  1835. nextPutString: aString
  1836. self nextPutAll: aString
  1837. !
  1838. print: anObject
  1839. anObject printOn: self
  1840. !
  1841. printSymbol: anObject
  1842. anObject asSymbolPrintOn: self
  1843. !
  1844. space
  1845. self nextPut: ' '
  1846. !
  1847. tab
  1848. ^ self nextPutAll: String tab
  1849. ! !
  1850. Object subclass: #Queue
  1851. instanceVariableNames: 'read readIndex write'
  1852. package: 'Kernel-Collections'!
  1853. !Queue commentStamp!
  1854. I am a one-sided queue.
  1855. ## Usage
  1856. Use `#nextPut:` to add items to the queue.
  1857. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1858. ## Implementation notes
  1859. A Queue uses two OrderedCollections inside,
  1860. `read` is at the front, is not modified and only read using `readIndex`.
  1861. `write` is at the back and is appended new items.
  1862. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1863. As a consequence, no data moving is done by me, write appending may do data moving
  1864. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1865. !Queue methodsFor: 'accessing'!
  1866. next
  1867. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1868. !
  1869. nextIfAbsent: aBlock
  1870. | result |
  1871. result := read at: readIndex ifAbsent: [
  1872. write ifEmpty: [
  1873. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1874. ^ aBlock value ].
  1875. read := write.
  1876. readIndex := 1.
  1877. write := OrderedCollection new.
  1878. read first ].
  1879. read at: readIndex put: nil.
  1880. readIndex := readIndex + 1.
  1881. ^ result
  1882. !
  1883. nextPut: anObject
  1884. write add: anObject
  1885. ! !
  1886. !Queue methodsFor: 'initialization'!
  1887. initialize
  1888. super initialize.
  1889. read := OrderedCollection new.
  1890. write := OrderedCollection new.
  1891. readIndex := 1
  1892. ! !
  1893. Object subclass: #RegularExpression
  1894. instanceVariableNames: ''
  1895. package: 'Kernel-Collections'!
  1896. !RegularExpression commentStamp!
  1897. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1898. !RegularExpression methodsFor: 'evaluating'!
  1899. compile: aString
  1900. <inlineJS: 'return self.compile(aString)'>
  1901. !
  1902. exec: aString
  1903. <inlineJS: 'return self.exec(aString) || nil'>
  1904. !
  1905. test: aString
  1906. <inlineJS: 'return self.test(aString)'>
  1907. ! !
  1908. !RegularExpression class methodsFor: 'instance creation'!
  1909. fromString: aString
  1910. ^ self fromString: aString flag: ''
  1911. !
  1912. fromString: aString flag: anotherString
  1913. <inlineJS: 'return new RegExp(aString, anotherString)'>
  1914. ! !