1
0

Kernel-Collections.st 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. Smalltalk current createPackage: 'Kernel-Collections' properties: #{}!
  2. Object subclass: #Association
  3. instanceVariableNames: 'key value'
  4. package: 'Kernel-Collections'!
  5. !Association methodsFor: 'accessing'!
  6. key
  7. ^key
  8. !
  9. key: aKey
  10. key := aKey
  11. !
  12. value
  13. ^value
  14. !
  15. value: aValue
  16. value := aValue
  17. ! !
  18. !Association methodsFor: 'comparing'!
  19. = anAssociation
  20. ^self class = anAssociation class and: [
  21. self key = anAssociation key and: [
  22. self value = anAssociation value]]
  23. !
  24. storeOn: aStream
  25. "Store in the format (key->value)"
  26. "aStream nextPutAll: '('."
  27. key storeOn: aStream.
  28. aStream nextPutAll: '->'.
  29. value storeOn: aStream.
  30. "aStream nextPutAll: ')'"
  31. ! !
  32. !Association class methodsFor: 'instance creation'!
  33. key: aKey value: aValue
  34. ^self new
  35. key: aKey;
  36. value: aValue;
  37. yourself
  38. ! !
  39. Object subclass: #Collection
  40. instanceVariableNames: ''
  41. package: 'Kernel-Collections'!
  42. !Collection methodsFor: 'accessing'!
  43. readStream
  44. ^self stream
  45. !
  46. size
  47. self subclassResponsibility
  48. !
  49. stream
  50. ^self streamClass on: self
  51. !
  52. streamClass
  53. ^self class streamClass
  54. !
  55. writeStream
  56. ^self stream
  57. ! !
  58. !Collection methodsFor: 'adding/removing'!
  59. add: anObject
  60. self subclassResponsibility
  61. !
  62. addAll: aCollection
  63. aCollection do: [:each |
  64. self add: each].
  65. ^aCollection
  66. !
  67. remove: anObject
  68. ^self remove: anObject ifAbsent: [self errorNotFound]
  69. !
  70. remove: anObject ifAbsent: aBlock
  71. self subclassResponsibility
  72. ! !
  73. !Collection methodsFor: 'converting'!
  74. asArray
  75. ^Array withAll: self
  76. !
  77. asJSON
  78. ^self asArray collect: [:each | each asJSON]
  79. !
  80. asOrderedCollection
  81. ^self asArray
  82. !
  83. asSet
  84. ^Set withAll: self
  85. ! !
  86. !Collection methodsFor: 'copying'!
  87. , aCollection
  88. ^self copy
  89. addAll: aCollection;
  90. yourself
  91. !
  92. copyWith: anObject
  93. ^self copy add: anObject; yourself
  94. !
  95. copyWithAll: aCollection
  96. ^self copy addAll: aCollection; yourself
  97. !
  98. copyWithoutAll: aCollection
  99. "Answer a copy of the receiver that does not contain any elements
  100. equal to those in aCollection."
  101. ^ self reject: [:each | aCollection includes: each]
  102. ! !
  103. !Collection methodsFor: 'enumerating'!
  104. collect: aBlock
  105. | newCollection |
  106. newCollection := self class new.
  107. self do: [:each |
  108. newCollection add: (aBlock value: each)].
  109. ^newCollection
  110. !
  111. detect: aBlock
  112. ^self detect: aBlock ifNone: [self errorNotFound]
  113. !
  114. detect: aBlock ifNone: anotherBlock
  115. <
  116. for(var i = 0; i < self.length; i++)
  117. if(aBlock(self[i]))
  118. return self[i];
  119. return anotherBlock();
  120. >
  121. !
  122. do: aBlock
  123. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  124. !
  125. do: aBlock separatedBy: anotherBlock
  126. | first |
  127. first := true.
  128. self do: [:each |
  129. first
  130. ifTrue: [first := false]
  131. ifFalse: [anotherBlock value].
  132. aBlock value: each]
  133. !
  134. inject: anObject into: aBlock
  135. | result |
  136. result := anObject.
  137. self do: [:each |
  138. result := aBlock value: result value: each].
  139. ^result
  140. !
  141. reject: aBlock
  142. ^self select: [:each | (aBlock value: each) = false]
  143. !
  144. select: aBlock
  145. | stream |
  146. stream := self class new writeStream.
  147. self do: [:each |
  148. (aBlock value: each) ifTrue: [
  149. stream nextPut: each]].
  150. ^stream contents
  151. ! !
  152. !Collection methodsFor: 'error handling'!
  153. errorNotFound
  154. self error: 'Object is not in the collection'
  155. ! !
  156. !Collection methodsFor: 'testing'!
  157. ifEmpty: aBlock
  158. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver. Note that the fact that this method returns its argument in case the receiver is not empty allows one to write expressions like the following ones: self classifyMethodAs:
  159. (myProtocol ifEmpty: ['As yet unclassified'])"
  160. ^ self isEmpty
  161. ifTrue: [ aBlock value ]
  162. ifFalse: [ self ]
  163. !
  164. ifNotEmpty: aBlock
  165. self notEmpty ifTrue: aBlock.
  166. !
  167. includes: anObject
  168. <
  169. var i = self.length;
  170. while (i--) {
  171. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  172. }
  173. return false
  174. >
  175. !
  176. isEmpty
  177. ^self size = 0
  178. !
  179. notEmpty
  180. ^self isEmpty not
  181. ! !
  182. !Collection class methodsFor: 'accessing'!
  183. streamClass
  184. ^Stream
  185. ! !
  186. !Collection class methodsFor: 'instance creation'!
  187. new: anInteger
  188. ^self new
  189. !
  190. with: anObject
  191. ^self new
  192. add: anObject;
  193. yourself
  194. !
  195. with: anObject with: anotherObject
  196. ^self new
  197. add: anObject;
  198. add: anotherObject;
  199. yourself
  200. !
  201. with: firstObject with: secondObject with: thirdObject
  202. ^self new
  203. add: firstObject;
  204. add: secondObject;
  205. add: thirdObject;
  206. yourself
  207. !
  208. withAll: aCollection
  209. ^self new
  210. addAll: aCollection;
  211. yourself
  212. ! !
  213. Collection subclass: #HashedCollection
  214. instanceVariableNames: ''
  215. package: 'Kernel-Collections'!
  216. !HashedCollection commentStamp!
  217. A HashedCollection is a traditional JavaScript object, or a Smalltalk Dictionary.
  218. Unlike a Dictionary, it can only have strings as keys.!
  219. !HashedCollection methodsFor: 'accessing'!
  220. associations
  221. | associations |
  222. associations := #().
  223. self keys do: [:each |
  224. associations add: (Association key: each value: (self at: each))].
  225. ^associations
  226. !
  227. at: aKey
  228. ^self at: aKey ifAbsent: [self errorNotFound]
  229. !
  230. at: aKey ifAbsent: aBlock
  231. ^(self includesKey: aKey)
  232. ifTrue: [self basicAt: aKey]
  233. ifFalse: aBlock
  234. !
  235. at: aKey ifAbsentPut: aBlock
  236. ^self at: aKey ifAbsent: [
  237. self at: aKey put: aBlock value]
  238. !
  239. at: aKey ifPresent: aBlock
  240. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  241. !
  242. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  243. ^(self basicAt: aKey)
  244. ifNil: anotherBlock
  245. ifNotNil: [aBlock value: (self at: aKey)]
  246. !
  247. at: aKey put: aValue
  248. ^self basicAt: aKey put: aValue
  249. !
  250. keys
  251. <
  252. var keys = [];
  253. for(var i in self) {
  254. if(self.hasOwnProperty(i)) {
  255. keys.push(i);
  256. }
  257. };
  258. return keys;
  259. >
  260. !
  261. size
  262. ^self keys size
  263. !
  264. values
  265. ^self keys collect: [:each | self at: each]
  266. ! !
  267. !HashedCollection methodsFor: 'adding/removing'!
  268. add: anAssociation
  269. self at: anAssociation key put: anAssociation value
  270. !
  271. addAll: aHashedCollection
  272. super addAll: aHashedCollection associations.
  273. ^aHashedCollection
  274. !
  275. remove: aKey ifAbsent: aBlock
  276. ^self removeKey: aKey ifAbsent: aBlock
  277. !
  278. removeKey: aKey
  279. ^self remove: aKey
  280. !
  281. removeKey: aKey ifAbsent: aBlock
  282. ^(self includesKey: aKey)
  283. ifFalse: [aBlock value]
  284. ifTrue: [self basicDelete: aKey]
  285. ! !
  286. !HashedCollection methodsFor: 'comparing'!
  287. = aHashedCollection
  288. self class = aHashedCollection class ifFalse: [^false].
  289. self size = aHashedCollection size ifFalse: [^false].
  290. ^self associations = aHashedCollection associations
  291. ! !
  292. !HashedCollection methodsFor: 'converting'!
  293. asDictionary
  294. ^Dictionary fromPairs: self associations
  295. !
  296. asJSON
  297. | c |
  298. c := self class new.
  299. self keysAndValuesDo: [:key :value |
  300. c at: key put: value asJSON].
  301. ^c
  302. ! !
  303. !HashedCollection methodsFor: 'copying'!
  304. , aCollection
  305. self shouldNotImplement
  306. !
  307. copyFrom: anIndex to: anotherIndex
  308. self shouldNotImplement
  309. !
  310. deepCopy
  311. | copy |
  312. copy := self class new.
  313. self associationsDo: [:each |
  314. copy at: each key put: each value deepCopy].
  315. ^copy
  316. !
  317. shallowCopy
  318. | copy |
  319. copy := self class new.
  320. self associationsDo: [:each |
  321. copy at: each key put: each value].
  322. ^copy
  323. ! !
  324. !HashedCollection methodsFor: 'enumerating'!
  325. associationsDo: aBlock
  326. self associations do: aBlock
  327. !
  328. collect: aBlock
  329. | newDict |
  330. newDict := self class new.
  331. self keysAndValuesDo: [:key :value |
  332. newDict at: key put: (aBlock value: value)].
  333. ^newDict
  334. !
  335. detect: aBlock ifNone: anotherBlock
  336. ^self values detect: aBlock ifNone: anotherBlock
  337. !
  338. do: aBlock
  339. self values do: aBlock
  340. !
  341. includes: anObject
  342. ^self values includes: anObject
  343. !
  344. keysAndValuesDo: aBlock
  345. self associationsDo: [:each |
  346. aBlock value: each key value: each value]
  347. !
  348. select: aBlock
  349. | newDict |
  350. newDict := self class new.
  351. self keysAndValuesDo: [:key :value |
  352. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  353. ^newDict
  354. ! !
  355. !HashedCollection methodsFor: 'printing'!
  356. printString
  357. ^String streamContents: [:aStream|
  358. aStream
  359. nextPutAll: super printString;
  360. nextPutAll: '('.
  361. self associations
  362. do: [:anAssociation|
  363. aStream
  364. nextPutAll: anAssociation key printString;
  365. nextPutAll: ' -> ';
  366. nextPutAll: anAssociation value printString]
  367. separatedBy: [aStream nextPutAll: ' , '].
  368. aStream nextPutAll: ')']
  369. !
  370. storeOn: aStream
  371. aStream nextPutAll: '#{'.
  372. self associations
  373. do: [:each | each storeOn: aStream]
  374. separatedBy: [ aStream nextPutAll: '. '].
  375. aStream nextPutAll: '}'
  376. ! !
  377. !HashedCollection methodsFor: 'testing'!
  378. includesKey: aKey
  379. <return self.hasOwnProperty(aKey)>
  380. ! !
  381. !HashedCollection class methodsFor: 'instance creation'!
  382. fromPairs: aCollection
  383. | dict |
  384. dict := self new.
  385. aCollection do: [:each | dict add: each].
  386. ^dict
  387. ! !
  388. HashedCollection subclass: #Dictionary
  389. instanceVariableNames: 'keys values'
  390. package: 'Kernel-Collections'!
  391. !Dictionary methodsFor: 'accessing'!
  392. at: aKey ifAbsent: aBlock
  393. <
  394. var index;
  395. for(var i=0;i<self['@keys'].length;i++){
  396. if(self['@keys'][i].__eq(aKey)) {index = i;}
  397. };
  398. if(typeof index === 'undefined') {
  399. return aBlock();
  400. } else {
  401. return self['@values'][index];
  402. }
  403. >
  404. !
  405. at: aKey put: aValue
  406. <
  407. var index = self['@keys'].indexOf(aKey);
  408. if(index === -1) {
  409. self['@values'].push(aValue);
  410. self['@keys'].push(aKey);
  411. } else {
  412. self['@values'][index] = aValue;
  413. };
  414. return aValue;
  415. >
  416. !
  417. keys
  418. ^keys copy
  419. !
  420. values
  421. ^values copy
  422. ! !
  423. !Dictionary methodsFor: 'adding/removing'!
  424. removeKey: aKey ifAbsent: aBlock
  425. <
  426. var index = self['@keys'].indexOf(aKey);
  427. if(index === -1) {
  428. return aBlock()
  429. } else {
  430. var value;
  431. self['@keys'].splice(index, 1);
  432. value = self['@values'].splice(index, 1);
  433. return value[0];
  434. };
  435. >
  436. ! !
  437. !Dictionary methodsFor: 'converting'!
  438. asHashedCollection
  439. ^HashedCollection fromPairs: self associations
  440. !
  441. asJSON
  442. ^self asHashedCollection asJSON
  443. ! !
  444. !Dictionary methodsFor: 'initialization'!
  445. initialize
  446. super initialize.
  447. keys := #().
  448. values := #()
  449. ! !
  450. !Dictionary methodsFor: 'testing'!
  451. includesKey: aKey
  452. ^keys includes: aKey
  453. ! !
  454. Collection subclass: #SequenceableCollection
  455. instanceVariableNames: ''
  456. package: 'Kernel-Collections'!
  457. !SequenceableCollection methodsFor: 'accessing'!
  458. allButFirst
  459. ^self copyFrom: 2 to: self size
  460. !
  461. allButLast
  462. ^self copyFrom: 1 to: self size - 1
  463. !
  464. at: anIndex
  465. ^self at: anIndex ifAbsent: [
  466. self errorNotFound]
  467. !
  468. at: anIndex ifAbsent: aBlock
  469. self subclassResponsibility
  470. !
  471. at: anIndex put: anObject
  472. self subclassResponsibility
  473. !
  474. atRandom
  475. ^ self at: self size atRandom
  476. !
  477. first
  478. ^self at: 1
  479. !
  480. first: n
  481. "Answer the first n elements of the receiver.
  482. Raise an error if there are not enough elements."
  483. ^ self copyFrom: 1 to: n
  484. !
  485. fourth
  486. ^self at: 4
  487. !
  488. indexOf: anObject
  489. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  490. !
  491. indexOf: anObject ifAbsent: aBlock
  492. <
  493. for(var i=0;i<self.length;i++){
  494. if(self[i].__eq(anObject)) {return i+1}
  495. }
  496. return aBlock();
  497. >
  498. !
  499. indexOf: anObject startingAt: start
  500. "Answer the index of the first occurence of anElement after start
  501. within the receiver. If the receiver does not contain anElement,
  502. answer 0."
  503. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  504. !
  505. indexOf: anObject startingAt: start ifAbsent: aBlock
  506. <
  507. for(var i=start-1;i<self.length;i++){
  508. if(self[i].__eq(anObject)) {return i+1}
  509. }
  510. return aBlock();
  511. >
  512. !
  513. last
  514. ^self at: self size
  515. !
  516. second
  517. ^self at: 2
  518. !
  519. third
  520. ^self at: 3
  521. ! !
  522. !SequenceableCollection methodsFor: 'adding'!
  523. addLast: anObject
  524. self add: anObject
  525. !
  526. removeLast
  527. self remove: self last
  528. ! !
  529. !SequenceableCollection methodsFor: 'comparing'!
  530. = aCollection
  531. (self class = aCollection class and: [
  532. self size = aCollection size]) ifFalse: [^false].
  533. self withIndexDo: [:each :i |
  534. (aCollection at: i) = each ifFalse: [^false]].
  535. ^true
  536. ! !
  537. !SequenceableCollection methodsFor: 'converting'!
  538. reversed
  539. self subclassResponsibility
  540. ! !
  541. !SequenceableCollection methodsFor: 'copying'!
  542. copyFrom: anIndex to: anotherIndex
  543. | range newCollection |
  544. range := anIndex to: anotherIndex.
  545. newCollection := self class new: range size.
  546. range withIndexDo: [:each :i |
  547. newCollection at: i put: (self at: each)].
  548. ^newCollection
  549. !
  550. deepCopy
  551. | newCollection |
  552. newCollection := self class new: self size.
  553. self withIndexDo: [:each :index |
  554. newCollection at: index put: each deepCopy].
  555. ^newCollection
  556. !
  557. shallowCopy
  558. | newCollection |
  559. newCollection := self class new: self size.
  560. self withIndexDo: [ :each :index |
  561. newCollection at: index put: each].
  562. ^newCollection
  563. ! !
  564. !SequenceableCollection methodsFor: 'enumerating'!
  565. withIndexDo: aBlock
  566. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  567. ! !
  568. !SequenceableCollection methodsFor: 'printing'!
  569. printString
  570. | str |
  571. str := '' writeStream.
  572. str nextPutAll: super printString, ' ('.
  573. self
  574. do: [:each | str nextPutAll: each printString]
  575. separatedBy: [str nextPutAll: ' '].
  576. str nextPutAll: ')'.
  577. ^str contents
  578. ! !
  579. SequenceableCollection subclass: #Array
  580. instanceVariableNames: ''
  581. package: 'Kernel-Collections'!
  582. !Array methodsFor: 'accessing'!
  583. at: anIndex ifAbsent: aBlock
  584. <
  585. var value = self[anIndex - 1];
  586. if(value === undefined) {
  587. return aBlock();
  588. } else {
  589. return value;
  590. }
  591. >
  592. !
  593. at: anIndex put: anObject
  594. <return self[anIndex - 1] = anObject>
  595. !
  596. size
  597. <return self.length>
  598. ! !
  599. !Array methodsFor: 'adding/removing'!
  600. add: anObject
  601. <self.push(anObject); return anObject;>
  602. !
  603. remove: anObject
  604. <
  605. for(var i=0;i<self.length;i++) {
  606. if(self[i] == anObject) {
  607. self.splice(i,1);
  608. break;
  609. }
  610. }
  611. >
  612. !
  613. removeFrom: aNumber to: anotherNumber
  614. <self.splice(aNumber - 1,anotherNumber - 1)>
  615. ! !
  616. !Array methodsFor: 'converting'!
  617. asJavascript
  618. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  619. !
  620. reversed
  621. <return self._copy().reverse()>
  622. ! !
  623. !Array methodsFor: 'enumerating'!
  624. join: aString
  625. <return self.join(aString)>
  626. !
  627. sort
  628. ^self basicPerform: 'sort'
  629. !
  630. sort: aBlock
  631. <
  632. return self.sort(function(a, b) {
  633. if(aBlock(a,b)) {return -1} else {return 1}
  634. })
  635. >
  636. !
  637. sorted
  638. ^self copy sort
  639. !
  640. sorted: aBlock
  641. ^self copy sort: aBlock
  642. ! !
  643. !Array class methodsFor: 'instance creation'!
  644. new: anInteger
  645. <return new Array(anInteger)>
  646. !
  647. with: anObject
  648. ^(self new: 1)
  649. at: 1 put: anObject;
  650. yourself
  651. !
  652. with: anObject with: anObject2
  653. ^(self new: 2)
  654. at: 1 put: anObject;
  655. at: 2 put: anObject2;
  656. yourself
  657. !
  658. with: anObject with: anObject2 with: anObject3
  659. ^(self new: 3)
  660. at: 1 put: anObject;
  661. at: 2 put: anObject2;
  662. at: 3 put: anObject3;
  663. yourself
  664. !
  665. withAll: aCollection
  666. | instance |
  667. instance := self new: aCollection size.
  668. aCollection withIndexDo: [:each :index |
  669. instance at: index put: each].
  670. ^instance
  671. ! !
  672. SequenceableCollection subclass: #CharacterArray
  673. instanceVariableNames: ''
  674. package: 'Kernel-Collections'!
  675. !CharacterArray methodsFor: 'accessing'!
  676. at: anIndex put: anObject
  677. self errorReadOnly
  678. ! !
  679. !CharacterArray methodsFor: 'adding'!
  680. add: anObject
  681. self errorReadOnly
  682. !
  683. remove: anObject
  684. self errorReadOnly
  685. ! !
  686. !CharacterArray methodsFor: 'converting'!
  687. asLowercase
  688. ^self class fromString: self asString asLowercase
  689. !
  690. asNumber
  691. ^self asString asNumber
  692. !
  693. asString
  694. ^self subclassResponsibility
  695. !
  696. asSymbol
  697. ^self subclassResponsibility
  698. !
  699. asUppercase
  700. ^self class fromString: self asString asUppercase
  701. ! !
  702. !CharacterArray methodsFor: 'copying'!
  703. , aString
  704. ^self asString, aString asString
  705. ! !
  706. !CharacterArray methodsFor: 'error handling'!
  707. errorReadOnly
  708. self error: 'Object is read-only'
  709. ! !
  710. !CharacterArray methodsFor: 'printing'!
  711. printString
  712. ^self asString printString
  713. ! !
  714. !CharacterArray class methodsFor: 'instance creation'!
  715. fromString: aString
  716. self subclassResponsibility
  717. ! !
  718. CharacterArray subclass: #String
  719. instanceVariableNames: ''
  720. package: 'Kernel-Collections'!
  721. !String methodsFor: 'accessing'!
  722. asciiValue
  723. <return self.charCodeAt(0);>
  724. !
  725. at: anIndex ifAbsent: aBlock
  726. <return self[anIndex - 1] || aBlock()>
  727. !
  728. escaped
  729. <return escape(self)>
  730. !
  731. size
  732. <return self.length>
  733. !
  734. unescaped
  735. <return unescape(self)>
  736. ! !
  737. !String methodsFor: 'comparing'!
  738. < aString
  739. <return String(self) < aString._asString()>
  740. !
  741. <= aString
  742. <return String(self) <= aString._asString()>
  743. !
  744. = aString
  745. aString class = self class ifFalse: [^false].
  746. <return String(self) === String(aString)>
  747. !
  748. > aString
  749. <return String(self) >> aString._asString()>
  750. !
  751. >= aString
  752. <return String(self) >>= aString._asString()>
  753. ! !
  754. !String methodsFor: 'converting'!
  755. asJSON
  756. ^self
  757. !
  758. asJavaScriptSelector
  759. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  760. !
  761. asJavascript
  762. <
  763. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  764. return "unescape(\"" + escape(self) + "\")";
  765. else
  766. return "\"" + self + "\"";
  767. >
  768. !
  769. asLowercase
  770. <return self.toLowerCase()>
  771. !
  772. asNumber
  773. <return Number(self)>
  774. !
  775. asSelector
  776. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  777. | selector |
  778. selector := '_', self.
  779. selector := selector replace: ':' with: '_'.
  780. selector := selector replace: '[+]' with: '_plus'.
  781. selector := selector replace: '-' with: '_minus'.
  782. selector := selector replace: '[*]' with: '_star'.
  783. selector := selector replace: '[/]' with: '_slash'.
  784. selector := selector replace: '>' with: '_gt'.
  785. selector := selector replace: '<' with: '_lt'.
  786. selector := selector replace: '=' with: '_eq'.
  787. selector := selector replace: ',' with: '_comma'.
  788. selector := selector replace: '[@]' with: '_at'.
  789. ^selector
  790. !
  791. asString
  792. ^self
  793. !
  794. asSymbol
  795. ^Symbol lookup: self
  796. !
  797. asUppercase
  798. <return self.toUpperCase()>
  799. !
  800. reversed
  801. <return self.split("").reverse().join("")>
  802. !
  803. tokenize: aString
  804. <return self.split(aString)>
  805. ! !
  806. !String methodsFor: 'copying'!
  807. , aString
  808. <return self + aString>
  809. !
  810. copyFrom: anIndex to: anotherIndex
  811. <return self.substring(anIndex - 1, anotherIndex)>
  812. !
  813. deepCopy
  814. ^self shallowCopy
  815. !
  816. shallowCopy
  817. ^self class fromString: self
  818. ! !
  819. !String methodsFor: 'printing'!
  820. printNl
  821. <console.log(self)>
  822. !
  823. printString
  824. ^'''', self, ''''
  825. ! !
  826. !String methodsFor: 'regular expressions'!
  827. match: aRegexp
  828. <return self.search(aRegexp) !!= -1>
  829. !
  830. replace: aString with: anotherString
  831. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  832. !
  833. replaceRegexp: aRegexp with: aString
  834. <return self.replace(aRegexp, aString)>
  835. !
  836. trimBoth
  837. ^self trimBoth: '\s'
  838. !
  839. trimBoth: separators
  840. ^(self trimLeft: separators) trimRight: separators
  841. !
  842. trimLeft
  843. ^self trimLeft: '\s'
  844. !
  845. trimLeft: separators
  846. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  847. !
  848. trimRight
  849. ^self trimRight: '\s'
  850. !
  851. trimRight: separators
  852. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  853. ! !
  854. !String methodsFor: 'split join'!
  855. join: aCollection
  856. ^ String
  857. streamContents: [:stream | aCollection
  858. do: [:each | stream nextPutAll: each asString]
  859. separatedBy: [stream nextPutAll: self]]
  860. !
  861. lineIndicesDo: aBlock
  862. "execute aBlock with 3 arguments for each line:
  863. - start index of line
  864. - end index of line without line delimiter
  865. - end index of line including line delimiter(s) CR, LF or CRLF"
  866. | cr lf start sz nextLF nextCR |
  867. start := 1.
  868. sz := self size.
  869. cr := String cr.
  870. nextCR := self indexOf: cr startingAt: 1.
  871. lf := String lf.
  872. nextLF := self indexOf: lf startingAt: 1.
  873. [ start <= sz ] whileTrue: [
  874. (nextLF = 0 and: [ nextCR = 0 ])
  875. ifTrue: [ "No more CR, nor LF, the string is over"
  876. aBlock value: start value: sz value: sz.
  877. ^self ].
  878. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  879. ifTrue: [ "Found a LF"
  880. aBlock value: start value: nextLF - 1 value: nextLF.
  881. start := 1 + nextLF.
  882. nextLF := self indexOf: lf startingAt: start ]
  883. ifFalse: [ 1 + nextCR = nextLF
  884. ifTrue: [ "Found a CR-LF pair"
  885. aBlock value: start value: nextCR - 1 value: nextLF.
  886. start := 1 + nextLF.
  887. nextCR := self indexOf: cr startingAt: start.
  888. nextLF := self indexOf: lf startingAt: start ]
  889. ifFalse: [ "Found a CR"
  890. aBlock value: start value: nextCR - 1 value: nextCR.
  891. start := 1 + nextCR.
  892. nextCR := self indexOf: cr startingAt: start ]]]
  893. !
  894. lineNumber: anIndex
  895. "Answer a string containing the characters in the given line number."
  896. | lineCount |
  897. lineCount := 0.
  898. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  899. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  900. ^nil
  901. !
  902. lines
  903. "Answer an array of lines composing this receiver without the line ending delimiters."
  904. | lines |
  905. lines := Array new.
  906. self linesDo: [:aLine | lines add: aLine].
  907. ^lines
  908. !
  909. linesDo: aBlock
  910. "Execute aBlock with each line in this string. The terminating line
  911. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  912. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  913. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  914. ! !
  915. !String methodsFor: 'testing'!
  916. includesSubString: subString
  917. < return self.indexOf(subString) !!= -1 >
  918. !
  919. isString
  920. ^true
  921. ! !
  922. !String class methodsFor: 'accessing'!
  923. cr
  924. <return '\r'>
  925. !
  926. crlf
  927. <return '\r\n'>
  928. !
  929. lf
  930. <return '\n'>
  931. !
  932. space
  933. <return ' '>
  934. !
  935. streamClass
  936. ^StringStream
  937. !
  938. tab
  939. <return '\t'>
  940. ! !
  941. !String class methodsFor: 'instance creation'!
  942. fromString: aString
  943. <return new self.fn(aString)>
  944. !
  945. streamContents: blockWithArg
  946. |stream|
  947. stream := (self streamClass on: String new).
  948. blockWithArg value: stream.
  949. ^ stream contents
  950. !
  951. value: aUTFCharCode
  952. <return String.fromCharCode(aUTFCharCode);>
  953. ! !
  954. CharacterArray subclass: #Symbol
  955. instanceVariableNames: ''
  956. package: 'Kernel-Collections'!
  957. !Symbol methodsFor: 'accessing'!
  958. at: anIndex ifAbsent: aBlock
  959. ^self asString at: anIndex ifAbsent: aBlock
  960. !
  961. size
  962. ^self asString size
  963. ! !
  964. !Symbol methodsFor: 'comparing'!
  965. < aSymbol
  966. ^self asString < aSymbol asString
  967. !
  968. <= aSymbol
  969. ^self asString <= aSymbol asString
  970. !
  971. = aSymbol
  972. aSymbol class = self class ifFalse: [^false].
  973. ^self asString = aSymbol asString
  974. !
  975. > aSymbol
  976. ^self asString > aSymbol asString
  977. !
  978. >= aSymbol
  979. ^self asString >= aSymbol asString
  980. ! !
  981. !Symbol methodsFor: 'converting'!
  982. asJSON
  983. ^self asString asJSON
  984. !
  985. asJavascript
  986. ^'smalltalk.symbolFor("', self asString, '")'
  987. !
  988. asSelector
  989. ^self asString asSelector
  990. !
  991. asString
  992. <return self.value>
  993. !
  994. asSymbol
  995. ^self
  996. ! !
  997. !Symbol methodsFor: 'copying'!
  998. copyFrom: anIndex to: anotherIndex
  999. ^self class fromString: (self asString copyFrom: anIndex to: anotherIndex)
  1000. !
  1001. deepCopy
  1002. ^self
  1003. !
  1004. shallowCopy
  1005. ^self
  1006. ! !
  1007. !Symbol methodsFor: 'printing'!
  1008. isSymbol
  1009. ^true
  1010. !
  1011. printString
  1012. ^'#', self asString
  1013. ! !
  1014. !Symbol class methodsFor: 'instance creation'!
  1015. basicNew
  1016. self shouldNotImplement
  1017. !
  1018. fromString: aString
  1019. ^self lookup: aString
  1020. !
  1021. lookup: aString
  1022. <return smalltalk.symbolFor(aString);>
  1023. ! !
  1024. Collection subclass: #Set
  1025. instanceVariableNames: 'elements'
  1026. package: 'Kernel-Collections'!
  1027. !Set methodsFor: 'accessing'!
  1028. size
  1029. ^elements size
  1030. ! !
  1031. !Set methodsFor: 'adding/removing'!
  1032. add: anObject
  1033. <
  1034. var found;
  1035. for(var i=0; i < self['@elements'].length; i++) {
  1036. if(anObject == self['@elements'][i]) {
  1037. found = true;
  1038. break;
  1039. }
  1040. }
  1041. if(!!found) {self['@elements'].push(anObject)}
  1042. >
  1043. !
  1044. remove: anObject
  1045. elements remove: anObject
  1046. ! !
  1047. !Set methodsFor: 'comparing'!
  1048. = aCollection
  1049. ^self class = aCollection class and: [
  1050. elements = aCollection asArray]
  1051. ! !
  1052. !Set methodsFor: 'converting'!
  1053. asArray
  1054. ^elements copy
  1055. ! !
  1056. !Set methodsFor: 'enumerating'!
  1057. detect: aBlock ifNone: anotherBlock
  1058. ^elements detect: aBlock ifNone: anotherBlock
  1059. !
  1060. do: aBlock
  1061. elements do: aBlock
  1062. !
  1063. select: aBlock
  1064. | collection |
  1065. collection := self class new.
  1066. self do: [:each |
  1067. (aBlock value: each) ifTrue: [
  1068. collection add: each]].
  1069. ^collection
  1070. ! !
  1071. !Set methodsFor: 'initialization'!
  1072. initialize
  1073. super initialize.
  1074. elements := #()
  1075. ! !
  1076. !Set methodsFor: 'testing'!
  1077. includes: anObject
  1078. ^elements includes: anObject
  1079. ! !
  1080. Object subclass: #RegularExpression
  1081. instanceVariableNames: ''
  1082. package: 'Kernel-Collections'!
  1083. !RegularExpression methodsFor: 'evaluating'!
  1084. compile: aString
  1085. <return self.compile(aString)>
  1086. !
  1087. exec: aString
  1088. <return self.exec(aString) || nil>
  1089. !
  1090. test: aString
  1091. <return self.test(aString)>
  1092. ! !
  1093. !RegularExpression class methodsFor: 'instance creation'!
  1094. fromString: aString
  1095. ^self fromString: aString flag: ''
  1096. !
  1097. fromString: aString flag: anotherString
  1098. <return new RegExp(aString, anotherString)>
  1099. ! !
  1100. Object subclass: #Stream
  1101. instanceVariableNames: 'collection position streamSize'
  1102. package: 'Kernel-Collections'!
  1103. !Stream methodsFor: 'accessing'!
  1104. collection
  1105. ^collection
  1106. !
  1107. contents
  1108. ^self collection
  1109. copyFrom: 1
  1110. to: self streamSize
  1111. !
  1112. position
  1113. ^position ifNil: [position := 0]
  1114. !
  1115. position: anInteger
  1116. position := anInteger
  1117. !
  1118. setCollection: aCollection
  1119. collection := aCollection
  1120. !
  1121. setStreamSize: anInteger
  1122. streamSize := anInteger
  1123. !
  1124. size
  1125. ^self streamSize
  1126. !
  1127. streamSize
  1128. ^streamSize
  1129. ! !
  1130. !Stream methodsFor: 'actions'!
  1131. close
  1132. !
  1133. flush
  1134. !
  1135. reset
  1136. self position: 0
  1137. !
  1138. resetContents
  1139. self reset.
  1140. self setStreamSize: 0
  1141. ! !
  1142. !Stream methodsFor: 'enumerating'!
  1143. do: aBlock
  1144. [self atEnd] whileFalse: [aBlock value: self next]
  1145. ! !
  1146. !Stream methodsFor: 'positioning'!
  1147. setToEnd
  1148. self position: self size
  1149. !
  1150. skip: anInteger
  1151. self position: ((self position + anInteger) min: self size max: 0)
  1152. ! !
  1153. !Stream methodsFor: 'reading'!
  1154. next
  1155. ^self atEnd
  1156. ifTrue: [nil]
  1157. ifFalse: [
  1158. self position: self position + 1.
  1159. collection at: self position]
  1160. !
  1161. next: anInteger
  1162. | tempCollection |
  1163. tempCollection := self collection class new.
  1164. anInteger timesRepeat: [
  1165. self atEnd ifFalse: [
  1166. tempCollection add: self next]].
  1167. ^tempCollection
  1168. !
  1169. peek
  1170. ^self atEnd ifFalse: [
  1171. self collection at: self position + 1]
  1172. ! !
  1173. !Stream methodsFor: 'testing'!
  1174. atEnd
  1175. ^self position = self size
  1176. !
  1177. atStart
  1178. ^self position = 0
  1179. !
  1180. isEmpty
  1181. ^self size = 0
  1182. ! !
  1183. !Stream methodsFor: 'writing'!
  1184. nextPut: anObject
  1185. self position: self position + 1.
  1186. self collection at: self position put: anObject.
  1187. self setStreamSize: (self streamSize max: self position)
  1188. !
  1189. nextPutAll: aCollection
  1190. aCollection do: [:each |
  1191. self nextPut: each]
  1192. ! !
  1193. !Stream class methodsFor: 'instance creation'!
  1194. on: aCollection
  1195. ^self new
  1196. setCollection: aCollection;
  1197. setStreamSize: aCollection size;
  1198. yourself
  1199. ! !
  1200. Stream subclass: #StringStream
  1201. instanceVariableNames: ''
  1202. package: 'Kernel-Collections'!
  1203. !StringStream methodsFor: 'reading'!
  1204. next: anInteger
  1205. | tempCollection |
  1206. tempCollection := self collection class new.
  1207. anInteger timesRepeat: [
  1208. self atEnd ifFalse: [
  1209. tempCollection := tempCollection, self next]].
  1210. ^tempCollection
  1211. ! !
  1212. !StringStream methodsFor: 'writing'!
  1213. cr
  1214. ^self nextPutAll: String cr
  1215. !
  1216. crlf
  1217. ^self nextPutAll: String crlf
  1218. !
  1219. lf
  1220. ^self nextPutAll: String lf
  1221. !
  1222. nextPut: aString
  1223. self nextPutAll: aString
  1224. !
  1225. nextPutAll: aString
  1226. self setCollection:
  1227. (self collection copyFrom: 1 to: self position),
  1228. aString,
  1229. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1230. self position: self position + aString size.
  1231. self setStreamSize: (self streamSize max: self position)
  1232. !
  1233. space
  1234. self nextPut: ' '
  1235. ! !