|
1 " |
|
2 COPYRIGHT (c) 1989-93 by Claus Gittinger |
|
3 All Rights Reserved |
|
4 |
|
5 This software is furnished under a license and may be used |
|
6 only in accordance with the terms of that license and with the |
|
7 inclusion of the above copyright notice. This software may not |
|
8 be provided or otherwise made available to, or used by, any |
|
9 other person. No title to or ownership of the software is |
|
10 hereby transferred. |
|
11 " |
|
12 |
|
13 VariableArray subclass:#Text |
|
14 instanceVariableNames:'' |
|
15 classVariableNames:'' |
|
16 poolDictionaries:'' |
|
17 category:'Collections-Text' |
|
18 ! |
|
19 |
|
20 Text comment:' |
|
21 |
|
22 COPYRIGHT (c) 1989-93 by Claus Gittinger |
|
23 All Rights Reserved |
|
24 |
|
25 text is an array of lines which are strings. |
|
26 this is just temporary - may change in the future. |
|
27 |
|
28 %W% %E% |
|
29 '! |
|
30 |
|
31 !Text class methodsFor:'instance creation'! |
|
32 |
|
33 from:aString |
|
34 "return a new text object with lines taken from the argument, aString" |
|
35 |
|
36 ^ (self new:1) from:aString |
|
37 ! |
|
38 |
|
39 fromArray:anArray |
|
40 "return a new text object with lines taken from the argument, an array |
|
41 of strings" |
|
42 |
|
43 |newText |
|
44 size "{ Class: SmallInteger }" | |
|
45 |
|
46 size := anArray size. |
|
47 newText := self new:size. |
|
48 1 to:size do:[:line | |
|
49 newText at:line put:(anArray at:line) |
|
50 ]. |
|
51 ^ newText |
|
52 ! ! |
|
53 |
|
54 !Text methodsFor:'growing'! |
|
55 |
|
56 grow:newSize |
|
57 "grow to newsize - new elements are initialized with empty strings - |
|
58 not nil" |
|
59 |
|
60 |oldSize "{ Class:SmallInteger }"| |
|
61 |
|
62 oldSize := tally. |
|
63 super grow:newSize. |
|
64 (oldSize < newSize) ifTrue:[ |
|
65 contentsArray from:(oldSize + 1) to:newSize put:'' |
|
66 ] |
|
67 ! |
|
68 |
|
69 add:aString |
|
70 "append the argument, aString to myself - |
|
71 we increase physical size by 50 to avoid lots of copying around" |
|
72 |
|
73 |oldSize "{ Class:SmallInteger }"| |
|
74 |
|
75 oldSize := tally. |
|
76 super grow:(oldSize + 50). |
|
77 tally := oldSize + 1. |
|
78 contentsArray at:tally put:aString |
|
79 ! ! |
|
80 |
|
81 !Text methodsFor:'converting'! |
|
82 |
|
83 asString |
|
84 "return myself as a string with embedded cr's" |
|
85 |
|
86 |totalLength "{ Class:SmallInteger }" |
|
87 pos "{ Class:SmallInteger }" |
|
88 newString | |
|
89 |
|
90 totalLength := 0. |
|
91 self do:[:lineString | |
|
92 lineString isNil ifTrue:[ |
|
93 totalLength := totalLength + 1 |
|
94 ] ifFalse: [ |
|
95 totalLength := totalLength + lineString size + 1 |
|
96 ]. |
|
97 0 |
|
98 ]. |
|
99 newString := String new:totalLength. |
|
100 pos := 1. |
|
101 self do:[:lineString | |
|
102 lineString isNil ifFalse:[ |
|
103 newString replaceFrom:pos with:lineString. |
|
104 pos := pos + (lineString size) |
|
105 ]. |
|
106 newString at:pos put:(Character cr). |
|
107 pos := pos + 1 |
|
108 ]. |
|
109 ^ newString |
|
110 ! |
|
111 |
|
112 from:aString |
|
113 "setup my contents from the argument, aString" |
|
114 |
|
115 |numberOfLines "{ Class:SmallInteger }" |
|
116 start "{ Class:SmallInteger }" |
|
117 stop "{ Class:SmallInteger }" | |
|
118 |
|
119 numberOfLines := aString occurrencesOf:(Character cr). |
|
120 numberOfLines := numberOfLines + 1. |
|
121 self grow:numberOfLines. |
|
122 start := 1. |
|
123 1 to:numberOfLines do:[:lineNr | |
|
124 stop := aString indexOf:(Character cr) |
|
125 startingAt:start |
|
126 ifAbsent:[aString size + 1]. |
|
127 stop := stop - 1. |
|
128 (stop < start) ifTrue: [ |
|
129 self at:lineNr put:(String new:0) |
|
130 ] ifFalse: [ |
|
131 self at:lineNr put:(aString copyFrom:start to:stop) |
|
132 ]. |
|
133 start := stop + 2 |
|
134 ] |
|
135 ! |
|
136 |
|
137 asText |
|
138 ^ self |
|
139 ! ! |
|
140 |
|
141 !Text methodsFor:'printing'! |
|
142 |
|
143 printString |
|
144 ^ self asString |
|
145 ! ! |
|
146 |
|
147 !Text methodsFor:'searching'! |
|
148 |
|
149 indexOfLineStartingWith:aString |
|
150 "return the index of the first line starting with the argument, aString" |
|
151 |
|
152 |index "{ Class:SmallInteger }" | |
|
153 |
|
154 index := 1. |
|
155 [index <= self size] whileTrue:[ |
|
156 ((self at:index) startsWith:aString) ifTrue:[ |
|
157 ^ index |
|
158 ]. |
|
159 index := index + 1 |
|
160 ]. |
|
161 ^ 0 |
|
162 ! ! |