Kernel-Collections.st 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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. if ('function'===typeof Object.keys) return Object.keys(self);
  253. var keys = [];
  254. for(var i in self) {
  255. if(self.hasOwnProperty(i)) {
  256. keys.push(i);
  257. }
  258. };
  259. return keys;
  260. >
  261. !
  262. size
  263. ^self keys size
  264. !
  265. values
  266. ^self keys collect: [:each | self at: each]
  267. ! !
  268. !HashedCollection methodsFor: 'adding/removing'!
  269. add: anAssociation
  270. self at: anAssociation key put: anAssociation value
  271. !
  272. addAll: aHashedCollection
  273. super addAll: aHashedCollection associations.
  274. ^aHashedCollection
  275. !
  276. remove: aKey ifAbsent: aBlock
  277. ^self removeKey: aKey ifAbsent: aBlock
  278. !
  279. removeKey: aKey
  280. ^self remove: aKey
  281. !
  282. removeKey: aKey ifAbsent: aBlock
  283. ^(self includesKey: aKey)
  284. ifFalse: [aBlock value]
  285. ifTrue: [self basicDelete: aKey]
  286. ! !
  287. !HashedCollection methodsFor: 'comparing'!
  288. = aHashedCollection
  289. self class = aHashedCollection class ifFalse: [^false].
  290. self size = aHashedCollection size ifFalse: [^false].
  291. ^self associations = aHashedCollection associations
  292. ! !
  293. !HashedCollection methodsFor: 'converting'!
  294. asDictionary
  295. ^Dictionary fromPairs: self associations
  296. !
  297. asJSON
  298. | c |
  299. c := self class new.
  300. self keysAndValuesDo: [:key :value |
  301. c at: key put: value asJSON].
  302. ^c
  303. ! !
  304. !HashedCollection methodsFor: 'copying'!
  305. , aCollection
  306. self shouldNotImplement
  307. !
  308. copyFrom: anIndex to: anotherIndex
  309. self shouldNotImplement
  310. !
  311. deepCopy
  312. | copy |
  313. copy := self class new.
  314. self associationsDo: [:each |
  315. copy at: each key put: each value deepCopy].
  316. ^copy
  317. !
  318. shallowCopy
  319. | copy |
  320. copy := self class new.
  321. self associationsDo: [:each |
  322. copy at: each key put: each value].
  323. ^copy
  324. ! !
  325. !HashedCollection methodsFor: 'enumerating'!
  326. associationsDo: aBlock
  327. self associations do: aBlock
  328. !
  329. collect: aBlock
  330. | newDict |
  331. newDict := self class new.
  332. self keysAndValuesDo: [:key :value |
  333. newDict at: key put: (aBlock value: value)].
  334. ^newDict
  335. !
  336. detect: aBlock ifNone: anotherBlock
  337. ^self values detect: aBlock ifNone: anotherBlock
  338. !
  339. do: aBlock
  340. self values do: aBlock
  341. !
  342. includes: anObject
  343. ^self values includes: anObject
  344. !
  345. keysAndValuesDo: aBlock
  346. self associationsDo: [:each |
  347. aBlock value: each key value: each value]
  348. !
  349. select: aBlock
  350. | newDict |
  351. newDict := self class new.
  352. self keysAndValuesDo: [:key :value |
  353. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  354. ^newDict
  355. ! !
  356. !HashedCollection methodsFor: 'printing'!
  357. printString
  358. ^String streamContents: [:aStream|
  359. aStream
  360. nextPutAll: super printString;
  361. nextPutAll: '('.
  362. self associations
  363. do: [:anAssociation|
  364. aStream
  365. nextPutAll: anAssociation key printString;
  366. nextPutAll: ' -> ';
  367. nextPutAll: anAssociation value printString]
  368. separatedBy: [aStream nextPutAll: ' , '].
  369. aStream nextPutAll: ')']
  370. !
  371. storeOn: aStream
  372. aStream nextPutAll: '#{'.
  373. self associations
  374. do: [:each | each storeOn: aStream]
  375. separatedBy: [ aStream nextPutAll: '. '].
  376. aStream nextPutAll: '}'
  377. ! !
  378. !HashedCollection methodsFor: 'testing'!
  379. includesKey: aKey
  380. <return self.hasOwnProperty(aKey)>
  381. ! !
  382. !HashedCollection class methodsFor: 'instance creation'!
  383. fromPairs: aCollection
  384. | dict |
  385. dict := self new.
  386. aCollection do: [:each | dict add: each].
  387. ^dict
  388. ! !
  389. HashedCollection subclass: #Dictionary
  390. instanceVariableNames: 'keys values'
  391. package: 'Kernel-Collections'!
  392. !Dictionary methodsFor: 'accessing'!
  393. at: aKey ifAbsent: aBlock
  394. <
  395. var index;
  396. for(var i=0;i<self['@keys'].length;i++){
  397. if(self['@keys'][i].__eq(aKey)) {index = i;}
  398. };
  399. if(typeof index === 'undefined') {
  400. return aBlock();
  401. } else {
  402. return self['@values'][index];
  403. }
  404. >
  405. !
  406. at: aKey put: aValue
  407. <
  408. var index = self['@keys'].indexOf(aKey);
  409. if(index === -1) {
  410. self['@values'].push(aValue);
  411. self['@keys'].push(aKey);
  412. } else {
  413. self['@values'][index] = aValue;
  414. };
  415. return aValue;
  416. >
  417. !
  418. keys
  419. ^keys copy
  420. !
  421. values
  422. ^values copy
  423. ! !
  424. !Dictionary methodsFor: 'adding/removing'!
  425. removeKey: aKey ifAbsent: aBlock
  426. <
  427. var index = self['@keys'].indexOf(aKey);
  428. if(index === -1) {
  429. return aBlock()
  430. } else {
  431. var value;
  432. self['@keys'].splice(index, 1);
  433. value = self['@values'].splice(index, 1);
  434. return value[0];
  435. };
  436. >
  437. ! !
  438. !Dictionary methodsFor: 'converting'!
  439. asHashedCollection
  440. ^HashedCollection fromPairs: self associations
  441. !
  442. asJSON
  443. ^self asHashedCollection asJSON
  444. ! !
  445. !Dictionary methodsFor: 'initialization'!
  446. initialize
  447. super initialize.
  448. keys := #().
  449. values := #()
  450. ! !
  451. !Dictionary methodsFor: 'testing'!
  452. includesKey: aKey
  453. ^keys includes: aKey
  454. ! !
  455. Collection subclass: #SequenceableCollection
  456. instanceVariableNames: ''
  457. package: 'Kernel-Collections'!
  458. !SequenceableCollection methodsFor: 'accessing'!
  459. allButFirst
  460. ^self copyFrom: 2 to: self size
  461. !
  462. allButLast
  463. ^self copyFrom: 1 to: self size - 1
  464. !
  465. at: anIndex
  466. ^self at: anIndex ifAbsent: [
  467. self errorNotFound]
  468. !
  469. at: anIndex ifAbsent: aBlock
  470. self subclassResponsibility
  471. !
  472. at: anIndex put: anObject
  473. self subclassResponsibility
  474. !
  475. atRandom
  476. ^ self at: self size atRandom
  477. !
  478. first
  479. ^self at: 1
  480. !
  481. first: n
  482. "Answer the first n elements of the receiver.
  483. Raise an error if there are not enough elements."
  484. ^ self copyFrom: 1 to: n
  485. !
  486. fourth
  487. ^self at: 4
  488. !
  489. indexOf: anObject
  490. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  491. !
  492. indexOf: anObject ifAbsent: aBlock
  493. <
  494. for(var i=0;i<self.length;i++){
  495. if(self[i].__eq(anObject)) {return i+1}
  496. }
  497. return aBlock();
  498. >
  499. !
  500. indexOf: anObject startingAt: start
  501. "Answer the index of the first occurence of anElement after start
  502. within the receiver. If the receiver does not contain anElement,
  503. answer 0."
  504. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  505. !
  506. indexOf: anObject startingAt: start ifAbsent: aBlock
  507. <
  508. for(var i=start-1;i<self.length;i++){
  509. if(self[i].__eq(anObject)) {return i+1}
  510. }
  511. return aBlock();
  512. >
  513. !
  514. last
  515. ^self at: self size
  516. !
  517. second
  518. ^self at: 2
  519. !
  520. third
  521. ^self at: 3
  522. ! !
  523. !SequenceableCollection methodsFor: 'adding'!
  524. addLast: anObject
  525. self add: anObject
  526. !
  527. removeLast
  528. self remove: self last
  529. ! !
  530. !SequenceableCollection methodsFor: 'comparing'!
  531. = aCollection
  532. (self class = aCollection class and: [
  533. self size = aCollection size]) ifFalse: [^false].
  534. self withIndexDo: [:each :i |
  535. (aCollection at: i) = each ifFalse: [^false]].
  536. ^true
  537. ! !
  538. !SequenceableCollection methodsFor: 'converting'!
  539. reversed
  540. self subclassResponsibility
  541. ! !
  542. !SequenceableCollection methodsFor: 'copying'!
  543. copyFrom: anIndex to: anotherIndex
  544. | range newCollection |
  545. range := anIndex to: anotherIndex.
  546. newCollection := self class new: range size.
  547. range withIndexDo: [:each :i |
  548. newCollection at: i put: (self at: each)].
  549. ^newCollection
  550. !
  551. deepCopy
  552. | newCollection |
  553. newCollection := self class new: self size.
  554. self withIndexDo: [:each :index |
  555. newCollection at: index put: each deepCopy].
  556. ^newCollection
  557. !
  558. shallowCopy
  559. | newCollection |
  560. newCollection := self class new: self size.
  561. self withIndexDo: [ :each :index |
  562. newCollection at: index put: each].
  563. ^newCollection
  564. ! !
  565. !SequenceableCollection methodsFor: 'enumerating'!
  566. withIndexDo: aBlock
  567. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  568. ! !
  569. !SequenceableCollection methodsFor: 'printing'!
  570. printString
  571. | str |
  572. str := '' writeStream.
  573. str nextPutAll: super printString, ' ('.
  574. self
  575. do: [:each | str nextPutAll: each printString]
  576. separatedBy: [str nextPutAll: ' '].
  577. str nextPutAll: ')'.
  578. ^str contents
  579. ! !
  580. SequenceableCollection subclass: #Array
  581. instanceVariableNames: ''
  582. package: 'Kernel-Collections'!
  583. !Array methodsFor: 'accessing'!
  584. at: anIndex ifAbsent: aBlock
  585. <
  586. if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
  587. return self[anIndex - 1];
  588. >
  589. !
  590. at: anIndex put: anObject
  591. <return self[anIndex - 1] = anObject>
  592. !
  593. size
  594. <return self.length>
  595. ! !
  596. !Array methodsFor: 'adding/removing'!
  597. add: anObject
  598. <self.push(anObject); return anObject;>
  599. !
  600. remove: anObject
  601. <
  602. for(var i=0;i<self.length;i++) {
  603. if(self[i] == anObject) {
  604. self.splice(i,1);
  605. break;
  606. }
  607. }
  608. >
  609. !
  610. removeFrom: aNumber to: anotherNumber
  611. <self.splice(aNumber - 1,anotherNumber - 1)>
  612. ! !
  613. !Array methodsFor: 'converting'!
  614. asJavascript
  615. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  616. !
  617. reversed
  618. <return self._copy().reverse()>
  619. ! !
  620. !Array methodsFor: 'enumerating'!
  621. join: aString
  622. <return self.join(aString)>
  623. !
  624. sort
  625. ^self basicPerform: 'sort'
  626. !
  627. sort: aBlock
  628. <
  629. return self.sort(function(a, b) {
  630. if(aBlock(a,b)) {return -1} else {return 1}
  631. })
  632. >
  633. !
  634. sorted
  635. ^self copy sort
  636. !
  637. sorted: aBlock
  638. ^self copy sort: aBlock
  639. ! !
  640. !Array class methodsFor: 'instance creation'!
  641. new: anInteger
  642. <return new Array(anInteger)>
  643. !
  644. with: anObject
  645. ^(self new: 1)
  646. at: 1 put: anObject;
  647. yourself
  648. !
  649. with: anObject with: anObject2
  650. ^(self new: 2)
  651. at: 1 put: anObject;
  652. at: 2 put: anObject2;
  653. yourself
  654. !
  655. with: anObject with: anObject2 with: anObject3
  656. ^(self new: 3)
  657. at: 1 put: anObject;
  658. at: 2 put: anObject2;
  659. at: 3 put: anObject3;
  660. yourself
  661. !
  662. withAll: aCollection
  663. | instance |
  664. instance := self new: aCollection size.
  665. aCollection withIndexDo: [:each :index |
  666. instance at: index put: each].
  667. ^instance
  668. ! !
  669. SequenceableCollection subclass: #CharacterArray
  670. instanceVariableNames: ''
  671. package: 'Kernel-Collections'!
  672. !CharacterArray methodsFor: 'accessing'!
  673. at: anIndex put: anObject
  674. self errorReadOnly
  675. ! !
  676. !CharacterArray methodsFor: 'adding'!
  677. add: anObject
  678. self errorReadOnly
  679. !
  680. remove: anObject
  681. self errorReadOnly
  682. ! !
  683. !CharacterArray methodsFor: 'converting'!
  684. asLowercase
  685. ^self class fromString: self asString asLowercase
  686. !
  687. asNumber
  688. ^self asString asNumber
  689. !
  690. asString
  691. ^self subclassResponsibility
  692. !
  693. asSymbol
  694. ^self subclassResponsibility
  695. !
  696. asUppercase
  697. ^self class fromString: self asString asUppercase
  698. ! !
  699. !CharacterArray methodsFor: 'copying'!
  700. , aString
  701. ^self asString, aString asString
  702. ! !
  703. !CharacterArray methodsFor: 'error handling'!
  704. errorReadOnly
  705. self error: 'Object is read-only'
  706. ! !
  707. !CharacterArray methodsFor: 'printing'!
  708. printString
  709. ^self asString printString
  710. ! !
  711. !CharacterArray class methodsFor: 'instance creation'!
  712. fromString: aString
  713. self subclassResponsibility
  714. ! !
  715. CharacterArray subclass: #String
  716. instanceVariableNames: ''
  717. package: 'Kernel-Collections'!
  718. !String methodsFor: 'accessing'!
  719. asciiValue
  720. <return self.charCodeAt(0);>
  721. !
  722. at: anIndex ifAbsent: aBlock
  723. <return self[anIndex - 1] || aBlock()>
  724. !
  725. escaped
  726. <return escape(self)>
  727. !
  728. size
  729. <return self.length>
  730. !
  731. unescaped
  732. <return unescape(self)>
  733. ! !
  734. !String methodsFor: 'comparing'!
  735. < aString
  736. <return String(self) < aString._asString()>
  737. !
  738. <= aString
  739. <return String(self) <= aString._asString()>
  740. !
  741. = aString
  742. aString class = self class ifFalse: [^false].
  743. <return String(self) === String(aString)>
  744. !
  745. > aString
  746. <return String(self) >> aString._asString()>
  747. !
  748. >= aString
  749. <return String(self) >>= aString._asString()>
  750. ! !
  751. !String methodsFor: 'converting'!
  752. asJSON
  753. ^self
  754. !
  755. asJavaScriptSelector
  756. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  757. !
  758. asJavascript
  759. <
  760. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  761. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  762. else
  763. return "\"" + self + "\"";
  764. >
  765. !
  766. asLowercase
  767. <return self.toLowerCase()>
  768. !
  769. asNumber
  770. <return Number(self)>
  771. !
  772. asSelector
  773. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  774. | selector |
  775. selector := '_', self.
  776. selector := selector replace: ':' with: '_'.
  777. selector := selector replace: '[+]' with: '_plus'.
  778. selector := selector replace: '-' with: '_minus'.
  779. selector := selector replace: '[*]' with: '_star'.
  780. selector := selector replace: '[/]' with: '_slash'.
  781. selector := selector replace: '>' with: '_gt'.
  782. selector := selector replace: '<' with: '_lt'.
  783. selector := selector replace: '=' with: '_eq'.
  784. selector := selector replace: ',' with: '_comma'.
  785. selector := selector replace: '[@]' with: '_at'.
  786. ^selector
  787. !
  788. asString
  789. ^self
  790. !
  791. asSymbol
  792. ^Symbol lookup: self
  793. !
  794. asUppercase
  795. <return self.toUpperCase()>
  796. !
  797. reversed
  798. <return self.split("").reverse().join("")>
  799. !
  800. tokenize: aString
  801. <return self.split(aString)>
  802. ! !
  803. !String methodsFor: 'copying'!
  804. , aString
  805. <return self + aString>
  806. !
  807. copyFrom: anIndex to: anotherIndex
  808. <return self.substring(anIndex - 1, anotherIndex)>
  809. !
  810. deepCopy
  811. ^self shallowCopy
  812. !
  813. shallowCopy
  814. ^self class fromString: self
  815. ! !
  816. !String methodsFor: 'printing'!
  817. printNl
  818. <console.log(self)>
  819. !
  820. printString
  821. ^'''', self, ''''
  822. ! !
  823. !String methodsFor: 'regular expressions'!
  824. match: aRegexp
  825. <return self.search(aRegexp) !!= -1>
  826. !
  827. matchesOf: aRegularExpression
  828. <return self.match(aRegularExpression)>
  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. ! !