-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathString.som
133 lines (103 loc) · 3.96 KB
/
String.som
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"
$Id: String.som 29 2009-07-31 11:28:44Z michael.haupt $
Copyright (c) 2001-2013 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
String = (
"Strings are immutable"
"Concatenate: returns a new string object"
concatenate: argument = primitive
+ argument = ( ^self concatenate: argument asString )
"Converting"
asString = ( ^self )
asSymbol = primitive
hashcode = primitive
"Info"
length = primitive
"Returns true if all characters in the string are whitespace.
False otherwise, including for the empty string."
isWhiteSpace = primitive
"Returns true if all characters in the string are letters.
False otherwise, including for the empty string."
isLetters = primitive
"Returns true if all characters in the string are digits.
False otherwise, including for the empty string."
isDigits = primitive
"Comparing"
= argument = primitive
"substring: from 'start' to (and including) 'end'."
primSubstringFrom: start to: end = primitive
substringFrom: start to: end = (
((end <= self length) && (start > 0) && (start <= end))
ifTrue: [^self primSubstringFrom: start to: end]
ifFalse: [
self error: 'Attempting to index string out of its bounds (start: ' + start asString + ' end: ' + end asString + ' length: ' + self length asString + ')' ]
)
beginsWith: prefix = (
self length < prefix length ifTrue: [ ^ false ].
1 to: prefix length do: [:i |
((self charAt: i) = (prefix charAt: i)) ifFalse: [ ^ false ].
].
^ true
)
endsWith: suffix = (
| l sufL |
l := self length.
sufL := suffix length.
l < sufL ifTrue: [ ^ false ].
1 to: sufL do: [:i |
(self charAt: l - sufL + i) = (suffix charAt: i) ifFalse: [ ^ false ]
].
^ true
)
asInteger = (
^ Integer fromString: self
)
charAt: argument = (
^self substringFrom: argument to: argument
)
indexOf: aString = (
^ self indexOf: aString startingAt: 1
)
indexOf: aString startingAt: start = (
| l |
l := aString length.
start + l > (self length + 1) ifTrue: [ ^ -1 ].
start to: self length - l + 1 do: [:i |
(self primSubstringFrom: i to: i + l - 1) = aString ifTrue: [ ^ i ].
].
^ -1
)
split: split = (
| start newStart result |
self length < split length ifTrue: [ ^ Array new: self ].
start := 1.
result := Vector new.
[start > 0] whileTrue: [
newStart := self indexOf: split startingAt: start.
newStart = -1
ifTrue: [
result append: (self primSubstringFrom: start to: self length).
^ result asArray ]
ifFalse: [
result append: (self primSubstringFrom: start to: newStart - 1).
start := newStart + split length ] ].
^ result asArray
)
"Printing"
print = ( system printString: self )
)