forked from SeasideSt/Grease
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.extension.st
More file actions
90 lines (75 loc) · 2.79 KB
/
String.extension.st
File metadata and controls
90 lines (75 loc) · 2.79 KB
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
Extension { #name : 'String' }
{ #category : '*grease-core' }
String >> excerpt: aString [
"Answer an excerpt of the receiver that matches the first occurence of aString. If aString isn't found, nil is answered."
^ self excerpt: aString radius: 100
]
{ #category : '*grease-core' }
String >> excerpt: aString radius: anInteger [
"Answer an excerpt of the receiver that matches the first occurence of aString. The radius anInteger expands the excerpt on each side of the first occurrence by the number of characters defined in radius. If aString isn't found, nil is answered."
^ self excerpt: aString radius: anInteger ellipsis: '...'
]
{ #category : '*grease-core' }
String >> excerpt: aString radius: anInteger ellipsis: anEllipsisString [
"Answer an excerpt of the receiver that matches the first occurence of aString. The radius anInteger expands the excerpt on each side of the first occurrence by the number of characters defined in radius. If aString isn't found, nil is answered."
| index start stop |
(index := self indexOfSubCollection: aString) = 0
ifTrue: [ ^ nil ].
start := index - anInteger max: 1.
stop := index + anInteger + aString size - 1 min: self size.
^ (start > 1 ifTrue: [ anEllipsisString ] ifFalse: [ '' ]) ,
(self copyFrom: start to: stop) ,
(stop < self size ifTrue: [ anEllipsisString ] ifFalse: [ '' ])
]
{ #category : '*grease-core' }
String >> greaseInteger [
"Convert the receiver to an integer, answer nil if this is not a number."
| number negated |
number := nil.
negated := false.
1 to: self size do: [ : i |
| char |
char := self at: i.
(i = 1 and: [ char = $- ])
ifTrue: [ negated := true ]
ifFalse: [
(char >= $0 and: [ char <= $9 ])
ifTrue: [
number := (number isNil
ifTrue: [ 0 ]
ifFalse: [ 10 * number ]) + (char greaseInteger - $0 greaseInteger) ]
ifFalse: [
^ number isNil ifFalse: [
negated not
ifFalse: [ number negated ]
ifTrue: [ number ] ] ] ] ].
^ number isNil ifFalse: [
negated not
ifFalse: [ number negated ]
ifTrue: [ number ] ]
]
{ #category : '*grease-core' }
String >> pluralize [
^ GRInflector pluralize: self
]
{ #category : '*grease-core' }
String >> print: anObject on: aStream [
aStream nextPutAll: self
]
{ #category : '*grease-core' }
String >> truncate [
"Truncate the receiver to 30 characters."
^ self truncate: 30
]
{ #category : '*grease-core' }
String >> truncate: anInteger [
"Truncate the receiver to anInteger characters."
^ self truncate: anInteger ellipsis: '...'
]
{ #category : '*grease-core' }
String >> truncate: anInteger ellipsis: aString [
"Truncate the receiver to anInteger characters and append aString as ellipsis if necessary."
^ anInteger < self size
ifTrue: [ (self copyFrom: 1 to: anInteger) , aString ]
ifFalse: [ self copy ]
]