Kernel-Collections.st 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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 from: 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. from: aCollection
  446. | newCollection |
  447. newCollection := self new.
  448. aCollection do: [ :each | newCollection add: each ].
  449. ^ newCollection
  450. !
  451. fromPairs: aCollection
  452. "This message is poorly named and has been replaced by #from:"
  453. ^ self from: aCollection
  454. !
  455. newFromPairs: aCollection
  456. "Accept an array of elements where every two elements form an
  457. association - the odd element being the key, and the even element the value."
  458. | newCollection |
  459. aCollection size even ifFalse: [
  460. self error: '#newFromPairs only accepts arrays of an even length' ].
  461. newCollection := self new.
  462. ( 1 to: aCollection size by: 2 ) do: [ :each |
  463. newCollection at: (aCollection at: each) put: (aCollection at: each + 1) ].
  464. ^ newCollection
  465. ! !
  466. HashedCollection subclass: #Dictionary
  467. instanceVariableNames: 'keys values'
  468. package: 'Kernel-Collections'!
  469. !Dictionary commentStamp!
  470. I represent a set of elements that can be viewed from one of two perspectives: a set of associations,
  471. or a container of values that are externally named where the name can be any object that responds to `=`.
  472. The external name is referred to as the key.!
  473. !Dictionary methodsFor: 'accessing'!
  474. at: aKey ifAbsent: aBlock
  475. <
  476. var index = self._positionOfKey_(aKey);
  477. return index >>=0 ? self['@values'][index] : aBlock._value();
  478. >
  479. !
  480. at: aKey put: aValue
  481. <
  482. var index = self._positionOfKey_(aKey);
  483. if(index === -1) {
  484. var keys = self['@keys'];
  485. index = keys.length;
  486. keys.push(aKey);
  487. }
  488. return self['@values'][index] = aValue;
  489. >
  490. !
  491. indexOf: anObject ifAbsent: aBlock
  492. | index |
  493. index := values indexOf: anObject ifAbsent: [0].
  494. ^ index = 0 ifTrue: [ aBlock value ] ifFalse: [ keys at: index ]
  495. !
  496. keys
  497. ^keys copy
  498. !
  499. values
  500. ^values copy
  501. ! !
  502. !Dictionary methodsFor: 'adding/removing'!
  503. removeKey: aKey ifAbsent: aBlock
  504. <
  505. var index = self._positionOfKey_(aKey);
  506. if(index === -1) {
  507. return aBlock._value()
  508. } else {
  509. var keys = self['@keys'], values = self['@values'];
  510. var value = values[index], l = keys.length;
  511. keys[index] = keys[l-1];
  512. keys.pop();
  513. values[index] = values[l-1];
  514. values.pop();
  515. return value;
  516. }
  517. >
  518. ! !
  519. !Dictionary methodsFor: 'converting'!
  520. asHashedCollection
  521. ^HashedCollection from: self associations
  522. !
  523. asJSON
  524. ^self asHashedCollection asJSON
  525. ! !
  526. !Dictionary methodsFor: 'enumerating'!
  527. keysAndValuesDo: aBlock
  528. ^keys with: values do: aBlock
  529. !
  530. keysDo: aBlock
  531. ^keys do: aBlock
  532. !
  533. valuesDo: aBlock
  534. ^values do: aBlock
  535. ! !
  536. !Dictionary methodsFor: 'initialization'!
  537. initialize
  538. super initialize.
  539. keys := #().
  540. values := #()
  541. ! !
  542. !Dictionary methodsFor: 'private'!
  543. positionOfKey: anObject
  544. <
  545. var keys = self['@keys'];
  546. for(var i=0;i<keys.length;i++){
  547. if(keys[i].__eq(anObject)) { return i;}
  548. }
  549. return -1;
  550. >
  551. ! !
  552. !Dictionary methodsFor: 'testing'!
  553. includesKey: aKey
  554. < return self._positionOfKey_(aKey) >>= 0; >
  555. ! !
  556. IndexableCollection subclass: #SequenceableCollection
  557. instanceVariableNames: ''
  558. package: 'Kernel-Collections'!
  559. !SequenceableCollection commentStamp!
  560. I am an IndexableCollection
  561. with numeric indexes starting with 1.!
  562. !SequenceableCollection methodsFor: 'accessing'!
  563. allButFirst
  564. ^self copyFrom: 2 to: self size
  565. !
  566. allButLast
  567. ^self copyFrom: 1 to: self size - 1
  568. !
  569. atRandom
  570. ^ self at: self size atRandom
  571. !
  572. first
  573. ^self at: 1
  574. !
  575. first: n
  576. "Answer the first n elements of the receiver.
  577. Raise an error if there are not enough elements."
  578. ^ self copyFrom: 1 to: n
  579. !
  580. fourth
  581. ^self at: 4
  582. !
  583. indexOf: anObject ifAbsent: aBlock
  584. <
  585. for(var i=0;i<self.length;i++) {
  586. if(self[i].__eq(anObject)) {return i+1}
  587. };
  588. return aBlock._value();
  589. >
  590. !
  591. indexOf: anObject startingAt: start
  592. "Answer the index of the first occurence of anElement after start
  593. within the receiver. If the receiver does not contain anElement,
  594. answer 0."
  595. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  596. !
  597. indexOf: anObject startingAt: start ifAbsent: aBlock
  598. <
  599. for(var i=start-1;i<self.length;i++){
  600. if(self[i].__eq(anObject)) {return i+1}
  601. }
  602. return aBlock._value();
  603. >
  604. !
  605. last
  606. ^self at: self size
  607. !
  608. second
  609. ^self at: 2
  610. !
  611. third
  612. ^self at: 3
  613. ! !
  614. !SequenceableCollection methodsFor: 'adding/removing'!
  615. addLast: anObject
  616. self add: anObject
  617. !
  618. removeLast
  619. self remove: self last
  620. ! !
  621. !SequenceableCollection methodsFor: 'comparing'!
  622. = aCollection
  623. (self class = aCollection class and: [
  624. self size = aCollection size]) ifFalse: [^false].
  625. self withIndexDo: [:each :i |
  626. (aCollection at: i) = each ifFalse: [^false]].
  627. ^true
  628. ! !
  629. !SequenceableCollection methodsFor: 'converting'!
  630. reversed
  631. self subclassResponsibility
  632. ! !
  633. !SequenceableCollection methodsFor: 'copying'!
  634. copyFrom: anIndex to: anotherIndex
  635. | range newCollection |
  636. range := anIndex to: anotherIndex.
  637. newCollection := self class new: range size.
  638. range withIndexDo: [:each :i |
  639. newCollection at: i put: (self at: each)].
  640. ^newCollection
  641. !
  642. deepCopy
  643. | newCollection |
  644. newCollection := self class new: self size.
  645. self withIndexDo: [:each :index |
  646. newCollection at: index put: each deepCopy].
  647. ^newCollection
  648. !
  649. shallowCopy
  650. | newCollection |
  651. newCollection := self class new: self size.
  652. self withIndexDo: [ :each :index |
  653. newCollection at: index put: each].
  654. ^newCollection
  655. ! !
  656. !SequenceableCollection methodsFor: 'enumerating'!
  657. detect: aBlock ifNone: anotherBlock
  658. <
  659. for(var i = 0; i < self.length; i++)
  660. if(aBlock._value_(self[i]))
  661. return self[i];
  662. return anotherBlock._value();
  663. >
  664. !
  665. do: aBlock
  666. <for(var i=0;i<self.length;i++){aBlock._value_(self[i]);}>
  667. !
  668. do: aBlock displayingProgress: aString
  669. ProgressHandler current
  670. do: aBlock on: self displaying: aString
  671. !
  672. with: anotherCollection do: aBlock
  673. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], anotherCollection[i]);}>
  674. !
  675. withIndexDo: aBlock
  676. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], i+1);}>
  677. ! !
  678. !SequenceableCollection methodsFor: 'streaming'!
  679. newStream
  680. ^self streamClass on: self
  681. !
  682. readStream
  683. "For Pharo compatibility"
  684. ^self stream
  685. !
  686. stream
  687. ^self newStream
  688. !
  689. streamClass
  690. ^self class streamClass
  691. !
  692. writeStream
  693. "For Pharo compatibility"
  694. ^self stream
  695. ! !
  696. !SequenceableCollection methodsFor: 'testing'!
  697. includes: anObject
  698. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  699. ! !
  700. !SequenceableCollection class methodsFor: 'accessing'!
  701. streamClass
  702. ^Stream
  703. ! !
  704. !SequenceableCollection class methodsFor: 'streaming'!
  705. streamContents: aBlock
  706. | stream |
  707. stream := (self streamClass on: self new).
  708. aBlock value: stream.
  709. ^ stream contents
  710. ! !
  711. SequenceableCollection subclass: #Array
  712. instanceVariableNames: ''
  713. package: 'Kernel-Collections'!
  714. !Array commentStamp!
  715. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  716. I am directly mapped to JavaScript Number.
  717. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  718. !Array methodsFor: 'accessing'!
  719. at: anIndex ifAbsent: aBlock
  720. <
  721. if((anIndex < 1) || (self.length < anIndex)) {return aBlock._value()};
  722. return self[anIndex - 1];
  723. >
  724. !
  725. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  726. <return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>
  727. !
  728. at: anIndex put: anObject
  729. <return self[anIndex - 1] = anObject>
  730. !
  731. size
  732. <return self.length>
  733. ! !
  734. !Array methodsFor: 'adding/removing'!
  735. add: anObject
  736. <self.push(anObject); return anObject;>
  737. !
  738. remove: anObject ifAbsent: aBlock
  739. <
  740. for(var i=0;i<self.length;i++) {
  741. if(_st(self[i]).__eq(anObject)) {
  742. self.splice(i,1);
  743. return self;
  744. }
  745. };
  746. aBlock._value();
  747. >
  748. !
  749. removeFrom: aNumber to: anotherNumber
  750. <self.splice(aNumber -1, anotherNumber - aNumber + 1)>
  751. !
  752. removeIndex: anInteger
  753. <self.splice(anInteger - 1, 1)>
  754. ! !
  755. !Array methodsFor: 'converting'!
  756. asJavascript
  757. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  758. !
  759. reversed
  760. <return self._copy().reverse()>
  761. ! !
  762. !Array methodsFor: 'enumerating'!
  763. join: aString
  764. <return self.join(aString)>
  765. !
  766. sort
  767. ^self basicPerform: 'sort'
  768. !
  769. sort: aBlock
  770. <
  771. return self.sort(function(a, b) {
  772. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  773. })
  774. >
  775. !
  776. sorted
  777. ^self copy sort
  778. !
  779. sorted: aBlock
  780. ^self copy sort: aBlock
  781. ! !
  782. !Array methodsFor: 'printing'!
  783. printOn: aStream
  784. super printOn: aStream.
  785. aStream nextPutAll: ' ('.
  786. self
  787. do: [ :each | each printOn: aStream ]
  788. separatedBy: [ aStream nextPutAll: ' ' ].
  789. aStream nextPutAll: ')'
  790. ! !
  791. !Array class methodsFor: 'instance creation'!
  792. new: anInteger
  793. <return new Array(anInteger)>
  794. !
  795. with: anObject
  796. ^(self new: 1)
  797. at: 1 put: anObject;
  798. yourself
  799. !
  800. with: anObject with: anObject2
  801. ^(self new: 2)
  802. at: 1 put: anObject;
  803. at: 2 put: anObject2;
  804. yourself
  805. !
  806. with: anObject with: anObject2 with: anObject3
  807. ^(self new: 3)
  808. at: 1 put: anObject;
  809. at: 2 put: anObject2;
  810. at: 3 put: anObject3;
  811. yourself
  812. !
  813. withAll: aCollection
  814. | instance index |
  815. index := 1.
  816. instance := self new: aCollection size.
  817. aCollection do: [:each |
  818. instance at: index put: each.
  819. index := index + 1].
  820. ^instance
  821. ! !
  822. SequenceableCollection subclass: #CharacterArray
  823. instanceVariableNames: ''
  824. package: 'Kernel-Collections'!
  825. !CharacterArray commentStamp!
  826. I am the abstract superclass of string-like collections.!
  827. !CharacterArray methodsFor: 'accessing'!
  828. at: anIndex put: anObject
  829. self errorReadOnly
  830. ! !
  831. !CharacterArray methodsFor: 'adding/removing'!
  832. add: anObject
  833. self errorReadOnly
  834. !
  835. remove: anObject
  836. self errorReadOnly
  837. ! !
  838. !CharacterArray methodsFor: 'converting'!
  839. asLowercase
  840. ^self class fromString: self asString asLowercase
  841. !
  842. asNumber
  843. ^self asString asNumber
  844. !
  845. asString
  846. ^self subclassResponsibility
  847. !
  848. asSymbol
  849. ^self asString
  850. !
  851. asUppercase
  852. ^self class fromString: self asString asUppercase
  853. ! !
  854. !CharacterArray methodsFor: 'copying'!
  855. , aString
  856. ^self asString, aString asString
  857. ! !
  858. !CharacterArray methodsFor: 'error handling'!
  859. errorReadOnly
  860. self error: 'Object is read-only'
  861. ! !
  862. !CharacterArray methodsFor: 'printing'!
  863. printOn: aStream
  864. self asString printOn: aStream
  865. ! !
  866. !CharacterArray methodsFor: 'streaming'!
  867. putOn: aStream
  868. aStream nextPutString: self
  869. ! !
  870. !CharacterArray class methodsFor: 'instance creation'!
  871. fromString: aString
  872. self subclassResponsibility
  873. ! !
  874. CharacterArray subclass: #String
  875. instanceVariableNames: ''
  876. package: 'Kernel-Collections'!
  877. !String commentStamp!
  878. 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.
  879. String inherits many useful methods from its hierarchy, such as
  880. `Collection >> #,`!
  881. !String methodsFor: 'accessing'!
  882. asciiValue
  883. <return self.charCodeAt(0);>
  884. !
  885. at: anIndex ifAbsent: aBlock
  886. <return String(self).charAt(anIndex - 1) || aBlock._value()>
  887. !
  888. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  889. <
  890. var result = String(self).charAt(anIndex - 1);
  891. return result ? aBlock._value_(result) : anotherBlock._value();
  892. >
  893. !
  894. charCodeAt: anInteger
  895. < return self.charCodeAt(anInteger - 1) >
  896. !
  897. escaped
  898. <return escape(self)>
  899. !
  900. size
  901. <return self.length>
  902. !
  903. unescaped
  904. <return unescape(self)>
  905. ! !
  906. !String methodsFor: 'comparing'!
  907. < aString
  908. <return String(self) < aString._asString()>
  909. !
  910. <= aString
  911. <return String(self) <= aString._asString()>
  912. !
  913. = aString
  914. <
  915. if(typeof aString === 'undefined') { return false }
  916. if(!!aString._isString || !! aString._isString()) {
  917. return false;
  918. }
  919. return String(self) === String(aString)
  920. >
  921. !
  922. == aString
  923. ^self = aString
  924. !
  925. > aString
  926. <return String(self) >> aString._asString()>
  927. !
  928. >= aString
  929. <return String(self) >>= aString._asString()>
  930. ! !
  931. !String methodsFor: 'converting'!
  932. asJSON
  933. ^self
  934. !
  935. asJavascript
  936. <
  937. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  938. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  939. else
  940. return "\"" + self + "\"";
  941. >
  942. !
  943. asLowercase
  944. <return self.toLowerCase()>
  945. !
  946. asNumber
  947. <return Number(self)>
  948. !
  949. asRegexp
  950. ^ RegularExpression fromString: self
  951. !
  952. asSelector
  953. <return smalltalk.selector(self)>
  954. !
  955. asString
  956. ^self
  957. !
  958. asSymbol
  959. ^ self
  960. !
  961. asUppercase
  962. <return self.toUpperCase()>
  963. !
  964. crlfSanitized
  965. ^self lines join: String lf
  966. !
  967. reversed
  968. <return self.split("").reverse().join("")>
  969. ! !
  970. !String methodsFor: 'copying'!
  971. , aString
  972. <return self + aString>
  973. !
  974. copyFrom: anIndex to: anotherIndex
  975. <return self.substring(anIndex - 1, anotherIndex)>
  976. !
  977. deepCopy
  978. ^self shallowCopy
  979. !
  980. shallowCopy
  981. ^self class fromString: self
  982. ! !
  983. !String methodsFor: 'enumerating'!
  984. do: aBlock
  985. <for(var i=0;i<self.length;i++){aBlock._value_(self.charAt(i));}>
  986. !
  987. withIndexDo: aBlock
  988. <for(var i=0;i<self.length;i++){aBlock._value_value_(self.charAt(i), i+1);}>
  989. ! !
  990. !String methodsFor: 'printing'!
  991. printNl
  992. <console.log(self)>
  993. !
  994. printOn: aStream
  995. aStream
  996. nextPutAll: '''';
  997. nextPutAll: self;
  998. nextPutAll: ''''
  999. ! !
  1000. !String methodsFor: 'regular expressions'!
  1001. match: aRegexp
  1002. <return self.search(aRegexp) !!= -1>
  1003. !
  1004. matchesOf: aRegularExpression
  1005. <return self.match(aRegularExpression)>
  1006. !
  1007. replace: aString with: anotherString
  1008. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1009. !
  1010. replaceRegexp: aRegexp with: aString
  1011. <return self.replace(aRegexp, aString)>
  1012. !
  1013. trimBoth
  1014. ^self trimBoth: '\s'
  1015. !
  1016. trimBoth: separators
  1017. ^(self trimLeft: separators) trimRight: separators
  1018. !
  1019. trimLeft
  1020. ^self trimLeft: '\s'
  1021. !
  1022. trimLeft: separators
  1023. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1024. !
  1025. trimRight
  1026. ^self trimRight: '\s'
  1027. !
  1028. trimRight: separators
  1029. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1030. ! !
  1031. !String methodsFor: 'split join'!
  1032. join: aCollection
  1033. ^ String
  1034. streamContents: [:stream | aCollection
  1035. do: [:each | stream nextPutAll: each asString]
  1036. separatedBy: [stream nextPutAll: self]]
  1037. !
  1038. lineIndicesDo: aBlock
  1039. "execute aBlock with 3 arguments for each line:
  1040. - start index of line
  1041. - end index of line without line delimiter
  1042. - end index of line including line delimiter(s) CR, LF or CRLF"
  1043. | cr lf start sz nextLF nextCR |
  1044. start := 1.
  1045. sz := self size.
  1046. cr := String cr.
  1047. nextCR := self indexOf: cr startingAt: 1.
  1048. lf := String lf.
  1049. nextLF := self indexOf: lf startingAt: 1.
  1050. [ start <= sz ] whileTrue: [
  1051. (nextLF = 0 and: [ nextCR = 0 ])
  1052. ifTrue: [ "No more CR, nor LF, the string is over"
  1053. aBlock value: start value: sz value: sz.
  1054. ^self ].
  1055. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1056. ifTrue: [ "Found a LF"
  1057. aBlock value: start value: nextLF - 1 value: nextLF.
  1058. start := 1 + nextLF.
  1059. nextLF := self indexOf: lf startingAt: start ]
  1060. ifFalse: [ 1 + nextCR = nextLF
  1061. ifTrue: [ "Found a CR-LF pair"
  1062. aBlock value: start value: nextCR - 1 value: nextLF.
  1063. start := 1 + nextLF.
  1064. nextCR := self indexOf: cr startingAt: start.
  1065. nextLF := self indexOf: lf startingAt: start ]
  1066. ifFalse: [ "Found a CR"
  1067. aBlock value: start value: nextCR - 1 value: nextCR.
  1068. start := 1 + nextCR.
  1069. nextCR := self indexOf: cr startingAt: start ]]]
  1070. !
  1071. lineNumber: anIndex
  1072. "Answer a string containing the characters in the given line number."
  1073. | lineCount |
  1074. lineCount := 0.
  1075. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1076. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1077. ^nil
  1078. !
  1079. lines
  1080. "Answer an array of lines composing this receiver without the line ending delimiters."
  1081. | lines |
  1082. lines := Array new.
  1083. self linesDo: [:aLine | lines add: aLine].
  1084. ^lines
  1085. !
  1086. linesDo: aBlock
  1087. "Execute aBlock with each line in this string. The terminating line
  1088. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1089. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1090. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1091. !
  1092. subStrings: aString
  1093. ^ self tokenize: aString
  1094. !
  1095. tokenize: aString
  1096. <return self.split(aString)>
  1097. ! !
  1098. !String methodsFor: 'testing'!
  1099. includesSubString: subString
  1100. < return self.indexOf(subString) !!= -1 >
  1101. !
  1102. isImmutable
  1103. ^ true
  1104. !
  1105. isString
  1106. ^true
  1107. !
  1108. isVowel
  1109. "Answer true if the receiver is a one character string containing a voyel"
  1110. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1111. ! !
  1112. !String class methodsFor: 'accessing'!
  1113. cr
  1114. <return '\r'>
  1115. !
  1116. crlf
  1117. <return '\r\n'>
  1118. !
  1119. lf
  1120. <return '\n'>
  1121. !
  1122. space
  1123. <return ' '>
  1124. !
  1125. streamClass
  1126. ^StringStream
  1127. !
  1128. tab
  1129. <return '\t'>
  1130. ! !
  1131. !String class methodsFor: 'instance creation'!
  1132. fromCharCode: anInteger
  1133. <return String.fromCharCode(anInteger)>
  1134. !
  1135. fromString: aString
  1136. <return String(aString)>
  1137. !
  1138. value: aUTFCharCode
  1139. <return String.fromCharCode(aUTFCharCode);>
  1140. ! !
  1141. !String class methodsFor: 'random'!
  1142. random
  1143. "Returns random alphanumeric string beginning with letter"
  1144. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1145. !
  1146. randomNotIn: aString
  1147. | result |
  1148. [ result := self random. aString includesSubString: result ] whileTrue.
  1149. ^result
  1150. ! !
  1151. Collection subclass: #Set
  1152. instanceVariableNames: 'elements'
  1153. package: 'Kernel-Collections'!
  1154. !Set commentStamp!
  1155. I represent an unordered set of objects without duplicates.!
  1156. !Set methodsFor: 'accessing'!
  1157. size
  1158. ^elements size
  1159. ! !
  1160. !Set methodsFor: 'adding/removing'!
  1161. add: anObject
  1162. <
  1163. var found;
  1164. for(var i=0; i < self['@elements'].length; i++) {
  1165. if(_st(anObject).__eq(self['@elements'][i])) {
  1166. found = true;
  1167. break;
  1168. }
  1169. }
  1170. if(!!found) {self['@elements'].push(anObject)}
  1171. >
  1172. !
  1173. remove: anObject
  1174. elements remove: anObject
  1175. !
  1176. remove: anObject ifAbsent: aBlock
  1177. elements remove: anObject ifAbsent: aBlock
  1178. ! !
  1179. !Set methodsFor: 'comparing'!
  1180. = aCollection
  1181. self class = aCollection class ifFalse: [ ^ false ].
  1182. self size = aCollection size ifFalse: [ ^ false ].
  1183. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1184. ^ true
  1185. ! !
  1186. !Set methodsFor: 'converting'!
  1187. asArray
  1188. ^elements copy
  1189. ! !
  1190. !Set methodsFor: 'enumerating'!
  1191. collect: aBlock
  1192. ^self class withAll: (elements collect: aBlock)
  1193. !
  1194. detect: aBlock ifNone: anotherBlock
  1195. ^elements detect: aBlock ifNone: anotherBlock
  1196. !
  1197. do: aBlock
  1198. elements do: aBlock
  1199. !
  1200. select: aBlock
  1201. | collection |
  1202. collection := self class new.
  1203. self do: [:each |
  1204. (aBlock value: each) ifTrue: [
  1205. collection add: each]].
  1206. ^collection
  1207. ! !
  1208. !Set methodsFor: 'initialization'!
  1209. initialize
  1210. super initialize.
  1211. elements := #()
  1212. ! !
  1213. !Set methodsFor: 'printing'!
  1214. printOn: aStream
  1215. super printOn: aStream.
  1216. aStream nextPutAll: ' ('.
  1217. self
  1218. do: [ :each | each printOn: aStream ]
  1219. separatedBy: [ aStream nextPutAll: ' ' ].
  1220. aStream nextPutAll: ')'
  1221. ! !
  1222. !Set methodsFor: 'testing'!
  1223. includes: anObject
  1224. ^elements includes: anObject
  1225. ! !
  1226. Object subclass: #Queue
  1227. instanceVariableNames: 'read readIndex write'
  1228. package: 'Kernel-Collections'!
  1229. !Queue commentStamp!
  1230. I am a one-sided queue.
  1231. ## Usage
  1232. Use `#nextPut:` to add items to the queue.
  1233. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1234. ## Implementation notes
  1235. A Queue uses two OrderedCollections inside,
  1236. `read` is at the front, is not modified and only read using `readIndex`.
  1237. `write` is at the back and is appended new items.
  1238. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1239. As a consequence, no data moving is done by me, write appending may do data moving
  1240. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1241. !Queue methodsFor: 'accessing'!
  1242. next
  1243. ^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1244. !
  1245. nextIfAbsent: aBlock
  1246. | result |
  1247. result := read at: readIndex ifAbsent: [
  1248. write isEmpty ifTrue: [
  1249. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1250. ^aBlock value ].
  1251. read := write.
  1252. readIndex := 1.
  1253. write := OrderedCollection new.
  1254. read first ].
  1255. read at: readIndex put: nil.
  1256. readIndex := readIndex + 1.
  1257. ^result
  1258. !
  1259. nextPut: anObject
  1260. write add: anObject
  1261. ! !
  1262. !Queue methodsFor: 'initialization'!
  1263. initialize
  1264. super initialize.
  1265. read := OrderedCollection new.
  1266. write := OrderedCollection new.
  1267. readIndex := 1
  1268. ! !
  1269. Object subclass: #RegularExpression
  1270. instanceVariableNames: ''
  1271. package: 'Kernel-Collections'!
  1272. !RegularExpression commentStamp!
  1273. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1274. !RegularExpression methodsFor: 'evaluating'!
  1275. compile: aString
  1276. <return self.compile(aString)>
  1277. !
  1278. exec: aString
  1279. <return self.exec(aString) || nil>
  1280. !
  1281. test: aString
  1282. <return self.test(aString)>
  1283. ! !
  1284. !RegularExpression class methodsFor: 'instance creation'!
  1285. fromString: aString
  1286. ^self fromString: aString flag: ''
  1287. !
  1288. fromString: aString flag: anotherString
  1289. <return new RegExp(aString, anotherString)>
  1290. ! !
  1291. Object subclass: #Stream
  1292. instanceVariableNames: 'collection position streamSize'
  1293. package: 'Kernel-Collections'!
  1294. !Stream commentStamp!
  1295. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1296. My instances are read/write streams to the contents sequence collection.!
  1297. !Stream methodsFor: 'accessing'!
  1298. collection
  1299. ^collection
  1300. !
  1301. contents
  1302. ^self collection
  1303. copyFrom: 1
  1304. to: self streamSize
  1305. !
  1306. position
  1307. ^position ifNil: [position := 0]
  1308. !
  1309. position: anInteger
  1310. position := anInteger
  1311. !
  1312. setCollection: aCollection
  1313. collection := aCollection
  1314. !
  1315. setStreamSize: anInteger
  1316. streamSize := anInteger
  1317. !
  1318. size
  1319. ^self streamSize
  1320. !
  1321. streamSize
  1322. ^streamSize
  1323. ! !
  1324. !Stream methodsFor: 'actions'!
  1325. close
  1326. !
  1327. flush
  1328. !
  1329. reset
  1330. self position: 0
  1331. !
  1332. resetContents
  1333. self reset.
  1334. self setStreamSize: 0
  1335. ! !
  1336. !Stream methodsFor: 'enumerating'!
  1337. do: aBlock
  1338. [self atEnd] whileFalse: [aBlock value: self next]
  1339. ! !
  1340. !Stream methodsFor: 'positioning'!
  1341. setToEnd
  1342. self position: self size
  1343. !
  1344. skip: anInteger
  1345. self position: ((self position + anInteger) min: self size max: 0)
  1346. ! !
  1347. !Stream methodsFor: 'reading'!
  1348. next
  1349. ^self atEnd
  1350. ifTrue: [nil]
  1351. ifFalse: [
  1352. self position: self position + 1.
  1353. collection at: self position]
  1354. !
  1355. next: anInteger
  1356. | tempCollection |
  1357. tempCollection := self collection class new.
  1358. anInteger timesRepeat: [
  1359. self atEnd ifFalse: [
  1360. tempCollection add: self next]].
  1361. ^tempCollection
  1362. !
  1363. peek
  1364. ^self atEnd ifFalse: [
  1365. self collection at: self position + 1]
  1366. ! !
  1367. !Stream methodsFor: 'testing'!
  1368. atEnd
  1369. ^self position = self size
  1370. !
  1371. atStart
  1372. ^self position = 0
  1373. !
  1374. isEmpty
  1375. ^self size = 0
  1376. ! !
  1377. !Stream methodsFor: 'writing'!
  1378. << anObject
  1379. self write: anObject
  1380. !
  1381. nextPut: anObject
  1382. self position: self position + 1.
  1383. self collection at: self position put: anObject.
  1384. self setStreamSize: (self streamSize max: self position)
  1385. !
  1386. nextPutAll: aCollection
  1387. aCollection do: [:each |
  1388. self nextPut: each]
  1389. !
  1390. nextPutString: aString
  1391. self nextPut: aString
  1392. !
  1393. write: anObject
  1394. anObject putOn: self
  1395. ! !
  1396. !Stream class methodsFor: 'instance creation'!
  1397. on: aCollection
  1398. ^self new
  1399. setCollection: aCollection;
  1400. setStreamSize: aCollection size;
  1401. yourself
  1402. ! !
  1403. Stream subclass: #StringStream
  1404. instanceVariableNames: ''
  1405. package: 'Kernel-Collections'!
  1406. !StringStream commentStamp!
  1407. I am a Stream specific to `String` objects.!
  1408. !StringStream methodsFor: 'reading'!
  1409. next: anInteger
  1410. | tempCollection |
  1411. tempCollection := self collection class new.
  1412. anInteger timesRepeat: [
  1413. self atEnd ifFalse: [
  1414. tempCollection := tempCollection, self next]].
  1415. ^tempCollection
  1416. ! !
  1417. !StringStream methodsFor: 'writing'!
  1418. cr
  1419. ^self nextPutAll: String cr
  1420. !
  1421. crlf
  1422. ^self nextPutAll: String crlf
  1423. !
  1424. lf
  1425. ^self nextPutAll: String lf
  1426. !
  1427. nextPut: aString
  1428. self nextPutAll: aString
  1429. !
  1430. nextPutAll: aString
  1431. | pre post |
  1432. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1433. pre := self collection copyFrom: 1 to: self position.
  1434. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1435. self setCollection: pre, aString, post
  1436. ].
  1437. self position: self position + aString size.
  1438. self setStreamSize: (self streamSize max: self position)
  1439. !
  1440. nextPutString: aString
  1441. self nextPutAll: aString
  1442. !
  1443. space
  1444. self nextPut: ' '
  1445. !
  1446. tab
  1447. ^self nextPutAll: String tab
  1448. ! !