Kernel-Collections.st 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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();
  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()
  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();
  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();
  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(self[i]))
  646. return self[i];
  647. return anotherBlock();
  648. >
  649. !
  650. do: aBlock
  651. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  652. !
  653. with: anotherCollection do: aBlock
  654. <for(var i=0;i<self.length;i++){aBlock(self[i], anotherCollection[i]);}>
  655. !
  656. withIndexDo: aBlock
  657. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  658. ! !
  659. !SequenceableCollection methodsFor: 'streaming'!
  660. newStream
  661. ^self streamClass on: self
  662. !
  663. readStream
  664. "For Pharo compatibility"
  665. ^self stream
  666. !
  667. stream
  668. ^self newStream
  669. !
  670. streamClass
  671. ^self class streamClass
  672. !
  673. writeStream
  674. "For Pharo compatibility"
  675. ^self stream
  676. ! !
  677. !SequenceableCollection methodsFor: 'testing'!
  678. includes: anObject
  679. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  680. ! !
  681. !SequenceableCollection class methodsFor: 'accessing'!
  682. streamClass
  683. ^Stream
  684. ! !
  685. !SequenceableCollection class methodsFor: 'streaming'!
  686. streamContents: aBlock
  687. | stream |
  688. stream := (self streamClass on: self new).
  689. aBlock value: stream.
  690. ^ stream contents
  691. ! !
  692. SequenceableCollection subclass: #Array
  693. instanceVariableNames: ''
  694. package: 'Kernel-Collections'!
  695. !Array commentStamp!
  696. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  697. I am directly mapped to JavaScript Number.
  698. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  699. !Array methodsFor: 'accessing'!
  700. at: anIndex ifAbsent: aBlock
  701. <
  702. if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
  703. return self[anIndex - 1];
  704. >
  705. !
  706. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  707. <return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>
  708. !
  709. at: anIndex put: anObject
  710. <return self[anIndex - 1] = anObject>
  711. !
  712. size
  713. <return self.length>
  714. ! !
  715. !Array methodsFor: 'adding/removing'!
  716. add: anObject
  717. <self.push(anObject); return anObject;>
  718. !
  719. remove: anObject ifAbsent: aBlock
  720. <
  721. for(var i=0;i<self.length;i++) {
  722. if(self[i] == anObject) {
  723. self.splice(i,1);
  724. return self;
  725. }
  726. };
  727. aBlock._value();
  728. >
  729. !
  730. removeFrom: aNumber to: anotherNumber
  731. <self.splice(aNumber - 1,anotherNumber - 1)>
  732. ! !
  733. !Array methodsFor: 'converting'!
  734. asJavascript
  735. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  736. !
  737. reversed
  738. <return self._copy().reverse()>
  739. ! !
  740. !Array methodsFor: 'enumerating'!
  741. join: aString
  742. <return self.join(aString)>
  743. !
  744. sort
  745. ^self basicPerform: 'sort'
  746. !
  747. sort: aBlock
  748. <
  749. return self.sort(function(a, b) {
  750. if(aBlock(a,b)) {return -1} else {return 1}
  751. })
  752. >
  753. !
  754. sorted
  755. ^self copy sort
  756. !
  757. sorted: aBlock
  758. ^self copy sort: aBlock
  759. ! !
  760. !Array methodsFor: 'printing'!
  761. printOn: aStream
  762. super printOn: aStream.
  763. aStream nextPutAll: ' ('.
  764. self
  765. do: [ :each | each printOn: aStream ]
  766. separatedBy: [ aStream nextPutAll: ' ' ].
  767. aStream nextPutAll: ')'
  768. ! !
  769. !Array class methodsFor: 'instance creation'!
  770. new: anInteger
  771. <return new Array(anInteger)>
  772. !
  773. with: anObject
  774. ^(self new: 1)
  775. at: 1 put: anObject;
  776. yourself
  777. !
  778. with: anObject with: anObject2
  779. ^(self new: 2)
  780. at: 1 put: anObject;
  781. at: 2 put: anObject2;
  782. yourself
  783. !
  784. with: anObject with: anObject2 with: anObject3
  785. ^(self new: 3)
  786. at: 1 put: anObject;
  787. at: 2 put: anObject2;
  788. at: 3 put: anObject3;
  789. yourself
  790. !
  791. withAll: aCollection
  792. | instance index |
  793. index := 1.
  794. instance := self new: aCollection size.
  795. aCollection do: [:each |
  796. instance at: index put: each.
  797. index := index + 1].
  798. ^instance
  799. ! !
  800. SequenceableCollection subclass: #CharacterArray
  801. instanceVariableNames: ''
  802. package: 'Kernel-Collections'!
  803. !CharacterArray commentStamp!
  804. I am the abstract superclass of string-like collections.!
  805. !CharacterArray methodsFor: 'accessing'!
  806. at: anIndex put: anObject
  807. self errorReadOnly
  808. ! !
  809. !CharacterArray methodsFor: 'adding'!
  810. add: anObject
  811. self errorReadOnly
  812. !
  813. remove: anObject
  814. self errorReadOnly
  815. ! !
  816. !CharacterArray methodsFor: 'converting'!
  817. asLowercase
  818. ^self class fromString: self asString asLowercase
  819. !
  820. asNumber
  821. ^self asString asNumber
  822. !
  823. asString
  824. ^self subclassResponsibility
  825. !
  826. asSymbol
  827. ^self asString
  828. !
  829. asUppercase
  830. ^self class fromString: self asString asUppercase
  831. ! !
  832. !CharacterArray methodsFor: 'copying'!
  833. , aString
  834. ^self asString, aString asString
  835. ! !
  836. !CharacterArray methodsFor: 'error handling'!
  837. errorReadOnly
  838. self error: 'Object is read-only'
  839. ! !
  840. !CharacterArray methodsFor: 'printing'!
  841. printOn: aStream
  842. self asString printOn: aStream
  843. ! !
  844. !CharacterArray methodsFor: 'streaming'!
  845. putOn: aStream
  846. aStream nextPutString: self
  847. ! !
  848. !CharacterArray class methodsFor: 'instance creation'!
  849. fromString: aString
  850. self subclassResponsibility
  851. ! !
  852. CharacterArray subclass: #String
  853. instanceVariableNames: ''
  854. package: 'Kernel-Collections'!
  855. !String commentStamp!
  856. 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.
  857. String inherits many useful methods from its hierarchy, such as
  858. `Collection >> #,`!
  859. !String methodsFor: 'accessing'!
  860. asciiValue
  861. <return self.charCodeAt(0);>
  862. !
  863. at: anIndex ifAbsent: aBlock
  864. <return String(self).charAt(anIndex - 1) || aBlock()>
  865. !
  866. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  867. <
  868. var result = String(self).charAt(anIndex - 1);
  869. return result ? aBlock._value_(result) : anotherBlock._value();
  870. >
  871. !
  872. charCodeAt: anInteger
  873. < return self.charCodeAt(anInteger - 1) >
  874. !
  875. escaped
  876. <return escape(self)>
  877. !
  878. size
  879. <return self.length>
  880. !
  881. unescaped
  882. <return unescape(self)>
  883. ! !
  884. !String methodsFor: 'comparing'!
  885. < aString
  886. <return String(self) < aString._asString()>
  887. !
  888. <= aString
  889. <return String(self) <= aString._asString()>
  890. !
  891. = aString
  892. <
  893. if(typeof aString === 'undefined') { return false }
  894. if(!!aString._isString || !! aString._isString()) {
  895. return false;
  896. }
  897. return String(self) === String(aString)
  898. >
  899. !
  900. == aString
  901. ^self = aString
  902. !
  903. > aString
  904. <return String(self) >> aString._asString()>
  905. !
  906. >= aString
  907. <return String(self) >>= aString._asString()>
  908. ! !
  909. !String methodsFor: 'converting'!
  910. asJSON
  911. ^self
  912. !
  913. asJavascript
  914. <
  915. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  916. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  917. else
  918. return "\"" + self + "\"";
  919. >
  920. !
  921. asLowercase
  922. <return self.toLowerCase()>
  923. !
  924. asNumber
  925. <return Number(self)>
  926. !
  927. asRegexp
  928. ^ RegularExpression fromString: self
  929. !
  930. asSelector
  931. <return smalltalk.selector(self)>
  932. !
  933. asString
  934. ^self
  935. !
  936. asSymbol
  937. ^ self
  938. !
  939. asUppercase
  940. <return self.toUpperCase()>
  941. !
  942. crlfSanitized
  943. ^self lines join: String lf
  944. !
  945. reversed
  946. <return self.split("").reverse().join("")>
  947. ! !
  948. !String methodsFor: 'copying'!
  949. , aString
  950. <return self + aString>
  951. !
  952. copyFrom: anIndex to: anotherIndex
  953. <return self.substring(anIndex - 1, anotherIndex)>
  954. !
  955. deepCopy
  956. ^self shallowCopy
  957. !
  958. shallowCopy
  959. ^self class fromString: self
  960. ! !
  961. !String methodsFor: 'enumerating'!
  962. do: aBlock
  963. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i));}>
  964. !
  965. withIndexDo: aBlock
  966. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i), i+1);}>
  967. ! !
  968. !String methodsFor: 'printing'!
  969. printNl
  970. <console.log(self)>
  971. !
  972. printOn: aStream
  973. aStream
  974. nextPutAll: '''';
  975. nextPutAll: self;
  976. nextPutAll: ''''
  977. ! !
  978. !String methodsFor: 'regular expressions'!
  979. match: aRegexp
  980. <return self.search(aRegexp) !!= -1>
  981. !
  982. matchesOf: aRegularExpression
  983. <return self.match(aRegularExpression)>
  984. !
  985. replace: aString with: anotherString
  986. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  987. !
  988. replaceRegexp: aRegexp with: aString
  989. <return self.replace(aRegexp, aString)>
  990. !
  991. trimBoth
  992. ^self trimBoth: '\s'
  993. !
  994. trimBoth: separators
  995. ^(self trimLeft: separators) trimRight: separators
  996. !
  997. trimLeft
  998. ^self trimLeft: '\s'
  999. !
  1000. trimLeft: separators
  1001. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1002. !
  1003. trimRight
  1004. ^self trimRight: '\s'
  1005. !
  1006. trimRight: separators
  1007. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1008. ! !
  1009. !String methodsFor: 'split join'!
  1010. join: aCollection
  1011. ^ String
  1012. streamContents: [:stream | aCollection
  1013. do: [:each | stream nextPutAll: each asString]
  1014. separatedBy: [stream nextPutAll: self]]
  1015. !
  1016. lineIndicesDo: aBlock
  1017. "execute aBlock with 3 arguments for each line:
  1018. - start index of line
  1019. - end index of line without line delimiter
  1020. - end index of line including line delimiter(s) CR, LF or CRLF"
  1021. | cr lf start sz nextLF nextCR |
  1022. start := 1.
  1023. sz := self size.
  1024. cr := String cr.
  1025. nextCR := self indexOf: cr startingAt: 1.
  1026. lf := String lf.
  1027. nextLF := self indexOf: lf startingAt: 1.
  1028. [ start <= sz ] whileTrue: [
  1029. (nextLF = 0 and: [ nextCR = 0 ])
  1030. ifTrue: [ "No more CR, nor LF, the string is over"
  1031. aBlock value: start value: sz value: sz.
  1032. ^self ].
  1033. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1034. ifTrue: [ "Found a LF"
  1035. aBlock value: start value: nextLF - 1 value: nextLF.
  1036. start := 1 + nextLF.
  1037. nextLF := self indexOf: lf startingAt: start ]
  1038. ifFalse: [ 1 + nextCR = nextLF
  1039. ifTrue: [ "Found a CR-LF pair"
  1040. aBlock value: start value: nextCR - 1 value: nextLF.
  1041. start := 1 + nextLF.
  1042. nextCR := self indexOf: cr startingAt: start.
  1043. nextLF := self indexOf: lf startingAt: start ]
  1044. ifFalse: [ "Found a CR"
  1045. aBlock value: start value: nextCR - 1 value: nextCR.
  1046. start := 1 + nextCR.
  1047. nextCR := self indexOf: cr startingAt: start ]]]
  1048. !
  1049. lineNumber: anIndex
  1050. "Answer a string containing the characters in the given line number."
  1051. | lineCount |
  1052. lineCount := 0.
  1053. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1054. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1055. ^nil
  1056. !
  1057. lines
  1058. "Answer an array of lines composing this receiver without the line ending delimiters."
  1059. | lines |
  1060. lines := Array new.
  1061. self linesDo: [:aLine | lines add: aLine].
  1062. ^lines
  1063. !
  1064. linesDo: aBlock
  1065. "Execute aBlock with each line in this string. The terminating line
  1066. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1067. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1068. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1069. !
  1070. subStrings: aString
  1071. ^ self tokenize: aString
  1072. !
  1073. tokenize: aString
  1074. <return self.split(aString)>
  1075. ! !
  1076. !String methodsFor: 'testing'!
  1077. includesSubString: subString
  1078. < return self.indexOf(subString) !!= -1 >
  1079. !
  1080. isImmutable
  1081. ^ true
  1082. !
  1083. isString
  1084. ^true
  1085. !
  1086. isVowel
  1087. "Answer true if the receiver is a one character string containing a voyel"
  1088. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1089. ! !
  1090. !String class methodsFor: 'accessing'!
  1091. cr
  1092. <return '\r'>
  1093. !
  1094. crlf
  1095. <return '\r\n'>
  1096. !
  1097. lf
  1098. <return '\n'>
  1099. !
  1100. space
  1101. <return ' '>
  1102. !
  1103. streamClass
  1104. ^StringStream
  1105. !
  1106. tab
  1107. <return '\t'>
  1108. ! !
  1109. !String class methodsFor: 'instance creation'!
  1110. fromCharCode: anInteger
  1111. <return String.fromCharCode(anInteger)>
  1112. !
  1113. fromString: aString
  1114. <return String(aString)>
  1115. !
  1116. value: aUTFCharCode
  1117. <return String.fromCharCode(aUTFCharCode);>
  1118. ! !
  1119. !String class methodsFor: 'random'!
  1120. random
  1121. "Returns random alphanumeric string beginning with letter"
  1122. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1123. !
  1124. randomNotIn: aString
  1125. | result |
  1126. [ result := self random. aString includesSubString: result ] whileTrue.
  1127. ^result
  1128. ! !
  1129. Collection subclass: #Set
  1130. instanceVariableNames: 'elements'
  1131. package: 'Kernel-Collections'!
  1132. !Set commentStamp!
  1133. I represent an unordered set of objects without duplicates.!
  1134. !Set methodsFor: 'accessing'!
  1135. size
  1136. ^elements size
  1137. ! !
  1138. !Set methodsFor: 'adding/removing'!
  1139. add: anObject
  1140. <
  1141. var found;
  1142. for(var i=0; i < self['@elements'].length; i++) {
  1143. if(anObject == self['@elements'][i]) {
  1144. found = true;
  1145. break;
  1146. }
  1147. }
  1148. if(!!found) {self['@elements'].push(anObject)}
  1149. >
  1150. !
  1151. remove: anObject
  1152. elements remove: anObject
  1153. !
  1154. remove: anObject ifAbsent: aBlock
  1155. elements remove: anObject ifAbsent: aBlock
  1156. ! !
  1157. !Set methodsFor: 'comparing'!
  1158. = aCollection
  1159. self class = aCollection class ifFalse: [ ^ false ].
  1160. self size = aCollection size ifFalse: [ ^ false ].
  1161. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1162. ^ true
  1163. ! !
  1164. !Set methodsFor: 'converting'!
  1165. asArray
  1166. ^elements copy
  1167. ! !
  1168. !Set methodsFor: 'enumerating'!
  1169. collect: aBlock
  1170. ^self class withAll: (elements collect: aBlock)
  1171. !
  1172. detect: aBlock ifNone: anotherBlock
  1173. ^elements detect: aBlock ifNone: anotherBlock
  1174. !
  1175. do: aBlock
  1176. elements do: aBlock
  1177. !
  1178. select: aBlock
  1179. | collection |
  1180. collection := self class new.
  1181. self do: [:each |
  1182. (aBlock value: each) ifTrue: [
  1183. collection add: each]].
  1184. ^collection
  1185. ! !
  1186. !Set methodsFor: 'initialization'!
  1187. initialize
  1188. super initialize.
  1189. elements := #()
  1190. ! !
  1191. !Set methodsFor: 'printing'!
  1192. printOn: aStream
  1193. super printOn: aStream.
  1194. aStream nextPutAll: ' ('.
  1195. self
  1196. do: [ :each | each printOn: aStream ]
  1197. separatedBy: [ aStream nextPutAll: ' ' ].
  1198. aStream nextPutAll: ')'
  1199. ! !
  1200. !Set methodsFor: 'testing'!
  1201. includes: anObject
  1202. ^elements includes: anObject
  1203. ! !
  1204. Object subclass: #Queue
  1205. instanceVariableNames: 'read readIndex write'
  1206. package: 'Kernel-Collections'!
  1207. !Queue commentStamp!
  1208. I am a one-sided queue.
  1209. ## Usage
  1210. Use `#nextPut:` to add items to the queue.
  1211. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1212. ## Implementation notes
  1213. A Queue uses two OrderedCollections inside,
  1214. `read` is at the front, is not modified and only read using `readIndex`.
  1215. `write` is at the back and is appended new items.
  1216. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1217. As a consequence, no data moving is done by me, write appending may do data moving
  1218. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1219. !Queue methodsFor: 'accessing'!
  1220. next
  1221. ^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1222. !
  1223. nextIfAbsent: aBlock
  1224. | result |
  1225. result := read at: readIndex ifAbsent: [
  1226. write isEmpty ifTrue: [
  1227. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1228. ^aBlock value ].
  1229. read := write.
  1230. readIndex := 1.
  1231. write := OrderedCollection new.
  1232. read first ].
  1233. read at: readIndex put: nil.
  1234. readIndex := readIndex + 1.
  1235. ^result
  1236. !
  1237. nextPut: anObject
  1238. write add: anObject
  1239. ! !
  1240. !Queue methodsFor: 'initialization'!
  1241. initialize
  1242. super initialize.
  1243. read := OrderedCollection new.
  1244. write := OrderedCollection new.
  1245. readIndex := 1
  1246. ! !
  1247. Object subclass: #RegularExpression
  1248. instanceVariableNames: ''
  1249. package: 'Kernel-Collections'!
  1250. !RegularExpression commentStamp!
  1251. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1252. !RegularExpression methodsFor: 'evaluating'!
  1253. compile: aString
  1254. <return self.compile(aString)>
  1255. !
  1256. exec: aString
  1257. <return self.exec(aString) || nil>
  1258. !
  1259. test: aString
  1260. <return self.test(aString)>
  1261. ! !
  1262. !RegularExpression class methodsFor: 'instance creation'!
  1263. fromString: aString
  1264. ^self fromString: aString flag: ''
  1265. !
  1266. fromString: aString flag: anotherString
  1267. <return new RegExp(aString, anotherString)>
  1268. ! !
  1269. Object subclass: #Stream
  1270. instanceVariableNames: 'collection position streamSize'
  1271. package: 'Kernel-Collections'!
  1272. !Stream commentStamp!
  1273. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1274. My instances are read/write streams to the contents sequence collection.!
  1275. !Stream methodsFor: 'accessing'!
  1276. collection
  1277. ^collection
  1278. !
  1279. contents
  1280. ^self collection
  1281. copyFrom: 1
  1282. to: self streamSize
  1283. !
  1284. position
  1285. ^position ifNil: [position := 0]
  1286. !
  1287. position: anInteger
  1288. position := anInteger
  1289. !
  1290. setCollection: aCollection
  1291. collection := aCollection
  1292. !
  1293. setStreamSize: anInteger
  1294. streamSize := anInteger
  1295. !
  1296. size
  1297. ^self streamSize
  1298. !
  1299. streamSize
  1300. ^streamSize
  1301. ! !
  1302. !Stream methodsFor: 'actions'!
  1303. close
  1304. !
  1305. flush
  1306. !
  1307. reset
  1308. self position: 0
  1309. !
  1310. resetContents
  1311. self reset.
  1312. self setStreamSize: 0
  1313. ! !
  1314. !Stream methodsFor: 'enumerating'!
  1315. do: aBlock
  1316. [self atEnd] whileFalse: [aBlock value: self next]
  1317. ! !
  1318. !Stream methodsFor: 'positioning'!
  1319. setToEnd
  1320. self position: self size
  1321. !
  1322. skip: anInteger
  1323. self position: ((self position + anInteger) min: self size max: 0)
  1324. ! !
  1325. !Stream methodsFor: 'reading'!
  1326. next
  1327. ^self atEnd
  1328. ifTrue: [nil]
  1329. ifFalse: [
  1330. self position: self position + 1.
  1331. collection at: self position]
  1332. !
  1333. next: anInteger
  1334. | tempCollection |
  1335. tempCollection := self collection class new.
  1336. anInteger timesRepeat: [
  1337. self atEnd ifFalse: [
  1338. tempCollection add: self next]].
  1339. ^tempCollection
  1340. !
  1341. peek
  1342. ^self atEnd ifFalse: [
  1343. self collection at: self position + 1]
  1344. ! !
  1345. !Stream methodsFor: 'testing'!
  1346. atEnd
  1347. ^self position = self size
  1348. !
  1349. atStart
  1350. ^self position = 0
  1351. !
  1352. isEmpty
  1353. ^self size = 0
  1354. ! !
  1355. !Stream methodsFor: 'writing'!
  1356. << anObject
  1357. self write: anObject
  1358. !
  1359. nextPut: anObject
  1360. self position: self position + 1.
  1361. self collection at: self position put: anObject.
  1362. self setStreamSize: (self streamSize max: self position)
  1363. !
  1364. nextPutAll: aCollection
  1365. aCollection do: [:each |
  1366. self nextPut: each]
  1367. !
  1368. nextPutString: aString
  1369. self nextPut: aString
  1370. !
  1371. write: anObject
  1372. anObject putOn: self
  1373. ! !
  1374. !Stream class methodsFor: 'instance creation'!
  1375. on: aCollection
  1376. ^self new
  1377. setCollection: aCollection;
  1378. setStreamSize: aCollection size;
  1379. yourself
  1380. ! !
  1381. Stream subclass: #StringStream
  1382. instanceVariableNames: ''
  1383. package: 'Kernel-Collections'!
  1384. !StringStream commentStamp!
  1385. I am a Stream specific to `String` objects.!
  1386. !StringStream methodsFor: 'reading'!
  1387. next: anInteger
  1388. | tempCollection |
  1389. tempCollection := self collection class new.
  1390. anInteger timesRepeat: [
  1391. self atEnd ifFalse: [
  1392. tempCollection := tempCollection, self next]].
  1393. ^tempCollection
  1394. ! !
  1395. !StringStream methodsFor: 'writing'!
  1396. cr
  1397. ^self nextPutAll: String cr
  1398. !
  1399. crlf
  1400. ^self nextPutAll: String crlf
  1401. !
  1402. lf
  1403. ^self nextPutAll: String lf
  1404. !
  1405. nextPut: aString
  1406. self nextPutAll: aString
  1407. !
  1408. nextPutAll: aString
  1409. | pre post |
  1410. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1411. pre := self collection copyFrom: 1 to: self position.
  1412. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1413. self setCollection: pre, aString, post
  1414. ].
  1415. self position: self position + aString size.
  1416. self setStreamSize: (self streamSize max: self position)
  1417. !
  1418. nextPutString: aString
  1419. self nextPutAll: aString
  1420. !
  1421. space
  1422. self nextPut: ' '
  1423. !
  1424. tab
  1425. ^self nextPutAll: String tab
  1426. ! !