Kernel-Collections.st 34 KB

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