Kernel-Collections.st 35 KB

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